3 struct VertexParserStrategy: MapParserStrategy {
4 private let regex = MapParsingPatterns.vertex
6 func canHandle(line: String) -> Bool {
7 let range = NSRange(location: 0, length: line.utf16.count)
8 let matches = regex.matches(in: String(line), options: [], range: range)
9 return matches.count > 0 && matches[0].numberOfRanges >= 4
12 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
13 let range = NSRange(location: 0, length: line.utf16.count)
14 let matches = regex.matches(in: String(line), options: [], range: range)
16 let match = matches[0]
17 let key = String(line[Range(match.range(at: 1), in: line)!])
18 let xString = String(line[Range(match.range(at: 2), in: line)!])
19 let yString = String(line[Range(match.range(at: 3), in: line)!])
20 let x = CGFloat(truncating: NumberFormatter().number(from: xString) ?? 0.0)
21 let y = CGFloat(truncating: NumberFormatter().number(from: yString) ?? 0.0)
26 position: CGPoint(x: x, y: y)
29 if let range = Range(match.range(at: 4), in: line) {
30 let shapeString = String(line[range])
31 vertex.shape = VertexShape(rawValue: shapeString.lowercased()) ?? .circle
34 return (Vertex.self, vertex)