]> git.r.bdr.sh - rbdr/map/blame_incremental - Map/MapParser/Strategies/VertexParserStrategy.swift
Release 1.1.0
[rbdr/map] / Map / MapParser / Strategies / VertexParserStrategy.swift
... / ...
CommitLineData
1import Foundation
2
3struct VertexParserStrategy: MapParserStrategy {
4 private let regex = MapParsingPatterns.vertex
5
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
10 }
11
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)
15
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)
22
23 var vertex = Vertex(
24 id: index,
25 label: key,
26 position: CGPoint(x: x, y: y)
27 )
28
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
32 }
33
34 return (Vertex.self, vertex)
35 }
36}