Skip to content

Commit a4d90d3

Browse files
committed
Accept .5 for half
Previous refactor pruned too much by half.
1 parent 627b122 commit a4d90d3

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/library/scala/collection/StringParsers.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ final private[scala] object StringParsers {
1616
private final val longOverflowBoundary = -922337203685477580L
1717
private final val longOverflowDigit = 9
1818

19-
2019
@inline
2120
private[this] final def decValue(ch: Char): Int = java.lang.Character.digit(ch, 10)
2221

@@ -38,6 +37,9 @@ final private[scala] object StringParsers {
3837
rec(1, agg)
3938
}
4039

40+
@inline
41+
private[this] final def isDigit(c: Char): Boolean = c >= '0' && c <= '9'
42+
4143
//bool
4244
@inline
4345
final def parseBool(from: String): Option[Boolean] =
@@ -240,19 +242,20 @@ final private[scala] object StringParsers {
240242
//but not just .
241243
val startChar = format.charAt(startIndex)
242244
if (startChar == '.') {
243-
val noSignificant = skipIndexWhile(ch => ch >= '0' && ch <= '9', startIndex + 1, endIndex)
244-
(noSignificant != startIndex + 1) && { //not just "." or ".Exxx"
245+
val noSignificant = skipIndexWhile(isDigit, startIndex + 1, endIndex)
246+
// a digit is required followed by optional exp
247+
(noSignificant > startIndex + 1) && (noSignificant >= endIndex || {
245248
val e = format.charAt(noSignificant)
246249
(e == 'e' || e == 'E') && expOK(noSignificant + 1, endIndex)
247-
}
250+
})
248251
}
249-
else if (startChar >= '0' && startChar <= '9'){
252+
else if (isDigit(startChar)) {
250253
//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)
254+
val noInt = skipIndexWhile(isDigit, startIndex, endIndex)
252255
(noInt == endIndex) || { //just the digits
253256
val afterIntChar = format.charAt(noInt)
254257
if (afterIntChar == '.') {
255-
val noSignificant = skipIndexWhile(ch => ch >= '0' && ch <= '9', noInt + 1, endIndex)
258+
val noSignificant = skipIndexWhile(isDigit, noInt + 1, endIndex)
256259
(noSignificant >= endIndex) || { //no exponent
257260
val e = format.charAt(noSignificant)
258261
(e == 'e' || e == 'E') && expOK(noSignificant + 1, endIndex)
@@ -307,4 +310,4 @@ final private[scala] object StringParsers {
307310
if (checkFloatFormat(from)) Some(java.lang.Double.parseDouble(from))
308311
else None
309312

310-
}
313+
}

test/junit/scala/collection/StringParsersTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ class StringParsersTest {
242242
"-0x0.000000000000090000000000000000001p-1022",
243243
"-0x0.00000000000009fffffffffffffffffffffffffffffffffp-1022",
244244
"-0x0.0000000000000fffffffffffffffffffffffffffffffffffp-1022",
245+
".4",
245246
".E4",
246247
"1.1E4",
247248
"1.E4",
@@ -307,4 +308,4 @@ class StringParsersTest {
307308
@Test
308309
def nullDouble: Unit = assertThrows[NullPointerException](nullstring.toDoubleOption)
309310

310-
}
311+
}

0 commit comments

Comments
 (0)