From fa0f6ba017bda2057772f870e2853ec5567edd14 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Wed, 28 Dec 2022 15:19:30 +0800 Subject: [PATCH] LMReplacements // Upgrade parsing methods. --- .../SubLMs/lmReplacements.swift | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/SubLMs/lmReplacements.swift b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/SubLMs/lmReplacements.swift index ba75d4b1..9fe00802 100644 --- a/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/SubLMs/lmReplacements.swift +++ b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/SubLMs/lmReplacements.swift @@ -1,5 +1,4 @@ // (c) 2021 and onwards The vChewing Project (MIT-NTL License). -// StringView Ranges extension by (c) 2022 and onwards Isaac Xen (MIT License). // ==================== // This code is released under the MIT license (SPDX-License-Identifier: MIT) // ... with NTL restriction stating that: @@ -51,16 +50,15 @@ extension vChewingLM { public mutating func replaceData(textData rawStrData: String) { if strData == rawStrData { return } strData = rawStrData - strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach { - let neta = strData[$0].split(separator: " ") - if neta.count >= 2 { - let theKey = String(neta[0]) - if !neta[0].isEmpty, !neta[1].isEmpty, theKey.first != "#" { - let theValue = $0 - rangeMap[theKey] = theValue - } - } + var newMap: [String: Range] = [:] + strData.parse(splitee: "\n") { theRange in + let theCells = rawStrData[theRange].split(separator: " ") + if theCells.count < 2 { return } + let theKey = theCells[0].description + if theKey.first != "#" { newMap[theKey] = theRange } } + rangeMap = newMap + newMap.removeAll() } public mutating func clear() { @@ -102,17 +100,3 @@ extension vChewingLM { } } } - -// MARK: - StringView Ranges Extension (by Isaac Xen) - -extension String { - fileprivate func ranges(splitBy separator: Element) -> [Range] { - var startIndex = startIndex - return split(separator: separator).reduce(into: []) { ranges, substring in - _ = range(of: substring, range: startIndex..