Skip to content

[stdlib] Fix issue with UTF16 index(_:offsetBy:limitedBy) #12378

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

Merged
merged 2 commits into from
Oct 16, 2017
Merged
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/core/StringUTF16.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ extension String {
) -> Index? {
// FIXME: swift-3-indexing-model: range check i?
let d = i.encodedOffset.distance(to: limit.encodedOffset)
if (d > 0) ? (d < n) : (d > n) {
if (d >= 0) ? (d < n) : (d > n) {
return nil
}
return Index(encodedOffset: i.encodedOffset.advanced(by: n))
Expand Down
8 changes: 8 additions & 0 deletions test/Interpreter/SDK/Foundation_test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ FoundationTestSuite.test("RangeConversion") {
let nsrFrom = NSRange(b..., in: s)
expectEqual(nsrFrom.location,5)
expectEqual(nsrFrom.length, 5)

expectNil(Range(NSRange(location: 100, length: 0), in: s))
expectNil(Range(NSRange(location: 0, length: 100), in: s))

let empty = ""
expectNil(Range(NSRange(location: 1, length: 0), in: empty))
expectNil(Range(NSRange(location: 0, length: 1), in: empty))
expectNotNil(Range(NSRange(location: 0, length: 0), in: empty))

// FIXME: enable once indices conform to RangeExpression
// let nsrFull = NSRange(s.indices, in: s)
Expand Down
49 changes: 39 additions & 10 deletions test/stdlib/StringTraps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ StringTraps.test("startIndex/predecessor")
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
var s = "abc"
let s = "abc"
var i = s.startIndex
i = s.index(after: i)
i = s.index(before: i)
Expand All @@ -30,7 +30,7 @@ StringTraps.test("endIndex/successor")
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
var s = "abc"
let s = "abc"
var i = s.startIndex
i = s.index(after: i)
i = s.index(after: i)
Expand All @@ -44,21 +44,21 @@ StringTraps.test("subscript(_:)/endIndex")
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
var s = "abc"
let s = "abc"
var i = s.startIndex
i = s.index(after: i)
i = s.index(after: i)
i = s.index(after: i)
expectCrashLater()
s[i]
_ = s[i]
}

StringTraps.test("UTF8ViewEndIndexSuccessor")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
var s = "abc"
let s = "abc"
var i = s.utf8.startIndex
i = s.utf8.index(after: i)
i = s.utf8.index(after: i)
Expand All @@ -72,25 +72,25 @@ StringTraps.test("UTF8ViewSubscript/endIndex")
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
var s = "abc"
let s = "abc"
var i = s.utf8.startIndex
i = s.utf8.index(after: i)
i = s.utf8.index(after: i)
i = s.utf8.index(after: i)
expectCrashLater()
s.utf8[i]
_ = s.utf8[i]
}

StringTraps.test("UTF16ViewSubscript/DecrementedStartIndex")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
var s = "abc"
let s = "abc"
var i = s.utf16.startIndex
expectCrashLater()
i = s.utf16.index(before: i)
s.utf16[i]
_ = s.utf16[i]
}

StringTraps.test("UTF16ViewSubscript/endIndex")
Expand All @@ -104,7 +104,36 @@ StringTraps.test("UTF16ViewSubscript/endIndex")
i = s.utf16.index(after: i)
i = s.utf16.index(after: i)
expectCrashLater()
s.utf16[i]
_ = s.utf16[i]
}

StringTraps.test("UTF16ViewIndex/offsetLimited")
.code {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.code is not necessary.

let sa = "foo"
let u16a = sa.utf16
let s16 = sa + "🤦🏻‍♀️"
let u16 = s16.utf16

let iaBegin = u16a.index(sa.startIndex, offsetBy: 99, limitedBy: sa.endIndex)
expectNil(iaBegin)
let iaEnd = u16a.index(sa.endIndex, offsetBy: 99, limitedBy: sa.endIndex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be interesting to test for non-positive offsets starting from an endIndex.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, good point!

expectNil(iaEnd)
let i16Begin = u16.index(u16.startIndex, offsetBy: 99, limitedBy: u16.endIndex)
expectNil(i16Begin)
let i16End = u16.index(u16.startIndex, offsetBy: 99, limitedBy: u16.endIndex)
expectNil(i16End)
}

StringTraps.test("UTF16ViewIndex/offsetCrash")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
let s16 = "foo🤦🏻‍♀️"
let u16 = s16.utf16
expectCrashLater()
let i = u16.index(u16.startIndex, offsetBy: 99)
_ = s16.utf16[i]
}

runAllTests()
Expand Down