Skip to content

Commit 8d1c13f

Browse files
committed
[SR-1988] Ranges of String.UTF16View.Index have negative count
1 parent cf47315 commit 8d1c13f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

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 {

TestFoundation/TestNSString.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class TestNSString : XCTestCase {
8787
("test_ExternalRepresentation", test_ExternalRepresentation),
8888
("test_mutableStringConstructor", test_mutableStringConstructor),
8989
("test_PrefixSuffix", test_PrefixSuffix),
90+
("test_utf16StringRangeCount", test_utf16StringRangeCount),
9091
("test_reflection", { _ in test_reflection }),
9192
]
9293
}
@@ -937,6 +938,13 @@ class TestNSString : XCTestCase {
937938
}
938939
}
939940

941+
//[SR-1988] Ranges of String.UTF16View.Index have negative count
942+
func test_utf16StringRangeCount(){
943+
let str = "How many elements do I contain?".utf16
944+
let indices: Range = str.startIndex ..< str.endIndex
945+
XCTAssertEqual(indices.count , 31, "Range should not be negative")
946+
}
947+
940948
func test_mutableStringConstructor() {
941949
let mutableString = NSMutableString(string: "Test")
942950
XCTAssertEqual(mutableString, "Test")

0 commit comments

Comments
 (0)