Repository navigation
Expand file tree
/
Copy pathConvert.lua
More file actions
333 lines (302 loc) · 9.28 KB
/
Copy pathConvert.lua
File metadata and controls
333 lines (302 loc) · 9.28 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
--[[
Copyright 2017 YANG Huan ([email protected]).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local System = System
local throw = System.throw
local cast = System.cast
local as = System.as
local trunc = System.trunc
local identityFn = System.identityFn
local IConvertible = System.IConvertible
local OverflowException = System.OverflowException
local FormatException = System.FormatException
local ArgumentNullException = System.ArgumentNullException
local InvalidCastException = System.InvalidCastException
local SByte = System.SByte
local Byte = System.Byte
local Int16 = System.Int16
local UInt16 = System.UInt16
local Int32 = System.Int32
local UInt32 = System.UInt32
local Int64 = System.Int64
local UInt64 = System.UInt64
local Single = System.Single
local Double = System.Double
local Boolean = System.Boolean
local Char = System.Char
local DateTime = System.DateTime
local String = System.String
local Object = System.Object
local ParseSByte = SByte.Parse
local ParseByte = Byte.Parse
local ParseInt16 = Int16.Parse
local ParseUInt16 = UInt16.Parse
local ParseInt32 = Int32.Parse
local ParseUInt32 = UInt32.Parse
local ParseInt64 = Int64.Parse
local ParseUInt64 = UInt64.Parse
local ParseSingle = Single.Parse
local ParseDouble = Double.Parse
local ParseBoolean = Boolean.Parse
local type = type
local string = string
local sbyte = string.byte
local floor = math.floor
local getmetatable = getmetatable
local function toBoolean(value)
if value == nil then return false end
local typename = type(value)
if typename == "number" then
return value ~= 0
elseif typename == "string" then
return ParseBoolean(value)
elseif typename == "boolean" then
return value
else
return cast(IConvertible, value):ToBoolean()
end
end
local function toChar(value)
if value == nil then return 0 end
local typename = type(value)
if typename == "number" then
if value < 0 or value > 65535 then
throw(OverflowException("Overflow_Char"))
end
if value ~= floor(value) then
throw(InvalidCastException("InvalidCast_FromTo_Char"))
end
return value
elseif typename == "string" then
if #value ~= 1 then
throw(FormatException("Format_NeedSingleChar"))
end
return sbyte(value)
else
return cast(IConvertible, value):ToChar()
end
end
local function toNumber(value, min, max, parse, objectTo, sign)
if value == nil then return 0 end
local typename = type(value)
if typename == "number" then
if sign == nil then
if value < min or value > max then
throw(OverflowException())
end
return trunc(value)
elseif sign == 1 then
if value < min or value > max then
throw(OverflowException())
end
end
return value
elseif typename == "string" then
return parse(value)
elseif typename == "boolean" then
return value and 1 or 0
else
return objectTo(value)
end
end
local function toSByte(value)
local function objectTo(v)
return cast(IConvertible, value):ToSByte()
end
return toNumber(value, -128, 127, ParseSByte, objectTo)
end
local function toByte(value)
local function objectTo(v)
return cast(IConvertible, value):ToByte()
end
return toNumber(value, 0, 255, ParseByte, objectTo)
end
local function toInt16(value)
local function objectTo(v)
return cast(IConvertible, value):ToInt16()
end
return toNumber(value, -32768, 32767, ParseInt16, objectTo)
end
local function toUInt16(value)
local function objectTo(v)
return cast(IConvertible, value):ToUInt16()
end
return toNumber(value, 0, 65535, ParseUInt16, objectTo)
end
local function toInt32(value)
local function objectTo(v)
return cast(IConvertible, value):ToInt32()
end
return toNumber(value, -2147483648, 2147483647, ParseUInt16, objectTo)
end
local function toUInt32(value)
local function objectTo(v)
return cast(IConvertible, value):ToUInt32()
end
return toNumber(value, 0, 4294967295, ParseUInt32, objectTo)
end
local function toInt64(value)
local function objectTo(v)
return cast(IConvertible, value):ToInt64()
end
return toNumber(value, -9223372036854775808, 9223372036854775807, ParseInt64, objectTo)
end
local function toUInt64(value)
local function objectTo(v)
return cast(IConvertible, value):ToUInt64()
end
return toNumber(value, 0, 18446744073709551615, ParseUInt64, objectTo)
end
local function toSingle(value)
local function objectTo(v)
return cast(IConvertible, value):ToSingle()
end
return toNumber(value, -3.40282347E+38, 3.40282347E+38, ParseSingle, objectTo, 1)
end
local function toDouble(value)
local function objectTo(v)
return cast(IConvertible, value):ToDouble()
end
return toNumber(value, nil, nil, ParseDouble, objectTo, 2)
end
local function toDateTime(value)
if value == nil then return DateTime.MinValue end
if getmetatable(value) == DateTime then return value end
if type(value) == "string" then return DateTime.Parse(value) end
return cast(IConvertible, value):ToDateTime()
end
local function toBaseType(ic, targetType)
local cls = targetType.c
if cls == Boolean then return ic:ToBoolean() end
if cls == Char then return ic:ToChar() end
if cls == SByte then return ic:ToSByte() end
if cls == Byte then return ic:ToByte() end
if cls == Int16 then return ic:ToInt16() end
if cls == UInt16 then return ic:ToUInt16() end
if cls == Int32 then return ic:ToInt32() end
if cls == UInt32 then return ic:ToUInt32() end
if cls == Int64 then return ic:ToInt64() end
if cls == UInt64 then return ic:ToUInt64() end
if cls == Single then return ic:ToSingle() end
if cls == Double then return ic:ToDouble() end
if cls == DateTime then return ic:ToDateTime() end
if cls == String then return ic:ToString() end
if cls == Object then return value end
end
local function defaultToType(value, targetType)
if targetType == nil then throw(ArgumentNullException("targetType")) end
if value:GetType() == targetType then return value end
local v = toBaseType(value, targetType)
if v ~= nil then
return v
end
throw(InvalidCastException())
end
local function changeType(value, conversionType)
if conversionType == nil then
throw(ArgumentNullException("conversionType"))
end
if value == nil then
if conversionType:getIsValueType() then
throw(InvalidCastException("InvalidCast_CannotCastNullToValueType"))
end
return nil
end
local ic = as(value, IConvertible)
if ic == nil then
if value:GetType() == conversionType then
return value
end
throw(InvalidCastException("InvalidCast_IConvertible"))
end
local v = toBaseType(ic, conversionType)
if v ~= nil then
return v
end
return ic.ToType(conversionType)
end
System.define("System.Convert", {
ToBoolean = toBoolean,
ToChar = toChar,
ToSByte = toSByte,
ToByte = toByte,
ToInt16 = toInt16,
ToUInt16 = toUInt16,
ToInt32 = toInt32,
ToUInt32 = toUInt32,
ToInt64 = toInt64,
ToUInt64 = toUInt64,
ToSingle = toSingle,
ToDouble = toDouble,
ToDateTime = toDateTime,
ChangeType = changeType
})
String.ToBoolean = toBoolean
String.ToChar = toChar
String.ToSByte = toSByte
String.ToByte = toByte
String.ToInt16 = toInt16
String.ToUInt16 = toUInt16
String.ToInt32 = toInt32
String.ToUInt32 = toUInt32
String.ToInt64 = toInt64
String.ToUInt64 = toUInt64
String.ToSingle = identityFn
String.ToDouble = toDouble
String.ToDateTime = toDateTime
String.ToType = defaultToType
local function throwInvalidCastException()
throw(InvalidCastException())
end
local Number = System.Number
Number.ToBoolean = toBoolean
Number.ToChar = toChar
Number.ToSByte = toSByte
Number.ToByte = toByte
Number.ToInt16 = toInt16
Number.ToUInt16 = toUInt16
Number.ToInt32 = toInt32
Number.ToUInt32 = toUInt32
Number.ToInt64 = toInt64
Number.ToUInt64 = toUInt64
Number.ToSingle = toSingle
Number.ToDouble = toDouble
Number.ToDateTime = throwInvalidCastException
Number.ToType = defaultToType
Boolean.ToBoolean = identityFn
Boolean.ToChar = throwInvalidCastException
Boolean.ToSByte = toSByte
Boolean.ToByte = toByte
Boolean.ToInt16 = toInt16
Boolean.ToUInt16 = toUInt16
Boolean.ToInt32 = toInt32
Boolean.ToUInt32 = toUInt32
Boolean.ToInt64 = toInt64
Boolean.ToUInt64 = toUInt64
Boolean.ToSingle = toSingle
Boolean.ToDouble = toDouble
Boolean.ToDateTime = throwInvalidCastException
Boolean.ToType = defaultToType
DateTime.ToBoolean = throwInvalidCastException
DateTime.ToChar = throwInvalidCastException
DateTime.ToSByte = throwInvalidCastException
DateTime.ToByte = throwInvalidCastException
DateTime.ToInt16 = throwInvalidCastException
DateTime.ToUInt16 = throwInvalidCastException
DateTime.ToInt32 = throwInvalidCastException
DateTime.ToUInt32 = throwInvalidCastException
DateTime.ToInt64 = throwInvalidCastException
DateTime.ToUInt64 = throwInvalidCastException
DateTime.ToSingle = throwInvalidCastException
DateTime.ToDouble = throwInvalidCastException
DateTime.ToDateTime = identityFn
DateTime.ToType = defaultToType