The TOML string is never transformed into an array

This commit is contained in:
moandji.ezana 2015-02-12 23:25:10 +02:00
parent 8388e394ed
commit 96220b7a88
13 changed files with 63 additions and 211 deletions

View file

@ -20,15 +20,14 @@ class ArrayConverter implements ValueConverter {
AtomicInteger line = context.line;
int startLine = line.get();
int startIndex = index.get();
char[] chars = s.toCharArray();
List<Object> arrayItems = new ArrayList<Object>();
boolean terminated = false;
boolean inComment = false;
Results.Errors errors = new Results.Errors();
for (int i = index.incrementAndGet(); i < chars.length; i = index.incrementAndGet()) {
for (int i = index.incrementAndGet(); i < s.length(); i = index.incrementAndGet()) {
char c = chars[i];
char c = s.charAt(i);
if (c == '#' && !inComment) {
inComment = true;

View file

@ -16,10 +16,8 @@ class DateConverter implements ValueConverter {
return false;
}
char[] chars = s.toCharArray();
for (int i = 0; i < 5; i++) {
char c = chars[i];
char c = s.charAt(i);
if (i < 4) {
if (!Character.isDigit(c)) {

View file

@ -93,13 +93,11 @@ class Identifier {
}
boolean quoted = false;
char[] chars = name.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (c == '"' && (i == 0 || chars[i - 1] != '\\')) {
if (!quoted && i > 0 && chars [i - 1] != '.') {
if (c == '"' && (i == 0 || name.charAt(i - 1) != '\\')) {
if (!quoted && i > 0 && name.charAt(i - 1) != '.') {
context.errors.invalidKey(name, context.line.get());
return false;
}
@ -130,14 +128,13 @@ class Identifier {
return false;
}
char[] chars = trimmed.toCharArray();
boolean quoted = false;
boolean dotAllowed = false;
boolean quoteAllowed = true;
boolean charAllowed = true;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
for (int i = 0; i < trimmed.length(); i++) {
char c = trimmed.charAt(i);
if (!valid) {
break;
@ -146,7 +143,7 @@ class Identifier {
if (c == '"') {
if (!quoteAllowed) {
valid = false;
} else if (quoted && chars[i - 1] != '\\') {
} else if (quoted && trimmed.charAt(i - 1) != '\\') {
charAllowed = false;
dotAllowed = true;
quoteAllowed = false;
@ -166,7 +163,7 @@ class Identifier {
return false;
}
} else if (Character.isWhitespace(c)) {
char prev = chars[i - 1];
char prev = trimmed.charAt(i - 1);
if (!Character.isWhitespace(prev) && prev != '.' && prev != '"') {
charAllowed = false;
dotAllowed = true;
@ -209,15 +206,13 @@ class Identifier {
return false;
}
char[] chars = trimmed.toCharArray();
boolean quoted = false;
boolean dotAllowed = false;
boolean quoteAllowed = true;
boolean charAllowed = true;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
for (int i = 0; i < trimmed.length(); i++) {
char c = trimmed.charAt(i);
if (!valid) {
break;
@ -226,7 +221,7 @@ class Identifier {
if (c == '"') {
if (!quoteAllowed) {
valid = false;
} else if (quoted && chars[i - 1] != '\\') {
} else if (quoted && trimmed.charAt(i - 1) != '\\') {
charAllowed = false;
dotAllowed = true;
quoteAllowed = false;
@ -246,7 +241,7 @@ class Identifier {
return false;
}
} else if (Character.isWhitespace(c)) {
char prev = chars[i - 1];
char prev = trimmed.charAt(i - 1);
if (!Character.isWhitespace(prev) && prev != '.' && prev != '"') {
charAllowed = false;
dotAllowed = true;

View file

@ -6,20 +6,17 @@ class IdentifierConverter {
static final IdentifierConverter IDENTIFIER_CONVERTER = new IdentifierConverter();
private static final String ALLOWED_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_-.";
Identifier convert(char[] chars, AtomicInteger index, Context context) {
Identifier convert(String s, AtomicInteger index, Context context) {
boolean quoted = false;
StringBuilder name = new StringBuilder();
Identifier identifier = null;
boolean terminated = false;
boolean isKey = chars[index.get()] != '[';
boolean isTableArray = !isKey && chars.length > index.get() + 1 && chars[index.get() + 1] == '[';
boolean isKey = s.charAt(index.get()) != '[';
boolean isTableArray = !isKey && s.length() > index.get() + 1 && s.charAt(index.get() + 1) == '[';
boolean inComment = false;
for (int i = index.get(); i < chars.length; i = index.incrementAndGet()) {
char c = chars[i];
if (c == '"' && (i == 0 || chars[i - 1] != '\\')) {
for (int i = index.get(); i < s.length(); i = index.incrementAndGet()) {
char c = s.charAt(i);
if (c == '"' && (i == 0 || s.charAt(i - 1) != '\\')) {
quoted = !quoted;
name.append('"');
} else if (c == '\n') {
@ -30,7 +27,7 @@ class IdentifierConverter {
terminated = true;
break;
} else if (c == ']' && !isKey) {
if (!isTableArray || chars.length > index.get() + 1 && chars[index.get() + 1] == ']') {
if (!isTableArray || s.length() > index.get() + 1 && s.charAt(index.get() + 1) == ']') {
terminated = true;
name.append(']');
if (isTableArray) {
@ -61,131 +58,4 @@ class IdentifierConverter {
}
private IdentifierConverter() {}
private static String getKey(String key) {
key = key.trim();
if (key.isEmpty()) {
return null;
}
boolean quoted = false;
char[] chars = key.toCharArray();
StringBuilder sb = new StringBuilder(key.length());
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c == '"' && (i == 0 || chars[i - 1] != '\\')) {
if (!quoted && i > 0 && chars [i - 1] != '.') {
return null;
}
quoted = !quoted;
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1)) {
return null;
}
sb.append(c);
}
return sb.toString();
}
private static String getTableArrayName(String line) {
StringBuilder sb = new StringBuilder();
char[] chars = line.toCharArray();
boolean quoted = false;
boolean terminated = false;
int endIndex = -1;
boolean preKey = true;
for (int i = 2; i < chars.length; i++) {
char c = chars[i];
if (c == '"' && chars[i - 1] != '\\') {
if (!quoted && i > 1 && chars [i - 1] != '.' && !Character.isWhitespace(chars[i - 1])) {
break;
}
quoted = !quoted;
} else if (!quoted && c == ']') {
if (chars.length > i + 1 && chars[i + 1] == ']') {
terminated = true;
endIndex = i + 1;
break;
}
} else if (!quoted && c == '.') {
preKey = true;
} else if (!quoted && Character.isWhitespace(c)) {
if (preKey && i > 2 && chars[i - 1] != '.' && !Character.isWhitespace(chars[i - 1])) {
break;
}
if (!preKey && chars.length > i + 1 && chars[i + 1] != '.' && chars[i + 1] != ']' && !Character.isWhitespace(chars[i + 1])) {
break;
}
continue;
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1)) {
break;
} else {
preKey = false;
}
sb.append(c);
}
if (!terminated || sb.length() == 0) {
return null;
}
String tableName = sb.insert(0, "[[").append("]]").toString();
return StringConverter.STRING_PARSER.replaceUnicodeCharacters(tableName);
}
private static String getTableName(String line) {
StringBuilder sb = new StringBuilder();
char[] chars = line.toCharArray();
boolean quoted = false;
boolean terminated = false;
int endIndex = -1;
boolean preKey = true;
for (int i = 1; i < chars.length; i++) {
char c = chars[i];
if (c == '"' && chars[i - 1] != '\\') {
if (!quoted && i > 1 && chars [i - 1] != '.' && !Character.isWhitespace(chars[i - 1])) {
break;
}
quoted = !quoted;
} else if (!quoted && c == ']') {
terminated = true;
endIndex = i;
break;
} else if (!quoted && c == '.') {
preKey = true;
} else if (!quoted && Character.isWhitespace(c)) {
if (preKey && i > 1 && chars[i - 1] != '.' && !Character.isWhitespace(chars[i - 1])) {
break;
}
if (!preKey && chars.length > i + 1 && chars[i + 1] != '.' && chars[i + 1] != ']' && !Character.isWhitespace(chars[i + 1])) {
break;
}
continue;
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1)) {
break;
} else if (!quoted) {
preKey = false;
}
sb.append(c);
}
if (!terminated) {
return null;
}
sb.insert(0, '[').append(']');
String tableName = sb.toString();
return StringConverter.STRING_PARSER.replaceUnicodeCharacters(tableName);
}
}

View file

@ -19,7 +19,6 @@ class InlineTableConverter implements ValueConverter {
AtomicInteger line = context.line;
int startLine = line.get();
int startIndex = sharedIndex.get();
char[] chars = s.toCharArray();
boolean inKey = true;
boolean inValue = false;
boolean quoted = false;
@ -28,8 +27,8 @@ class InlineTableConverter implements ValueConverter {
HashMap<String, Object> results = new HashMap<String, Object>();
Results.Errors errors = new Results.Errors();
for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < chars.length; i = sharedIndex.incrementAndGet()) {
char c = chars[i];
for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet()) {
char c = s.charAt(i);
if (c == '"' && inKey) {
quoted = !quoted;

View file

@ -26,14 +26,13 @@ class Keys {
static Keys.Key[] split(String key) {
List<Key> splitKey = new ArrayList<Key>();
StringBuilder current = new StringBuilder();
char[] chars = key.toCharArray();
boolean quoted = false;
boolean indexable = true;
boolean inIndex = false;
int index = -1;
for (int i = chars.length - 1; i > -1; i--) {
char c = chars[i];
for (int i = key.length() - 1; i > -1; i--) {
char c = key.charAt(i);
if (c == ']' && indexable) {
inIndex = true;
continue;
@ -45,7 +44,7 @@ class Keys {
current = new StringBuilder();
continue;
}
if (c == '"' && (i == 0 || chars[i - 1] != '\\')) {
if (c == '"' && (i == 0 || key.charAt(i - 1) != '\\')) {
quoted = !quoted;
indexable = false;
}

View file

@ -15,12 +15,11 @@ class LiteralStringConverter implements ValueConverter {
@Override
public Object convert(String s, AtomicInteger index, Context context) {
int startLine = context.line.get();
char[] chars = s.toCharArray();
boolean terminated = false;
int startIndex = index.incrementAndGet();
for (int i = index.get(); i < chars.length; i = index.incrementAndGet()) {
char c = chars[i];
for (int i = index.get(); i < s.length(); i = index.incrementAndGet()) {
char c = s.charAt(i);
if (c == '\'') {
terminated = true;

View file

@ -15,24 +15,23 @@ class MultilineLiteralStringConverter implements ValueConverter {
public Object convert(String s, AtomicInteger index, Context context) {
AtomicInteger line = context.line;
int startLine = line.get();
char[] chars = s.toCharArray();
int originalStartIndex = index.get();
int startIndex = index.addAndGet(3);
int endIndex = -1;
if (chars[startIndex] == '\n') {
if (s.charAt(startIndex) == '\n') {
startIndex = index.incrementAndGet();
line.incrementAndGet();
}
for (int i = startIndex; i < chars.length; i = index.incrementAndGet()) {
char c = chars[i];
for (int i = startIndex; i < s.length(); i = index.incrementAndGet()) {
char c = s.charAt(i);
if (c == '\n') {
line.incrementAndGet();
}
if (c == '\'' && chars.length > i + 2 && chars[i + 1] == '\'' && chars[i + 2] == '\'') {
if (c == '\'' && s.length() > i + 2 && s.charAt(i + 1) == '\'' && s.charAt(i + 2) == '\'') {
endIndex = i;
index.addAndGet(2);
break;

View file

@ -15,22 +15,21 @@ class MultilineStringConverter implements ValueConverter {
public Object convert(String s, AtomicInteger index, Context context) {
AtomicInteger line = context.line;
int startLine = line.get();
char[] chars = s.toCharArray();
int originalStartIndex = index.get();
int startIndex = index.addAndGet(3);
int endIndex = -1;
if (chars[startIndex] == '\n') {
if (s.charAt(startIndex) == '\n') {
startIndex = index.incrementAndGet();
line.incrementAndGet();
}
for (int i = startIndex; i < chars.length; i = index.incrementAndGet()) {
char c = chars[i];
for (int i = startIndex; i < s.length(); i = index.incrementAndGet()) {
char c = s.charAt(i);
if (c == '\n') {
line.incrementAndGet();
} else if (c == '"' && chars.length > i + 2 && chars[i + 1] == '"' && chars[i + 2] == '"') {
} else if (c == '"' && s.length() > i + 2 && s.charAt(i + 1) == '"' && s.charAt(i + 2) == '"') {
endIndex = i;
index.addAndGet(2);
break;

View file

@ -14,7 +14,6 @@ class NumberConverter implements ValueConverter {
@Override
public Object convert(String s, AtomicInteger index, Context context) {
char[] chars = s.toCharArray();
boolean signable = true;
boolean dottable = false;
boolean exponentable = false;
@ -22,8 +21,8 @@ class NumberConverter implements ValueConverter {
String type = "";
StringBuilder sb = new StringBuilder();
for (int i = index.get(); i < chars.length; i = index.incrementAndGet()) {
char c = chars[i];
for (int i = index.get(); i < s.length(); i = index.incrementAndGet()) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
sb.append(c);
@ -34,19 +33,19 @@ class NumberConverter implements ValueConverter {
dottable = true;
}
exponentable = !type.equals("exponent");
} else if ((c == '+' || c == '-') && signable && chars.length > i + 1) {
} else if ((c == '+' || c == '-') && signable && s.length() > i + 1) {
signable = false;
terminatable = false;
if (c == '-') {
sb.append('-');
}
} else if (c == '.' && dottable && chars.length > i + 1) {
} else if (c == '.' && dottable && s.length() > i + 1) {
sb.append('.');
type = "float";
terminatable = false;
dottable = false;
exponentable = false;
} else if ((c == 'E' || c == 'e') && exponentable && chars.length > i + 1) {
} else if ((c == 'E' || c == 'e') && exponentable && s.length() > i + 1) {
sb.append('E');
type = "exponent";
terminatable = false;

View file

@ -15,14 +15,13 @@ class StringConverter implements ValueConverter {
}
@Override
public Object convert(String value, AtomicInteger sharedIndex, Context context) {
int startIndex = sharedIndex.incrementAndGet();
public Object convert(String s, AtomicInteger index, Context context) {
int startIndex = index.incrementAndGet();
int endIndex = -1;
char[] chars = value.toCharArray();
for (int i = sharedIndex.get(); i < chars.length; i = sharedIndex.incrementAndGet()) {
char ch = chars[i];
if (ch == '"' && chars[i - 1] != '\\') {
for (int i = index.get(); i < s.length(); i = index.incrementAndGet()) {
char ch = s.charAt(i);
if (ch == '"' && s.charAt(i - 1) != '\\') {
endIndex = i;
break;
}
@ -30,21 +29,21 @@ class StringConverter implements ValueConverter {
if (endIndex == -1) {
Results.Errors errors = new Results.Errors();
errors.unterminated(context.identifier.getName(), value.substring(startIndex - 1), context.line.get());
errors.unterminated(context.identifier.getName(), s.substring(startIndex - 1), context.line.get());
return errors;
}
String raw = value.substring(startIndex, endIndex);
value = replaceUnicodeCharacters(raw);
value = replaceSpecialCharacters(value);
String raw = s.substring(startIndex, endIndex);
s = replaceUnicodeCharacters(raw);
s = replaceSpecialCharacters(s);
if (value == null) {
if (s == null) {
Results.Errors errors = new Results.Errors();
errors.invalidValue(context.identifier.getName(), raw, context.line.get());
return errors;
}
return value;
return s;
}
String replaceUnicodeCharacters(String value) {
@ -56,11 +55,10 @@ class StringConverter implements ValueConverter {
return value;
}
String replaceSpecialCharacters(String value) {
char[] chars = value.toCharArray();
for (int i = 0; i < chars.length - 1; i++) {
char ch = chars[i];
char next = chars[i + 1];
String replaceSpecialCharacters(String s) {
for (int i = 0; i < s.length() - 1; i++) {
char ch = s.charAt(i);
char next = s.charAt(i + 1);
if (ch == '\\' && next == '\\') {
i++;
@ -69,7 +67,7 @@ class StringConverter implements ValueConverter {
}
}
return value.replace("\\n", "\n")
return s.replace("\\n", "\n")
.replace("\\\"", "\"")
.replace("\\t", "\t")
.replace("\\r", "\r")

View file

@ -13,20 +13,19 @@ class TomlParser {
return results;
}
char[] chars = tomlString.toCharArray();
AtomicInteger index = new AtomicInteger();
boolean inComment = false;
AtomicInteger line = new AtomicInteger(1);
Identifier identifier = null;
Object value = null;
for (int i = index.get(); i < chars.length; i = index.incrementAndGet()) {
char c = chars[i];
for (int i = index.get(); i < tomlString.length(); i = index.incrementAndGet()) {
char c = tomlString.charAt(i);
if (c == '#' && !inComment) {
inComment = true;
} else if (!Character.isWhitespace(c) && !inComment && identifier == null) {
Identifier id = IDENTIFIER_CONVERTER.convert(chars, index, new Context(null, line, results.errors));
Identifier id = IDENTIFIER_CONVERTER.convert(tomlString, index, new Context(null, line, results.errors));
if (id != Identifier.INVALID) {
if (id.isKey()) {

View file

@ -21,9 +21,8 @@ class ValueConverters {
return true;
}
char[] chars = line.toCharArray();
for (char c : chars) {
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (Character.isWhitespace(c)) {
continue;
}