Skip to content

fix neg count SR-1988 #4072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/ExtraStringAPIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension String.UTF16View.Index : Strideable {
}

public func distance(to other: String.UTF16View.Index) -> Int {
return other._offset.distance(to: _offset)
return _offset.distance(to: other._offset)
}

public func advanced(by n: Int) -> String.UTF16View.Index {
Expand Down
16 changes: 16 additions & 0 deletions test/1_stdlib/StringAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,22 @@ StringTests.test("CompareStringsWithUnpairedSurrogates")
)
}

//[SR-1988] Ranges of String.UTF16View.Index have negative count
StringTests.test("String.UTF16View.Index-count") {
let str = "How many elements do I contain?".utf16
let indices: Range = str.startIndex ..< str.endIndex
expectEqual(indices.count , 31)
expectTrue(indices.count >= 0)

let blank = "".utf16
let blankindices: Range = blank.startIndex ..< blank.endIndex
expectEqual(indices.count , 0)

let one = "1".utf16
let oneindices: Range = one.startIndex ..< one.endIndex
expectEqual(indices.count , 1)
}

var CStringTests = TestSuite("CStringTests")

func getNullUTF8() -> UnsafeMutablePointer<UInt8>? {
Expand Down