Skip to content

Commit 7c3bd36

Browse files
DYLDaveLiu888
authored andcommitted
fix neg count SR-1988
1 parent 32f2c99 commit 7c3bd36

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

stdlib/public/SDK/Foundation/ExtraStringAPIs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension String.UTF16View.Index : Strideable {
2121
}
2222

2323
public func distance(to other: String.UTF16View.Index) -> Int {
24-
return other._offset.distance(to: _offset)
24+
return _offset.distance(to: other._offset)
2525
}
2626

2727
public func advanced(by n: Int) -> String.UTF16View.Index {

test/1_stdlib/StringAPI.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,22 @@ StringTests.test("CompareStringsWithUnpairedSurrogates")
323323
)
324324
}
325325

326+
//[SR-1988] Ranges of String.UTF16View.Index have negative count
327+
StringTests.test("String.UTF16View.Index-count") {
328+
let str = "How many elements do I contain?".utf16
329+
let indices: Range = str.startIndex ..< str.endIndex
330+
expectEqual(indices.count , 31)
331+
expectTrue(indices.count >= 0)
332+
333+
let blank = "".utf16
334+
let blankindices: Range = blank.startIndex ..< blank.endIndex
335+
expectEqual(indices.count , 0)
336+
337+
let one = "1".utf16
338+
let oneindices: Range = one.startIndex ..< one.endIndex
339+
expectEqual(indices.count , 1)
340+
}
341+
326342
var CStringTests = TestSuite("CStringTests")
327343

328344
func getNullUTF8() -> UnsafeMutablePointer<UInt8>? {

0 commit comments

Comments
 (0)