forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicNumber.cs
More file actions
562 lines (472 loc) · 20.6 KB
/
DynamicNumber.cs
File metadata and controls
562 lines (472 loc) · 20.6 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using ServiceStack.Text;
using ServiceStack.Text.Common;
#if NETSTANDARD1_1
using Microsoft.Extensions.Primitives;
#endif
namespace ServiceStack
{
public interface IDynamicNumber
{
Type Type { get; }
object ConvertFrom(object value);
bool TryParse(string str, out object result);
string ToString(object value);
object add(object lhs, object rhs);
object sub(object lhs, object rhs);
object mul(object lhs, object rhs);
object div(object lhs, object rhs);
}
public class DynamicSByte : IDynamicNumber
{
public static DynamicSByte Instance = new DynamicSByte();
public Type Type => typeof(sbyte);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sbyte Convert(object value) => System.Convert.ToSByte(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToSByte(value);
public bool TryParse(string str, out object result)
{
if (sbyte.TryParse(str, out sbyte value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString();
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicByte : IDynamicNumber
{
public static DynamicByte Instance = new DynamicByte();
public Type Type => typeof(byte);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte Convert(object value) => System.Convert.ToByte(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToByte(value);
public bool TryParse(string str, out object result)
{
if (byte.TryParse(str, out byte value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString();
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicShort : IDynamicNumber
{
public static DynamicShort Instance = new DynamicShort();
public Type Type => typeof(short);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short Convert(object value) => System.Convert.ToInt16(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToInt16(value);
public bool TryParse(string str, out object result)
{
if (short.TryParse(str, out short value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString();
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicUShort : IDynamicNumber
{
public static DynamicUShort Instance = new DynamicUShort();
public Type Type => typeof(ushort);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ushort Convert(object value) => System.Convert.ToUInt16(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToUInt16(value);
public bool TryParse(string str, out object result)
{
if (ushort.TryParse(str, out ushort value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString();
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicInt : IDynamicNumber
{
public static DynamicInt Instance = new DynamicInt();
public Type Type => typeof(int);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Convert(object value) => System.Convert.ToInt32(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToInt32(value);
public bool TryParse(string str, out object result)
{
if (int.TryParse(str, out int value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString();
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicUInt : IDynamicNumber
{
public static DynamicUInt Instance = new DynamicUInt();
public Type Type => typeof(uint);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint Convert(object value) => System.Convert.ToUInt32(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToUInt32(value);
public bool TryParse(string str, out object result)
{
if (uint.TryParse(str, out uint value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString();
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicLong : IDynamicNumber
{
public static DynamicLong Instance = new DynamicLong();
public Type Type => typeof(long);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public long Convert(object value) => System.Convert.ToInt64(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToInt64(value);
public bool TryParse(string str, out object result)
{
if (long.TryParse(str, out long value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString();
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicULong : IDynamicNumber
{
public static DynamicULong Instance = new DynamicULong();
public Type Type => typeof(ulong);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong Convert(object value) => System.Convert.ToUInt64(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToUInt64(value);
public bool TryParse(string str, out object result)
{
if (ulong.TryParse(str, out ulong value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString();
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicFloat : IDynamicNumber
{
public static DynamicFloat Instance = new DynamicFloat();
public Type Type => typeof(float);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float Convert(object value) => System.Convert.ToSingle(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToSingle(value);
public bool TryParse(string str, out object result)
{
if (new StringSegment(str).TryParseFloat(out float value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString("r", CultureInfo.InvariantCulture);
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicDouble : IDynamicNumber
{
public static DynamicDouble Instance = new DynamicDouble();
public Type Type => typeof(double);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Convert(object value) => System.Convert.ToDouble(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToDouble(value);
public bool TryParse(string str, out object result)
{
if (new StringSegment(str).TryParseDouble(out double value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString("r", CultureInfo.InvariantCulture);
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public class DynamicDecimal : IDynamicNumber
{
public static DynamicDecimal Instance = new DynamicDecimal();
public Type Type => typeof(decimal);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public decimal Convert(object value) => System.Convert.ToDecimal(this.ParseString(value) ?? value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object ConvertFrom(object value) => this.ParseString(value)
?? System.Convert.ToDecimal(value);
public bool TryParse(string str, out object result)
{
if (new StringSegment(str).TryParseDecimal(out decimal value))
{
result = value;
return true;
}
result = null;
return false;
}
public string ToString(object value) => Convert(value).ToString(CultureInfo.InvariantCulture);
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
public object mul(object lhs, object rhs) => Convert(lhs) * Convert(rhs);
public object div(object lhs, object rhs) => Convert(lhs) / Convert(rhs);
}
public static class DynamicNumber
{
static readonly Dictionary<int, IDynamicNumber> RankNumbers = new Dictionary<int, IDynamicNumber>
{
{1, DynamicSByte.Instance},
{2, DynamicByte.Instance},
{3, DynamicShort.Instance},
{4, DynamicUShort.Instance},
{5, DynamicInt.Instance},
{6, DynamicUInt.Instance},
{7, DynamicLong.Instance},
{8, DynamicULong.Instance},
{9, DynamicFloat.Instance},
{10, DynamicDouble.Instance},
{11, DynamicDecimal.Instance},
};
public static bool IsNumber(Type type) => TryGetRanking(type, out _);
public static bool TryGetRanking(Type type, out int ranking)
{
ranking = -1;
switch (type.GetTypeCode())
{
case TypeCode.SByte:
ranking = 1;
break;
case TypeCode.Byte:
ranking = 2;
break;
case TypeCode.Int16:
ranking = 3;
break;
case TypeCode.UInt16:
ranking = 4;
break;
case TypeCode.Char:
case TypeCode.Int32:
ranking = 5;
break;
case TypeCode.UInt32:
ranking = 6;
break;
case TypeCode.Int64:
ranking = 7;
break;
case TypeCode.UInt64:
ranking = 8;
break;
case TypeCode.Single:
ranking = 9;
break;
case TypeCode.Double:
ranking = 10;
break;
case TypeCode.Decimal:
ranking = 11;
break;
}
return ranking > 0;
}
public static IDynamicNumber GetNumber(Type type)
{
if (!TryGetRanking(type, out int objIndex))
return null;
var maxNumber = RankNumbers[objIndex];
return maxNumber;
}
public static IDynamicNumber GetNumber(object lhs, object rhs)
{
if (lhs == null || rhs == null)
return null;
if (lhs is string lhsString && !TryParse(lhsString, out lhs))
return null;
if (rhs is string rhsString && !TryParse(rhsString, out rhs))
return null;
if (!TryGetRanking(lhs.GetType(), out int lhsRanking) || !TryGetRanking(rhs.GetType(), out int rhsRanking))
return null;
var maxRanking = Math.Max(lhsRanking, rhsRanking);
var maxNumber = RankNumbers[maxRanking];
return maxNumber;
}
public static IDynamicNumber AssertNumbers(string name, object lhs, object rhs)
{
var number = GetNumber(lhs, rhs);
if (number == null)
{
throw new ArgumentException($"Invalid numbers passed to {name}: " +
$"({lhs?.GetType().Name ?? "null"} '{lhs?.ToString().SubstringWithElipsis(0, 100)}', " +
$"{rhs?.GetType().Name ?? "null"} '{rhs?.ToString().SubstringWithElipsis(0, 100)}')");
}
return number;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Add(object lhs, object rhs) => AssertNumbers(nameof(Add), lhs, rhs).add(lhs, rhs);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Sub(object lhs, object rhs) => AssertNumbers(nameof(Subtract), lhs, rhs).sub(lhs, rhs);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Subtract(object lhs, object rhs) => AssertNumbers(nameof(Subtract), lhs, rhs).sub(lhs, rhs);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Mul(object lhs, object rhs) => AssertNumbers(nameof(Multiply), lhs, rhs).mul(lhs, rhs);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Multiply(object lhs, object rhs) => AssertNumbers(nameof(Multiply), lhs, rhs).mul(lhs, rhs);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Div(object lhs, object rhs) => AssertNumbers(nameof(Divide), lhs, rhs).div(lhs, rhs);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object Divide(object lhs, object rhs) => AssertNumbers(nameof(Divide), lhs, rhs).div(lhs, rhs);
public static bool TryParse(string strValue, out object result)
{
if (JsConfig.TryParseIntoBestFit)
return TryParseIntoBestFit(strValue, out result);
result = null;
if (!(strValue?.Length > 0))
return false;
if (strValue.Length == 1)
{
int singleDigit = strValue[0];
if (singleDigit >= 48 || singleDigit <= 57) // 0 - 9
{
result = singleDigit - 48; // 0
return true;
}
}
var hasDecimal = strValue.IndexOf('.') >= 0;
if (!hasDecimal)
{
if (int.TryParse(strValue, out int intValue))
{
result = intValue;
return true;
}
if (long.TryParse(strValue, out long longValue))
{
result = longValue;
return true;
}
if (ulong.TryParse(strValue, out ulong ulongValue))
{
result = ulongValue;
return true;
}
}
var segValue = new StringSegment(strValue);
if (segValue.TryParseDouble(out double doubleValue))
{
result = doubleValue;
return true;
}
if (segValue.TryParseDecimal(out decimal decimalValue))
{
result = decimalValue;
return true;
}
return false;
}
public static bool TryParseIntoBestFit(string strValue, out object result)
{
result = null;
if (!(strValue?.Length > 0))
return false;
var segValue = new StringSegment(strValue);
result = segValue.ParseNumber(bestFit:true);
return result != null;
}
}
internal static class DynamicNumberExtensions
{
internal static object ParseString(this IDynamicNumber number, object value)
{
if (value is string s)
return number.TryParse(s, out object x) ? x : null;
if (value is char c)
return number.TryParse(c.ToString(), out object x) ? x : null;
return null;
}
}
}