Skip to content

Commit 0e7d41d

Browse files
author
Dave Abrahams
authored
Merge pull request #10974 from apple/rdar-33307780-swift-4.0-branch
Swift 3 Backward Compatibility Fixes
2 parents 058ca94 + ea2c200 commit 0e7d41d

File tree

4 files changed

+70
-5
lines changed

4 files changed

+70
-5
lines changed

stdlib/public/SDK/Foundation/ExtraStringAPIs.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ extension String.UTF16View.Index {
2121

2222
@available(swift, deprecated: 3.2)
2323
@available(swift, obsoleted: 4.0)
24-
public func distance(to other: String.UTF16View.Index) -> Int {
25-
return _offset.distance(to: other._offset)
24+
public func distance(to other: String.UTF16View.Index?) -> Int {
25+
return _offset.distance(to: other!._offset)
2626
}
2727

2828
@available(swift, deprecated: 3.2)

stdlib/public/core/StringUTF16.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ extension String.UTF16View {
487487
swift, obsoleted: 4.0,
488488
message: "Any String view index conversion can fail in Swift 4; please unwrap the optional index")
489489
public func index(after i: Index?) -> Index {
490-
return index(after: i)
490+
return index(after: i!)
491491
}
492492
@available(
493493
swift, obsoleted: 4.0,

stdlib/public/core/StringUnicodeScalarView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ extension String.UnicodeScalarView {
504504
swift, obsoleted: 4.0,
505505
message: "Any String view index conversion can fail in Swift 4; please unwrap the optional index")
506506
public func index(after i: Index?) -> Index {
507-
return index(after: i)
507+
return index(after: i!)
508508
}
509509
@available(
510510
swift, obsoleted: 4.0,

test/stdlib/StringCompatibility.swift

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,72 @@ let swift = 3
185185

186186
#endif
187187

188-
var Tests = TestSuite("SubstringCompatibility")
188+
var Tests = TestSuite("StringCompatibility")
189+
190+
#if !swift(>=4) && _runtime(_ObjC)
191+
import Foundation
192+
193+
Tests.test("String/Legacy/UTF16View.Index/StrideableAPIs") {
194+
let i = String.UTF16View.Index(0)
195+
expectEqual(0, i.distance(to: i))
196+
expectEqual(0, i.distance(to: i.samePosition(in: "")))
197+
expectEqual(i, i.advanced(by: 0))
198+
}
199+
200+
Tests.test("String/Legacy/UTF16View/OptionalIndices") {
201+
let s = "somethingToTest".utf16
202+
let i = s.startIndex
203+
let j = Optional(i)
204+
expectEqual(s.index(after: i), s.index(after: j))
205+
expectEqual(s.index(i, offsetBy: 2), s.index(j, offsetBy: 2))
206+
expectEqual(
207+
s.distance(from: i, to: s.endIndex),
208+
s.distance(from: j, to: s.endIndex))
209+
expectEqual(
210+
s.distance(from: i, to: s.endIndex),
211+
s.distance(from: j, to: Optional(s.endIndex)))
212+
expectEqual(
213+
s.distance(from: i, to: s.endIndex),
214+
s.distance(from: i, to: Optional(s.endIndex)))
215+
expectEqual(s[i], s[j])
216+
}
217+
218+
Tests.test("String/Legacy/UTF8View/OptionalIndices") {
219+
let s = "somethingToTest".utf8
220+
let i = s.startIndex
221+
let j = Optional(i)
222+
expectEqual(s.index(after: i), s.index(after: j))
223+
expectEqual(s.index(i, offsetBy: 2), s.index(j, offsetBy: 2))
224+
expectEqual(
225+
s.distance(from: i, to: s.endIndex),
226+
s.distance(from: j, to: s.endIndex))
227+
expectEqual(
228+
s.distance(from: i, to: s.endIndex),
229+
s.distance(from: j, to: Optional(s.endIndex)))
230+
expectEqual(
231+
s.distance(from: i, to: s.endIndex),
232+
s.distance(from: i, to: Optional(s.endIndex)))
233+
expectEqual(s[i], s[j])
234+
}
235+
236+
Tests.test("String/Legacy/UnicodeScalarView/OptionalIndices") {
237+
let s = "somethingToTest".unicodeScalars
238+
let i = s.startIndex
239+
let j = Optional(i)
240+
expectEqual(s.index(after: i), s.index(after: j))
241+
expectEqual(s.index(i, offsetBy: 2), s.index(j, offsetBy: 2))
242+
expectEqual(
243+
s.distance(from: i, to: s.endIndex),
244+
s.distance(from: j, to: s.endIndex))
245+
expectEqual(
246+
s.distance(from: i, to: s.endIndex),
247+
s.distance(from: j, to: Optional(s.endIndex)))
248+
expectEqual(
249+
s.distance(from: i, to: s.endIndex),
250+
s.distance(from: i, to: Optional(s.endIndex)))
251+
expectEqual(s[i], s[j])
252+
}
253+
#endif
189254

190255
Tests.test("String/Range/Slice/ExpectedType/\(swift)") {
191256
var s = "hello world"

0 commit comments

Comments
 (0)