Skip to content

Commit

Permalink
feat: adjusted Text parser to support char in the string (#2189)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
luckman212 authored Oct 27, 2024
1 parent bd3da69 commit 2721635
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Kit/Widgets/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}

0 comments on commit 2721635

Please sign in to comment.