Skip to content

Commit f52e394

Browse files
committed
Use regex to insert dots before and after any non number
1 parent 0a631e3 commit f52e394

File tree

1 file changed

+1
-27
lines changed

1 file changed

+1
-27
lines changed

WooCommerce/Classes/Extensions/String+Helpers.swift

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,7 @@ extension String {
9191
/// Insert a . before and after any non number
9292
///
9393
func insertDotsBeforeAndAfterAnyNonNumber(inString: String) -> String {
94-
guard inString.count > 1 else {
95-
return inString
96-
}
97-
98-
/// Initialize our output with the first character of the input string
99-
var outString = String(inString[inString.index(inString.startIndex, offsetBy: 0)])
100-
101-
/// Loop over the remaining characters in the string, inserting . as needed
102-
for index in 1...inString.count - 1 {
103-
let characterAtIndex = inString[inString.index(inString.startIndex, offsetBy: index)]
104-
let characterBefore = inString[inString.index(inString.startIndex, offsetBy: index - 1)]
105-
106-
let characterAtIndexIsNumberOrDot = characterAtIndex.isNumber || characterAtIndex == "."
107-
let characterBeforeIsNumberOrDot = characterBefore.isNumber || characterBefore == "."
108-
109-
if !characterAtIndexIsNumberOrDot && characterBefore.isNumber {
110-
outString += "."
111-
}
112-
113-
if characterAtIndex.isNumber && !characterBeforeIsNumberOrDot {
114-
outString += "."
115-
}
116-
117-
outString += String(characterAtIndex)
118-
}
119-
120-
return outString
94+
inString.replacingOccurrences(of: "([^0-9.]+)", with: ".$1.", options: .regularExpression)
12195
}
12296

12397
/// Score and compare two string components

0 commit comments

Comments
 (0)