-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathStationData.swift
More file actions
89 lines (78 loc) · 2.37 KB
/
StationData.swift
File metadata and controls
89 lines (78 loc) · 2.37 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// StationDataService.swift
// Train12306
//
// Created by fancymax on 15/8/4.
// Copyright (c) 2015年 fancy. All rights reserved.
//
import Foundation
struct Station {
//首字母拼音 比如 bj
var FirstLetter:String
//车站名
var Name:String
//电报码
var Code:String
//全拼
var Spell:String
}
class Regex {
let internalExpression: NSRegularExpression?
let pattern: String
init(_ pattern: String) {
self.pattern = pattern
do {
self.internalExpression = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
} catch {
self.internalExpression = nil
}
}
func getMatches(_ input: String) -> [[String]]? {
var res = [[String]]()
let myRange = NSMakeRange(0, input.count)
if let matches = self.internalExpression?.matches(in: input, options: [], range:myRange)
{
for match in matches
{
var groupMatch = [String]()
for i in 1..<match.numberOfRanges
{
let rangeText = (input as NSString).substring(with: match.range(at: i))
groupMatch.append(rangeText)
}
res.append(groupMatch)
}
}
if res.count > 0{
return res
}
else{
return nil
}
}
}
// "https://kyfw.12306.cn/otn/resources/js/framework/station_name.js"
class StationData{
var allStation:[Station]
var allStationMap:[String:Station]
init()
{
self.allStation = [Station]()
self.allStationMap = [String:Station]()
let path = Bundle.main.path(forResource: "station_name", ofType: "js")
let stationInfo = try! NSString(contentsOfFile: path!, encoding: String.Encoding.utf8.rawValue) as String
if let matches = Regex("@[a-z]+\\|([^\\|]+)\\|([a-z]+)\\|([a-z]+)\\|([a-z]+)\\|").getMatches(stationInfo)
{
for match in matches
{
let oneStation = Station(FirstLetter:match[3],Name:match[0],Code:match[1],Spell:match[2])
self.allStation.append(oneStation)
self.allStationMap[oneStation.Name] = oneStation
}
}
else
{
print("match station fail")
}
}
}