Skip to content

Commit 54c948e

Browse files
Pushkar N Kulkarniparkera
authored andcommitted
Changing the NSRegularExpression API to match the Darwin version (#318)
* Changing the NSRegularExpression API to match the Darwin version * fixed typos and moved back to constants
1 parent 4fb767c commit 54c948e

File tree

3 files changed

+118
-84
lines changed

3 files changed

+118
-84
lines changed

Foundation/NSRegularExpression.swift

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public struct NSRegularExpressionOptions : OptionSet {
1616
public let rawValue : UInt
1717
public init(rawValue: UInt) { self.rawValue = rawValue }
1818

19-
public static let CaseInsensitive = NSRegularExpressionOptions(rawValue: 1 << 0) /* Match letters in the pattern independent of case. */
20-
public static let AllowCommentsAndWhitespace = NSRegularExpressionOptions(rawValue: 1 << 1) /* Ignore whitespace and #-prefixed comments in the pattern. */
21-
public static let IgnoreMetacharacters = NSRegularExpressionOptions(rawValue: 1 << 2) /* Treat the entire pattern as a literal string. */
22-
public static let DotMatchesLineSeparators = NSRegularExpressionOptions(rawValue: 1 << 3) /* Allow . to match any character, including line separators. */
23-
public static let AnchorsMatchLines = NSRegularExpressionOptions(rawValue: 1 << 4) /* Allow ^ and $ to match the start and end of lines. */
24-
public static let UseUnixLineSeparators = NSRegularExpressionOptions(rawValue: 1 << 5) /* Treat only \n as a line separator (otherwise, all standard line separators are used). */
25-
public static let UseUnicodeWordBoundaries = NSRegularExpressionOptions(rawValue: 1 << 6) /* Use Unicode TR#29 to specify word boundaries (otherwise, traditional regular expression word boundaries are used). */
19+
public static let caseInsensitive = NSRegularExpressionOptions(rawValue: 1 << 0) /* Match letters in the pattern independent of case. */
20+
public static let allowCommentsAndWhitespace = NSRegularExpressionOptions(rawValue: 1 << 1) /* Ignore whitespace and #-prefixed comments in the pattern. */
21+
public static let ignoreMetacharacters = NSRegularExpressionOptions(rawValue: 1 << 2) /* Treat the entire pattern as a literal string. */
22+
public static let dotMatchesLineSeparators = NSRegularExpressionOptions(rawValue: 1 << 3) /* Allow . to match any character, including line separators. */
23+
public static let anchorsMatchLines = NSRegularExpressionOptions(rawValue: 1 << 4) /* Allow ^ and $ to match the start and end of lines. */
24+
public static let useUnixLineSeparators = NSRegularExpressionOptions(rawValue: 1 << 5) /* Treat only \n as a line separator (otherwise, all standard line separators are used). */
25+
public static let useUnicodeWordBoundaries = NSRegularExpressionOptions(rawValue: 1 << 6) /* Use Unicode TR#29 to specify word boundaries (otherwise, traditional regular expression word boundaries are used). */
2626
}
2727

2828
public class NSRegularExpression : NSObject, NSCopying, NSCoding {
@@ -81,7 +81,7 @@ public class NSRegularExpression : NSObject, NSCopying, NSCoding {
8181

8282
/* This class method will produce a string by adding backslash escapes as necessary to the given string, to escape any characters that would otherwise be treated as pattern metacharacters.
8383
*/
84-
public class func escapedPatternForString(_ string: String) -> String {
84+
public class func escapedPattern(for string: String) -> String {
8585
return _CFRegularExpressionCreateEscapedPattern(string._cfObject)._swiftObject
8686
}
8787
}
@@ -90,23 +90,23 @@ public struct NSMatchingOptions : OptionSet {
9090
public let rawValue : UInt
9191
public init(rawValue: UInt) { self.rawValue = rawValue }
9292

93-
public static let ReportProgress = NSMatchingOptions(rawValue: 1 << 0) /* Call the block periodically during long-running match operations. */
94-
public static let ReportCompletion = NSMatchingOptions(rawValue: 1 << 1) /* Call the block once after the completion of any matching. */
95-
public static let Anchored = NSMatchingOptions(rawValue: 1 << 2) /* Limit matches to those at the start of the search range. */
96-
public static let WithTransparentBounds = NSMatchingOptions(rawValue: 1 << 3) /* Allow matching to look beyond the bounds of the search range. */
97-
public static let WithoutAnchoringBounds = NSMatchingOptions(rawValue: 1 << 4) /* Prevent ^ and $ from automatically matching the beginning and end of the search range. */
93+
public static let reportProgress = NSMatchingOptions(rawValue: 1 << 0) /* Call the block periodically during long-running match operations. */
94+
public static let reportCompletion = NSMatchingOptions(rawValue: 1 << 1) /* Call the block once after the completion of any matching. */
95+
public static let anchored = NSMatchingOptions(rawValue: 1 << 2) /* Limit matches to those at the start of the search range. */
96+
public static let withTransparentBounds = NSMatchingOptions(rawValue: 1 << 3) /* Allow matching to look beyond the bounds of the search range. */
97+
public static let withoutAnchoringBounds = NSMatchingOptions(rawValue: 1 << 4) /* Prevent ^ and $ from automatically matching the beginning and end of the search range. */
9898
internal static let OmitResult = NSMatchingOptions(rawValue: 1 << 13)
9999
}
100100

101101
public struct NSMatchingFlags : OptionSet {
102102
public let rawValue : UInt
103103
public init(rawValue: UInt) { self.rawValue = rawValue }
104104

105-
public static let Progress = NSMatchingFlags(rawValue: 1 << 0) /* Set when the block is called to report progress during a long-running match operation. */
106-
public static let Completed = NSMatchingFlags(rawValue: 1 << 1) /* Set when the block is called after completion of any matching. */
107-
public static let HitEnd = NSMatchingFlags(rawValue: 1 << 2) /* Set when the current match operation reached the end of the search range. */
108-
public static let RequiredEnd = NSMatchingFlags(rawValue: 1 << 3) /* Set when the current match depended on the location of the end of the search range. */
109-
public static let InternalError = NSMatchingFlags(rawValue: 1 << 4) /* Set when matching failed due to an internal error. */
105+
public static let progress = NSMatchingFlags(rawValue: 1 << 0) /* Set when the block is called to report progress during a long-running match operation. */
106+
public static let completed = NSMatchingFlags(rawValue: 1 << 1) /* Set when the block is called after completion of any matching. */
107+
public static let hitEnd = NSMatchingFlags(rawValue: 1 << 2) /* Set when the current match operation reached the end of the search range. */
108+
public static let requiredEnd = NSMatchingFlags(rawValue: 1 << 3) /* Set when the current match depended on the location of the end of the search range. */
109+
public static let internalError = NSMatchingFlags(rawValue: 1 << 4) /* Set when matching failed due to an internal error. */
110110
}
111111

112112
internal class _NSRegularExpressionMatcher {
@@ -143,7 +143,7 @@ extension NSRegularExpression {
143143
/* The fundamental matching method on NSRegularExpression is a block iterator. There are several additional convenience methods, for returning all matches at once, the number of matches, the first match, or the range of the first match. Each match is specified by an instance of NSTextCheckingResult (of type NSTextCheckingTypeRegularExpression) in which the overall match range is given by the range property (equivalent to rangeAtIndex:0) and any capture group ranges are given by rangeAtIndex: for indexes from 1 to numberOfCaptureGroups. {NSNotFound, 0} is used if a particular capture group does not participate in the match.
144144
*/
145145

146-
public func enumerateMatchesInString(_ string: String, options: NSMatchingOptions, range: NSRange, usingBlock block: (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Void) {
146+
public func enumerateMatches(in string: String, options: NSMatchingOptions, range: NSRange, using block: (NSTextCheckingResult?, NSMatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
147147
let matcher = _NSRegularExpressionMatcher(regex: self, block: block)
148148
withExtendedLifetime(matcher) { (m: _NSRegularExpressionMatcher) -> Void in
149149
#if os(OSX) || os(iOS)
@@ -155,9 +155,9 @@ extension NSRegularExpression {
155155
}
156156
}
157157

158-
public func matchesInString(_ string: String, options: NSMatchingOptions, range: NSRange) -> [NSTextCheckingResult] {
158+
public func matches(in string: String, options: NSMatchingOptions, range: NSRange) -> [NSTextCheckingResult] {
159159
var matches = [NSTextCheckingResult]()
160-
enumerateMatchesInString(string, options: options.subtract(.ReportProgress).subtract(.ReportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
160+
enumerateMatches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
161161
if let match = result {
162162
matches.append(match)
163163
}
@@ -166,26 +166,26 @@ extension NSRegularExpression {
166166

167167
}
168168

169-
public func numberOfMatchesInString(_ string: String, options: NSMatchingOptions, range: NSRange) -> Int {
169+
public func numberOfMatches(in string: String, options: NSMatchingOptions, range: NSRange) -> Int {
170170
var count = 0
171-
enumerateMatchesInString(string, options: options.subtract(.ReportProgress).subtract(.ReportCompletion).union(.OmitResult), range: range) {_,_,_ in
171+
enumerateMatches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion).union(.OmitResult), range: range) {_,_,_ in
172172
count += 1
173173
}
174174
return count
175175
}
176176

177-
public func firstMatchInString(_ string: String, options: NSMatchingOptions, range: NSRange) -> NSTextCheckingResult? {
177+
public func firstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSTextCheckingResult? {
178178
var first: NSTextCheckingResult?
179-
enumerateMatchesInString(string, options: options.subtract(.ReportProgress).subtract(.ReportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
179+
enumerateMatches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
180180
first = result
181181
stop.pointee = true
182182
}
183183
return first
184184
}
185185

186-
public func rangeOfFirstMatchInString(_ string: String, options: NSMatchingOptions, range: NSRange) -> NSRange {
186+
public func rangeOfFirstMatch(in string: String, options: NSMatchingOptions, range: NSRange) -> NSRange {
187187
var firstRange = NSMakeRange(NSNotFound, 0)
188-
enumerateMatchesInString(string, options: options.subtract(.ReportProgress).subtract(.ReportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
188+
enumerateMatches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range) { (result: NSTextCheckingResult?, flags: NSMatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) in
189189
if let match = result {
190190
firstRange = match.range
191191
} else {
@@ -210,16 +210,16 @@ extension NSRegularExpression {
210210

211211
/* NSRegularExpression also provides find-and-replace methods for both immutable and mutable strings. The replacement is treated as a template, with $0 being replaced by the contents of the matched range, $1 by the contents of the first capture group, and so on. Additional digits beyond the maximum required to represent the number of capture groups will be treated as ordinary characters, as will a $ not followed by digits. Backslash will escape both $ and itself.
212212
*/
213-
public func stringByReplacingMatchesInString(_ string: String, options: NSMatchingOptions, range: NSRange, withTemplate templ: String) -> String {
213+
public func stringByReplacingMatches(in string: String, options: NSMatchingOptions, range: NSRange, withTemplate templ: String) -> String {
214214
var str: String = ""
215215
let length = string.length
216216
var previousRange = NSMakeRange(0, 0)
217-
let results = matchesInString(string, options: options.subtract(.ReportProgress).subtract(.ReportCompletion), range: range)
217+
let results = matches(in: string, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range)
218218
let start = string.utf16.startIndex
219219

220220
for result in results {
221221
let currentRange = result.range
222-
let replacement = replacementStringForResult(result, inString: string, offset: 0, template: templ)
222+
let replacement = replacementString(for: result, in: string, offset: 0, template: templ)
223223
if currentRange.location > NSMaxRange(previousRange) {
224224
let min = start.advanced(by: NSMaxRange(previousRange))
225225
let max = start.advanced(by: currentRange.location)
@@ -238,13 +238,13 @@ extension NSRegularExpression {
238238
return str
239239
}
240240

241-
public func replaceMatchesInString(_ string: NSMutableString, options: NSMatchingOptions, range: NSRange, withTemplate templ: String) -> Int {
242-
let results = matchesInString(string._swiftObject, options: options.subtract(.ReportProgress).subtract(.ReportCompletion), range: range)
241+
public func replaceMatches(in string: NSMutableString, options: NSMatchingOptions, range: NSRange, withTemplate templ: String) -> Int {
242+
let results = matches(in: string._swiftObject, options: options.subtract(.reportProgress).subtract(.reportCompletion), range: range)
243243
var count = 0
244244
var offset = 0
245245
for result in results {
246246
var currentRnage = result.range
247-
let replacement = replacementStringForResult(result, inString: string._swiftObject, offset: offset, template: templ)
247+
let replacement = replacementString(for: result, in: string._swiftObject, offset: offset, template: templ)
248248
currentRnage.location += offset
249249

250250
string.replaceCharactersInRange(currentRnage, withString: replacement)
@@ -256,7 +256,7 @@ extension NSRegularExpression {
256256

257257
/* For clients implementing their own replace functionality, this is a method to perform the template substitution for a single result, given the string from which the result was matched, an offset to be added to the location of the result in the string (for example, in case modifications to the string moved the result since it was matched), and a replacement template.
258258
*/
259-
public func replacementStringForResult(_ result: NSTextCheckingResult, inString string: String, offset: Int, template templ: String) -> String {
259+
public func replacementString(for result: NSTextCheckingResult, in string: String, offset: Int, template templ: String) -> String {
260260
// ??? need to consider what happens if offset takes range out of bounds due to replacement
261261
struct once {
262262
static let characterSet = NSCharacterSet(charactersInString: "\\$")
@@ -329,7 +329,7 @@ extension NSRegularExpression {
329329

330330
/* This class method will produce a string by adding backslash escapes as necessary to the given string, to escape any characters that would otherwise be treated as template metacharacters.
331331
*/
332-
public class func escapedTemplateForString(_ string: String) -> String {
332+
public class func escapedTemplate(for string: String) -> String {
333333
return _CFRegularExpressionCreateEscapedPattern(string._cfObject)._swiftObject
334334
}
335335
}

Foundation/NSString.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,10 +529,10 @@ extension NSString {
529529

530530
internal func _rangeOfRegularExpressionPattern(regex pattern: String, options mask: NSStringCompareOptions, range searchRange: NSRange, locale: NSLocale?) -> NSRange {
531531
var matchedRange = NSMakeRange(NSNotFound, 0)
532-
let regexOptions: NSRegularExpressionOptions = mask.contains(.CaseInsensitiveSearch) ? .CaseInsensitive : []
533-
let matchingOptions: NSMatchingOptions = mask.contains(.AnchoredSearch) ? .Anchored : []
532+
let regexOptions: NSRegularExpressionOptions = mask.contains(.CaseInsensitiveSearch) ? .caseInsensitive : []
533+
let matchingOptions: NSMatchingOptions = mask.contains(.AnchoredSearch) ? .anchored : []
534534
if let regex = _createRegexForPattern(pattern, regexOptions) {
535-
matchedRange = regex.rangeOfFirstMatchInString(_swiftObject, options: matchingOptions, range: searchRange)
535+
matchedRange = regex.rangeOfFirstMatch(in: _swiftObject, options: matchingOptions, range: searchRange)
536536
}
537537
return matchedRange
538538
}
@@ -1093,10 +1093,10 @@ extension NSString {
10931093
}
10941094

10951095
internal func _stringByReplacingOccurrencesOfRegularExpressionPattern(_ pattern: String, withTemplate replacement: String, options: NSStringCompareOptions, range: NSRange) -> String {
1096-
let regexOptions: NSRegularExpressionOptions = options.contains(.CaseInsensitiveSearch) ? .CaseInsensitive : []
1097-
let matchingOptions: NSMatchingOptions = options.contains(.AnchoredSearch) ? .Anchored : []
1096+
let regexOptions: NSRegularExpressionOptions = options.contains(.CaseInsensitiveSearch) ? .caseInsensitive : []
1097+
let matchingOptions: NSMatchingOptions = options.contains(.AnchoredSearch) ? .anchored : []
10981098
if let regex = _createRegexForPattern(pattern, regexOptions) {
1099-
return regex.stringByReplacingMatchesInString(_swiftObject, options: matchingOptions, range: range, withTemplate: replacement)
1099+
return regex.stringByReplacingMatches(in: _swiftObject, options: matchingOptions, range: range, withTemplate: replacement)
11001100
}
11011101
return ""
11021102
}
@@ -1372,10 +1372,10 @@ extension NSMutableString {
13721372
}
13731373

13741374
internal func _replaceOccurrencesOfRegularExpressionPattern(_ pattern: String, withTemplate replacement: String, options: NSStringCompareOptions, range searchRange: NSRange) -> Int {
1375-
let regexOptions: NSRegularExpressionOptions = options.contains(.CaseInsensitiveSearch) ? .CaseInsensitive : []
1376-
let matchingOptions: NSMatchingOptions = options.contains(.AnchoredSearch) ? .Anchored : []
1375+
let regexOptions: NSRegularExpressionOptions = options.contains(.CaseInsensitiveSearch) ? .caseInsensitive : []
1376+
let matchingOptions: NSMatchingOptions = options.contains(.AnchoredSearch) ? .anchored : []
13771377
if let regex = _createRegexForPattern(pattern, regexOptions) {
1378-
return regex.replaceMatchesInString(self, options: matchingOptions, range: searchRange, withTemplate: replacement)
1378+
return regex.replaceMatches(in: self, options: matchingOptions, range: searchRange, withTemplate: replacement)
13791379
}
13801380
return 0
13811381
}

0 commit comments

Comments
 (0)