]> git.r.bdr.sh - rbdr/map/blame - Map/Logic/MapParser/Strategies/NoteParserStrategy.swift
3.0.0
[rbdr/map] / Map / Logic / MapParser / Strategies / NoteParserStrategy.swift
CommitLineData
77d0155b
RBR
1import Foundation
2
fdb4633d
RBR
3struct NoteParserStrategy: MapParserStrategy {
4 private let regex = MapParsingPatterns.note
77d0155b
RBR
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)
fdb4633d 9 return matches.count > 0 && matches[0].numberOfRanges == 5
77d0155b
RBR
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]
fdb4633d 17 let text = String(line[Range(match.range(at: 4), in: line)!])
77d0155b
RBR
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
fdb4633d 23 let note = Note(
77d0155b 24 id: index,
fdb4633d
RBR
25 position: CGPoint(x: x, y: y),
26 text: text
77d0155b
RBR
27 )
28
fdb4633d 29 return (Note.self, note)
77d0155b
RBR
30 }
31}