Skip to content

implement NSString.rangeOfCharacterFromSet #75

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 1 commit into from
Dec 8, 2015
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
30 changes: 27 additions & 3 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,39 @@ extension NSString {
}

public func rangeOfCharacterFromSet(searchSet: NSCharacterSet) -> NSRange {
NSUnimplemented()
return rangeOfCharacterFromSet(searchSet, options: [])
}

public func rangeOfCharacterFromSet(searchSet: NSCharacterSet, options mask: NSStringCompareOptions) -> NSRange {
NSUnimplemented()
return rangeOfCharacterFromSet(searchSet, options: mask, range: NSMakeRange(0, length))
}

public func rangeOfCharacterFromSet(searchSet: NSCharacterSet, options mask: NSStringCompareOptions, range searchRange: NSRange) -> NSRange {
NSUnimplemented()
if mask.contains(.RegularExpressionSearch) {
Copy link
Contributor

Choose a reason for hiding this comment

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

from my understanding this is actually ignored for character set searches

NSUnimplemented()
}
if searchRange.length == 0 {
return NSMakeRange(NSNotFound, 0)
}

#if os(Linux)
var cfflags = CFStringCompareFlags(mask.rawValue)
if mask.contains(.LiteralSearch) {
cfflags |= UInt(kCFCompareNonliteral)
Copy link
Contributor

Choose a reason for hiding this comment

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

probably these constants would be good to expose as a uniform re-export on OS X/iOS targets so that the linux (and potentially other platforms) would all have a similar interface.

}
#else
var cfflags = CFStringCompareFlags(rawValue: mask.rawValue)
if mask.contains(.LiteralSearch) {
cfflags.unionInPlace(.CompareNonliteral)
}
#endif
var result = CFRangeMake(kCFNotFound, 0)

Copy link
Contributor

Choose a reason for hiding this comment

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

there probably should be a precondition here but overall the implementation looks good to me.

if CFStringFindCharacterFromSet(_cfObject, searchSet._cfObject, CFRangeMake(searchRange.location, searchRange.length), cfflags, &result) {
return NSMakeRange(result.location, result.length)
} else {
return NSMakeRange(NSNotFound, 0)
}
}

public func rangeOfComposedCharacterSequenceAtIndex(index: Int) -> NSRange {
Expand Down
13 changes: 12 additions & 1 deletion TestFoundation/TestNSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TestNSString : XCTestCase {
("test_FromNullTerminatedCStringInASCII", test_FromNullTerminatedCStringInASCII ),
("test_FromNullTerminatedCStringInUTF8", test_FromNullTerminatedCStringInUTF8 ),
("test_FromMalformedNullTerminatedCStringInUTF8", test_FromMalformedNullTerminatedCStringInUTF8 ),
("test_rangeOfCharacterFromSet", test_rangeOfCharacterFromSet ),
]
}

Expand Down Expand Up @@ -141,4 +142,14 @@ class TestNSString : XCTestCase {
let string = NSString(CString: bytes.map { Int8(bitPattern: $0) }, encoding: NSUTF8StringEncoding)
XCTAssertNil(string)
}
}

func test_rangeOfCharacterFromSet() {
let string: NSString = "0Az"
let letters = NSCharacterSet.letterCharacterSet()
let decimalDigits = NSCharacterSet.decimalDigitCharacterSet()
XCTAssertEqual(string.rangeOfCharacterFromSet(letters).location, 1)
XCTAssertEqual(string.rangeOfCharacterFromSet(decimalDigits).location, 0)
XCTAssertEqual(string.rangeOfCharacterFromSet(letters, options: [.BackwardsSearch]).location, 2)
XCTAssertEqual(string.rangeOfCharacterFromSet(letters, options: [], range: NSMakeRange(2, 1)).location, 2)
}
}