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:
Dmitri Hrybenko
2014-05-30 23:07:21 +00:00
parent 3e72294ab0
commit a69e341aa2
2 changed files with 45 additions and 4 deletions

View File

@@ -77,6 +77,15 @@ extension String {
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
/// non-`nil`, convert the buffer to an `Index` and write it into the
/// memory referred to by `index`
@@ -1062,8 +1071,8 @@ extension String {
aSet: NSCharacterSet,
options mask:NSStringCompareOptions = nil,
range aRange: Range<Index>? = nil
)-> Range<Index> {
return _range(
)-> Range<Index>? {
return _optionalRange(
_ns.rangeOfCharacterFromSet(
aSet, options: mask,
range: _toNSRange(aRange ? aRange! : indices(self))))