Reputation: 18591
The toCamel()
function in this String extension is supposed to remove any _
followed by a character in the middle of a String, and replace the two with the character to uppercase.
public extension String {
public func rangeFromNSRange(aRange: NSRange) -> Range<String.Index> {
let s = advance(self.startIndex, aRange.location)
let e = advance(self.startIndex, aRange.location + aRange.length)
return s..<e
}
public var ns : NSString {return self as NSString}
public subscript (aRange: NSRange) -> String? {
get {return self.substringWithRange(self.rangeFromNSRange(aRange))}
}
public var cdr: String {return isEmpty ? "" : String(characters.dropFirst())}
public func toCamel() throws -> String {
var s = self
let regex = try NSRegularExpression(pattern: "_.", options: [])
let matches = regex.matchesInString(s, options:[], range:NSMakeRange(0, s.ns.length)).reverse()
for match in matches {
print("match = \(s[match.range]!)")
let matchRange = s.rangeFromNSRange(match.range)
let uc = s[match.range]!.uppercaseString.cdr
s.replaceRange(matchRange, with: uc)
}
if s.hasPrefix("_") {s = s.cdr}
return s
}
}
The thing is that matchesInString
doesn't return the matches where the _
is already part of the previous match. And thus, it gives the following results :
try "hello_world".toCamel() //"helloWorld"
try "hello__world".toCamel() //"hello_world"
try "hello___world".toCamel() //"hello_World"
How to go around this?
Upvotes: 0
Views: 5414
Reputation: 93191
Change you pattern to _+(.)
. This matches one or more underscores followed by another character. The round brackets place that character into a capture group. You can get back the captured character with rangeAtIndex
public func toCamel() throws -> String {
var s = self
let regex = try NSRegularExpression(pattern: "_+(.)", options: [])
let matches = regex.matchesInString(s, options:[], range:NSMakeRange(0, s.ns.length)).reverse()
for match in matches {
print("match = \(s[match.range]!)")
let matchRange = s.rangeFromNSRange(match.range) // the whole match range
let replaceRange = match.rangeAtIndex(1) // range of the capture group
let uc = s[replaceRange]!.uppercaseString
s.replaceRange(matchRange, with: uc)
}
if s.hasPrefix("_") {s = s.cdr}
return s
}
Examples:
"hello_world".toCamel() // helloWorld
"hello___world".toCamel() // helloWorld
"hello_beautiful____world".toCamel() // helloBeautifulWolrd
public extension String {
public func rangeFromNSRange(aRange: NSRange) -> Range<String.Index> {
let s = self.startIndex.advancedBy(aRange.location)
let e = self.startIndex.advancedBy(aRange.location + aRange.length)
return s..<e
}
public var ns : NSString {return self as NSString}
public subscript (aRange: NSRange) -> String? {
get {return self.substringWithRange(self.rangeFromNSRange(aRange))}
}
public var cdr: String {return isEmpty ? "" : String(characters.dropFirst())}
public func toCamel() throws -> String {
var s = self
let regex = try NSRegularExpression(pattern: "_+(.)", options: [])
let matches = regex.matchesInString(s, options:[], range:NSMakeRange(0, s.ns.length)).reverse()
for match in matches {
print("match = \(s[match.range]!)")
let matchRange = s.rangeFromNSRange(match.range) // the whole match range
let replaceRange = match.rangeAtIndex(1) // range of the capture group
let uc = s[replaceRange]!.uppercaseString
s.replaceRange(matchRange, with: uc)
}
if s.hasPrefix("_") {s = s.cdr}
return s
}
}
Upvotes: 4
Reputation: 1242
There are two problems in your code.
First the regex matching your requirement should be _+[^_]{1}
.
Second in your cdr
computed proterty, String(characters.dropFirst())
only exclude the first _
character. You may use String(characters.suffix(1))
Edited code:
public extension String {
public func rangeFromNSRange(aRange: NSRange) -> Range<String.Index> {
let s = advance(self.startIndex, aRange.location)
let e = advance(self.startIndex, aRange.location + aRange.length)
return s..<e
}
public var ns : NSString {return self as NSString}
public subscript (aRange: NSRange) -> String? {
get {return self.substringWithRange(self.rangeFromNSRange(aRange))}
}
public var cdr: String {return isEmpty ? "" : String(characters.suffix(1))}
public func toCamel() throws -> String {
var s = self
let regex = try NSRegularExpression(pattern: "_+[^_]{1}", options: [])
let matches = regex.matchesInString(s, options:[], range:NSMakeRange(0, s.ns.length)).reverse()
for match in matches {
print("match = \(s[match.range]!)")
let matchRange = s.rangeFromNSRange(match.range)
let uc = s[match.range]!.uppercaseString.cdr
s.replaceRange(matchRange, with: uc)
}
if s.hasPrefix("_") {s = s.cdr}
return s
}
}
I tested this in the playground.
Upvotes: 2
Reputation: 42598
try NSRegularExpression(pattern: "_+.", options: [])
You should also read NSRegularExpression Class Reference.
+
Match 1 or more times. Match as many times as possible.
Upvotes: 0