From bd74f0e1992b7849aa5762e186d265f6859c7c6a Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Mon, 28 Feb 2022 14:05:33 +0800 Subject: [PATCH] UPE // Fix Surrogate-Pair Compatibility Issue with RegEx Function. --- UserPhraseEditor/StringExtension.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/UserPhraseEditor/StringExtension.swift b/UserPhraseEditor/StringExtension.swift index 8ab7384d..fc3ca498 100644 --- a/UserPhraseEditor/StringExtension.swift +++ b/UserPhraseEditor/StringExtension.swift @@ -20,9 +20,10 @@ import Foundation extension String { mutating func regReplace(pattern: String, replaceWith: String = "") { + // Ref: https://stackoverflow.com/a/40993403/4162914 && https://stackoverflow.com/a/71291137/4162914 do { 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) } catch { return } }