From 6d874e78d9f2c1f3919dc4ae851a84c870f27dc2 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Wed, 28 Dec 2022 14:43:16 +0800 Subject: [PATCH] LMAssembly // Introducing StringView Ranges Extension MK2. Co-authored-by: IsaacXen --- .../LangModelAssembly/RangeParserAPI.swift | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/RangeParserAPI.swift diff --git a/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/RangeParserAPI.swift b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/RangeParserAPI.swift new file mode 100644 index 00000000..ca5c7d4b --- /dev/null +++ b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/RangeParserAPI.swift @@ -0,0 +1,49 @@ +// (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: +// No trademark license is granted to use the trade names, trademarks, service +// marks, or product names of Contributor, except as required to fulfill notice +// requirements defined in MIT License. + +import Foundation + +// MARK: - StringView Ranges Extension MK2 + +// Credit: Isaac Xen (MK1) & Shiki Suen (MK2) + +extension String { + /// 分析傳入的原始辭典檔案(UTF-8 TXT)的資料。 + /// - Parameters: + /// - separator: 行內單元分隔符。 + /// - task: 要執行的外包任務。 + func parse( + splitee separator: Element, + task: @escaping (_ theRange: Range) -> Void + ) { + var startIndex = startIndex + split(separator: separator).forEach { substring in + let theRange = range(of: substring, range: startIndex.. [Range] { + var startIndex = startIndex + return split(separator: separator).reduce(into: []) { ranges, substring in + _ = range(of: substring, range: startIndex..