From dda28c5e2e5d1ec5d6450807ecc7ba8c2fc0b674 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Mon, 28 Feb 2022 14:05:46 +0800 Subject: [PATCH] KanjiConv // Fix Surrogate-Pair Compatibility Issue with RegEx Function. --- Source/Modules/ControllerModules/vChewingKanjiConverter.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Modules/ControllerModules/vChewingKanjiConverter.swift b/Source/Modules/ControllerModules/vChewingKanjiConverter.swift index e778b53c..ef8b52fd 100644 --- a/Source/Modules/ControllerModules/vChewingKanjiConverter.swift +++ b/Source/Modules/ControllerModules/vChewingKanjiConverter.swift @@ -21,10 +21,11 @@ import Cocoa // 這個模組果然還是要用 Regex 轉換會比較爽。 // 為防止與其他模組彼此發生可能的衝突,這段 String 擴展就用 private 開頭來私有化。 private extension String { + // Ref: https://stackoverflow.com/a/40993403/4162914 && https://stackoverflow.com/a/71291137/4162914 mutating func regReplace(pattern: String, replaceWith: String = "") { 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 } }