mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
stdlib/NSString APIs on String: fix a bug and add tests for
rangeOfCharacterFromSet(_:options:range:) The underlying Objective-C API could return an NSRange of NSNotFound. Swift's String.Index can not represent that, so change the API to return an optional Swift Range<Index> instead. Swift SVN r18679
This commit is contained in:
@@ -77,6 +77,15 @@ extension String {
|
|||||||
return _index(r.location).._index(r.location + r.length)
|
return _index(r.location).._index(r.location + r.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return a `Range<Index>?` corresponding to the given `NSRange` of
|
||||||
|
/// our UTF16 representation.
|
||||||
|
func _optionalRange(r: NSRange) -> Range<Index>? {
|
||||||
|
if r.location == NSNotFound {
|
||||||
|
return .None
|
||||||
|
}
|
||||||
|
return _range(r)
|
||||||
|
}
|
||||||
|
|
||||||
/// Invoke `body` on an `Int` buffer. If `index` was converted from
|
/// Invoke `body` on an `Int` buffer. If `index` was converted from
|
||||||
/// non-`nil`, convert the buffer to an `Index` and write it into the
|
/// non-`nil`, convert the buffer to an `Index` and write it into the
|
||||||
/// memory referred to by `index`
|
/// memory referred to by `index`
|
||||||
@@ -1062,8 +1071,8 @@ extension String {
|
|||||||
aSet: NSCharacterSet,
|
aSet: NSCharacterSet,
|
||||||
options mask:NSStringCompareOptions = nil,
|
options mask:NSStringCompareOptions = nil,
|
||||||
range aRange: Range<Index>? = nil
|
range aRange: Range<Index>? = nil
|
||||||
)-> Range<Index> {
|
)-> Range<Index>? {
|
||||||
return _range(
|
return _optionalRange(
|
||||||
_ns.rangeOfCharacterFromSet(
|
_ns.rangeOfCharacterFromSet(
|
||||||
aSet, options: mask,
|
aSet, options: mask,
|
||||||
range: _toNSRange(aRange ? aRange! : indices(self))))
|
range: _toNSRange(aRange ? aRange! : indices(self))))
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ func expectNotEqual<T : Equatable>(
|
|||||||
|
|
||||||
%end
|
%end
|
||||||
|
|
||||||
%for (Generic, EquatableType) in [('', 'String'), ('', 'Int'), ('', 'UInt'), ('', 'NSComparisonResult'), ('<T : ForwardIndex>', 'T'), ('<T, U : Equatable>', 'Dictionary<T, U>')]:
|
%for (Generic, EquatableType) in [('', 'String'), ('', 'String.Index'), ('', 'Int'), ('', 'UInt'), ('', 'NSComparisonResult'), ('<T : ForwardIndex>', 'T'), ('<T, U : Equatable>', 'Dictionary<T, U>')]:
|
||||||
|
|
||||||
func expectEqual${Generic}(
|
func expectEqual${Generic}(
|
||||||
expected: ${EquatableType}, actual: ${EquatableType},
|
expected: ${EquatableType}, actual: ${EquatableType},
|
||||||
@@ -1002,7 +1002,39 @@ NSStringAPIs.test("propertyListFromStringsFileFormat()") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NSStringAPIs.test("rangeOfCharacterFromSet(_:options:range:)") {
|
NSStringAPIs.test("rangeOfCharacterFromSet(_:options:range:)") {
|
||||||
// FIXME
|
if true {
|
||||||
|
let charset = NSCharacterSet(charactersInString: "абв")
|
||||||
|
if true {
|
||||||
|
let s = "Глокая куздра"
|
||||||
|
let r = s.rangeOfCharacterFromSet(charset)!
|
||||||
|
expectEqual(advance(s.startIndex, 4), r.startIndex)
|
||||||
|
expectEqual(advance(s.startIndex, 5), r.endIndex)
|
||||||
|
}
|
||||||
|
if true {
|
||||||
|
expectEmpty("клмн".rangeOfCharacterFromSet(charset))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if true {
|
||||||
|
let charset = NSCharacterSet(charactersInString: "\u305f\u3099")
|
||||||
|
expectEmpty("\u3060".rangeOfCharacterFromSet(charset))
|
||||||
|
}
|
||||||
|
if true {
|
||||||
|
let charset = NSCharacterSet(charactersInString: "\u3060")
|
||||||
|
expectEmpty("\u305f\u3099".rangeOfCharacterFromSet(charset))
|
||||||
|
}
|
||||||
|
|
||||||
|
if true {
|
||||||
|
let charset = NSCharacterSet(charactersInString: "\U0001F600")
|
||||||
|
if true {
|
||||||
|
let s = "abc\U0001F600"
|
||||||
|
expectEqual("\U0001F600",
|
||||||
|
s[s.rangeOfCharacterFromSet(charset)!])
|
||||||
|
}
|
||||||
|
if true {
|
||||||
|
expectEmpty("abc\U0001F601".rangeOfCharacterFromSet(charset))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NSStringAPIs.test("rangeOfComposedCharacterSequenceAtIndex(_:)") {
|
NSStringAPIs.test("rangeOfComposedCharacterSequenceAtIndex(_:)") {
|
||||||
|
|||||||
Reference in New Issue
Block a user