1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import Foundation
struct EdgeParserStrategy: MapParserStrategy {
private let regex = MapParsingPatterns.edge
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 == 4
}
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 arrowhead = String(line[Range(match.range(at: 2), in: line)!]) == ">"
let vertexA = String(line[Range(match.range(at: 1), in: line)!])
let vertexB = String(line[Range(match.range(at: 3), in: line)!])
if let origin = vertices[vertexA] {
if let destination = vertices[vertexB] {
let edge = MapEdge(
id: index, origin: origin.position, destination: destination.position,
arrowhead: arrowhead)
return (MapEdge.self, edge)
}
}
return (NSObject.self, NSObject()) // No matching object
}
}
|