Skip to content

Commit 1b878a0

Browse files
committed
Add a conversion to/from CFRange and NSRange that handles not found
1 parent c8d596b commit 1b878a0

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

Foundation/NSRange.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
import CoreFoundation
1011

1112
public struct _NSRange {
1213
public var location: Int
@@ -20,6 +21,18 @@ public struct _NSRange {
2021
self.location = location
2122
self.length = length
2223
}
24+
25+
internal init(_ range: CFRange) {
26+
location = range.location == kCFNotFound ? NSNotFound : range.location
27+
length = range.length
28+
}
29+
}
30+
31+
extension CFRange {
32+
internal init(_ range: NSRange) {
33+
location = range.location == NSNotFound ? kCFNotFound : range.location
34+
length = range.length
35+
}
2336
}
2437

2538
extension _NSRange {

Foundation/NSString.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,9 @@ extension NSString {
380380
public func compare(string: String, options mask: NSStringCompareOptions, range compareRange: NSRange, locale: AnyObject?) -> NSComparisonResult {
381381
var res: CFComparisonResult
382382
if let loc = locale {
383-
res = CFStringCompareWithOptionsAndLocale(_cfObject, string._cfObject, CFRangeMake(compareRange.location, compareRange.length), mask._cfValue(true), (loc as! NSLocale)._cfObject)
383+
res = CFStringCompareWithOptionsAndLocale(_cfObject, string._cfObject, CFRange(compareRange), mask._cfValue(true), (loc as! NSLocale)._cfObject)
384384
} else {
385-
res = CFStringCompareWithOptionsAndLocale(_cfObject, string._cfObject, CFRangeMake(compareRange.location, compareRange.length), mask._cfValue(true), nil)
385+
res = CFStringCompareWithOptionsAndLocale(_cfObject, string._cfObject, CFRange(compareRange), mask._cfValue(true), nil)
386386
}
387387
return NSComparisonResult._fromCF(res)
388388
}
@@ -520,9 +520,9 @@ extension NSString {
520520
var result = CFRange()
521521
let res = withUnsafeMutablePointer(&result) { (rangep: UnsafeMutablePointer<CFRange>) -> Bool in
522522
if let loc = locale {
523-
return CFStringFindWithOptionsAndLocale(_cfObject, searchString._cfObject, CFRangeMake(searchRange.location, searchRange.length), mask._cfValue(true), loc._cfObject, rangep)
523+
return CFStringFindWithOptionsAndLocale(_cfObject, searchString._cfObject, CFRange(searchRange), mask._cfValue(true), loc._cfObject, rangep)
524524
} else {
525-
return CFStringFindWithOptionsAndLocale(_cfObject, searchString._cfObject, CFRangeMake(searchRange.location, searchRange.length), mask._cfValue(true), nil, rangep)
525+
return CFStringFindWithOptionsAndLocale(_cfObject, searchString._cfObject, CFRange(searchRange), mask._cfValue(true), nil, rangep)
526526
}
527527
}
528528
if res {
@@ -547,7 +547,7 @@ extension NSString {
547547

548548
var result = CFRange()
549549
let res = withUnsafeMutablePointer(&result) { (rangep: UnsafeMutablePointer<CFRange>) -> Bool in
550-
return CFStringFindCharacterFromSet(_cfObject, searchSet._cfObject, CFRangeMake(searchRange.location, searchRange.length), mask._cfValue(), rangep)
550+
return CFStringFindCharacterFromSet(_cfObject, searchSet._cfObject, CFRange(searchRange), mask._cfValue(), rangep)
551551
}
552552
if res {
553553
return NSMakeRange(result.location, result.length)
@@ -1361,11 +1361,11 @@ extension NSMutableString {
13611361
}
13621362

13631363

1364-
if let findResults = CFStringCreateArrayWithFindResults(kCFAllocatorSystemDefault, _cfObject, target._cfObject, CFRangeMake(searchRange.location, searchRange.length), options._cfValue(true)) {
1364+
if let findResults = CFStringCreateArrayWithFindResults(kCFAllocatorSystemDefault, _cfObject, target._cfObject, CFRange(searchRange), options._cfValue(true)) {
13651365
let numOccurrences = CFArrayGetCount(findResults)
13661366
for cnt in 0..<numOccurrences {
13671367
let range = UnsafePointer<CFRange>(CFArrayGetValueAtIndex(findResults, backwards ? cnt : numOccurrences - cnt - 1))
1368-
replaceCharactersInRange(NSMakeRange(range.memory.location, range.memory.length), withString: replacement)
1368+
replaceCharactersInRange(NSRange(range.memory), withString: replacement)
13691369
}
13701370
return numOccurrences
13711371
} else {

Foundation/NSURL.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -701,40 +701,36 @@ public class NSURLComponents : NSObject, NSCopying {
701701

702702
/* These properties return the character range of a component in the URL string returned by -[NSURLComponents string]. If the component does not exist in the NSURLComponents object, {NSNotFound, 0} is returned. Note: Zero length components are legal. For example, the URL string "scheme://:@/?#" has a zero length user, password, host, query and fragment; the URL strings "scheme:" and "" both have a zero length path.
703703
*/
704-
private final func _convertRange(r : CFRange) -> NSRange {
705-
return NSMakeRange(r.location == kCFNotFound ? NSNotFound : r.location, r.length)
706-
}
707-
708704
public var rangeOfScheme: NSRange {
709-
return _convertRange(_CFURLComponentsGetRangeOfScheme(_components))
705+
return NSRange(_CFURLComponentsGetRangeOfScheme(_components))
710706
}
711707

712708
public var rangeOfUser: NSRange {
713-
return _convertRange(_CFURLComponentsGetRangeOfUser(_components))
709+
return NSRange(_CFURLComponentsGetRangeOfUser(_components))
714710
}
715711

716712
public var rangeOfPassword: NSRange {
717-
return _convertRange(_CFURLComponentsGetRangeOfPassword(_components))
713+
return NSRange(_CFURLComponentsGetRangeOfPassword(_components))
718714
}
719715

720716
public var rangeOfHost: NSRange {
721-
return _convertRange(_CFURLComponentsGetRangeOfHost(_components))
717+
return NSRange(_CFURLComponentsGetRangeOfHost(_components))
722718
}
723719

724720
public var rangeOfPort: NSRange {
725-
return _convertRange(_CFURLComponentsGetRangeOfPort(_components))
721+
return NSRange(_CFURLComponentsGetRangeOfPort(_components))
726722
}
727723

728724
public var rangeOfPath: NSRange {
729-
return _convertRange(_CFURLComponentsGetRangeOfPath(_components))
725+
return NSRange(_CFURLComponentsGetRangeOfPath(_components))
730726
}
731727

732728
public var rangeOfQuery: NSRange {
733-
return _convertRange(_CFURLComponentsGetRangeOfQuery(_components))
729+
return NSRange(_CFURLComponentsGetRangeOfQuery(_components))
734730
}
735731

736732
public var rangeOfFragment: NSRange {
737-
return _convertRange(_CFURLComponentsGetRangeOfFragment(_components))
733+
return NSRange(_CFURLComponentsGetRangeOfFragment(_components))
738734
}
739735

740736
// The getter method that underlies the queryItems property parses the query string based on these delimiters and returns an NSArray containing any number of NSURLQueryItem objects, each of which represents a single key-value pair, in the order in which they appear in the original query string. Note that a name may appear more than once in a single query string, so the name values are not guaranteed to be unique. If the NSURLComponents object has an empty query component, queryItems returns an empty NSArray. If the NSURLComponents object has no query component, queryItems returns nil.

0 commit comments

Comments
 (0)