From 27216355d91b0f25b44004432f6b973d13983abd Mon Sep 17 00:00:00 2001 From: Luke Hamburg <1992842+luckman212@users.noreply.github.com> Date: Sun, 27 Oct 2024 07:21:18 -0400 Subject: [PATCH] feat: adjusted Text parser to support char in the string (#2189) Current implementation does not handle e.g. ``` $type $interface.displayName ($interface.BSDName) ``` This changes to a regex parser that does allow this. Signed-off-by: Luke Hamburg <1992842+luckman212@users.noreply.github.com> --- Kit/Widgets/Text.swift | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Kit/Widgets/Text.swift b/Kit/Widgets/Text.swift index 040af668..f0888d9e 100644 --- a/Kit/Widgets/Text.swift +++ b/Kit/Widgets/Text.swift @@ -74,15 +74,28 @@ public class TextWidget: WidgetWrapper { self.display() }) } - + static public func parseText(_ raw: String) -> [KeyValue_t] { var pairs: [KeyValue_t] = [] - raw.split(separator: " ", omittingEmptySubsequences: true).filter({ $0.hasPrefix("$") }).forEach { v in - let arr = v.split(separator: ".", omittingEmptySubsequences: true) - guard let key = arr.first else { return } - let value = arr.count == 1 ? nil : arr.last - pairs.append(KeyValue_t(key: String(key), value: String(value ?? ""))) + do { + let regex = try NSRegularExpression(pattern: "(\\$[a-zA-Z0-9_]+)(?:\\.([a-zA-Z0-9_]+))?") + let matches = regex.matches(in: raw, range: NSRange(raw.startIndex..., in: raw)) + for match in matches { + if let keyRange = Range(match.range(at: 1), in: raw) { + let key = String(raw[keyRange]) + let value: String? + if match.range(at: 2).location != NSNotFound, let valueRange = Range(match.range(at: 2), in: raw) { + value = String(raw[valueRange]) + } else { + value = nil + } + pairs.append(KeyValue_t(key: key, value: value ?? "")) + } + } + } catch { + print("Error creating regex: \(error.localizedDescription)") } return pairs } + }