support tokens with braces #6479

This commit is contained in:
kifir 2024-05-09 22:37:19 +03:00 committed by rusefillc
parent 3462ac3e06
commit 697c4da88d
2 changed files with 34 additions and 5 deletions

View File

@ -32,10 +32,11 @@ public class IniFileReader {
ArrayList<String> strings = new ArrayList<>();
boolean inQuote = false;
boolean hadQuote = false;
int openedBraceCount = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '\"' || isTokenSeparator(c) && !inQuote) {
if (c == '\"' || (isTokenSeparator(c) && !inQuote && (openedBraceCount == 0))) {
if (c == '\"') {
inQuote = !inQuote;
if (!inQuote) {
@ -51,8 +52,23 @@ public class IniFileReader {
strings.add("");
hadQuote = false;
}
} else
} else {
sb.append(c);
switch (c) {
case '{': {
openedBraceCount++;
break;
}
case '}': {
if (0 < openedBraceCount) {
openedBraceCount--;
} else {
throw new IllegalArgumentException(String.format("Unexpected closing brace: %s", str));
}
break;
}
}
}
}
if (sb.length() > 0)
strings.add(sb.toString());

View File

@ -61,10 +61,15 @@ public class IniFileReaderTest {
}
@Test
public void testQuotedTokens() {
public void testBraces() {
{
String[] result = IniFileReader.splitTokens("\"hel lo\"");
assertArrayEquals(new String[] { "hel lo" }, result);
String[] result = IniFileReader.splitTokens(
"veLoadBins = array, U16, 17056, [16], {bitStringValue(fuelUnits, fuelAlgorithm) }, 1, 0, 0, 1000, 0"
);
assertArrayEquals(new String[] {
"veLoadBins", "array", "U16", "17056", "[16]", "{bitStringValue(fuelUnits, fuelAlgorithm) }", "1", "0",
"0", "1000", "0"
}, result);
}
}
@ -75,6 +80,14 @@ public class IniFileReaderTest {
assertEquals(result.length, 3);
}
@Test
public void testQuotedTokens() {
{
String[] result = IniFileReader.splitTokens("\"hel lo\"");
assertArrayEquals(new String[] { "hel lo" }, result);
}
}
@Test
public void testTotalPagesSize() {
String string = " nPages = 3\n" +