DataCompiler // Fix Surrogate-Pair Compatibility Issue with RegEx Function.

This commit is contained in:
ShikiSuen 2022-02-28 14:05:54 +08:00
parent 0f0fc84090
commit ece91e3e3f
1 changed files with 2 additions and 1 deletions

View File

@ -21,9 +21,10 @@ import Foundation
// MARK: - // MARK: -
fileprivate extension String { fileprivate extension String {
mutating func regReplace(pattern: String, replaceWith: String = "") { mutating func regReplace(pattern: String, replaceWith: String = "") {
// Ref: https://stackoverflow.com/a/40993403/4162914 && https://stackoverflow.com/a/71291137/4162914
do { do {
let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive) let regex = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let range = NSRange(location: 0, length: count) let range = NSRange(location: 0, length: self.utf16.count)
self = regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith) self = regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: replaceWith)
} catch { return } } catch { return }
} }