Fix trailing comma again, fix 'possiblyNumeric' DoubleTag parsing

This commit is contained in:
Nassim Jahnke 2021-09-25 14:56:08 +02:00
parent 88165088a9
commit 2b9df69627
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
2 changed files with 53 additions and 11 deletions

View File

@ -47,6 +47,8 @@ import java.util.stream.LongStream;
// - Use OpenNBT tags // - Use OpenNBT tags
// - Small byteArray() optimization // - Small byteArray() optimization
// - acceptLegacy = true by default // - acceptLegacy = true by default
// - Don't parse value as DoubleTag when possiblyNumeric
// - Fix trailing comma reading in compounds, lists, and arrays
final class TagStringReader { final class TagStringReader {
private static final int MAX_DEPTH = 512; private static final int MAX_DEPTH = 512;
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
@ -54,7 +56,7 @@ final class TagStringReader {
private static final long[] EMPTY_LONG_ARRAY = new long[0]; private static final long[] EMPTY_LONG_ARRAY = new long[0];
private final CharBuffer buffer; private final CharBuffer buffer;
private boolean acceptLegacy = true; private boolean acceptLegacy = true; // Via - always true
private int depth; private int depth;
TagStringReader(final CharBuffer buffer) { TagStringReader(final CharBuffer buffer) {
@ -125,11 +127,11 @@ final class TagStringReader {
return EMPTY_BYTE_ARRAY; return EMPTY_BYTE_ARRAY;
} }
final IntList bytes = new IntArrayList(); final IntList bytes = new IntArrayList(); // Via - no boxing
while (this.buffer.hasMore()) { while (this.buffer.hasMore()) {
final CharSequence value = this.buffer.skipWhitespace().takeUntil(Tokens.TYPE_BYTE); final CharSequence value = this.buffer.skipWhitespace().takeUntil(Tokens.TYPE_BYTE);
try { try {
bytes.add(Byte.parseByte(value.toString())); bytes.add(Byte.parseByte(value.toString())); // Via
} catch (final NumberFormatException ex) { } catch (final NumberFormatException ex) {
throw this.buffer.makeError("All elements of a byte array must be bytes!"); throw this.buffer.makeError("All elements of a byte array must be bytes!");
} }
@ -137,7 +139,7 @@ final class TagStringReader {
if (this.separatorOrCompleteWith(Tokens.ARRAY_END)) { if (this.separatorOrCompleteWith(Tokens.ARRAY_END)) {
final byte[] result = new byte[bytes.size()]; final byte[] result = new byte[bytes.size()];
for (int i = 0; i < bytes.size(); ++i) { for (int i = 0; i < bytes.size(); ++i) {
result[i] = (byte) bytes.getInt(i); result[i] = (byte) bytes.getInt(i); // Via
} }
return result; return result;
} }
@ -303,12 +305,8 @@ final class TagStringReader {
if (possiblyNumeric) { if (possiblyNumeric) {
try { try {
return new IntTag(Integer.parseInt(built)); return new IntTag(Integer.parseInt(built));
} catch (final NumberFormatException ex) { } catch (final NumberFormatException ignored) {
try { // Via - don't try to parse as DoubleTag here
return new DoubleTag(Double.parseDouble(built));
} catch (final NumberFormatException ex2) {
// ignore
}
} }
} }
@ -326,7 +324,7 @@ final class TagStringReader {
return true; return true;
} }
this.buffer.expect(Tokens.VALUE_SEPARATOR); this.buffer.expect(Tokens.VALUE_SEPARATOR);
return false; return this.buffer.takeIf(endCharacter); // Via - trailing commas are allowed
} }
/** /**

View File

@ -0,0 +1,44 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2021 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.common.nbt;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.viaversion.viaversion.api.minecraft.nbt.BinaryTagIO;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class NBTTagTest {
@Test
void test() throws IOException {
BinaryTagIO.readString("{id:test,test:1}");
BinaryTagIO.readString("{id:test,test:1,}");
BinaryTagIO.readString("{id:[1,2,3,]}");
BinaryTagIO.readString("{id:[I;1,2,3]}");
BinaryTagIO.readString("{id:[I;1,2,3,]}");
Assertions.assertTrue(BinaryTagIO.readString("{id:9000b,num:2147483649}").get("num") instanceof StringTag);
//TODO fix legacy
// BinaryTagIO.readString("{id:minecraft:stone}");
}
}