Skip to content

Commit

Permalink
Fixed support for keys that are all digits but longer than an int.
Browse files Browse the repository at this point in the history
Previously such config keys would still be parsed with
`Integer#parseInt` for the sake of sorting, resulting in a `NumberFormatException`.
Now the keys consisting of digits only are parsed to a `BigInteger` and
compared as such.

This addresses the following issues:
#604
#541
  • Loading branch information
michalmela authored and michalmela-tomtom committed Jan 8, 2019
1 parent e1c2640 commit fee9a3e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -426,15 +427,14 @@ private static boolean isAllDigits(String s) {
int length = s.length();

// empty string doesn't count as a number
// string longer than "max number of digits in a long" cannot be parsed as a long
if (length == 0)
return false;

for (int i = 0; i < length; ++i) {
char c = s.charAt(i);

if (Character.isDigit(c))
continue;
else
if (!Character.isDigit(c))
return false;
}
return true;
Expand All @@ -449,7 +449,7 @@ public int compare(String a, String b) {
boolean aDigits = isAllDigits(a);
boolean bDigits = isAllDigits(b);
if (aDigits && bDigits) {
return Integer.compare(Integer.parseInt(a), Integer.parseInt(b));
return new BigInteger(a).compareTo(new BigInteger(b));
} else if (aDigits) {
return -1;
} else if (bDigits) {
Expand Down
43 changes: 43 additions & 0 deletions config/src/test/resources/test12.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// this checks sorting map keys, where keys that look like numbers are treated differently
// specifically tests very long numbers which fit neither in an Integer nor in a Long

"10" = "42"
sth = 42
"1" = "42"
"12" = "42"
"123" = "42"
"1234" = "42"
"12345" = "42"
"123456" = "42"
"1234567" = "42"
"12345678" = "42"
"123456789" = "42"
"1234567890" = "42"
"12345678901" = "42"
"123456789012" = "42"
"1234567890123" = "42"
"12345678901234" = "42"
"123456789012345" = "42"
"1234567890123456" = "42"
"12345678901234567" = "42"
"123456789012345678" = "42"
"1234567890123456789" = "42"
"12345678901234567891" = "42"
"123456789012345678912" = "42"
"1234567890123456789123" = "42"
"12345678901234567891234" = "42"
"123456789012345678912345" = "42"
"1234567890123456789123456" = "42"
"12345678901234567891234567" = "42"
"123456789012345678912345678" = "42"
"1234567890123456789123456789" = "42"
"12345678901234567891234567890" = "42"
"123456789012345678912345678901" = "42"
"1234567890123456789123456789012" = "42"
"12345678901234567891234567890123" = "42"
"123456789012345678912345678901234" = "42"
"1234567890123456789123456789012345" = "42"
"12345678901234567891234567890123456" = "42"
"123456789012345678912345678901234567" = "42"
"1234567890123456789123456789012345678" = "42"
"12345678901234567891234567890123456789" = "42"
1 change: 1 addition & 0 deletions config/src/test/scala/Rendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ object RenderExample extends App {
render("test01")
render("test06")
render("test05")
render("test12")
}

object RenderOptions extends App {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -982,8 +982,8 @@ class ConfigValueTest extends TestUtils {

@Test
def renderSorting(): Unit = {
val config = parseConfig("""0=a,1=b,2=c,3=d,10=e,20=f,30=g""")
val config = parseConfig("""0=a,1=b,2=c,999999999999999999999999999999999999999999999=0,3=d,10=e,20a=f,20=g,30=h""")
val rendered = config.root.render(ConfigRenderOptions.concise())
assertEquals("""{"0":"a","1":"b","2":"c","3":"d","10":"e","20":"f","30":"g"}""", rendered)
assertEquals("""{"0":"a","1":"b","2":"c","3":"d","10":"e","20":"g","30":"h","999999999999999999999999999999999999999999999":0,"20a":"f"}""", rendered)
}
}

0 comments on commit fee9a3e

Please sign in to comment.