]>
Commit | Line | Data |
---|---|---|
1 | // Copyright (C) 2024 Rubén Beltrán del Río | |
2 | ||
3 | // This program is free software: you can redistribute it and/or modify | |
4 | // it under the terms of the GNU General Public License as published by | |
5 | // the Free Software Foundation, either version 3 of the License, or | |
6 | // (at your option) any later version. | |
7 | ||
8 | // This program is distributed in the hope that it will be useful, | |
9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | // GNU General Public License for more details. | |
12 | ||
13 | // You should have received a copy of the GNU General Public License | |
14 | // along with this program. If not, see https://map.tranquil.systems. | |
15 | import Foundation | |
16 | ||
17 | struct GroupParserStrategy: MapParserStrategy { | |
18 | private let regex = MapParsingPatterns.group | |
19 | ||
20 | func canHandle(line: String) -> Bool { | |
21 | let range = NSRange(location: 0, length: line.utf16.count) | |
22 | let matches = regex.matches(in: String(line), options: [], range: range) | |
23 | return matches.count > 0 && matches[0].numberOfRanges == 3 | |
24 | } | |
25 | ||
26 | func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) { | |
27 | let range = NSRange(location: 0, length: line.utf16.count) | |
28 | let matches = regex.matches(in: String(line), options: [], range: range) | |
29 | ||
30 | let match = matches[0] | |
31 | var groupVertices: [Vertex] = [] | |
32 | let vertexIdString = String(line[Range(match.range(at: 2), in: line)!]) | |
33 | let vertexIds = vertexIdString.split(separator: ",", omittingEmptySubsequences: true).map( | |
34 | String.init | |
35 | ).map({ vertexId in | |
36 | vertexId.trimmingCharacters(in: .whitespacesAndNewlines) | |
37 | }) | |
38 | ||
39 | for vertexId in vertexIds { | |
40 | if let vertex = vertices[vertexId] { | |
41 | groupVertices.append(vertex) | |
42 | } | |
43 | } | |
44 | ||
45 | if groupVertices.count > 0 { | |
46 | return ([Vertex].self, groupVertices) | |
47 | } | |
48 | ||
49 | return (NSObject.self, NSObject()) | |
50 | } | |
51 | } |