4
4
var Caml_builtin_exceptions = require ( "./caml_builtin_exceptions" ) ;
5
5
var Caml_utils = require ( "./caml_utils" ) ;
6
6
7
+ function caml_failwith ( s ) {
8
+ throw [
9
+ Caml_builtin_exceptions . failure ,
10
+ s
11
+ ] ;
12
+ }
13
+
7
14
function caml_invalid_argument ( s ) {
8
15
throw [
9
16
Caml_builtin_exceptions . invalid_argument ,
@@ -36,75 +43,125 @@ function parse_digit(c) {
36
43
}
37
44
}
38
45
39
-
40
-
41
- /**
42
- *
43
- * @param {string } s
44
- * @returns {number[] }
45
- */
46
- function $$parse_sign_and_base ( s ) {
47
- var i = 0 ;
48
- var len = s . length ;
49
- var base = 10 ;
50
- var sign = ( len > 0 && s . charCodeAt ( 0 ) == 45 ) ? ( i ++ , - 1 ) : 1 ;
51
- if ( i + 1 < len && s . charCodeAt ( i ) == 48 )
52
- switch ( s . charCodeAt ( i + 1 ) ) {
53
- case 120 :
54
- case 88 :
55
- base = 16 ;
56
- i += 2 ;
57
- break ;
58
- case 111 :
59
- case 79 :
60
- base = 8 ;
61
- i += 2 ;
62
- break ;
63
- case 98 :
64
- case 66 :
65
- base = 2 ;
66
- i += 2 ;
67
- break ;
46
+ function parse_sign_and_base ( s ) {
47
+ var sign = 1 ;
48
+ var base = 10 ;
49
+ var i = 0 ;
50
+ if ( s [ i ] === "-" ) {
51
+ sign = - 1 ;
52
+ ++ i ;
53
+ }
54
+ var match = s . charCodeAt ( i ) ;
55
+ var match$1 = s . charCodeAt ( i + 1 ) ;
56
+ if ( match === 48 ) {
57
+ if ( match$1 >= 89 ) {
58
+ if ( match$1 !== 98 ) {
59
+ if ( match$1 !== 111 ) {
60
+ if ( match$1 === 120 ) {
61
+ base = 16 ;
62
+ i += 2 ;
63
+ }
64
+
65
+ }
66
+ else {
67
+ base = 8 ;
68
+ i += 2 ;
69
+ }
70
+ }
71
+ else {
72
+ base = 2 ;
73
+ i += 2 ;
74
+ }
75
+ }
76
+ else if ( match$1 !== 66 ) {
77
+ if ( match$1 !== 79 ) {
78
+ if ( match$1 >= 88 ) {
79
+ base = 16 ;
80
+ i += 2 ;
68
81
}
69
- return [ i , sign , base ] ;
82
+
83
+ }
84
+ else {
85
+ base = 8 ;
86
+ i += 2 ;
87
+ }
88
+ }
89
+ else {
90
+ base = 2 ;
91
+ i += 2 ;
92
+ }
93
+ }
94
+ return /* tuple */ [
95
+ i ,
96
+ sign ,
97
+ base
98
+ ] ;
70
99
}
71
100
72
-
73
- /**
74
- *
75
- * @param {string } s
76
- * @returns {number }
77
- */
78
- function $$caml_int_of_string ( s ) {
79
- var _a = $$parse_sign_and_base ( s ) , i = _a [ 0 ] , sign = _a [ 1 ] , base = _a [ 2 ] ;
80
- var len = s . length ;
81
- var threshold = - 1 >>> 0 ;
82
- var c = ( i < len ) ? s . charCodeAt ( i ) : 0 ;
83
- var d = parse_digit ( c ) ;
84
- if ( d < 0 || d >= base )
85
- caml_failwith ( "int_of_string" ) ;
86
- var res = d ;
87
- for ( i ++ ; i < len ; i ++ ) {
88
- c = s . charCodeAt ( i ) ;
89
- if ( c == 95 )
90
- continue ;
91
- d = parse_digit ( c ) ;
92
- if ( d < 0 || d >= base )
93
- break ;
94
- res = base * res + d ;
95
- if ( res > threshold )
96
- caml_failwith ( "int_of_string" ) ;
97
- }
98
- if ( i != len )
99
- caml_failwith ( "int_of_string" ) ;
100
- // For base different from 10, we expect an unsigned representation,
101
- // hence any value of 'res' (less than 'threshold') is acceptable.
102
- // But we have to convert the result back to a signed integer.
103
- res = sign * res ;
104
- if ( ( base == 10 ) && ( ( res | 0 ) != res ) )
105
- /* Signed representation expected, allow -2^(nbits-1) to 2^(nbits-1) - 1 */
106
- caml_failwith ( "int_of_string" ) ;
107
- return res | 0 ;
101
+ function caml_int_of_string ( s ) {
102
+ var match = parse_sign_and_base ( s ) ;
103
+ var base = match [ 2 ] ;
104
+ var i = match [ 0 ] ;
105
+ var threshold = ( - 1 >>> 0 ) ;
106
+ var len = s . length ;
107
+ var c = i < len ? s . charCodeAt ( i ) : /* "\000" */ 0 ;
108
+ var d = parse_digit ( c ) ;
109
+ if ( d < 0 || d >= base ) {
110
+ throw [
111
+ Caml_builtin_exceptions . failure ,
112
+ "int_of_string"
113
+ ] ;
114
+ }
115
+ var aux = function ( _acc , _k ) {
116
+ while ( true ) {
117
+ var k = _k ;
118
+ var acc = _acc ;
119
+ if ( k === len ) {
120
+ return acc ;
121
+ }
122
+ else {
123
+ var a = s . charCodeAt ( k ) ;
124
+ if ( a === /* "_" */ 95 ) {
125
+ _k = k + 1 ;
126
+ continue ;
127
+
128
+ }
129
+ else {
130
+ var v = parse_digit ( a ) ;
131
+ if ( v < 0 || v >= base ) {
132
+ throw [
133
+ Caml_builtin_exceptions . failure ,
134
+ "int_of_string"
135
+ ] ;
136
+ }
137
+ else {
138
+ var acc$1 = base * acc + v ;
139
+ if ( acc$1 > threshold ) {
140
+ throw [
141
+ Caml_builtin_exceptions . failure ,
142
+ "int_of_string"
143
+ ] ;
144
+ }
145
+ else {
146
+ _k = k + 1 ;
147
+ _acc = acc$1 ;
148
+ continue ;
149
+
150
+ }
151
+ }
152
+ }
153
+ }
154
+ } ;
155
+ } ;
156
+ var res = match [ 1 ] * aux ( d , i + 1 ) ;
157
+ var or_res = res | 0 ;
158
+ if ( base === 10 && res !== or_res ) {
159
+ throw [
160
+ Caml_builtin_exceptions . failure ,
161
+ "int_of_string"
162
+ ] ;
163
+ }
164
+ return or_res ;
108
165
}
109
166
110
167
@@ -402,10 +459,6 @@ function caml_int32_format(prim, prim$1) {
402
459
return $$caml_format_int ( prim , prim$1 ) ;
403
460
}
404
461
405
- function caml_int32_of_string ( prim ) {
406
- return $$caml_int_of_string ( prim ) ;
407
- }
408
-
409
462
var repeat = Caml_utils . repeat ;
410
463
411
464
function caml_format_float ( prim , prim$1 ) {
@@ -420,15 +473,15 @@ function caml_float_of_string(prim) {
420
473
return $$caml_float_of_string ( prim ) ;
421
474
}
422
475
423
- function caml_int_of_string ( prim ) {
424
- return $$caml_int_of_string ( prim ) ;
425
- }
476
+ var caml_int32_of_string = caml_int_of_string ;
426
477
427
- var caml_nativeint_of_string = caml_int32_of_string ;
478
+ var caml_nativeint_of_string = caml_int_of_string ;
428
479
429
480
exports . parse_digit = parse_digit ;
430
481
exports . caml_invalid_argument = caml_invalid_argument ;
431
482
exports . repeat = repeat ;
483
+ exports . parse_sign_and_base = parse_sign_and_base ;
484
+ exports . caml_failwith = caml_failwith ;
432
485
exports . caml_format_float = caml_format_float ;
433
486
exports . caml_format_int = caml_format_int ;
434
487
exports . caml_nativeint_format = caml_nativeint_format ;
0 commit comments