Support single quotes in table name

Fixes #36
This commit is contained in:
Moandji Ezana 2016-07-27 15:35:20 +02:00
parent 2d2a2c2708
commit 214143fe84
5 changed files with 40 additions and 4 deletions

View File

@ -1,5 +1,9 @@
# toml4j Changelog
## 0.7.1 / 2016-07-27
* [Support literal strings in table names](https://github.com/mwanji/toml4j/issues/36) (thanks to __[bruno-medeiros](https://github.com/bruno-medeiros)__)
## 0.7.0 / 2016-07-12
## Added

View File

@ -139,7 +139,7 @@ class Identifier {
break;
}
if (c == '"') {
if (Keys.isQuote(c)) {
if (!quoteAllowed) {
valid = false;
} else if (quoted && trimmed.charAt(i - 1) != '\\') {

View File

@ -16,9 +16,9 @@ class IdentifierConverter {
for (int i = index.get(); i < s.length(); i = index.incrementAndGet()) {
char c = s.charAt(i);
if (c == '"' && (i == 0 || s.charAt(i - 1) != '\\')) {
if (Keys.isQuote(c) && (i == 0 || s.charAt(i - 1) != '\\')) {
quoted = !quoted;
name.append('"');
name.append(c);
} else if (c == '\n') {
index.decrementAndGet();
break;

View File

@ -42,7 +42,7 @@ class Keys {
current = new StringBuilder();
continue;
}
if (c == '"' && (i == 0 || key.charAt(i - 1) != '\\')) {
if (isQuote(c) && (i == 0 || key.charAt(i - 1) != '\\')) {
quoted = !quoted;
indexable = false;
}
@ -60,6 +60,10 @@ class Keys {
return splitKey.toArray(new Key[0]);
}
static boolean isQuote(char c) {
return c == '"' || c == '\'';
}
private Keys() {}
}

View File

@ -63,6 +63,34 @@ public class TableTest {
public void should_return_null_for_missing_table() throws Exception {
assertNull(new Toml().getTable("a"));
}
@Test
public void should_accept_table_name_with_basic_string() {
Toml toml = new Toml().read("[\"a\"]\nb = 'b'");
assertEquals("b", toml.getString("\"a\".b"));
}
@Test
public void should_accept_table_name_part_with_basic_string() {
Toml toml = new Toml().read("[target.\"cfg(unix)\".dependencies]\nb = 'b'");
assertEquals("b", toml.getString("target.\"cfg(unix)\".dependencies.b"));
}
@Test
public void should_accept_table_name_with_literal_string() {
Toml toml = new Toml().read("['a']\nb = 'b'");
assertEquals("b", toml.getString("'a'.b"));
}
@Test
public void should_accept_table_name_part_with_literal_string() {
Toml toml = new Toml().read("[target.'cfg(unix)'.dependencies]\nb = 'b'");
assertEquals("b", toml.getString("target.'cfg(unix)'.dependencies.b"));
}
@Test
public void should_return_null_when_navigating_to_missing_value() throws Exception {