/
utilities.dart
93 lines (76 loc) · 3.13 KB
/
utilities.dart
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
// Portions of this work are Copyright 2018 The Time Machine Authors. All rights reserved.
// Portions of this work are Copyright 2018 The Noda Time Authors. All rights reserved.
// Use of this source code is governed by the Apache License 2.0, as found in the LICENSE.txt file.
import 'dart:math' as math;
// todo: should this be called Utility? or even be packaged like it is?
abstract class Platform {
static bool _checkForDartVM() {
double n = 1.0;
String s = n.toString();
if (s == '1.0')
return true;
else if (s == '1') return false;
return false;
}
static void startWeb() {
_isWeb = true;
_isVM = false;
}
static void startVM() {
_isWeb = false;
_isVM = true;
}
static Object dirtyCheck() {
var isDartVM = _checkForDartVM();
_isVM = isDartVM;
_isWeb = !_isVM;
return null;
}
static bool _isWeb = null;
static bool _isVM = null;
static bool get isWeb => _isWeb ?? dirtyCheck() ?? _isWeb;
static bool get isVM => _isVM ?? dirtyCheck() ?? _isVM;
static const int intMaxValueJS = 9007199254740992; // math.pow(2, 53);
static const int intMinValueJS = -9007199254740992; // -math.pow(2, 53); appears to be the same (not 1 more, not 1 less)
static const int int32MinValue = -2147483648;
static const int int32MaxValue = 2147483647;
// representable in JS and VM: +\- 9223372036854775000 (but, constants in JS must be bounded by intMinValueJS and intMaxValueJS)
// Fix for: https://github.com/dart-lang/sdk/issues/33282 <-- bizarre
static const int valueCursorPrediction = 13860*66546695792603; // vm: 922337203685477580; js: 922337203685477600
static const int int64MinValue = 2147483648 * 2147483648 * -2; // vm: -9223372036854775808; js: -9223372036854776000
static const int int64MaxValue = 2147483648 * 2147483648 - 1 + 2147483648 * 2147483648; // vm: 9223372036854775807; js: 9223372036854776000
static int _intMaxValue = isVM ? _intMaxValue = int64MaxValue : _intMaxValue = intMaxValueJS;
static int _intMinValue = isVM ? _intMinValue = int64MinValue : _intMinValue = intMinValueJS;
static int get intMaxValue => _intMaxValue;
static int get intMinValue => _intMinValue;
}
// todo: remove me
abstract class TimeZoneInfo {
// This is a BCL class
}
// todo: remove me
abstract class IDateTimeZoneWriter {
//
}
// https://en.wikipedia.org/wiki/Modulo_operation
// we should only use this where 'x' can be negative
int arithmeticMod(num x, int y) {
if (x >= 0) return x % y;
return -((-x)%y);
}
BigInt bigArithmeticMod(BigInt x, BigInt y) {
if (x.isNegative) return -((-x)%y);
return x % y;
}
// https://en.wikipedia.org/wiki/Arithmetic_shift#Handling_the_issue_in_programming_languages
// JS does bit shifting with two's complement preserved
// DartVM (like nearly everything else) does bit shifting arithmetically
// These shifts always work, so... I don't now if the `x>=0` check actually saves anytime or not.
int negRightShift(int x, int y) {
return -(~x >> y) -1;
}
int negLeftShift(int x, int y) {
return -~(x << y) -1;
}
int safeRightShift(int x, int y) => x >= 0 ? x >> y : -(~x >> y) -1;
int safeLeftShift(int x, int y) => x >= 0 ? x << y : -~(x << y) -1;