forked from yanghuan/CSharp.lua
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNumber.lua
More file actions
420 lines (374 loc) · 9.59 KB
/
Number.lua
File metadata and controls
420 lines (374 loc) · 9.59 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
--[[
Copyright 2017 YANG Huan (sy.yanghuan@gmail.com).
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 define = System.defStc
local equals = System.equals
local zeroFn = System.zeroFn
local hash = System.hash
local debugsetmetatable = System.debugsetmetatable
local IComparable = System.IComparable
local IComparable_1 = System.IComparable_1
local IEquatable_1 = System.IEquatable_1
local IConvertible = System.IConvertible
local IFormattable = System.IFormattable
local ArgumentException = System.ArgumentException
local ArgumentNullException = System.ArgumentNullException
local FormatException = System.FormatException
local OverflowException = System.OverflowException
local type = type
local tonumber = tonumber
local floor = math.floor
local setmetatable = setmetatable
local tostring = tostring
local function hexForamt(x, n)
return n == "" and "%" .. x or "%0" .. n .. x
end
local function floatForamt(x, n)
return n == "" and "%.f" or "%." .. n .. 'f'
end
local function integerFormat(x, n)
return n == "" and "%d" or "%0" .. n .. 'd'
end
local function exponentialFormat(x, n)
return n == "" and "%" .. x or "%." .. n .. x
end
local formats = {
['x'] = hexForamt,
['X'] = hexForamt,
['f'] = floatForamt,
['F'] = floatForamt,
['d'] = integerFormat,
['D'] = integerFormat,
['e'] = exponentialFormat,
['E'] = exponentialFormat
}
local function toStringWithFormat(this, format)
if #format ~= 0 then
local i, j, x, n = format:find("^%s*([xXdDfFeE])(%d?)%s*$")
if i then
local f = formats[x]
if f then
format = f(x, n)
end
return format:format(this)
end
end
return tostring(this)
end
local function toString(this, format)
if format then
return toStringWithFormat(this, format)
end
return tostring(this)
end
local function compareInt(this, v)
if this < v then return -1 end
if this > v then return 1 end
return 0
end
local function inherits(_, T)
return { IComparable, IComparable_1(T), IEquatable_1(T), IConvertible, IFormattable }
end
local Int = define("System.Int", {
base = inherits,
default = zeroFn,
CompareTo = compareInt,
Equals = equals,
ToString = toString,
GetHashCode = hash,
CompareToObj = function (this, v)
if v == nil then return 1 end
if type(v) ~= "number" then
throw(ArgumentException("Arg_MustBeInt"))
end
return compareInt(this, v)
end,
EqualsObj = function (this, v)
if type(v) ~= "number" then
return false
end
return this == v
end
})
Int.__call = zeroFn
local function parseInt(s, min, max)
if s == nil then
return nil, 1
end
local v = tonumber(s)
if v == nil or v ~= floor(v) then
return nil, 2
end
if v < min or v > max then
return nil, 3
end
return v
end
local function tryParseInt(s, min, max)
local v = parseInt(s, min, max)
if v then
return true, v
end
return false, 0
end
local function parseIntWithException(s, min, max)
local v, err = parseInt(s, min, max)
if v then
return v
end
if err == 1 then
throw(ArgumentNullException())
elseif err == 2 then
throw(FormatException())
else
throw(OverflowException())
end
end
local SByte = define("System.SByte", {
Parse = function (s)
return parseIntWithException(s, -128, 127)
end,
TryParse = function (s)
return tryParseInt(s, -128, 127)
end
})
setmetatable(SByte, Int)
local Byte = define("System.Byte", {
Parse = function (s)
return parseIntWithException(s, 0, 255)
end,
TryParse = function (s)
return tryParseInt(s, 0, 255)
end
})
setmetatable(Byte, Int)
local Int16 = define("System.Int16", {
Parse = function (s)
return parseIntWithException(s, -32768, 32767)
end,
TryParse = function (s)
return tryParseInt(s, -32768, 32767)
end
})
setmetatable(Int16, Int)
local UInt16 = define("System.UInt16", {
Parse = function (s)
return parseIntWithException(s, 0, 65535)
end,
TryParse = function (s)
return tryParseInt(s, 0, 65535)
end
})
setmetatable(UInt16, Int)
local Int32 = define("System.Int32", {
Parse = function (s)
return parseIntWithException(s, -2147483648, 2147483647)
end,
TryParse = function (s)
return tryParseInt(s, -2147483648, 2147483647)
end
})
setmetatable(Int32, Int)
local UInt32 = define("System.UInt32", {
Parse = function (s)
return parseIntWithException(s, 0, 4294967295)
end,
TryParse = function (s)
return tryParseInt(s, 0, 4294967295)
end
})
setmetatable(UInt32, Int)
local Int64 = define("System.Int64", {
Parse = function (s)
return parseIntWithException(s, (-9223372036854775807 - 1), 9223372036854775807)
end,
TryParse = function (s)
return tryParseInt(s, (-9223372036854775807 - 1), 9223372036854775807)
end
})
setmetatable(Int64, Int)
local UInt64 = define("System.UInt64", {
Parse = function (s)
return parseIntWithException(s, 0, 18446744073709551615.0)
end,
TryParse = function (s)
return tryParseInt(s, 0, 18446744073709551615.0)
end
})
setmetatable(UInt64, Int)
local nan = 0 / 0
local posInf = 1 / 0
local negInf = - 1 / 0
--http://lua-users.org/wiki/InfAndNanComparisons
local function isNaN(v)
return v ~= v
end
local function compareDouble(this, v)
if this < v then return -1 end
if this > v then return 1 end
if this == v then return 0 end
if isNaN(this) then
return isNaN(v) and 0 or -1
else
return 1
end
end
local function equalsDouble(this, v)
if this == v then return true end
return isNaN(this) and isNaN(v)
end
local function equalsObj(this, v)
if type(v) ~= "number" then
return false
end
return equalsDouble(this, v)
end
local Number = define("System.Number", {
base = inherits,
default = zeroFn,
CompareTo = compareDouble,
Equals = equalsDouble,
ToString = toString,
NaN = nan,
IsNaN = isNaN,
NegativeInfinity = negInf,
PositiveInfinity = posInf,
EqualsObj = equalsObj,
GetHashCode = hash,
CompareToObj = function (this, v)
if v == nil then return 1 end
if type(v) ~= "number" then
throw(ArgumentException("Arg_MustBeNumber"))
end
return compareDouble(this, v)
end,
IsFinite = function (v)
return v ~= posInf and v ~= negInf and not isNaN(v)
end,
IsInfinity = function (v)
return v == posInf or v == negInf
end,
IsNegativeInfinity = function (v)
return v == negInf
end,
IsPositiveInfinity = function (v)
return v == posInf
end
})
Number.__call = zeroFn
if debugsetmetatable then
debugsetmetatable(0, Number)
end
local function parseDouble(s)
if s == nil then
return nil, 1
end
local v = tonumber(s)
if v == nil then
return nil, 2
end
return v
end
local function parseDoubleWithException(s)
local v, err = parseDouble(s)
if v then
return v
end
if err == 1 then
throw(ArgumentNullException())
else
throw(FormatException())
end
end
local Single = define("System.Single", {
Parse = function (s)
local v = parseDoubleWithException(s)
if v < -3.40282347E+38 or v > 3.40282347E+38 then
throw(OverflowException())
end
return v
end,
TryParse = function (s)
local v = parseDouble(s)
if v and v >= -3.40282347E+38 and v < 3.40282347E+38 then
return true, v
end
return false, 0
end
})
setmetatable(Single, Number)
local Double = define("System.Double", {
Parse = parseDoubleWithException,
TryParse = function (s)
local v = parseDouble(s)
if v then
return true, v
end
return false, 0
end
})
setmetatable(Double, Number)
if not debugsetmetatable then
local NullReferenceException = System.NullReferenceException
local systemToString = System.toString
function System.ObjectEqualsObj(this, obj)
if this == nil then throw(NullReferenceException()) end
local t = type(this)
if t == "number" then
return equalsObj(this, obj)
elseif t == "table" then
return this:EqualsObj(obj)
end
return this == obj
end
function System.ObjectGetHashCode(this)
if this == nil then throw(NullReferenceException()) end
if type(this) == "table" then
return this:GetHashCode()
end
return hash(this)
end
function System.ObjectToString(this)
if this == nil then throw(NullReferenceException()) end
return systemToString(this)
end
function System.IComparableCompareTo(this, other)
if this == nil then throw(NullReferenceException()) end
local t = type(this)
if t == "number" then
return compareDouble(this, other)
elseif t == "boolean" then
return System.Boolean.CompareTo(this, other)
end
return this:CompareTo(other)
end
function System.IEquatableEquals(this, other)
if this == nil then throw(NullReferenceException()) end
local t = type(this)
if t == "number" then
return equalsDouble(this, other)
elseif t == "boolean" then
return System.Boolean.Equals(this, other)
end
return this:Equals(other)
end
function System.IFormattableToString(this, format, formatProvider)
if this == nil then throw(NullReferenceException()) end
local t = type(this)
if t == "number" then
return toString(this, format, formatProvider)
end
return this:ToString(format, formatProvider)
end
end