diff --git a/java/core/src/main/java/com/google/protobuf/TextFormat.java b/java/core/src/main/java/com/google/protobuf/TextFormat.java index 9d28d06c95..d16c7bde16 100644 --- a/java/core/src/main/java/com/google/protobuf/TextFormat.java +++ b/java/core/src/main/java/com/google/protobuf/TextFormat.java @@ -1294,20 +1294,11 @@ public final class TextFormat { return consumeByteString().toStringUtf8(); } - /** If the next token is a string, consume it and return true. Otherwise, return false. */ - boolean tryConsumeString() { - try { - consumeString(); - return true; - } catch (ParseException e) { - return false; - } - } - /** * If the next token is a string, consume it, unescape it as a {@link ByteString}, and return * it. Otherwise, throw a {@link ParseException}. */ + @CanIgnoreReturnValue ByteString consumeByteString() throws ParseException { List list = new ArrayList(); consumeByteString(list); @@ -1317,6 +1308,16 @@ public final class TextFormat { return ByteString.copyFrom(list); } + /** If the next token is a string, consume it and return true. Otherwise, return false. */ + boolean tryConsumeByteString() { + try { + consumeByteString(); + return true; + } catch (ParseException e) { + return false; + } + } + /** * Like {@link #consumeByteString()} but adds each token of the string to the given list. String * literals (whether bytes or text) may come in multiple adjacent tokens which are automatically @@ -2282,11 +2283,8 @@ public final class TextFormat { /** Skips a field value. */ private void skipFieldValue(Tokenizer tokenizer) throws ParseException { - if (tokenizer.tryConsumeString()) { - while (tokenizer.tryConsumeString()) {} - return; - } - if (!tokenizer.tryConsumeIdentifier() // includes enum & boolean + if (!tokenizer.tryConsumeByteString() + && !tokenizer.tryConsumeIdentifier() // includes enum & boolean && !tokenizer.tryConsumeInt64() // includes int32 && !tokenizer.tryConsumeUInt64() // includes uint32 && !tokenizer.tryConsumeDouble() diff --git a/python/google/protobuf/text_format.py b/python/google/protobuf/text_format.py index e1a5ad5449..7a6ca7df04 100644 --- a/python/google/protobuf/text_format.py +++ b/python/google/protobuf/text_format.py @@ -1254,15 +1254,10 @@ class _Parser(object): Raises: ParseError: In case an invalid field value is found. """ - # String/bytes tokens can come in multiple adjacent string literals. - # If we can consume one, consume as many as we can. - if tokenizer.TryConsumeByteString(): - while tokenizer.TryConsumeByteString(): - pass - return - - if (not tokenizer.TryConsumeIdentifier() and - not _TryConsumeInt64(tokenizer) and not _TryConsumeUint64(tokenizer) and + if (not tokenizer.TryConsumeByteString()and + not tokenizer.TryConsumeIdentifier() and + not _TryConsumeInt64(tokenizer) and + not _TryConsumeUint64(tokenizer) and not tokenizer.TryConsumeFloat()): raise ParseError('Invalid field value: ' + tokenizer.token)