Skip to content

Commit

Permalink
Merge tag 'refs/tags/sync-piper' into sync-stage
Browse files Browse the repository at this point in the history
  • Loading branch information
acozzette committed Mar 2, 2022
2 parents 1d13b60 + 76398da commit 0ece18c
Show file tree
Hide file tree
Showing 69 changed files with 848 additions and 674 deletions.
1 change: 1 addition & 0 deletions cmake/libprotobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ set(libprotobuf_includes
${protobuf_source_dir}/src/google/protobuf/util/delimited_message_util.h
${protobuf_source_dir}/src/google/protobuf/util/field_comparator.h
${protobuf_source_dir}/src/google/protobuf/util/field_mask_util.h
${protobuf_source_dir}/src/google/protobuf/util/internal/json_escaping.h
${protobuf_source_dir}/src/google/protobuf/util/json_util.h
${protobuf_source_dir}/src/google/protobuf/util/message_differencer.h
${protobuf_source_dir}/src/google/protobuf/util/time_util.h
Expand Down
46 changes: 43 additions & 3 deletions java/core/src/main/java/com/google/protobuf/TextFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ private TextFormat() {}

private static final Logger logger = Logger.getLogger(TextFormat.class.getName());

private static final String DEBUG_STRING_SILENT_MARKER = "\t ";


/**
* Outputs a textual representation of the Protocol Message supplied into the parameter output.
Expand Down Expand Up @@ -945,6 +947,14 @@ private static final class Tokenizer {
Pattern.compile("-?inf(inity)?f?", Pattern.CASE_INSENSITIVE);
private static final Pattern FLOAT_NAN = Pattern.compile("nanf?", Pattern.CASE_INSENSITIVE);

/**
* {@link containsSilentMarkerAfterCurrentToken} indicates if there is a silent marker after the
* current token. This value is moved to {@link containsSilentMarkerAfterPrevToken} every time
* the next token is parsed.
*/
private boolean containsSilentMarkerAfterCurrentToken = false;
private boolean containsSilentMarkerAfterPrevToken = false;

/** Construct a tokenizer that parses tokens from the given text. */
private Tokenizer(final CharSequence text) {
this.text = text;
Expand All @@ -969,6 +979,14 @@ int getColumn() {
return column;
}

boolean getContainsSilentMarkerAfterCurrentToken() {
return containsSilentMarkerAfterCurrentToken;
}

boolean getContainsSilentMarkerAfterPrevToken() {
return containsSilentMarkerAfterPrevToken;
}

/** Are we at the end of the input? */
public boolean atEnd() {
return currentToken.length() == 0;
Expand Down Expand Up @@ -1534,6 +1552,23 @@ public static <T extends Message> T parse(
* control the parser behavior.
*/
public static class Parser {
private int debugStringSilentMarker;

int getSilentMarkerCount() {
return debugStringSilentMarker;
}

/**
* A valid silent marker appears between a field name and its value. If there is a ":" in
* between, the silent marker will only appear after the colon. This is called after a field
* name is parsed, and before the ":" if it exists. If the current token is ":", then
* containsSilentMarkerAfterCurrentToken indicates if there is a valid silent marker. Otherwise,
* the current token is part of the field value, so the silent marker is indicated by
* containsSilentMarkerAfterPrevToken.
*/
private void detectSilentMarker(Tokenizer tokenizer) {
}

/**
* Determines if repeated values for non-repeated fields and oneofs are permitted. For example,
* given required/optional field "foo" and a oneof containing "baz" and "qux":
Expand Down Expand Up @@ -1890,6 +1925,7 @@ private void mergeField(
// start with "{" or "<" which indicates the beginning of a message body.
// If there is no ":" or there is a "{" or "<" after ":", this field has
// to be a message or the input is ill-formed.
detectSilentMarker(tokenizer);
if (tokenizer.tryConsume(":") && !tokenizer.lookingAt("{") && !tokenizer.lookingAt("<")) {
skipFieldValue(tokenizer);
} else {
Expand All @@ -1900,6 +1936,7 @@ private void mergeField(

// Handle potential ':'.
if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
detectSilentMarker(tokenizer);
tokenizer.tryConsume(":"); // optional
if (parseTreeBuilder != null) {
TextFormatParseInfoTree.Builder childParseTreeBuilder =
Expand All @@ -1923,6 +1960,7 @@ private void mergeField(
unknownFields);
}
} else {
detectSilentMarker(tokenizer);
tokenizer.consume(":"); // required
consumeFieldValues(
tokenizer,
Expand Down Expand Up @@ -2184,6 +2222,7 @@ private void mergeAnyFieldValue(
throw tokenizer.parseExceptionPreviousToken("Expected a valid type URL.");
}
}
detectSilentMarker(tokenizer);
tokenizer.tryConsume(":");
final String anyEndToken;
if (tokenizer.tryConsume("<")) {
Expand Down Expand Up @@ -2220,7 +2259,7 @@ private void mergeAnyFieldValue(
}

/** Skips the next field including the field's name and value. */
private static void skipField(Tokenizer tokenizer) throws ParseException {
private void skipField(Tokenizer tokenizer) throws ParseException {
if (tokenizer.tryConsume("[")) {
// Extension name.
do {
Expand All @@ -2237,6 +2276,7 @@ private static void skipField(Tokenizer tokenizer) throws ParseException {
// start with "{" or "<" which indicates the beginning of a message body.
// If there is no ":" or there is a "{" or "<" after ":", this field has
// to be a message or the input is ill-formed.
detectSilentMarker(tokenizer);
if (tokenizer.tryConsume(":") && !tokenizer.lookingAt("<") && !tokenizer.lookingAt("{")) {
skipFieldValue(tokenizer);
} else {
Expand All @@ -2252,7 +2292,7 @@ private static void skipField(Tokenizer tokenizer) throws ParseException {
/**
* Skips the whole body of a message including the beginning delimiter and the ending delimiter.
*/
private static void skipFieldMessage(Tokenizer tokenizer) throws ParseException {
private void skipFieldMessage(Tokenizer tokenizer) throws ParseException {
final String delimiter;
if (tokenizer.tryConsume("<")) {
delimiter = ">";
Expand All @@ -2267,7 +2307,7 @@ private static void skipFieldMessage(Tokenizer tokenizer) throws ParseException
}

/** Skips a field value. */
private static void skipFieldValue(Tokenizer tokenizer) throws ParseException {
private void skipFieldValue(Tokenizer tokenizer) throws ParseException {
if (tokenizer.tryConsumeString()) {
while (tokenizer.tryConsumeString()) {}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ private static void assertEqualBytes(OutputType outputType, byte[] a, byte[] b)
* Writes the given value using writeRawVarint32() and writeRawVarint64() and checks that the
* result matches the given bytes.
*/
@SuppressWarnings("UnnecessaryLongToIntConversion") // Intentionally tests 32-bit int values.
private static void assertWriteVarint(byte[] data, long value) throws Exception {
for (OutputType outputType : OutputType.values()) {
// Only test 32-bit write if the value fits into an int.
Expand Down
Loading

0 comments on commit 0ece18c

Please sign in to comment.