@@ -16,7 +16,6 @@ final private[scala] object StringParsers {
16
16
private final val longOverflowBoundary = - 922337203685477580L
17
17
private final val longOverflowDigit = 9
18
18
19
-
20
19
@ inline
21
20
private [this ] final def decValue (ch : Char ): Int = java.lang.Character .digit(ch, 10 )
22
21
@@ -38,6 +37,9 @@ final private[scala] object StringParsers {
38
37
rec(1 , agg)
39
38
}
40
39
40
+ @ inline
41
+ private [this ] final def isDigit (c : Char ): Boolean = c >= '0' && c <= '9'
42
+
41
43
// bool
42
44
@ inline
43
45
final def parseBool (from : String ): Option [Boolean ] =
@@ -164,11 +166,7 @@ final private[scala] object StringParsers {
164
166
// some utilities for working with index bounds into the original string
165
167
@ inline
166
168
def forAllBetween (start : Int , end : Int , pred : Char => Boolean ): Boolean = {
167
- def rec (i : Int ): Boolean = {
168
- if (i >= end) true
169
- else if (pred(format.charAt(i))) rec(i + 1 )
170
- else false
171
- }
169
+ def rec (i : Int ): Boolean = i >= end || pred(format.charAt(i)) && rec(i + 1 )
172
170
rec(start)
173
171
}
174
172
@@ -209,11 +207,11 @@ final private[scala] object StringParsers {
209
207
210
208
def postfixOK (startIndex : Int , endIndex : Int ): Boolean =
211
209
(startIndex < endIndex) && {
212
- (forAllBetween(startIndex, endIndex, ch => ch >= '0' && ch <= '9' )) || {
210
+ (forAllBetween(startIndex, endIndex, isDigit )) || {
213
211
val startchar = format.charAt(startIndex)
214
212
(startchar == '+' || startchar == '-' ) &&
215
213
(endIndex - startIndex > 1 ) &&
216
- forAllBetween(startIndex + 1 , endIndex, ch => ch >= '0' && ch <= '9' )
214
+ forAllBetween(startIndex + 1 , endIndex, isDigit )
217
215
}
218
216
}
219
217
// prefix [pP] postfix
@@ -224,13 +222,15 @@ final private[scala] object StringParsers {
224
222
def isDecFloatLiteral (startIndex : Int , endIndex : Int ): Boolean = {
225
223
// invariant: endIndex > startIndex
226
224
225
+ def isExp (c : Char ): Boolean = c == 'e' || c == 'E'
226
+
227
227
def expOK (startIndex : Int , endIndex : Int ): Boolean =
228
228
(startIndex < endIndex) && {
229
229
val startChar = format.charAt(startIndex)
230
230
if (startChar == '+' || startChar == '-' )
231
231
(endIndex > (startIndex + 1 )) &&
232
- skipIndexWhile(ch => ch >= '0' && ch <= '9' , startIndex + 1 , endIndex) == endIndex
233
- else skipIndexWhile(ch => ch >= '0' && ch <= '9' , startIndex, endIndex) == endIndex
232
+ skipIndexWhile(isDigit , startIndex + 1 , endIndex) == endIndex
233
+ else skipIndexWhile(isDigit , startIndex, endIndex) == endIndex
234
234
}
235
235
236
236
// significant can be one of
@@ -240,30 +240,26 @@ final private[scala] object StringParsers {
240
240
// but not just .
241
241
val startChar = format.charAt(startIndex)
242
242
if (startChar == '.' ) {
243
- val noSignificant = skipIndexWhile(ch => ch >= '0' && ch <= '9' , startIndex + 1 , endIndex)
244
- (noSignificant != startIndex + 1 ) && { // not just "." or ".Exxx"
245
- val e = format.charAt (noSignificant)
246
- (e == 'e' || e == 'E' ) && expOK(noSignificant + 1 , endIndex)
247
- }
243
+ val noSignificant = skipIndexWhile(isDigit , startIndex + 1 , endIndex)
244
+ // a digit is required followed by optional exp
245
+ (noSignificant > startIndex + 1 ) && (noSignificant >= endIndex ||
246
+ isExp(format.charAt(noSignificant) ) && expOK(noSignificant + 1 , endIndex)
247
+ )
248
248
}
249
- else if (startChar >= '0' && startChar <= '9' ){
250
- // one set of digits, then optionally a period, then optionally another set of digits, then optionally an exponent
251
- val noInt = skipIndexWhile(ch => ch >= '0' && ch <= '9' , startIndex, endIndex)
252
- (noInt == endIndex) || { // just the digits
253
- val afterIntChar = format.charAt(noInt)
254
- if (afterIntChar == '.' ) {
255
- val noSignificant = skipIndexWhile(ch => ch >= '0' && ch <= '9' , noInt + 1 , endIndex)
256
- (noSignificant >= endIndex) || { // no exponent
257
- val e = format.charAt(noSignificant)
258
- (e == 'e' || e == 'E' ) && expOK(noSignificant + 1 , endIndex)
259
- }
260
- }
261
- else if (afterIntChar == 'e' || afterIntChar == 'E' ) expOK(noInt + 1 , endIndex)
262
- else false
249
+ else if (isDigit(startChar)) {
250
+ // one set of digits, then optionally a period, then optionally another set of digits, then optionally an exponent
251
+ val noInt = skipIndexWhile(isDigit, startIndex, endIndex)
252
+ // just the digits
253
+ (noInt == endIndex) || {
254
+ if (format.charAt(noInt) == '.' ) {
255
+ val noSignificant = skipIndexWhile(isDigit, noInt + 1 , endIndex)
256
+ (noSignificant >= endIndex) || // no exponent
257
+ isExp(format.charAt(noSignificant)) && expOK(noSignificant + 1 , endIndex)
258
+ } else
259
+ isExp(format.charAt(noInt)) && expOK(noInt + 1 , endIndex)
263
260
}
264
261
}
265
262
else false
266
-
267
263
}
268
264
269
265
// count 0x00 to 0x20 as "whitespace", and nothing else
@@ -307,4 +303,4 @@ final private[scala] object StringParsers {
307
303
if (checkFloatFormat(from)) Some (java.lang.Double .parseDouble(from))
308
304
else None
309
305
310
- }
306
+ }
0 commit comments