]> git.r.bdr.sh - rbdr/map/blob - Map/MapParser/Strategies/StageParserStrategy.swift
Fix performance and undo
[rbdr/map] / Map / MapParser / Strategies / StageParserStrategy.swift
1 import Foundation
2
3 struct StageParserStrategy: MapParserStrategy {
4 private let regex = MapParsingPatterns.stage
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 == 3
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 stage = String(line[Range(match.range(at: 1), in: line)!])
18 let dimensionsString = String(line[Range(match.range(at: 2), in: line)!])
19 let dimensions = CGFloat(truncating: NumberFormatter().number(from: dimensionsString) ?? 0.0)
20
21 let stageDimensions = StageDimensions(index: stage.count - 1, dimensions: dimensions)
22 return (StageDimensions.self, stageDimensions)
23 }
24 }