]> git.r.bdr.sh - rbdr/map/blob - Map/Logic/MapParser/Strategies/NoteParserStrategy.swift
4dae4c7aaed40f91d588c4b5b42514b2e75f3a83
[rbdr/map] / Map / Logic / MapParser / Strategies / NoteParserStrategy.swift
1 import Foundation
2
3 struct NoteParserStrategy: MapParserStrategy {
4 private let regex = MapParsingPatterns.note
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 == 5
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 let text = String(line[Range(match.range(at: 4), in: line)!])
18 let xString = String(line[Range(match.range(at: 2), in: line)!])
19 let yString = String(line[Range(match.range(at: 3), in: line)!])
20 let x = CGFloat(truncating: NumberFormatter().number(from: xString) ?? 0.0)
21 let y = CGFloat(truncating: NumberFormatter().number(from: yString) ?? 0.0)
22
23 let note = Note(
24 id: index,
25 position: CGPoint(x: x, y: y),
26 text: text
27 )
28
29 return (Note.self, note)
30 }
31 }