33 * representations.
44 */
55
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+
633/**
734 * 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 .
1037 *
1138 * ```
1239 * "0" => 0
1643 */
1744bindingset [ hex]
1845int 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+ )
2656}
2757
2858/**
2959 * 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.
3263 *
3364 * ```
3465 * "0" => 0
@@ -38,13 +69,17 @@ int parseHexInt(string hex) {
3869 */
3970bindingset [ octal]
4071int 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+ )
4883}
4984
5085/** Gets the integer value of the `hex` char. */
@@ -65,33 +100,30 @@ private int toHex(string hex) {
65100 result = 15 and hex = [ "f" , "F" ]
66101}
67102
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+ */
88107int sixteenToThe ( int n ) {
89108 // 16**7 is the largest power of 16 that fits in an int.
90109 n in [ 0 .. 7 ] and result = 1 .bitShiftLeft ( 4 * n )
91110}
92111
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+ */
94116int eightToThe ( int n ) {
95117 // 8**10 is the largest power of 8 that fits in an int.
96118 n in [ 0 .. 10 ] and result = 1 .bitShiftLeft ( 3 * n )
97119}
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