Skip to content

Commit 86e7697

Browse files
committed
Minor refactor, more tests around dot
As requested, more dotted tests. No benchmarks around the refactor.
1 parent a4d90d3 commit 86e7697

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

src/library/scala/collection/StringParsers.scala

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,7 @@ final private[scala] object StringParsers {
166166
//some utilities for working with index bounds into the original string
167167
@inline
168168
def forAllBetween(start: Int, end: Int, pred: Char => Boolean): Boolean = {
169-
def rec(i: Int): Boolean = {
170-
if (i >= end) true
171-
else if (pred(format.charAt(i))) rec(i + 1)
172-
else false
173-
}
169+
def rec(i: Int): Boolean = i >= end || pred(format.charAt(i)) && rec(i + 1)
174170
rec(start)
175171
}
176172

@@ -211,11 +207,11 @@ final private[scala] object StringParsers {
211207

212208
def postfixOK(startIndex: Int, endIndex: Int): Boolean =
213209
(startIndex < endIndex) && {
214-
(forAllBetween(startIndex, endIndex, ch => ch >= '0' && ch <= '9')) || {
210+
(forAllBetween(startIndex, endIndex, isDigit)) || {
215211
val startchar = format.charAt(startIndex)
216212
(startchar == '+' || startchar == '-') &&
217213
(endIndex - startIndex > 1) &&
218-
forAllBetween(startIndex + 1, endIndex, ch => ch >= '0' && ch <= '9')
214+
forAllBetween(startIndex + 1, endIndex, isDigit)
219215
}
220216
}
221217
// prefix [pP] postfix
@@ -226,13 +222,15 @@ final private[scala] object StringParsers {
226222
def isDecFloatLiteral(startIndex: Int, endIndex: Int): Boolean = {
227223
//invariant: endIndex > startIndex
228224

225+
def isExp(c: Char): Boolean = c == 'e' || c == 'E'
226+
229227
def expOK(startIndex: Int, endIndex: Int): Boolean =
230228
(startIndex < endIndex) && {
231229
val startChar = format.charAt(startIndex)
232230
if (startChar == '+' || startChar == '-')
233231
(endIndex > (startIndex + 1)) &&
234-
skipIndexWhile(ch => ch >= '0' && ch <= '9', startIndex + 1, endIndex) == endIndex
235-
else skipIndexWhile(ch => ch >= '0' && ch <= '9', startIndex, endIndex) == endIndex
232+
skipIndexWhile(isDigit, startIndex + 1, endIndex) == endIndex
233+
else skipIndexWhile(isDigit, startIndex, endIndex) == endIndex
236234
}
237235

238236
//significant can be one of
@@ -244,29 +242,24 @@ final private[scala] object StringParsers {
244242
if (startChar == '.') {
245243
val noSignificant = skipIndexWhile(isDigit, startIndex + 1, endIndex)
246244
// a digit is required followed by optional exp
247-
(noSignificant > startIndex + 1) && (noSignificant >= endIndex || {
248-
val e = format.charAt(noSignificant)
249-
(e == 'e' || e == 'E') && expOK(noSignificant + 1, endIndex)
250-
})
245+
(noSignificant > startIndex + 1) && (noSignificant >= endIndex ||
246+
isExp(format.charAt(noSignificant)) && expOK(noSignificant + 1, endIndex)
247+
)
251248
}
252249
else if (isDigit(startChar)) {
253-
//one set of digits, then optionally a period, then optionally another set of digits, then optionally an exponent
250+
// one set of digits, then optionally a period, then optionally another set of digits, then optionally an exponent
254251
val noInt = skipIndexWhile(isDigit, startIndex, endIndex)
255-
(noInt == endIndex) || { //just the digits
256-
val afterIntChar = format.charAt(noInt)
257-
if (afterIntChar == '.') {
252+
// just the digits
253+
(noInt == endIndex) || {
254+
if (format.charAt(noInt) == '.') {
258255
val noSignificant = skipIndexWhile(isDigit, noInt + 1, endIndex)
259-
(noSignificant >= endIndex) || { //no exponent
260-
val e = format.charAt(noSignificant)
261-
(e == 'e' || e == 'E') && expOK(noSignificant + 1, endIndex)
262-
}
263-
}
264-
else if (afterIntChar == 'e' || afterIntChar == 'E') expOK(noInt + 1, endIndex)
265-
else false
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)
266260
}
267261
}
268262
else false
269-
270263
}
271264

272265
//count 0x00 to 0x20 as "whitespace", and nothing else

test/junit/scala/collection/StringParsersTest.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class StringParsersTest {
1313
def doubleOK(str: String): Unit = assertTrue(
1414
s"str.toDouble <> str.toDoubleOption for $str",
1515
(str.toDoubleOption, Try(str.toDouble).toOption) match {
16-
case (Some(d1), Some(d2)) if d1.isNaN && d2.isNaN => true
16+
case (Some(d1), Some(d2)) => d1.isNaN && d2.isNaN || d1 == d2
1717
case (o1, o2) => o1 == o2
1818
})
1919

@@ -242,11 +242,16 @@ class StringParsersTest {
242242
"-0x0.000000000000090000000000000000001p-1022",
243243
"-0x0.00000000000009fffffffffffffffffffffffffffffffffp-1022",
244244
"-0x0.0000000000000fffffffffffffffffffffffffffffffffffp-1022",
245+
"",
246+
".",
245247
".4",
246248
".E4",
249+
".E",
250+
".x",
251+
".1E4",
252+
"4.",
247253
"1.1E4",
248254
"1.E4",
249-
".1E4",
250255
"1E4",
251256
"E4",
252257
"0.0",

0 commit comments

Comments
 (0)