import Foundation struct GroupParserStrategy: MapParserStrategy { private let regex = MapParsingPatterns.group func canHandle(line: String) -> Bool { let range = NSRange(location: 0, length: line.utf16.count) let matches = regex.matches(in: String(line), options: [], range: range) return matches.count > 0 && matches[0].numberOfRanges == 3 } func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) { let range = NSRange(location: 0, length: line.utf16.count) let matches = regex.matches(in: String(line), options: [], range: range) let match = matches[0] var groupVertices: [Vertex] = [] let vertexIdString = String(line[Range(match.range(at: 2), in: line)!]) let vertexIds = vertexIdString.split(separator: " ", omittingEmptySubsequences: true).map( String.init) for vertexId in vertexIds { if let vertex = vertices[vertexId] { groupVertices.append(vertex) } } if groupVertices.count > 0 { return ([Vertex].self, groupVertices) } return (NSObject.self, NSObject()) } }