forked from studoverse/Kotlift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kotlift.swift
225 lines (201 loc) · 4.92 KB
/
kotlift.swift
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import Foundation
extension Array {
var size: Int { return self.count }
mutating func add(element: Element) {
self.append(element)
}
mutating func addAll(other: Array) {
for element in other {
self.append(element)
}
}
func get(index: Int) -> Element {
return self[index]
}
mutating func clear() {
self.removeAll()
}
}
extension Dictionary {
var size: Int { return self.count }
mutating func put(key: Key, _ value: Value) {
self[key] = value
}
mutating func putAll(other: Dictionary) {
for (key, value) in other {
self.updateValue(value, forKey: key)
}
}
func isEmpty() -> Bool {
return self.isEmpty
}
mutating func remove(key: Key) -> Value? {
return self.removeValueForKey(key)
}
mutating func clear() {
self.removeAll()
}
}
extension Set {
var size: Int { return self.count }
mutating func add(element: Element) {
self.insert(element)
}
mutating func addAll(other: Set) {
for element in other {
self.insert(element)
}
}
func isEmpty() -> Bool {
return self.isEmpty
}
mutating func clear() {
self.removeAll()
}
}
extension String {
var length: Int { return characters.count }
func substring(startIndex: Int, _ endIndex: Int) -> String {
let start = self.startIndex.advancedBy(startIndex)
let end = self.startIndex.advancedBy(endIndex)
return self.substringWithRange(Range<String.Index>(start: start, end: end))
}
func substring(startIndex: Int) -> String {
return substring(startIndex, self.length)
}
func contains(string: String) -> Bool {
return self.rangeOfString(string) != nil
}
func replace(target: String, _ withString: String) -> String {
return self.stringByReplacingOccurrencesOfString(target,
withString: withString,
options: NSStringCompareOptions.LiteralSearch,
range: nil)
}
func isEmpty() -> Bool {
return isEmpty
}
func split(p: String) -> [String] {
return self.componentsSeparatedByString(p)
}
}
protocol _StringType { }
extension String: _StringType { }
extension Array where Element: _StringType {
func joinToString(separator separator: String = "") -> String {
var retval = ""
var first = true
for rawObject in self {
let element = rawObject as! String
if (first) {
retval += element
} else {
retval += separator + element
}
first = false
}
return retval
}
}
enum Exception: ErrorType {
case NumberFormat
}
class Integer {
static func parseInt(str: String) throws -> Int {
if let intVal = Int(str) {
return intVal
}
throw Exception.NumberFormat
}
}
class System {
static func currentTimeMillis() -> Int64 {
return (Int64) (NSDate().timeIntervalSince1970 * 1000.0)
}
}
func / (lhs: Double, rhs: Int) -> Double {
return Double(lhs) / Double(rhs)
}
func / (lhs: Int, rhs: Double) -> Double {
return Double(lhs) / Double(rhs)
}
protocol Addable {}
extension Int: Addable {}
extension Double: Addable {}
func + (a: Int, b: Double) -> Double {
return Double(a) + b
}
func + (a: Double, b: Int) -> Double {
return Double(b) + a
}
func + (a: Int64, b: Double) -> Double {
return Double(a) + b
}
func + (a: Double, b: Int64) -> Double {
return Double(b) + a
}
func + (a: Int32, b: Double) -> Double {
return Double(a) + b
}
func + (a: Double, b: Int32) -> Double {
return Double(b) + a
}
func + (a: Int16, b: Double) -> Double {
return Double(a) + b
}
func + (a: Double, b: Int16) -> Double {
return Double(b) + a
}
func + (a: Int8, b: Double) -> Double {
return Double(a) + b
}
func + (a: Double, b: Int8) -> Double {
return Double(b) + a
}
func == (a: Int32, b: Int) -> Bool {
return Int(a) == b
}
func == (a: Int, b: Int32) -> Bool {
return Int(b) == a
}
func == (a: Int32, b: Int64) -> Bool {
return Int64(a) == b
}
func == (a: Int64, b: Int32) -> Bool {
return Int64(b) == a
}
func == (a: Int, b: Int64) -> Bool {
return Int64(a) == b
}
func == (a: Int64, b: Int) -> Bool {
return Int64(b) == a
}
func != (a: Int32, b: Int) -> Bool {
return Int(a) != b
}
func != (a: Int, b: Int32) -> Bool {
return Int(b) != a
}
func != (a: Int32, b: Int64) -> Bool {
return Int64(a) != b
}
func != (a: Int64, b: Int32) -> Bool {
return Int64(b) != a
}
func != (a: Int, b: Int64) -> Bool {
return Int64(a) != b
}
func != (a: Int64, b: Int) -> Bool {
return Int64(b) != a
}
// Nil Coalescing Operator for use with statements. E.g. try a ?! { throw Error() }
infix operator ?! { associativity right precedence 131 }
public func ?! <T> (optional: Optional<T>, throwsStatement: () throws -> Void) rethrows -> T {
switch optional {
case .Some(let value):
return value
case .None:
try throwsStatement()
return optional! // This line will never be executed when thowsStatement actually throws
}
}