diff --git a/src/main/java/com/moandjiezana/toml/IntegerConverter.java b/src/main/java/com/moandjiezana/toml/IntegerConverter.java index edd32a8..13d5aec 100644 --- a/src/main/java/com/moandjiezana/toml/IntegerConverter.java +++ b/src/main/java/com/moandjiezana/toml/IntegerConverter.java @@ -1,28 +1,52 @@ package com.moandjiezana.toml; -import static com.moandjiezana.toml.ValueConverterUtils.parse; -import static com.moandjiezana.toml.ValueConverterUtils.parser; - -import java.util.List; class IntegerConverter implements ValueConverter { static final IntegerConverter INTEGER_PARSER = new IntegerConverter(); @Override public boolean canConvert(String s) { - return parse(parser().Integer(), s) != null; + char[] chars = s.toCharArray(); + boolean whitespace = false; + + for (int i = 0; i < chars.length; i++) { + char c = chars[i]; + + if (Character.isDigit(c)) { + continue; + } + + if (i == 0 && (c == '+' || c == '-')) { + continue; + } + + if (Character.isWhitespace(c)) { + whitespace = true; + continue; + } + + if (whitespace && c == '#') { + return true; + } + + return false; + } + + return true; } @Override public Object convert(String s) { - List resultValue = parse(parser().Integer(), s); - - String longString = resultValue.get(0); - if (longString.startsWith("+")) { - longString = longString.substring(1); + if (s.startsWith("+")) { + s = s.substring(1); } - return Long.valueOf(longString); + int startOfComment = s.indexOf('#'); + if (startOfComment > -1) { + s = s.substring(0, startOfComment).trim(); + } + + return Long.valueOf(s); } private IntegerConverter() {} diff --git a/src/main/java/com/moandjiezana/toml/ValueParser.java b/src/main/java/com/moandjiezana/toml/ValueParser.java index 84a85e2..2357101 100644 --- a/src/main/java/com/moandjiezana/toml/ValueParser.java +++ b/src/main/java/com/moandjiezana/toml/ValueParser.java @@ -13,10 +13,6 @@ class ValueParser extends BaseParser> { public Rule Array() { return FirstOf(EmptyArray(), Sequence('[', startList(), OneOrMore(FirstOf(NonEmptyArray(), ' ', ',')), ']', endList())); } - - public Rule Table() { - return Sequence('[', startList(), Sequence(OneOrMore(NoneOf("[]")), pushToken(match())), ']', endList(), Comment()); - } public Rule LiteralString() { return FirstOf(EmptyLiteralString(), Sequence('\'', OneOrMore(TestNot("'"), ANY), startList(), pushToken(match()) , '\'', endList(), Comment())); @@ -26,10 +22,6 @@ class ValueParser extends BaseParser> { return FirstOf(EmptyMultilineLiteralString(), Sequence("'''", startList(), Sequence(OneOrMore(TestNot("'''"), ANY), pushToken(match())), "'''", endList(), Comment())); } - public Rule Integer() { - return Sequence(startList(), Sequence(SignedNumber(), pushToken(match())), endList(), Comment()); - } - public Rule Exponent() { return Sequence(startList(), Sequence(Sequence(SignedNumber(), Optional(Sequence('.', Number())), FirstOf('e', 'E'), SignedNumber()), pushToken(match())), endList(), Comment()); }