Remove loop when skipping string field values.

No need for a loop that that calls tryConsumeString or tryConsumeByteString repeatedly, since ConsumeByteString already loops itself.

PiperOrigin-RevId: 517202228
pull/12243/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 5d669c5cfb
commit 8eac9a031e
  1. 28
      java/core/src/main/java/com/google/protobuf/TextFormat.java
  2. 13
      python/google/protobuf/text_format.py

@ -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<ByteString> list = new ArrayList<ByteString>();
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()

@ -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)

Loading…
Cancel
Save