From ece91e3e3f92bc9a7d754ef2eca546107151721c Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Mon, 28 Feb 2022 14:05:54 +0800 Subject: [PATCH] DataCompiler // Fix Surrogate-Pair Compatibility Issue with RegEx Function. --- DataCompiler/dataCompiler.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DataCompiler/dataCompiler.swift b/DataCompiler/dataCompiler.swift index 8a2f21ef..4128f4c6 100644 --- a/DataCompiler/dataCompiler.swift +++ b/DataCompiler/dataCompiler.swift @@ -21,9 +21,10 @@ import Foundation // MARK: - 前導工作 fileprivate 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 } }