Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions toml/src/main/java/tools/jackson/dataformat/toml/TomlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,17 @@ private JsonNode parseInt(int nextState) throws IOException {

for (int i = 0; i < length; i++) {
if (buffer[start + i] == '_') {
// slow path to remove underscores
buffer = new String(buffer, start, length).replace("_", "").toCharArray();
start = 0;
length = buffer.length;
// slow path to remove underscores: compact into the already-owned
// buffer itself (chars before the first '_' are already in place),
// avoiding a second array allocation
int pos = start + i;
for (int j = pos + 1; j < start + length; j++) {
char c = buffer[j];
if (c != '_') {
buffer[pos++] = c;
}
}
length = pos - start;
break;
}
}
Expand Down Expand Up @@ -447,7 +454,8 @@ private ValueNode parseIntFromBuffer(char[] buffer, int start, int length) throw
}

private JsonNode parseFloat(int nextState) throws IOException {
final String text = lexer.yytext().replace("_", "");
String rawText = lexer.yytext();
final String text = rawText.indexOf('_') >= 0 ? rawText.replace("_", "") : rawText;
pollExpected(TomlToken.FLOAT, nextState);
if (text.endsWith("nan")) {
return factory.numberNode(Double.NaN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ public void integerTooLongHex() throws IOException {
assertRadixIntegerRejected("0xa");
}

@Test
public void integerUnderscoreBufferGrowth() throws IOException {
// Digits interleaved with underscores, long enough to force the lexer's
// token buffer (initial size 4000) to grow while removing the underscores,
// followed by another key/value pair to prove later tokens are unaffected.
StringBuilder digits = new StringBuilder();
for (int i = 0; i < SCALE; i++) {
digits.append((char) ('0' + (i % 10)));
if (i % 3 == 2 && i != SCALE - 1) {
digits.append('_');
}
}
String toml = "foo = 1" + digits + "\nbar = 42";

ObjectNode node = (ObjectNode) NO_LIMITS_MAPPER.readTree(toml);

BigInteger expected = new BigInteger("1" + digits.toString().replace("_", ""));
assertEquals(expected, node.get("foo").bigIntegerValue());
assertEquals(42, node.get("bar").intValue());
}

private void assertRadixIntegerRejected(String prefixAndFirstDigit) throws IOException {
final ObjectMapper mapper = newTomlMapper();
StringBuilder toml = new StringBuilder("foo = ").append(prefixAndFirstDigit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public JsonGenerator writeName(SerializableString name)
@Override
public JsonGenerator writePropertyId(long id) throws JacksonException {
// 24-Jul-2019, tatu: Should not force construction of a String here...
String idStr = Long.valueOf(id).toString(); // since instances for small values cached
String idStr = Long.toString(id);
if (!_streamWriteContext.writeName(idStr)) {
_reportError("Cannot write a property id, expecting a value");
}
Expand Down Expand Up @@ -684,7 +684,7 @@ public JsonGenerator writeNumber(BigInteger v) throws JacksonException
return writeNull();
}
_verifyValueWrite("write number");
_writeScalar(String.valueOf(v.toString()), "java.math.BigInteger", STYLE_SCALAR);
_writeScalar(v.toString(), "java.math.BigInteger", STYLE_SCALAR);
return this;
}

Expand Down Expand Up @@ -792,7 +792,7 @@ public JsonGenerator writeObjectRef(Object id)
throws JacksonException
{
_verifyValueWrite("write Object reference");
AliasEvent evt = new AliasEvent(Optional.of(String.valueOf(id)).map(s -> new Anchor(s)));
AliasEvent evt = new AliasEvent(id == null ? Optional.empty() : Optional.of(new Anchor(id.toString())));
_emit(evt);
return this;
}
Expand Down