diff options
Diffstat (limited to 'Map/Logic/MapParser/Strategies/BlockerParserStrategy.swift')
| -rw-r--r-- | Map/Logic/MapParser/Strategies/BlockerParserStrategy.swift | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Map/Logic/MapParser/Strategies/BlockerParserStrategy.swift b/Map/Logic/MapParser/Strategies/BlockerParserStrategy.swift new file mode 100644 index 0000000..d9b6f22 --- /dev/null +++ b/Map/Logic/MapParser/Strategies/BlockerParserStrategy.swift @@ -0,0 +1,26 @@ +import Foundation + +struct BlockerParserStrategy: MapParserStrategy { + private let regex = MapParsingPatterns.blocker + + 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] + let vertexA = String(line[Range(match.range(at: 2), in: line)!]) + + if let vertex = vertices[vertexA] { + let blocker = Blocker(id: index, position: vertex.position) + return (Blocker.self, blocker) + } + + return (NSObject.self, NSObject()) // No matching object + } +} |