]> git.r.bdr.sh - rbdr/map/blob - Map/Logic/MapParser/Strategies/GroupParserStrategy.swift
Begin re-licensing
[rbdr/map] / Map / Logic / MapParser / Strategies / GroupParserStrategy.swift
1 import Foundation
2
3 struct GroupParserStrategy: MapParserStrategy {
4 private let regex = MapParsingPatterns.group
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 == 3
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 var groupVertices: [Vertex] = []
18 let vertexIdString = String(line[Range(match.range(at: 2), in: line)!])
19 let vertexIds = vertexIdString.split(separator: " ", omittingEmptySubsequences: true).map(
20 String.init)
21
22 for vertexId in vertexIds {
23 if let vertex = vertices[vertexId] {
24 groupVertices.append(vertex)
25 }
26 }
27
28 if groupVertices.count > 0 {
29 return ([Vertex].self, groupVertices)
30 }
31
32 return (NSObject.self, NSObject())
33 }
34 }