Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed support for keys that look like numbers longer than an int. #568

Merged
merged 1 commit into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, good cleanup

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)
}
}