Handle weird input without properties

This commit is contained in:
themode 2021-12-28 13:07:21 +01:00 committed by TheMode
parent 9c73e72771
commit 29d7cd1c8a
2 changed files with 34 additions and 4 deletions

View File

@ -71,7 +71,7 @@ public class BlockUtils {
while (index < length) {
if (propertiesString.charAt(index) == ',' || index == length - 1) {
if (index + 1 == length) index++;
final String property = propertiesString.substring(start, index);
final String property = propertiesString.substring(start, index).replace(',', '\0');
final int equalIndex = property.indexOf('=');
if (equalIndex != -1) {
final String key = property.substring(0, equalIndex).trim();
@ -83,8 +83,8 @@ public class BlockUtils {
}
index++;
}
assert entryCount == entries;
return switch (entryCount) {
case 0 -> Map.of();
case 1 -> Map.of(keys[0], values[0]);
case 2 -> Map.of(keys[0], values[0], keys[1], values[1]);
case 3 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2]);
@ -105,7 +105,18 @@ public class BlockUtils {
case 10 -> Map.of(keys[0], values[0], keys[1], values[1], keys[2], values[2],
keys[3], values[3], keys[4], values[4], keys[5], values[5], keys[6],
values[6], keys[7], values[7], keys[8], values[8], keys[9], values[9]);
default -> Map.copyOf(new Object2ObjectArrayMap<>(keys, values));
default -> {
if (entryCount == keys.length) {
yield Map.copyOf(new Object2ObjectArrayMap<>(keys, values));
} else {
// Arrays must be resized
final String[] newKeys = new String[entryCount];
final String[] newValues = new String[entryCount];
System.arraycopy(keys, 0, newKeys, 0, entryCount);
System.arraycopy(values, 0, newValues, 0, entryCount);
yield Map.copyOf(new Object2ObjectArrayMap<>(newKeys, newValues));
}
}
};
}
}

View File

@ -49,11 +49,12 @@ public class BlockTest {
assertEquals(Map.of(), BlockUtils.parseProperties("random test without brackets"));
assertEquals(Map.of(), BlockUtils.parseProperties("[]"));
assertEquals(Map.of(), BlockUtils.parseProperties("[ ]"));
assertEquals(Map.of(), BlockUtils.parseProperties("[ , , ,,,, ]"));
assertEquals(Map.of("facing", "east"), BlockUtils.parseProperties("[facing=east]"));
assertEquals(Map.of("facing", "east", "key", "value"), BlockUtils.parseProperties("[facing=east,key=value ]"));
assertEquals(Map.of("facing", "east", "key", "value"), BlockUtils.parseProperties("[ facing = east, key= value ]"));
// Verify until the limit of 10 entries
// Verify all length variations
for (int i = 0; i < 13; i++) {
StringBuilder properties = new StringBuilder("[");
for (int j = 0; j < i; j++) {
@ -68,6 +69,24 @@ public class BlockTest {
assertEquals("value" + j, map.get("key" + j));
}
}
// Semi-corrupted properties
{
final int size = 12;
StringBuilder properties = new StringBuilder("[");
for (int j = 0; j < size; j++) {
properties.append("key").append(j).append("=value").append(j);
if (j != size - 1) properties.append(",");
}
properties.append(", , ,]");
var map = BlockUtils.parseProperties(properties.toString());
assertEquals(size, map.size());
for (int j = 0; j < size; j++) {
assertEquals("value" + j, map.get("key" + j));
}
}
}
@Test