3
3
* representations.
4
4
*/
5
5
6
+ /**
7
+ * Gets the integer value of `binary` when interpreted as binary. `binary` must
8
+ * contain only the digits 0 and 1. For values greater than
9
+ * 01111111111111111111111111111111 (2^31-1, the maximum value that `int` can
10
+ * represent), there is no result.
11
+ *
12
+ * ```
13
+ * "0" => 0
14
+ * "01" => 1
15
+ * "1010101" => 85
16
+ * ```
17
+ */
18
+ bindingset [ binary]
19
+ int parseBinaryInt ( string binary ) {
20
+ exists ( string stripped | stripped = stripLeadingZeros ( binary ) |
21
+ stripped .length ( ) <= 31 and
22
+ result >= 0 and
23
+ result =
24
+ sum ( int index , string c , int digit |
25
+ c = stripped .charAt ( index ) and
26
+ digit = "01" .indexOf ( c )
27
+ |
28
+ twoToThe ( stripped .length ( ) - 1 - index ) * digit
29
+ )
30
+ )
31
+ }
32
+
6
33
/**
7
34
* Gets the integer value of `hex` when interpreted as hex. `hex` must be a
8
- * valid hexadecimal string and, for integer-wrapping reasons, no longer than 6
9
- * digits .
35
+ * valid hexadecimal string. For values greater than 7FFFFFFF (2^31-1, the
36
+ * maximum value that `int` can represent), there is no result .
10
37
*
11
38
* ```
12
39
* "0" => 0
16
43
*/
17
44
bindingset [ hex]
18
45
int parseHexInt ( string hex ) {
19
- hex .length ( ) <= 6 and
20
- result =
21
- sum ( int index , string c |
22
- c = hex .charAt ( index )
23
- |
24
- sixteenToThe ( hex .length ( ) - 1 - index ) * toHex ( c )
25
- )
46
+ exists ( string stripped | stripped = stripLeadingZeros ( hex ) |
47
+ stripped .length ( ) <= 8 and
48
+ result >= 0 and
49
+ result =
50
+ sum ( int index , string c |
51
+ c = stripped .charAt ( index )
52
+ |
53
+ sixteenToThe ( stripped .length ( ) - 1 - index ) * toHex ( c )
54
+ )
55
+ )
26
56
}
27
57
28
58
/**
29
59
* Gets the integer value of `octal` when interpreted as octal. `octal` must be
30
- * a valid octal string and, for integer-wrapping reasons, no longer than 10
31
- * digits.
60
+ * a valid octal string containing only the digits 0-7. For values greater than
61
+ * 17777777777 (2^31-1, the maximum value that `int` can represent), there is no
62
+ * result.
32
63
*
33
64
* ```
34
65
* "0" => 0
@@ -38,13 +69,17 @@ int parseHexInt(string hex) {
38
69
*/
39
70
bindingset [ octal]
40
71
int parseOctalInt ( string octal ) {
41
- octal .length ( ) <= 10 and
42
- result =
43
- sum ( int index , string c |
44
- c = octal .charAt ( index )
45
- |
46
- eightToThe ( octal .length ( ) - 1 - index ) * toOctal ( c )
47
- )
72
+ exists ( string stripped | stripped = stripLeadingZeros ( octal ) |
73
+ stripped .length ( ) <= 11 and
74
+ result >= 0 and
75
+ result =
76
+ sum ( int index , string c , int digit |
77
+ c = stripped .charAt ( index ) and
78
+ digit = "01234567" .indexOf ( c )
79
+ |
80
+ eightToThe ( stripped .length ( ) - 1 - index ) * digit
81
+ )
82
+ )
48
83
}
49
84
50
85
/** Gets the integer value of the `hex` char. */
@@ -65,33 +100,30 @@ private int toHex(string hex) {
65
100
result = 15 and hex = [ "f" , "F" ]
66
101
}
67
102
68
- /** Gets the integer value of the `octal` char. */
69
- private int toOctal ( string octal ) {
70
- octal = "0" and result = 0
71
- or
72
- octal = "1" and result = 1
73
- or
74
- octal = "2" and result = 2
75
- or
76
- octal = "3" and result = 3
77
- or
78
- octal = "4" and result = 4
79
- or
80
- octal = "5" and result = 5
81
- or
82
- octal = "6" and result = 6
83
- or
84
- octal = "7" and result = 7
85
- }
86
-
87
- /** Gets the value of 16 to the power of `n`. */
103
+ /**
104
+ * Gets the value of 16 to the power of `n`. Holds only for `n` in the range
105
+ * 0..7 (inclusive).
106
+ */
88
107
int sixteenToThe ( int n ) {
89
108
// 16**7 is the largest power of 16 that fits in an int.
90
109
n in [ 0 .. 7 ] and result = 1 .bitShiftLeft ( 4 * n )
91
110
}
92
111
93
- /** Gets the value of 8 to the power of `n`. */
112
+ /**
113
+ * Gets the value of 8 to the power of `n`. Holds only for `n` in the range
114
+ * 0..10 (inclusive).
115
+ */
94
116
int eightToThe ( int n ) {
95
117
// 8**10 is the largest power of 8 that fits in an int.
96
118
n in [ 0 .. 10 ] and result = 1 .bitShiftLeft ( 3 * n )
97
119
}
120
+
121
+ /**
122
+ * Gets the value of 2 to the power of `n`. Holds only for `n` in the range
123
+ * 0..30 (inclusive).
124
+ */
125
+ int twoToThe ( int n ) { n in [ 0 .. 30 ] and result = 1 .bitShiftLeft ( n ) }
126
+
127
+ /** Gets `s` with any leading "0" characters removed. */
128
+ bindingset [ s]
129
+ private string stripLeadingZeros ( string s ) { result = s .regexpCapture ( "0*(.*)" , 1 ) }
0 commit comments