TextFormat.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
37 import java.io.IOException;
38 import java.math.BigInteger;
39 import java.nio.CharBuffer;
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.Map;
44 import java.util.logging.Logger;
45 import java.util.regex.Matcher;
46 import java.util.regex.Pattern;
47 
55 public final class TextFormat {
56  private TextFormat() {}
57 
58  private static final Logger logger = Logger.getLogger(TextFormat.class.getName());
59 
67  @Deprecated
68  public static void print(final MessageOrBuilder message, final Appendable output)
69  throws IOException {
71  }
72 
78  @Deprecated
79  public static void print(final UnknownFieldSet fields, final Appendable output)
80  throws IOException {
82  }
83 
89  @Deprecated
90  public static void printUnicode(final MessageOrBuilder message, final Appendable output)
91  throws IOException {
93  }
94 
100  @Deprecated
101  public static void printUnicode(final UnknownFieldSet fields, final Appendable output)
102  throws IOException {
104  }
105 
111  public static String shortDebugString(final MessageOrBuilder message) {
112  return printer().shortDebugString(message);
113  }
114 
121  @Deprecated
122  public static String shortDebugString(final FieldDescriptor field, final Object value) {
123  return printer().shortDebugString(field, value);
124  }
125 
132  @Deprecated
133  public static String shortDebugString(final UnknownFieldSet fields) {
134  return printer().shortDebugString(fields);
135  }
136 
142  @Deprecated
143  public static String printToString(final MessageOrBuilder message) {
144  return printer().printToString(message);
145  }
146 
152  @Deprecated
153  public static String printToString(final UnknownFieldSet fields) {
154  return printer().printToString(fields);
155  }
156 
163  @Deprecated
164  public static String printToUnicodeString(final MessageOrBuilder message) {
166  }
167 
174  @Deprecated
175  public static String printToUnicodeString(final UnknownFieldSet fields) {
176  return printer().escapingNonAscii(false).printToString(fields);
177  }
178 
180  @Deprecated
181  public static void printField(
182  final FieldDescriptor field, final Object value, final Appendable output) throws IOException {
184  }
185 
187  @Deprecated
188  public static String printFieldToString(final FieldDescriptor field, final Object value) {
190  }
191 
206  @Deprecated
207  public static void printUnicodeFieldValue(
208  final FieldDescriptor field, final Object value, final Appendable output) throws IOException {
210  }
211 
222  @Deprecated
223  public static void printFieldValue(
224  final FieldDescriptor field, final Object value, final Appendable output) throws IOException {
226  }
227 
237  public static void printUnknownFieldValue(
238  final int tag, final Object value, final Appendable output) throws IOException {
240  }
241 
242  private static void printUnknownFieldValue(
243  final int tag, final Object value, final TextGenerator generator) throws IOException {
244  switch (WireFormat.getTagWireType(tag)) {
246  generator.print(unsignedToString((Long) value));
247  break;
249  generator.print(String.format((Locale) null, "0x%08x", (Integer) value));
250  break;
252  generator.print(String.format((Locale) null, "0x%016x", (Long) value));
253  break;
255  try {
256  // Try to parse and print the field as an embedded message
258  generator.print("{");
259  generator.eol();
260  generator.indent();
261  Printer.printUnknownFields(message, generator);
262  generator.outdent();
263  generator.print("}");
264  } catch (InvalidProtocolBufferException e) {
265  // If not parseable as a message, print as a String
266  generator.print("\"");
267  generator.print(escapeBytes((ByteString) value));
268  generator.print("\"");
269  }
270  break;
273  break;
274  default:
275  throw new IllegalArgumentException("Bad tag: " + tag);
276  }
277  }
278 
280  public static Printer printer() {
281  return Printer.DEFAULT;
282  }
283 
285  public static final class Printer {
286 
287  // Printer instance which escapes non-ASCII characters.
288  private static final Printer DEFAULT = new Printer(true);
289 
291  private final boolean escapeNonAscii;
292 
293  private Printer(boolean escapeNonAscii) {
294  this.escapeNonAscii = escapeNonAscii;
295  }
296 
307  return new Printer(escapeNonAscii);
308  }
309 
315  public void print(final MessageOrBuilder message, final Appendable output) throws IOException {
317  }
318 
320  public void print(final UnknownFieldSet fields, final Appendable output) throws IOException {
322  }
323 
324  private void print(final MessageOrBuilder message, final TextGenerator generator)
325  throws IOException {
326  printMessage(message, generator);
327  }
328 
329  public String printFieldToString(final FieldDescriptor field, final Object value) {
330  try {
331  final StringBuilder text = new StringBuilder();
332  printField(field, value, text);
333  return text.toString();
334  } catch (IOException e) {
335  throw new IllegalStateException(e);
336  }
337  }
338 
339  public void printField(final FieldDescriptor field, final Object value, final Appendable output)
340  throws IOException {
342  }
343 
344  private void printField(
345  final FieldDescriptor field, final Object value, final TextGenerator generator)
346  throws IOException {
347  if (field.isRepeated()) {
348  // Repeated field. Print each element.
349  for (Object element : (List<?>) value) {
350  printSingleField(field, element, generator);
351  }
352  } else {
353  printSingleField(field, value, generator);
354  }
355  }
356 
366  public void printFieldValue(
367  final FieldDescriptor field, final Object value, final Appendable output)
368  throws IOException {
370  }
371 
372  private void printFieldValue(
373  final FieldDescriptor field, final Object value, final TextGenerator generator)
374  throws IOException {
375  switch (field.getType()) {
376  case INT32:
377  case SINT32:
378  case SFIXED32:
379  generator.print(((Integer) value).toString());
380  break;
381 
382  case INT64:
383  case SINT64:
384  case SFIXED64:
385  generator.print(((Long) value).toString());
386  break;
387 
388  case BOOL:
389  generator.print(((Boolean) value).toString());
390  break;
391 
392  case FLOAT:
393  generator.print(((Float) value).toString());
394  break;
395 
396  case DOUBLE:
397  generator.print(((Double) value).toString());
398  break;
399 
400  case UINT32:
401  case FIXED32:
402  generator.print(unsignedToString((Integer) value));
403  break;
404 
405  case UINT64:
406  case FIXED64:
407  generator.print(unsignedToString((Long) value));
408  break;
409 
410  case STRING:
411  generator.print("\"");
412  generator.print(
414  ? TextFormatEscaper.escapeText((String) value)
415  : escapeDoubleQuotesAndBackslashes((String) value).replace("\n", "\\n"));
416  generator.print("\"");
417  break;
418 
419  case BYTES:
420  generator.print("\"");
421  if (value instanceof ByteString) {
422  generator.print(escapeBytes((ByteString) value));
423  } else {
424  generator.print(escapeBytes((byte[]) value));
425  }
426  generator.print("\"");
427  break;
428 
429  case ENUM:
430  generator.print(((EnumValueDescriptor) value).getName());
431  break;
432 
433  case MESSAGE:
434  case GROUP:
435  print((Message) value, generator);
436  break;
437  }
438  }
439 
441  public String printToString(final MessageOrBuilder message) {
442  try {
443  final StringBuilder text = new StringBuilder();
444  print(message, text);
445  return text.toString();
446  } catch (IOException e) {
447  throw new IllegalStateException(e);
448  }
449  }
451  public String printToString(final UnknownFieldSet fields) {
452  try {
453  final StringBuilder text = new StringBuilder();
454  print(fields, text);
455  return text.toString();
456  } catch (IOException e) {
457  throw new IllegalStateException(e);
458  }
459  }
460 
466  try {
467  final StringBuilder text = new StringBuilder();
469  return text.toString();
470  } catch (IOException e) {
471  throw new IllegalStateException(e);
472  }
473  }
474 
479  public String shortDebugString(final FieldDescriptor field, final Object value) {
480  try {
481  final StringBuilder text = new StringBuilder();
483  return text.toString();
484  } catch (IOException e) {
485  throw new IllegalStateException(e);
486  }
487  }
488 
493  public String shortDebugString(final UnknownFieldSet fields) {
494  try {
495  final StringBuilder text = new StringBuilder();
497  return text.toString();
498  } catch (IOException e) {
499  throw new IllegalStateException(e);
500  }
501  }
502 
503  private static void printUnknownFieldValue(
504  final int tag, final Object value, final TextGenerator generator) throws IOException {
505  switch (WireFormat.getTagWireType(tag)) {
507  generator.print(unsignedToString((Long) value));
508  break;
510  generator.print(String.format((Locale) null, "0x%08x", (Integer) value));
511  break;
513  generator.print(String.format((Locale) null, "0x%016x", (Long) value));
514  break;
516  try {
517  // Try to parse and print the field as an embedded message
519  generator.print("{");
520  generator.eol();
521  generator.indent();
522  printUnknownFields(message, generator);
523  generator.outdent();
524  generator.print("}");
525  } catch (InvalidProtocolBufferException e) {
526  // If not parseable as a message, print as a String
527  generator.print("\"");
528  generator.print(escapeBytes((ByteString) value));
529  generator.print("\"");
530  }
531  break;
534  break;
535  default:
536  throw new IllegalArgumentException("Bad tag: " + tag);
537  }
538  }
539 
540  private void printMessage(final MessageOrBuilder message, final TextGenerator generator)
541  throws IOException {
542  for (Map.Entry<FieldDescriptor, Object> field : message.getAllFields().entrySet()) {
543  printField(field.getKey(), field.getValue(), generator);
544  }
545  printUnknownFields(message.getUnknownFields(), generator);
546  }
547 
548  private void printSingleField(
549  final FieldDescriptor field, final Object value, final TextGenerator generator)
550  throws IOException {
551  if (field.isExtension()) {
552  generator.print("[");
553  // We special-case MessageSet elements for compatibility with proto1.
554  if (field.getContainingType().getOptions().getMessageSetWireFormat()
555  && (field.getType() == FieldDescriptor.Type.MESSAGE)
556  && (field.isOptional())
557  // object equality
558  && (field.getExtensionScope() == field.getMessageType())) {
559  generator.print(field.getMessageType().getFullName());
560  } else {
561  generator.print(field.getFullName());
562  }
563  generator.print("]");
564  } else {
565  if (field.getType() == FieldDescriptor.Type.GROUP) {
566  // Groups must be serialized with their original capitalization.
567  generator.print(field.getMessageType().getName());
568  } else {
569  generator.print(field.getName());
570  }
571  }
572 
573  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
574  generator.print(" {");
575  generator.eol();
576  generator.indent();
577  } else {
578  generator.print(": ");
579  }
580 
581  printFieldValue(field, value, generator);
582 
583  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
584  generator.outdent();
585  generator.print("}");
586  }
587  generator.eol();
588  }
589 
590  private static void printUnknownFields(
591  final UnknownFieldSet unknownFields, final TextGenerator generator) throws IOException {
592  for (Map.Entry<Integer, UnknownFieldSet.Field> entry : unknownFields.asMap().entrySet()) {
593  final int number = entry.getKey();
594  final UnknownFieldSet.Field field = entry.getValue();
595  printUnknownField(number, WireFormat.WIRETYPE_VARINT, field.getVarintList(), generator);
596  printUnknownField(number, WireFormat.WIRETYPE_FIXED32, field.getFixed32List(), generator);
597  printUnknownField(number, WireFormat.WIRETYPE_FIXED64, field.getFixed64List(), generator);
599  number,
601  field.getLengthDelimitedList(),
602  generator);
603  for (final UnknownFieldSet value : field.getGroupList()) {
604  generator.print(entry.getKey().toString());
605  generator.print(" {");
606  generator.eol();
607  generator.indent();
608  printUnknownFields(value, generator);
609  generator.outdent();
610  generator.print("}");
611  generator.eol();
612  }
613  }
614  }
615 
616  private static void printUnknownField(
617  final int number, final int wireType, final List<?> values, final TextGenerator generator)
618  throws IOException {
619  for (final Object value : values) {
620  generator.print(String.valueOf(number));
621  generator.print(": ");
622  printUnknownFieldValue(wireType, value, generator);
623  generator.eol();
624  }
625  }
626  }
627 
629  public static String unsignedToString(final int value) {
630  if (value >= 0) {
631  return Integer.toString(value);
632  } else {
633  return Long.toString(value & 0x00000000FFFFFFFFL);
634  }
635  }
636 
638  public static String unsignedToString(final long value) {
639  if (value >= 0) {
640  return Long.toString(value);
641  } else {
642  // Pull off the most-significant bit so that BigInteger doesn't think
643  // the number is negative, then set it again using setBit().
644  return BigInteger.valueOf(value & 0x7FFFFFFFFFFFFFFFL).setBit(63).toString();
645  }
646  }
647 
648  private static TextGenerator multiLineOutput(Appendable output) {
649  return new TextGenerator(output, false);
650  }
651 
652  private static TextGenerator singleLineOutput(Appendable output) {
653  return new TextGenerator(output, true);
654  }
655 
657  private static final class TextGenerator {
658  private final Appendable output;
659  private final StringBuilder indent = new StringBuilder();
660  private final boolean singleLineMode;
661  // While technically we are "at the start of a line" at the very beginning of the output, all
662  // we would do in response to this is emit the (zero length) indentation, so it has no effect.
663  // Setting it false here does however suppress an unwanted leading space in single-line mode.
664  private boolean atStartOfLine = false;
665 
666  private TextGenerator(final Appendable output, boolean singleLineMode) {
667  this.output = output;
668  this.singleLineMode = singleLineMode;
669  }
670 
676  public void indent() {
677  indent.append(" ");
678  }
679 
681  public void outdent() {
682  final int length = indent.length();
683  if (length == 0) {
684  throw new IllegalArgumentException(" Outdent() without matching Indent().");
685  }
686  indent.setLength(length - 2);
687  }
688 
693  public void print(final CharSequence text) throws IOException {
694  if (atStartOfLine) {
695  atStartOfLine = false;
696  output.append(singleLineMode ? " " : indent);
697  }
698  output.append(text);
699  }
700 
706  public void eol() throws IOException {
707  if (!singleLineMode) {
708  output.append("\n");
709  }
710  atStartOfLine = true;
711  }
712  }
713 
714  // =================================================================
715  // Parsing
716 
741  private static final class Tokenizer {
742  private final CharSequence text;
743  private final Matcher matcher;
744  private String currentToken;
745 
746  // The character index within this.text at which the current token begins.
747  private int pos = 0;
748 
749  // The line and column numbers of the current token.
750  private int line = 0;
751  private int column = 0;
752 
753  // The line and column numbers of the previous token (allows throwing
754  // errors *after* consuming).
755  private int previousLine = 0;
756  private int previousColumn = 0;
757 
758  // We use possessive quantifiers (*+ and ++) because otherwise the Java
759  // regex matcher has stack overflows on large inputs.
760  private static final Pattern WHITESPACE = Pattern.compile("(\\s|(#.*$))++", Pattern.MULTILINE);
761  private static final Pattern TOKEN =
762  Pattern.compile(
763  "[a-zA-Z_][0-9a-zA-Z_+-]*+|" // an identifier
764  + "[.]?[0-9+-][0-9a-zA-Z_.+-]*+|" // a number
765  + "\"([^\"\n\\\\]|\\\\.)*+(\"|\\\\?$)|" // a double-quoted string
766  + "\'([^\'\n\\\\]|\\\\.)*+(\'|\\\\?$)", // a single-quoted string
767  Pattern.MULTILINE);
768 
769  private static final Pattern DOUBLE_INFINITY =
770  Pattern.compile("-?inf(inity)?", Pattern.CASE_INSENSITIVE);
771  private static final Pattern FLOAT_INFINITY =
772  Pattern.compile("-?inf(inity)?f?", Pattern.CASE_INSENSITIVE);
773  private static final Pattern FLOAT_NAN = Pattern.compile("nanf?", Pattern.CASE_INSENSITIVE);
774 
776  private Tokenizer(final CharSequence text) {
777  this.text = text;
778  this.matcher = WHITESPACE.matcher(text);
779  skipWhitespace();
780  nextToken();
781  }
782 
783  int getPreviousLine() {
784  return previousLine;
785  }
786 
787  int getPreviousColumn() {
788  return previousColumn;
789  }
790 
791  int getLine() {
792  return line;
793  }
794 
795  int getColumn() {
796  return column;
797  }
798 
800  public boolean atEnd() {
801  return currentToken.length() == 0;
802  }
803 
805  public void nextToken() {
806  previousLine = line;
808 
809  // Advance the line counter to the current position.
810  while (pos < matcher.regionStart()) {
811  if (text.charAt(pos) == '\n') {
812  ++line;
813  column = 0;
814  } else {
815  ++column;
816  }
817  ++pos;
818  }
819 
820  // Match the next token.
821  if (matcher.regionStart() == matcher.regionEnd()) {
822  // EOF
823  currentToken = "";
824  } else {
825  matcher.usePattern(TOKEN);
826  if (matcher.lookingAt()) {
827  currentToken = matcher.group();
828  matcher.region(matcher.end(), matcher.regionEnd());
829  } else {
830  // Take one character.
831  currentToken = String.valueOf(text.charAt(pos));
832  matcher.region(pos + 1, matcher.regionEnd());
833  }
834 
835  skipWhitespace();
836  }
837  }
838 
840  private void skipWhitespace() {
841  matcher.usePattern(WHITESPACE);
842  if (matcher.lookingAt()) {
843  matcher.region(matcher.end(), matcher.regionEnd());
844  }
845  }
846 
851  public boolean tryConsume(final String token) {
852  if (currentToken.equals(token)) {
853  nextToken();
854  return true;
855  } else {
856  return false;
857  }
858  }
859 
864  public void consume(final String token) throws ParseException {
865  if (!tryConsume(token)) {
866  throw parseException("Expected \"" + token + "\".");
867  }
868  }
869 
871  public boolean lookingAtInteger() {
872  if (currentToken.length() == 0) {
873  return false;
874  }
875 
876  final char c = currentToken.charAt(0);
877  return ('0' <= c && c <= '9') || c == '-' || c == '+';
878  }
879 
881  public boolean lookingAt(String text) {
882  return currentToken.equals(text);
883  }
884 
889  public String consumeIdentifier() throws ParseException {
890  for (int i = 0; i < currentToken.length(); i++) {
891  final char c = currentToken.charAt(i);
892  if (('a' <= c && c <= 'z')
893  || ('A' <= c && c <= 'Z')
894  || ('0' <= c && c <= '9')
895  || (c == '_')
896  || (c == '.')) {
897  // OK
898  } else {
899  throw parseException("Expected identifier. Found '" + currentToken + "'");
900  }
901  }
902 
903  final String result = currentToken;
904  nextToken();
905  return result;
906  }
907 
912  public boolean tryConsumeIdentifier() {
913  try {
915  return true;
916  } catch (ParseException e) {
917  return false;
918  }
919  }
920 
925  public int consumeInt32() throws ParseException {
926  try {
927  final int result = parseInt32(currentToken);
928  nextToken();
929  return result;
930  } catch (NumberFormatException e) {
931  throw integerParseException(e);
932  }
933  }
934 
939  public int consumeUInt32() throws ParseException {
940  try {
941  final int result = parseUInt32(currentToken);
942  nextToken();
943  return result;
944  } catch (NumberFormatException e) {
945  throw integerParseException(e);
946  }
947  }
948 
953  public long consumeInt64() throws ParseException {
954  try {
955  final long result = parseInt64(currentToken);
956  nextToken();
957  return result;
958  } catch (NumberFormatException e) {
959  throw integerParseException(e);
960  }
961  }
962 
967  public boolean tryConsumeInt64() {
968  try {
969  consumeInt64();
970  return true;
971  } catch (ParseException e) {
972  return false;
973  }
974  }
975 
980  public long consumeUInt64() throws ParseException {
981  try {
982  final long result = parseUInt64(currentToken);
983  nextToken();
984  return result;
985  } catch (NumberFormatException e) {
986  throw integerParseException(e);
987  }
988  }
989 
994  public boolean tryConsumeUInt64() {
995  try {
996  consumeUInt64();
997  return true;
998  } catch (ParseException e) {
999  return false;
1000  }
1001  }
1002 
1007  public double consumeDouble() throws ParseException {
1008  // We need to parse infinity and nan separately because
1009  // Double.parseDouble() does not accept "inf", "infinity", or "nan".
1010  if (DOUBLE_INFINITY.matcher(currentToken).matches()) {
1011  final boolean negative = currentToken.startsWith("-");
1012  nextToken();
1013  return negative ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
1014  }
1015  if (currentToken.equalsIgnoreCase("nan")) {
1016  nextToken();
1017  return Double.NaN;
1018  }
1019  try {
1020  final double result = Double.parseDouble(currentToken);
1021  nextToken();
1022  return result;
1023  } catch (NumberFormatException e) {
1024  throw floatParseException(e);
1025  }
1026  }
1027 
1032  public boolean tryConsumeDouble() {
1033  try {
1034  consumeDouble();
1035  return true;
1036  } catch (ParseException e) {
1037  return false;
1038  }
1039  }
1040 
1045  public float consumeFloat() throws ParseException {
1046  // We need to parse infinity and nan separately because
1047  // Float.parseFloat() does not accept "inf", "infinity", or "nan".
1048  if (FLOAT_INFINITY.matcher(currentToken).matches()) {
1049  final boolean negative = currentToken.startsWith("-");
1050  nextToken();
1051  return negative ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
1052  }
1053  if (FLOAT_NAN.matcher(currentToken).matches()) {
1054  nextToken();
1055  return Float.NaN;
1056  }
1057  try {
1058  final float result = Float.parseFloat(currentToken);
1059  nextToken();
1060  return result;
1061  } catch (NumberFormatException e) {
1062  throw floatParseException(e);
1063  }
1064  }
1065 
1070  public boolean tryConsumeFloat() {
1071  try {
1072  consumeFloat();
1073  return true;
1074  } catch (ParseException e) {
1075  return false;
1076  }
1077  }
1078 
1083  public boolean consumeBoolean() throws ParseException {
1084  if (currentToken.equals("true")
1085  || currentToken.equals("True")
1086  || currentToken.equals("t")
1087  || currentToken.equals("1")) {
1088  nextToken();
1089  return true;
1090  } else if (currentToken.equals("false")
1091  || currentToken.equals("False")
1092  || currentToken.equals("f")
1093  || currentToken.equals("0")) {
1094  nextToken();
1095  return false;
1096  } else {
1097  throw parseException("Expected \"true\" or \"false\". Found \"" + currentToken + "\".");
1098  }
1099  }
1100 
1105  public String consumeString() throws ParseException {
1106  return consumeByteString().toStringUtf8();
1107  }
1108 
1110  public boolean tryConsumeString() {
1111  try {
1112  consumeString();
1113  return true;
1114  } catch (ParseException e) {
1115  return false;
1116  }
1117  }
1118 
1124  List<ByteString> list = new ArrayList<ByteString>();
1125  consumeByteString(list);
1126  while (currentToken.startsWith("'") || currentToken.startsWith("\"")) {
1127  consumeByteString(list);
1128  }
1129  return ByteString.copyFrom(list);
1130  }
1131 
1137  private void consumeByteString(List<ByteString> list) throws ParseException {
1138  final char quote = currentToken.length() > 0 ? currentToken.charAt(0) : '\0';
1139  if (quote != '\"' && quote != '\'') {
1140  throw parseException("Expected string.");
1141  }
1142 
1143  if (currentToken.length() < 2 || currentToken.charAt(currentToken.length() - 1) != quote) {
1144  throw parseException("String missing ending quote.");
1145  }
1146 
1147  try {
1148  final String escaped = currentToken.substring(1, currentToken.length() - 1);
1149  final ByteString result = unescapeBytes(escaped);
1150  nextToken();
1151  list.add(result);
1152  } catch (InvalidEscapeSequenceException e) {
1153  throw parseException(e.getMessage());
1154  }
1155  }
1156 
1162  // Note: People generally prefer one-based line and column numbers.
1163  return new ParseException(line + 1, column + 1, description);
1164  }
1165 
1171  // Note: People generally prefer one-based line and column numbers.
1172  return new ParseException(previousLine + 1, previousColumn + 1, description);
1173  }
1174 
1179  private ParseException integerParseException(final NumberFormatException e) {
1180  return parseException("Couldn't parse integer: " + e.getMessage());
1181  }
1182 
1187  private ParseException floatParseException(final NumberFormatException e) {
1188  return parseException("Couldn't parse number: " + e.getMessage());
1189  }
1190 
1196  final String unknownField, final String description) {
1197  // Note: People generally prefer one-based line and column numbers.
1198  return new UnknownFieldParseException(
1199  previousLine + 1, previousColumn + 1, unknownField, description);
1200  }
1201  }
1202 
1204  public static class ParseException extends IOException {
1205  private static final long serialVersionUID = 3196188060225107702L;
1206 
1207  private final int line;
1208  private final int column;
1209 
1211  public ParseException(final String message) {
1212  this(-1, -1, message);
1213  }
1214 
1221  public ParseException(final int line, final int column, final String message) {
1222  super(Integer.toString(line) + ":" + column + ": " + message);
1223  this.line = line;
1224  this.column = column;
1225  }
1226 
1231  public int getLine() {
1232  return line;
1233  }
1234 
1239  public int getColumn() {
1240  return column;
1241  }
1242  }
1243 
1245  public static class UnknownFieldParseException extends ParseException {
1246  private final String unknownField;
1247 
1252  public UnknownFieldParseException(final String message) {
1253  this(-1, -1, "", message);
1254  }
1255 
1264  final int line, final int column, final String unknownField, final String message) {
1265  super(line, column, message);
1266  this.unknownField = unknownField;
1267  }
1268 
1272  public String getUnknownField() {
1273  return unknownField;
1274  }
1275  }
1276 
1277  private static final Parser PARSER = Parser.newBuilder().build();
1278 
1283  public static Parser getParser() {
1284  return PARSER;
1285  }
1286 
1288  public static void merge(final Readable input, final Message.Builder builder) throws IOException {
1289  PARSER.merge(input, builder);
1290  }
1291 
1293  public static void merge(final CharSequence input, final Message.Builder builder)
1294  throws ParseException {
1295  PARSER.merge(input, builder);
1296  }
1297 
1303  public static <T extends Message> T parse(final CharSequence input, final Class<T> protoClass)
1304  throws ParseException {
1305  Message.Builder builder = Internal.getDefaultInstance(protoClass).newBuilderForType();
1306  merge(input, builder);
1307  @SuppressWarnings("unchecked")
1308  T output = (T) builder.build();
1309  return output;
1310  }
1311 
1316  public static void merge(
1317  final Readable input,
1318  final ExtensionRegistry extensionRegistry,
1319  final Message.Builder builder)
1320  throws IOException {
1321  PARSER.merge(input, extensionRegistry, builder);
1322  }
1323 
1324 
1329  public static void merge(
1330  final CharSequence input,
1331  final ExtensionRegistry extensionRegistry,
1332  final Message.Builder builder)
1333  throws ParseException {
1334  PARSER.merge(input, extensionRegistry, builder);
1335  }
1336 
1343  public static <T extends Message> T parse(
1344  final CharSequence input,
1345  final ExtensionRegistry extensionRegistry,
1346  final Class<T> protoClass)
1347  throws ParseException {
1348  Message.Builder builder = Internal.getDefaultInstance(protoClass).newBuilderForType();
1349  merge(input, extensionRegistry, builder);
1350  @SuppressWarnings("unchecked")
1351  T output = (T) builder.build();
1352  return output;
1353  }
1354 
1355 
1363  public static class Parser {
1382  FORBID_SINGULAR_OVERWRITES
1383  }
1384 
1385  private final boolean allowUnknownFields;
1386  private final boolean allowUnknownEnumValues;
1387  private final boolean allowUnknownExtensions;
1390 
1391  private Parser(
1392  boolean allowUnknownFields,
1393  boolean allowUnknownEnumValues,
1394  boolean allowUnknownExtensions,
1397  this.allowUnknownFields = allowUnknownFields;
1398  this.allowUnknownEnumValues = allowUnknownEnumValues;
1399  this.allowUnknownExtensions = allowUnknownExtensions;
1400  this.singularOverwritePolicy = singularOverwritePolicy;
1402  }
1403 
1405  public static Builder newBuilder() {
1406  return new Builder();
1407  }
1408 
1410  public static class Builder {
1411  private boolean allowUnknownFields = false;
1412  private boolean allowUnknownEnumValues = false;
1413  private boolean allowUnknownExtensions = false;
1417 
1427  this.allowUnknownFields = allowUnknownFields;
1428  return this;
1429  }
1430 
1438  this.allowUnknownExtensions = allowUnknownExtensions;
1439  return this;
1440  }
1441 
1444  this.singularOverwritePolicy = p;
1445  return this;
1446  }
1447 
1450  return this;
1451  }
1452 
1453  public Parser build() {
1454  return new Parser(
1460  }
1461  }
1462 
1466  public void merge(final Readable input, final Message.Builder builder) throws IOException {
1468  }
1469 
1473  public void merge(final CharSequence input, final Message.Builder builder)
1474  throws ParseException {
1476  }
1477 
1482  public void merge(
1483  final Readable input,
1484  final ExtensionRegistry extensionRegistry,
1485  final Message.Builder builder)
1486  throws IOException {
1487  // Read the entire input to a String then parse that.
1488 
1489  // If StreamTokenizer were not quite so crippled, or if there were a kind
1490  // of Reader that could read in chunks that match some particular regex,
1491  // or if we wanted to write a custom Reader to tokenize our stream, then
1492  // we would not have to read to one big String. Alas, none of these is
1493  // the case. Oh well.
1494 
1495  merge(toStringBuilder(input), extensionRegistry, builder);
1496  }
1497 
1498 
1499  private static final int BUFFER_SIZE = 4096;
1500 
1501  // TODO(chrisn): See if working around java.io.Reader#read(CharBuffer)
1502  // overhead is worthwhile
1503  private static StringBuilder toStringBuilder(final Readable input) throws IOException {
1504  final StringBuilder text = new StringBuilder();
1505  final CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
1506  while (true) {
1507  final int n = input.read(buffer);
1508  if (n == -1) {
1509  break;
1510  }
1511  buffer.flip();
1512  text.append(buffer, 0, n);
1513  }
1514  return text;
1515  }
1516 
1517  static final class UnknownField {
1518  static enum Type {
1519  FIELD, EXTENSION;
1520  }
1521 
1522  final String message;
1523  final Type type;
1524 
1525  UnknownField(String message, Type type) {
1526  this.message = message;
1527  this.type = type;
1528  }
1529  }
1530 
1531  // Check both unknown fields and unknown extensions and log warning messages
1532  // or throw exceptions according to the flag.
1533  private void checkUnknownFields(final List<UnknownField> unknownFields) throws ParseException {
1534  if (unknownFields.isEmpty()) {
1535  return;
1536  }
1537 
1538  StringBuilder msg = new StringBuilder("Input contains unknown fields and/or extensions:");
1539  for (UnknownField field : unknownFields) {
1540  msg.append('\n').append(field.message);
1541  }
1542 
1543  if (allowUnknownFields) {
1544  logger.warning(msg.toString());
1545  return;
1546  }
1547 
1548  int firstErrorIndex = 0;
1549  if (allowUnknownExtensions) {
1550  boolean allUnknownExtensions = true;
1551  for (UnknownField field : unknownFields) {
1552  if (field.type == UnknownField.Type.FIELD) {
1553  allUnknownExtensions = false;
1554  break;
1555  }
1556  ++firstErrorIndex;
1557  }
1558  if (allUnknownExtensions) {
1559  logger.warning(msg.toString());
1560  return;
1561  }
1562  }
1563 
1564  String[] lineColumn = unknownFields.get(firstErrorIndex).message.split(":");
1565  throw new ParseException(
1566  Integer.parseInt(lineColumn[0]), Integer.parseInt(lineColumn[1]), msg.toString());
1567  }
1568 
1573  public void merge(
1574  final CharSequence input,
1575  final ExtensionRegistry extensionRegistry,
1576  final Message.Builder builder)
1577  throws ParseException {
1578  final Tokenizer tokenizer = new Tokenizer(input);
1579  MessageReflection.BuilderAdapter target = new MessageReflection.BuilderAdapter(builder);
1580 
1581  List<UnknownField> unknownFields = new ArrayList<UnknownField>();
1582 
1583  while (!tokenizer.atEnd()) {
1584  mergeField(tokenizer, extensionRegistry, target, unknownFields);
1585  }
1586 
1587  checkUnknownFields(unknownFields);
1588  }
1589 
1590 
1592  private void mergeField(
1593  final Tokenizer tokenizer,
1594  final ExtensionRegistry extensionRegistry,
1595  final MessageReflection.MergeTarget target,
1596  List<UnknownField> unknownFields)
1597  throws ParseException {
1598  mergeField(
1599  tokenizer,
1600  extensionRegistry,
1601  target,
1603  unknownFields);
1604  }
1605 
1607  private void mergeField(
1608  final Tokenizer tokenizer,
1609  final ExtensionRegistry extensionRegistry,
1610  final MessageReflection.MergeTarget target,
1611  TextFormatParseInfoTree.Builder parseTreeBuilder,
1612  List<UnknownField> unknownFields)
1613  throws ParseException {
1614  FieldDescriptor field = null;
1615  int startLine = tokenizer.getLine();
1616  int startColumn = tokenizer.getColumn();
1617  final Descriptor type = target.getDescriptorForType();
1619 
1620  if (tokenizer.tryConsume("[")) {
1621  // An extension.
1622  final StringBuilder name = new StringBuilder(tokenizer.consumeIdentifier());
1623  while (tokenizer.tryConsume(".")) {
1624  name.append('.');
1625  name.append(tokenizer.consumeIdentifier());
1626  }
1627 
1628  extension = target.findExtensionByName(extensionRegistry, name.toString());
1629 
1630  if (extension == null) {
1631  String message =
1632  (tokenizer.getPreviousLine() + 1)
1633  + ":"
1634  + (tokenizer.getPreviousColumn() + 1)
1635  + ":\t"
1636  + type.getFullName()
1637  + ".["
1638  + name
1639  + "]";
1640  unknownFields.add(new UnknownField(message, UnknownField.Type.EXTENSION));
1641  } else {
1642  if (extension.descriptor.getContainingType() != type) {
1643  throw tokenizer.parseExceptionPreviousToken(
1644  "Extension \""
1645  + name
1646  + "\" does not extend message type \""
1647  + type.getFullName()
1648  + "\".");
1649  }
1650  field = extension.descriptor;
1651  }
1652 
1653  tokenizer.consume("]");
1654  } else {
1655  final String name = tokenizer.consumeIdentifier();
1656  field = type.findFieldByName(name);
1657 
1658  // Group names are expected to be capitalized as they appear in the
1659  // .proto file, which actually matches their type names, not their field
1660  // names.
1661  if (field == null) {
1662  // Explicitly specify US locale so that this code does not break when
1663  // executing in Turkey.
1664  final String lowerName = name.toLowerCase(Locale.US);
1665  field = type.findFieldByName(lowerName);
1666  // If the case-insensitive match worked but the field is NOT a group,
1667  if (field != null && field.getType() != FieldDescriptor.Type.GROUP) {
1668  field = null;
1669  }
1670  }
1671  // Again, special-case group names as described above.
1672  if (field != null
1673  && field.getType() == FieldDescriptor.Type.GROUP
1674  && !field.getMessageType().getName().equals(name)) {
1675  field = null;
1676  }
1677 
1678  if (field == null) {
1679  String message = (tokenizer.getPreviousLine() + 1)
1680  + ":"
1681  + (tokenizer.getPreviousColumn() + 1)
1682  + ":\t"
1683  + type.getFullName()
1684  + "."
1685  + name;
1686  unknownFields.add(new UnknownField(message, UnknownField.Type.FIELD));
1687  }
1688  }
1689 
1690  // Skips unknown fields.
1691  if (field == null) {
1692  // Try to guess the type of this field.
1693  // If this field is not a message, there should be a ":" between the
1694  // field name and the field value and also the field value should not
1695  // start with "{" or "<" which indicates the beginning of a message body.
1696  // If there is no ":" or there is a "{" or "<" after ":", this field has
1697  // to be a message or the input is ill-formed.
1698  if (tokenizer.tryConsume(":") && !tokenizer.lookingAt("{") && !tokenizer.lookingAt("<")) {
1699  skipFieldValue(tokenizer);
1700  } else {
1701  skipFieldMessage(tokenizer);
1702  }
1703  return;
1704  }
1705 
1706  // Handle potential ':'.
1707  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
1708  tokenizer.tryConsume(":"); // optional
1709  if (parseTreeBuilder != null) {
1710  TextFormatParseInfoTree.Builder childParseTreeBuilder =
1711  parseTreeBuilder.getBuilderForSubMessageField(field);
1713  tokenizer,
1714  extensionRegistry,
1715  target,
1716  field,
1717  extension,
1718  childParseTreeBuilder,
1719  unknownFields);
1720  } else {
1722  tokenizer,
1723  extensionRegistry,
1724  target,
1725  field,
1726  extension,
1727  parseTreeBuilder,
1728  unknownFields);
1729  }
1730  } else {
1731  tokenizer.consume(":"); // required
1733  tokenizer,
1734  extensionRegistry,
1735  target,
1736  field,
1737  extension,
1738  parseTreeBuilder,
1739  unknownFields);
1740  }
1741 
1742  if (parseTreeBuilder != null) {
1743  parseTreeBuilder.setLocation(field, TextFormatParseLocation.create(startLine, startColumn));
1744  }
1745 
1746  // For historical reasons, fields may optionally be separated by commas or
1747  // semicolons.
1748  if (!tokenizer.tryConsume(";")) {
1749  tokenizer.tryConsume(",");
1750  }
1751  }
1752 
1756  private void consumeFieldValues(
1757  final Tokenizer tokenizer,
1758  final ExtensionRegistry extensionRegistry,
1759  final MessageReflection.MergeTarget target,
1760  final FieldDescriptor field,
1762  final TextFormatParseInfoTree.Builder parseTreeBuilder,
1763  List<UnknownField> unknownFields)
1764  throws ParseException {
1765  // Support specifying repeated field values as a comma-separated list.
1766  // Ex."foo: [1, 2, 3]"
1767  if (field.isRepeated() && tokenizer.tryConsume("[")) {
1768  if (!tokenizer.tryConsume("]")) { // Allow "foo: []" to be treated as empty.
1769  while (true) {
1771  tokenizer,
1772  extensionRegistry,
1773  target,
1774  field,
1775  extension,
1776  parseTreeBuilder,
1777  unknownFields);
1778  if (tokenizer.tryConsume("]")) {
1779  // End of list.
1780  break;
1781  }
1782  tokenizer.consume(",");
1783  }
1784  }
1785  } else {
1787  tokenizer,
1788  extensionRegistry,
1789  target,
1790  field,
1791  extension,
1792  parseTreeBuilder,
1793  unknownFields);
1794  }
1795  }
1796 
1798  private void consumeFieldValue(
1799  final Tokenizer tokenizer,
1800  final ExtensionRegistry extensionRegistry,
1801  final MessageReflection.MergeTarget target,
1802  final FieldDescriptor field,
1804  final TextFormatParseInfoTree.Builder parseTreeBuilder,
1805  List<UnknownField> unknownFields)
1806  throws ParseException {
1808  && !field.isRepeated()) {
1809  if (target.hasField(field)) {
1810  throw tokenizer.parseExceptionPreviousToken(
1811  "Non-repeated field \"" + field.getFullName() + "\" cannot be overwritten.");
1812  } else if (field.getContainingOneof() != null
1813  && target.hasOneof(field.getContainingOneof())) {
1814  Descriptors.OneofDescriptor oneof = field.getContainingOneof();
1815  throw tokenizer.parseExceptionPreviousToken(
1816  "Field \""
1817  + field.getFullName()
1818  + "\" is specified along with field \""
1819  + target.getOneofFieldDescriptor(oneof).getFullName()
1820  + "\", another member of oneof \""
1821  + oneof.getName()
1822  + "\".");
1823  }
1824  }
1825 
1826  Object value = null;
1827 
1828  if (field.getJavaType() == FieldDescriptor.JavaType.MESSAGE) {
1829  final String endToken;
1830  if (tokenizer.tryConsume("<")) {
1831  endToken = ">";
1832  } else {
1833  tokenizer.consume("{");
1834  endToken = "}";
1835  }
1836 
1837  Message defaultInstance = (extension == null) ? null : extension.defaultInstance;
1838  MessageReflection.MergeTarget subField =
1839  target.newMergeTargetForField(field, defaultInstance);
1840 
1841  while (!tokenizer.tryConsume(endToken)) {
1842  if (tokenizer.atEnd()) {
1843  throw tokenizer.parseException("Expected \"" + endToken + "\".");
1844  }
1845  mergeField(tokenizer, extensionRegistry, subField, parseTreeBuilder, unknownFields);
1846  }
1847 
1848  value = subField.finish();
1849  } else {
1850  switch (field.getType()) {
1851  case INT32:
1852  case SINT32:
1853  case SFIXED32:
1854  value = tokenizer.consumeInt32();
1855  break;
1856 
1857  case INT64:
1858  case SINT64:
1859  case SFIXED64:
1860  value = tokenizer.consumeInt64();
1861  break;
1862 
1863  case UINT32:
1864  case FIXED32:
1865  value = tokenizer.consumeUInt32();
1866  break;
1867 
1868  case UINT64:
1869  case FIXED64:
1870  value = tokenizer.consumeUInt64();
1871  break;
1872 
1873  case FLOAT:
1874  value = tokenizer.consumeFloat();
1875  break;
1876 
1877  case DOUBLE:
1878  value = tokenizer.consumeDouble();
1879  break;
1880 
1881  case BOOL:
1882  value = tokenizer.consumeBoolean();
1883  break;
1884 
1885  case STRING:
1886  value = tokenizer.consumeString();
1887  break;
1888 
1889  case BYTES:
1890  value = tokenizer.consumeByteString();
1891  break;
1892 
1893  case ENUM:
1894  final EnumDescriptor enumType = field.getEnumType();
1895 
1896  if (tokenizer.lookingAtInteger()) {
1897  final int number = tokenizer.consumeInt32();
1898  value = enumType.findValueByNumber(number);
1899  if (value == null) {
1900  String unknownValueMsg =
1901  "Enum type \""
1902  + enumType.getFullName()
1903  + "\" has no value with number "
1904  + number
1905  + '.';
1906  if (allowUnknownEnumValues) {
1907  logger.warning(unknownValueMsg);
1908  return;
1909  } else {
1910  throw tokenizer.parseExceptionPreviousToken(
1911  "Enum type \""
1912  + enumType.getFullName()
1913  + "\" has no value with number "
1914  + number
1915  + '.');
1916  }
1917  }
1918  } else {
1919  final String id = tokenizer.consumeIdentifier();
1920  value = enumType.findValueByName(id);
1921  if (value == null) {
1922  String unknownValueMsg =
1923  "Enum type \""
1924  + enumType.getFullName()
1925  + "\" has no value named \""
1926  + id
1927  + "\".";
1928  if (allowUnknownEnumValues) {
1929  logger.warning(unknownValueMsg);
1930  return;
1931  } else {
1932  throw tokenizer.parseExceptionPreviousToken(unknownValueMsg);
1933  }
1934  }
1935  }
1936 
1937  break;
1938 
1939  case MESSAGE:
1940  case GROUP:
1941  throw new RuntimeException("Can't get here.");
1942  }
1943  }
1944 
1945  if (field.isRepeated()) {
1946  // TODO(b/29122459): If field.isMapField() and FORBID_SINGULAR_OVERWRITES mode,
1947  // check for duplicate map keys here.
1948  target.addRepeatedField(field, value);
1949  } else {
1950  target.setField(field, value);
1951  }
1952  }
1953 
1954 
1956  private void skipField(Tokenizer tokenizer) throws ParseException {
1957  if (tokenizer.tryConsume("[")) {
1958  // Extension name.
1959  do {
1960  tokenizer.consumeIdentifier();
1961  } while (tokenizer.tryConsume("."));
1962  tokenizer.consume("]");
1963  } else {
1964  tokenizer.consumeIdentifier();
1965  }
1966 
1967  // Try to guess the type of this field.
1968  // If this field is not a message, there should be a ":" between the
1969  // field name and the field value and also the field value should not
1970  // start with "{" or "<" which indicates the beginning of a message body.
1971  // If there is no ":" or there is a "{" or "<" after ":", this field has
1972  // to be a message or the input is ill-formed.
1973  if (tokenizer.tryConsume(":") && !tokenizer.lookingAt("<") && !tokenizer.lookingAt("{")) {
1974  skipFieldValue(tokenizer);
1975  } else {
1976  skipFieldMessage(tokenizer);
1977  }
1978  // For historical reasons, fields may optionally be separated by commas or
1979  // semicolons.
1980  if (!tokenizer.tryConsume(";")) {
1981  tokenizer.tryConsume(",");
1982  }
1983  }
1984 
1988  private void skipFieldMessage(Tokenizer tokenizer) throws ParseException {
1989  final String delimiter;
1990  if (tokenizer.tryConsume("<")) {
1991  delimiter = ">";
1992  } else {
1993  tokenizer.consume("{");
1994  delimiter = "}";
1995  }
1996  while (!tokenizer.lookingAt(">") && !tokenizer.lookingAt("}")) {
1997  skipField(tokenizer);
1998  }
1999  tokenizer.consume(delimiter);
2000  }
2001 
2003  private void skipFieldValue(Tokenizer tokenizer) throws ParseException {
2004  if (tokenizer.tryConsumeString()) {
2005  while (tokenizer.tryConsumeString()) {}
2006  return;
2007  }
2008  if (!tokenizer.tryConsumeIdentifier() // includes enum & boolean
2009  && !tokenizer.tryConsumeInt64() // includes int32
2010  && !tokenizer.tryConsumeUInt64() // includes uint32
2011  && !tokenizer.tryConsumeDouble()
2012  && !tokenizer.tryConsumeFloat()) {
2013  throw tokenizer.parseException("Invalid field value: " + tokenizer.currentToken);
2014  }
2015  }
2016  }
2017 
2018  // =================================================================
2019  // Utility functions
2020  //
2021  // Some of these methods are package-private because Descriptors.java uses
2022  // them.
2023 
2030  public static String escapeBytes(ByteString input) {
2031  return TextFormatEscaper.escapeBytes(input);
2032  }
2033 
2035  public static String escapeBytes(byte[] input) {
2036  return TextFormatEscaper.escapeBytes(input);
2037  }
2038 
2043  public static ByteString unescapeBytes(final CharSequence charString)
2045  // First convert the Java character sequence to UTF-8 bytes.
2046  ByteString input = ByteString.copyFromUtf8(charString.toString());
2047  // Then unescape certain byte sequences introduced by ASCII '\\'. The valid
2048  // escapes can all be expressed with ASCII characters, so it is safe to
2049  // operate on bytes here.
2050  //
2051  // Unescaping the input byte array will result in a byte sequence that's no
2052  // longer than the input. That's because each escape sequence is between
2053  // two and four bytes long and stands for a single byte.
2054  final byte[] result = new byte[input.size()];
2055  int pos = 0;
2056  for (int i = 0; i < input.size(); i++) {
2057  byte c = input.byteAt(i);
2058  if (c == '\\') {
2059  if (i + 1 < input.size()) {
2060  ++i;
2061  c = input.byteAt(i);
2062  if (isOctal(c)) {
2063  // Octal escape.
2064  int code = digitValue(c);
2065  if (i + 1 < input.size() && isOctal(input.byteAt(i + 1))) {
2066  ++i;
2067  code = code * 8 + digitValue(input.byteAt(i));
2068  }
2069  if (i + 1 < input.size() && isOctal(input.byteAt(i + 1))) {
2070  ++i;
2071  code = code * 8 + digitValue(input.byteAt(i));
2072  }
2073  // TODO: Check that 0 <= code && code <= 0xFF.
2074  result[pos++] = (byte) code;
2075  } else {
2076  switch (c) {
2077  case 'a':
2078  result[pos++] = 0x07;
2079  break;
2080  case 'b':
2081  result[pos++] = '\b';
2082  break;
2083  case 'f':
2084  result[pos++] = '\f';
2085  break;
2086  case 'n':
2087  result[pos++] = '\n';
2088  break;
2089  case 'r':
2090  result[pos++] = '\r';
2091  break;
2092  case 't':
2093  result[pos++] = '\t';
2094  break;
2095  case 'v':
2096  result[pos++] = 0x0b;
2097  break;
2098  case '\\':
2099  result[pos++] = '\\';
2100  break;
2101  case '\'':
2102  result[pos++] = '\'';
2103  break;
2104  case '"':
2105  result[pos++] = '\"';
2106  break;
2107 
2108  case 'x':
2109  // hex escape
2110  int code = 0;
2111  if (i + 1 < input.size() && isHex(input.byteAt(i + 1))) {
2112  ++i;
2113  code = digitValue(input.byteAt(i));
2114  } else {
2116  "Invalid escape sequence: '\\x' with no digits");
2117  }
2118  if (i + 1 < input.size() && isHex(input.byteAt(i + 1))) {
2119  ++i;
2120  code = code * 16 + digitValue(input.byteAt(i));
2121  }
2122  result[pos++] = (byte) code;
2123  break;
2124 
2125  default:
2127  "Invalid escape sequence: '\\" + (char) c + '\'');
2128  }
2129  }
2130  } else {
2132  "Invalid escape sequence: '\\' at end of string.");
2133  }
2134  } else {
2135  result[pos++] = c;
2136  }
2137  }
2138 
2139  return result.length == pos
2140  ? ByteString.wrap(result) // This reference has not been out of our control.
2141  : ByteString.copyFrom(result, 0, pos);
2142  }
2143 
2148  public static class InvalidEscapeSequenceException extends IOException {
2149  private static final long serialVersionUID = -8164033650142593304L;
2150 
2152  super(description);
2153  }
2154  }
2155 
2161  static String escapeText(final String input) {
2162  return escapeBytes(ByteString.copyFromUtf8(input));
2163  }
2164 
2166  public static String escapeDoubleQuotesAndBackslashes(final String input) {
2167  return TextFormatEscaper.escapeDoubleQuotesAndBackslashes(input);
2168  }
2169 
2174  static String unescapeText(final String input) throws InvalidEscapeSequenceException {
2175  return unescapeBytes(input).toStringUtf8();
2176  }
2177 
2179  private static boolean isOctal(final byte c) {
2180  return '0' <= c && c <= '7';
2181  }
2182 
2184  private static boolean isHex(final byte c) {
2185  return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
2186  }
2187 
2192  private static int digitValue(final byte c) {
2193  if ('0' <= c && c <= '9') {
2194  return c - '0';
2195  } else if ('a' <= c && c <= 'z') {
2196  return c - 'a' + 10;
2197  } else {
2198  return c - 'A' + 10;
2199  }
2200  }
2201 
2207  static int parseInt32(final String text) throws NumberFormatException {
2208  return (int) parseInteger(text, true, false);
2209  }
2210 
2217  static int parseUInt32(final String text) throws NumberFormatException {
2218  return (int) parseInteger(text, false, false);
2219  }
2220 
2226  static long parseInt64(final String text) throws NumberFormatException {
2227  return parseInteger(text, true, true);
2228  }
2229 
2236  static long parseUInt64(final String text) throws NumberFormatException {
2237  return parseInteger(text, false, true);
2238  }
2239 
2240  private static long parseInteger(final String text, final boolean isSigned, final boolean isLong)
2241  throws NumberFormatException {
2242  int pos = 0;
2243 
2244  boolean negative = false;
2245  if (text.startsWith("-", pos)) {
2246  if (!isSigned) {
2247  throw new NumberFormatException("Number must be positive: " + text);
2248  }
2249  ++pos;
2250  negative = true;
2251  }
2252 
2253  int radix = 10;
2254  if (text.startsWith("0x", pos)) {
2255  pos += 2;
2256  radix = 16;
2257  } else if (text.startsWith("0", pos)) {
2258  radix = 8;
2259  }
2260 
2261  final String numberText = text.substring(pos);
2262 
2263  long result = 0;
2264  if (numberText.length() < 16) {
2265  // Can safely assume no overflow.
2266  result = Long.parseLong(numberText, radix);
2267  if (negative) {
2268  result = -result;
2269  }
2270 
2271  // Check bounds.
2272  // No need to check for 64-bit numbers since they'd have to be 16 chars
2273  // or longer to overflow.
2274  if (!isLong) {
2275  if (isSigned) {
2276  if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
2277  throw new NumberFormatException(
2278  "Number out of range for 32-bit signed integer: " + text);
2279  }
2280  } else {
2281  if (result >= (1L << 32) || result < 0) {
2282  throw new NumberFormatException(
2283  "Number out of range for 32-bit unsigned integer: " + text);
2284  }
2285  }
2286  }
2287  } else {
2288  BigInteger bigValue = new BigInteger(numberText, radix);
2289  if (negative) {
2290  bigValue = bigValue.negate();
2291  }
2292 
2293  // Check bounds.
2294  if (!isLong) {
2295  if (isSigned) {
2296  if (bigValue.bitLength() > 31) {
2297  throw new NumberFormatException(
2298  "Number out of range for 32-bit signed integer: " + text);
2299  }
2300  } else {
2301  if (bigValue.bitLength() > 32) {
2302  throw new NumberFormatException(
2303  "Number out of range for 32-bit unsigned integer: " + text);
2304  }
2305  }
2306  } else {
2307  if (isSigned) {
2308  if (bigValue.bitLength() > 63) {
2309  throw new NumberFormatException(
2310  "Number out of range for 64-bit signed integer: " + text);
2311  }
2312  } else {
2313  if (bigValue.bitLength() > 64) {
2314  throw new NumberFormatException(
2315  "Number out of range for 64-bit unsigned integer: " + text);
2316  }
2317  }
2318  }
2319 
2320  result = bigValue.longValue();
2321  }
2322 
2323  return result;
2324  }
2325 }
com.google.protobuf.TextFormat.Parser.BUFFER_SIZE
static final int BUFFER_SIZE
Definition: TextFormat.java:1499
com.google.protobuf.Descriptors
Definition: Descriptors.java:80
Map
struct Map Map
Definition: php/ext/google/protobuf/protobuf.h:648
com.google.protobuf.WireFormat.WIRETYPE_VARINT
static final int WIRETYPE_VARINT
Definition: WireFormat.java:55
com.google.protobuf.TextFormat.Parser.merge
void merge(final CharSequence input, final Message.Builder builder)
Definition: TextFormat.java:1473
com.google.protobuf.TextFormat.Tokenizer.parseException
ParseException parseException(final String description)
Definition: TextFormat.java:1161
com.google.protobuf.TextFormat.Tokenizer.consume
void consume(final String token)
Definition: TextFormat.java:864
com.google.protobuf.Descriptors.FieldDescriptor.Type.GROUP
GROUP
Definition: Descriptors.java:1225
com.google.protobuf.TextFormat.Parser.Builder.build
Parser build()
Definition: TextFormat.java:1453
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
com.google.protobuf.TextFormat.Tokenizer.tryConsume
boolean tryConsume(final String token)
Definition: TextFormat.java:851
com.google.protobuf.TextFormat.Parser.toStringBuilder
static StringBuilder toStringBuilder(final Readable input)
Definition: TextFormat.java:1503
com.google.protobuf.TextFormat.ParseException.getColumn
int getColumn()
Definition: TextFormat.java:1239
com.google.protobuf.TextFormat.printField
static void printField(final FieldDescriptor field, final Object value, final Appendable output)
Definition: TextFormat.java:181
com.google.protobuf.TextFormat.TextGenerator.eol
void eol()
Definition: TextFormat.java:706
com.google.protobuf.TextFormat.Tokenizer.FLOAT_INFINITY
static final Pattern FLOAT_INFINITY
Definition: TextFormat.java:771
com.google.protobuf.TextFormat.shortDebugString
static String shortDebugString(final MessageOrBuilder message)
Definition: TextFormat.java:111
com.google.protobuf.TextFormat.Printer.print
void print(final MessageOrBuilder message, final TextGenerator generator)
Definition: TextFormat.java:324
com.google.protobuf.TextFormat.UnknownFieldParseException.getUnknownField
String getUnknownField()
Definition: TextFormat.java:1272
com.google.protobuf.TextFormat.printToUnicodeString
static String printToUnicodeString(final MessageOrBuilder message)
Definition: TextFormat.java:164
google::protobuf::extension
const Descriptor::ReservedRange const EnumValueDescriptor const MethodDescriptor extension
Definition: src/google/protobuf/descriptor.h:2001
com.google.protobuf.TextFormat.unsignedToString
static String unsignedToString(final int value)
Definition: TextFormat.java:629
com.google.protobuf.TextFormat.Parser.mergeField
void mergeField(final Tokenizer tokenizer, final ExtensionRegistry extensionRegistry, final MessageReflection.MergeTarget target, List< UnknownField > unknownFields)
Definition: TextFormat.java:1592
com.google.protobuf.TextFormat.Tokenizer.integerParseException
ParseException integerParseException(final NumberFormatException e)
Definition: TextFormat.java:1179
com.google.protobuf.TextFormat.printToString
static String printToString(final MessageOrBuilder message)
Definition: TextFormat.java:143
com.google.protobuf.TextFormat.Tokenizer.previousLine
int previousLine
Definition: TextFormat.java:755
length
GLenum GLuint GLenum GLsizei length
Definition: glcorearb.h:2695
input
std::string input
Definition: tokenizer_unittest.cc:197
com.google.protobuf.TextFormat.multiLineOutput
static TextGenerator multiLineOutput(Appendable output)
Definition: TextFormat.java:648
com.google.protobuf.TextFormat.Tokenizer.nextToken
void nextToken()
Definition: TextFormat.java:805
com.google.protobuf.TextFormat.Tokenizer.previousColumn
int previousColumn
Definition: TextFormat.java:756
com.google.protobuf.ExtensionRegistry.ExtensionInfo
Definition: ExtensionRegistry.java:110
com.google.protobuf.TextFormat.Tokenizer.consumeByteString
ByteString consumeByteString()
Definition: TextFormat.java:1123
com.google.protobuf.TextFormat.Tokenizer.text
final CharSequence text
Definition: TextFormat.java:742
com.google.protobuf.TextFormat.Tokenizer.pos
int pos
Definition: TextFormat.java:747
com.google.protobuf.TextFormat.digitValue
static int digitValue(final byte c)
Definition: TextFormat.java:2192
com.google.protobuf.TextFormat.Tokenizer.consumeFloat
float consumeFloat()
Definition: TextFormat.java:1045
com.google.protobuf.TextFormat.Tokenizer.column
int column
Definition: TextFormat.java:751
com.google.protobuf.WireFormat.WIRETYPE_FIXED64
static final int WIRETYPE_FIXED64
Definition: WireFormat.java:56
com.google.protobuf.TextFormat.Printer.escapingNonAscii
Printer escapingNonAscii(boolean escapeNonAscii)
Definition: TextFormat.java:306
com.google.protobuf.TextFormat.Printer.shortDebugString
String shortDebugString(final FieldDescriptor field, final Object value)
Definition: TextFormat.java:479
com.google.protobuf.TextFormat.shortDebugString
static String shortDebugString(final UnknownFieldSet fields)
Definition: TextFormat.java:133
getName
ROSCONSOLE_CONSOLE_IMPL_DECL std::string getName(void *handle)
com.google.protobuf.TextFormat.Tokenizer.lookingAtInteger
boolean lookingAtInteger()
Definition: TextFormat.java:871
com.google.protobuf.TextFormat.Tokenizer.TOKEN
static final Pattern TOKEN
Definition: TextFormat.java:761
com.google.protobuf.TextFormat.escapeBytes
static String escapeBytes(byte[] input)
Definition: TextFormat.java:2035
setup.description
description
Definition: compatibility_tests/v2.5.0/setup.py:61
com.google.protobuf.TextFormat.Tokenizer.consumeBoolean
boolean consumeBoolean()
Definition: TextFormat.java:1083
com.google.protobuf.TextFormat.Parser.consumeFieldValues
void consumeFieldValues(final Tokenizer tokenizer, final ExtensionRegistry extensionRegistry, final MessageReflection.MergeTarget target, final FieldDescriptor field, final ExtensionRegistry.ExtensionInfo extension, final TextFormatParseInfoTree.Builder parseTreeBuilder, List< UnknownField > unknownFields)
Definition: TextFormat.java:1756
com.google.protobuf.TextFormat.Printer.printFieldValue
void printFieldValue(final FieldDescriptor field, final Object value, final Appendable output)
Definition: TextFormat.java:366
com.google.protobuf.TextFormat.Parser.Builder.setAllowUnknownExtensions
Builder setAllowUnknownExtensions(boolean allowUnknownExtensions)
Definition: TextFormat.java:1437
com.google.protobuf.TextFormat.merge
static void merge(final CharSequence input, final Message.Builder builder)
Definition: TextFormat.java:1293
com.google.protobuf.Descriptors.FieldDescriptor.Type.MESSAGE
MESSAGE
Definition: Descriptors.java:1226
com.google.protobuf.TextFormat.TextGenerator.indent
final StringBuilder indent
Definition: TextFormat.java:659
com.google.protobuf.TextFormat.escapeDoubleQuotesAndBackslashes
static String escapeDoubleQuotesAndBackslashes(final String input)
Definition: TextFormat.java:2166
com.google.protobuf.TextFormat.Tokenizer.atEnd
boolean atEnd()
Definition: TextFormat.java:800
com.google.protobuf.TextFormat.Parser.SingularOverwritePolicy.FORBID_SINGULAR_OVERWRITES
FORBID_SINGULAR_OVERWRITES
Definition: TextFormat.java:1382
com.google.protobuf.TextFormat.Tokenizer.currentToken
String currentToken
Definition: TextFormat.java:744
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.Descriptors.EnumDescriptor.findValueByName
EnumValueDescriptor findValueByName(final String name)
Definition: Descriptors.java:1643
com.google.protobuf.TextFormat.Tokenizer.consumeString
String consumeString()
Definition: TextFormat.java:1105
target
GLenum target
Definition: glcorearb.h:3739
com.google.protobuf.TextFormat.shortDebugString
static String shortDebugString(final FieldDescriptor field, final Object value)
Definition: TextFormat.java:122
com.google.protobuf.TextFormat.Parser.merge
void merge(final Readable input, final Message.Builder builder)
Definition: TextFormat.java:1466
com.google.protobuf.WireFormat
Definition: WireFormat.java:45
com.google.protobuf.Descriptors.FieldDescriptor.Type
Definition: Descriptors.java:1215
com.google.protobuf.TextFormat.TextFormat
TextFormat()
Definition: TextFormat.java:56
T
#define T(upbtypeconst, upbtype, ctype, default_value)
com.google.protobuf.TextFormat.Parser.skipFieldValue
void skipFieldValue(Tokenizer tokenizer)
Definition: TextFormat.java:2003
com.google.protobuf.TextFormat.ParseException.ParseException
ParseException(final String message)
Definition: TextFormat.java:1211
com.google.protobuf.TextFormat.TextGenerator.print
void print(final CharSequence text)
Definition: TextFormat.java:693
com.google.protobuf.TextFormat.parse
static< T extends Message > T parse(final CharSequence input, final ExtensionRegistry extensionRegistry, final Class< T > protoClass)
Definition: TextFormat.java:1343
com.google.protobuf.TextFormat.Parser.Builder.allowUnknownEnumValues
boolean allowUnknownEnumValues
Definition: TextFormat.java:1412
com.google.protobuf.TextFormat.UnknownFieldParseException.UnknownFieldParseException
UnknownFieldParseException(final String message)
Definition: TextFormat.java:1252
com.google.protobuf.TextFormat.Parser.SingularOverwritePolicy.ALLOW_SINGULAR_OVERWRITES
ALLOW_SINGULAR_OVERWRITES
Definition: TextFormat.java:1380
com.google.protobuf.TextFormat.Parser.singularOverwritePolicy
final SingularOverwritePolicy singularOverwritePolicy
Definition: TextFormat.java:1388
testing::internal::Double
FloatingPoint< double > Double
Definition: gtest-internal.h:429
com.google.protobuf.TextFormat.TextGenerator.TextGenerator
TextGenerator(final Appendable output, boolean singleLineMode)
Definition: TextFormat.java:666
com.google.protobuf.TextFormat.Printer.print
void print(final MessageOrBuilder message, final Appendable output)
Definition: TextFormat.java:315
com.google.protobuf.TextFormat.ParseException
Definition: TextFormat.java:1204
values
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:3591
com.google.protobuf.TextFormat.Printer.printField
void printField(final FieldDescriptor field, final Object value, final TextGenerator generator)
Definition: TextFormat.java:344
com.google.protobuf.TextFormat.Tokenizer.consumeDouble
double consumeDouble()
Definition: TextFormat.java:1007
com.google.protobuf.TextFormat.Printer.printUnknownFieldValue
static void printUnknownFieldValue(final int tag, final Object value, final TextGenerator generator)
Definition: TextFormat.java:503
com.google.protobuf.TextFormat.TextGenerator.singleLineMode
final boolean singleLineMode
Definition: TextFormat.java:660
com.google.protobuf.TextFormat.Parser.newBuilder
static Builder newBuilder()
Definition: TextFormat.java:1405
com.google.protobuf.TextFormat.singleLineOutput
static TextGenerator singleLineOutput(Appendable output)
Definition: TextFormat.java:652
com.google.protobuf.TextFormat.Printer.print
void print(final UnknownFieldSet fields, final Appendable output)
Definition: TextFormat.java:320
com.google.protobuf.TextFormat.merge
static void merge(final CharSequence input, final ExtensionRegistry extensionRegistry, final Message.Builder builder)
Definition: TextFormat.java:1329
com.google.protobuf.TextFormat.Printer.printUnknownFields
static void printUnknownFields(final UnknownFieldSet unknownFields, final TextGenerator generator)
Definition: TextFormat.java:590
com.google.protobuf.TextFormat.Parser.Builder.singularOverwritePolicy
SingularOverwritePolicy singularOverwritePolicy
Definition: TextFormat.java:1414
com.google.protobuf.TextFormat.printUnicode
static void printUnicode(final MessageOrBuilder message, final Appendable output)
Definition: TextFormat.java:90
com.google.protobuf.Descriptors.OneofDescriptor
Definition: Descriptors.java:2597
com.google.protobuf.Message.Builder
Definition: Message.java:104
com.google.protobuf.TextFormat.Printer.Printer
Printer(boolean escapeNonAscii)
Definition: TextFormat.java:293
com.google.protobuf.TextFormat.print
static void print(final UnknownFieldSet fields, final Appendable output)
Definition: TextFormat.java:79
com.google.protobuf.TextFormat.printUnknownFieldValue
static void printUnknownFieldValue(final int tag, final Object value, final Appendable output)
Definition: TextFormat.java:237
com.google.protobuf.UnknownFieldSet
Definition: UnknownFieldSet.java:58
com.google.protobuf.TextFormat.Printer.printMessage
void printMessage(final MessageOrBuilder message, final TextGenerator generator)
Definition: TextFormat.java:540
testing::internal::Float
FloatingPoint< float > Float
Definition: gtest-internal.h:428
com.google.protobuf.TextFormat.Parser.mergeField
void mergeField(final Tokenizer tokenizer, final ExtensionRegistry extensionRegistry, final MessageReflection.MergeTarget target, TextFormatParseInfoTree.Builder parseTreeBuilder, List< UnknownField > unknownFields)
Definition: TextFormat.java:1607
com.google.protobuf.TextFormat.Printer
Definition: TextFormat.java:285
com.google.protobuf.TextFormat.Tokenizer.parseExceptionPreviousToken
ParseException parseExceptionPreviousToken(final String description)
Definition: TextFormat.java:1170
com.google.protobuf.TextFormat.TextGenerator
Definition: TextFormat.java:657
com.google.protobuf.TextFormat.printToString
static String printToString(final UnknownFieldSet fields)
Definition: TextFormat.java:153
com.google.protobuf.TextFormat.Tokenizer.DOUBLE_INFINITY
static final Pattern DOUBLE_INFINITY
Definition: TextFormat.java:769
com.google.protobuf.TextFormat.Tokenizer.tryConsumeString
boolean tryConsumeString()
Definition: TextFormat.java:1110
com.google.protobuf.TextFormat.Parser.consumeFieldValue
void consumeFieldValue(final Tokenizer tokenizer, final ExtensionRegistry extensionRegistry, final MessageReflection.MergeTarget target, final FieldDescriptor field, final ExtensionRegistry.ExtensionInfo extension, final TextFormatParseInfoTree.Builder parseTreeBuilder, List< UnknownField > unknownFields)
Definition: TextFormat.java:1798
com.google.protobuf.TextFormat.Tokenizer.consumeUInt64
long consumeUInt64()
Definition: TextFormat.java:980
com.google.protobuf.TextFormat.Printer.DEFAULT
static final Printer DEFAULT
Definition: TextFormat.java:288
com.google.protobuf.Descriptors.FieldDescriptor.JavaType
Definition: Descriptors.java:1262
com.google.protobuf.TextFormat.Parser.Parser
Parser(boolean allowUnknownFields, boolean allowUnknownEnumValues, boolean allowUnknownExtensions, SingularOverwritePolicy singularOverwritePolicy, TextFormatParseInfoTree.Builder parseInfoTreeBuilder)
Definition: TextFormat.java:1391
p
const char * p
Definition: gmock-matchers_test.cc:3863
com.google.protobuf.Descriptors.EnumDescriptor.findValueByNumber
EnumValueDescriptor findValueByNumber(final int number)
Definition: Descriptors.java:1660
com.google.protobuf.TextFormat.PARSER
static final Parser PARSER
Definition: TextFormat.java:1277
com.google.protobuf.TextFormat.print
static void print(final MessageOrBuilder message, final Appendable output)
Definition: TextFormat.java:68
com.google.protobuf.TextFormat.Tokenizer.consumeInt64
long consumeInt64()
Definition: TextFormat.java:953
com.google.protobuf.MessageOrBuilder
Definition: MessageOrBuilder.java:42
com.google.protobuf.TextFormat.printUnknownFieldValue
static void printUnknownFieldValue(final int tag, final Object value, final TextGenerator generator)
Definition: TextFormat.java:242
com.google.protobuf.Descriptors.EnumDescriptor
Definition: Descriptors.java:1583
com.google.protobuf.TextFormat.Parser.allowUnknownEnumValues
final boolean allowUnknownEnumValues
Definition: TextFormat.java:1386
com.google.protobuf.TextFormat.merge
static void merge(final Readable input, final ExtensionRegistry extensionRegistry, final Message.Builder builder)
Definition: TextFormat.java:1316
com.google.protobuf.TextFormat.printUnicodeFieldValue
static void printUnicodeFieldValue(final FieldDescriptor field, final Object value, final Appendable output)
Definition: TextFormat.java:207
com.google.protobuf.TextFormat.Parser.allowUnknownExtensions
final boolean allowUnknownExtensions
Definition: TextFormat.java:1387
com.google.protobuf.TextFormat.Parser.checkUnknownFields
void checkUnknownFields(final List< UnknownField > unknownFields)
Definition: TextFormat.java:1533
com.google.protobuf.TextFormat.Parser.skipField
void skipField(Tokenizer tokenizer)
Definition: TextFormat.java:1956
com.google.protobuf.TextFormat.printToUnicodeString
static String printToUnicodeString(final UnknownFieldSet fields)
Definition: TextFormat.java:175
com.google.protobuf.TextFormatParseLocation
Definition: TextFormatParseLocation.java:40
buffer
Definition: buffer_processor.h:43
byte
SETUP_TEARDOWN_TESTCONTEXT typedef uint8_t byte
Definition: test_stream.cpp:12
com.google.protobuf.TextFormat.Tokenizer.FLOAT_NAN
static final Pattern FLOAT_NAN
Definition: TextFormat.java:773
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
com.google.protobuf.TextFormat.escapeBytes
static String escapeBytes(ByteString input)
Definition: TextFormat.java:2030
com.google.protobuf.UnknownFieldSet.Field
Definition: UnknownFieldSet.java:712
com.google.protobuf.ExtensionRegistry.getEmptyRegistry
static ExtensionRegistry getEmptyRegistry()
Definition: ExtensionRegistry.java:98
com.google.protobuf.TextFormat.Printer.printFieldValue
void printFieldValue(final FieldDescriptor field, final Object value, final TextGenerator generator)
Definition: TextFormat.java:372
com.google.protobuf.TextFormat.TextGenerator.output
final Appendable output
Definition: TextFormat.java:658
com.google.protobuf.TextFormat.Parser.Builder.setSingularOverwritePolicy
Builder setSingularOverwritePolicy(SingularOverwritePolicy p)
Definition: TextFormat.java:1443
com.google.protobuf.TextFormat.printFieldToString
static String printFieldToString(final FieldDescriptor field, final Object value)
Definition: TextFormat.java:188
com.google.protobuf.WireFormat.WIRETYPE_FIXED32
static final int WIRETYPE_FIXED32
Definition: WireFormat.java:60
com.google.protobuf.TextFormat.Tokenizer.lookingAt
boolean lookingAt(String text)
Definition: TextFormat.java:881
n
GLdouble n
Definition: glcorearb.h:4153
com.google.protobuf.TextFormat.Tokenizer.tryConsumeFloat
boolean tryConsumeFloat()
Definition: TextFormat.java:1070
com.google.protobuf.WireFormat.WIRETYPE_START_GROUP
static final int WIRETYPE_START_GROUP
Definition: WireFormat.java:58
com.google.protobuf.TextFormat.Tokenizer.skipWhitespace
void skipWhitespace()
Definition: TextFormat.java:840
i
int i
Definition: gmock-matchers_test.cc:764
com.google.protobuf.TextFormat.parseInteger
static long parseInteger(final String text, final boolean isSigned, final boolean isLong)
Definition: TextFormat.java:2240
com.google.protobuf.TextFormat.ParseException.serialVersionUID
static final long serialVersionUID
Definition: TextFormat.java:1205
com.google.protobuf.TextFormatParseInfoTree.Builder.getBuilderForSubMessageField
Builder getBuilderForSubMessageField(final FieldDescriptor fieldDescriptor)
Definition: TextFormatParseInfoTree.java:201
com.google.protobuf.TextFormat.Parser.Builder.setAllowUnknownFields
Builder setAllowUnknownFields(boolean allowUnknownFields)
Definition: TextFormat.java:1426
com.google.protobuf.TextFormat.printer
static Printer printer()
Definition: TextFormat.java:280
com.google.protobuf.TextFormat.Parser.Builder
Definition: TextFormat.java:1410
java
com.google.protobuf.TextFormat.Tokenizer.matcher
final Matcher matcher
Definition: TextFormat.java:743
com.google.protobuf.TextFormat.Parser.Builder.setParseInfoTreeBuilder
Builder setParseInfoTreeBuilder(TextFormatParseInfoTree.Builder parseInfoTreeBuilder)
Definition: TextFormat.java:1448
com.google.protobuf.Descriptors.Descriptor
Definition: Descriptors.java:629
fields
static const upb_fielddef fields[107]
Definition: ruby/ext/google/protobuf_c/upb.c:7671
type
GLenum type
Definition: glcorearb.h:2695
com.google.protobuf.TextFormat.unescapeBytes
static ByteString unescapeBytes(final CharSequence charString)
Definition: TextFormat.java:2043
com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED
static final int WIRETYPE_LENGTH_DELIMITED
Definition: WireFormat.java:57
com.google.protobuf.TextFormat.isOctal
static boolean isOctal(final byte c)
Definition: TextFormat.java:2179
com.google.protobuf.TextFormatParseInfoTree
Definition: TextFormatParseInfoTree.java:50
com.google.protobuf.WireFormat.getTagWireType
static int getTagWireType(final int tag)
Definition: WireFormat.java:66
com.google.protobuf.Internal.getDefaultInstance
static< T extends MessageLite > T getDefaultInstance(Class< T > clazz)
Definition: Internal.java:364
com.google.protobuf.TextFormat.Parser.Builder.allowUnknownExtensions
boolean allowUnknownExtensions
Definition: TextFormat.java:1413
com.google.protobuf.TextFormat.Printer.printFieldToString
String printFieldToString(final FieldDescriptor field, final Object value)
Definition: TextFormat.java:329
com.google.protobuf.TextFormat.Tokenizer.unknownFieldParseExceptionPreviousToken
UnknownFieldParseException unknownFieldParseExceptionPreviousToken(final String unknownField, final String description)
Definition: TextFormat.java:1195
com.google.protobuf.TextFormat.Parser.merge
void merge(final CharSequence input, final ExtensionRegistry extensionRegistry, final Message.Builder builder)
Definition: TextFormat.java:1573
com.google.protobuf.TextFormat.Tokenizer.tryConsumeIdentifier
boolean tryConsumeIdentifier()
Definition: TextFormat.java:912
com.google.protobuf.TextFormat.logger
static final Logger logger
Definition: TextFormat.java:58
com.google.protobuf.TextFormat.InvalidEscapeSequenceException.serialVersionUID
static final long serialVersionUID
Definition: TextFormat.java:2149
com.google.protobuf.TextFormat.Tokenizer.line
int line
Definition: TextFormat.java:750
com.google.protobuf.TextFormat.InvalidEscapeSequenceException
Definition: TextFormat.java:2148
com.google.protobuf.TextFormat.Tokenizer.tryConsumeInt64
boolean tryConsumeInt64()
Definition: TextFormat.java:967
com.google.protobuf.TextFormat.isHex
static boolean isHex(final byte c)
Definition: TextFormat.java:2184
com.google
com.google.protobuf.TextFormat.Parser.SingularOverwritePolicy
Definition: TextFormat.java:1375
com.google.protobuf.TextFormat.TextGenerator.atStartOfLine
boolean atStartOfLine
Definition: TextFormat.java:664
com
com.google.protobuf.TextFormat.Tokenizer.Tokenizer
Tokenizer(final CharSequence text)
Definition: TextFormat.java:776
com.google.protobuf.ExtensionRegistry
Definition: ExtensionRegistry.java:91
com.google.protobuf.TextFormat.Printer.printField
void printField(final FieldDescriptor field, final Object value, final Appendable output)
Definition: TextFormat.java:339
com.google.protobuf.TextFormat.Printer.escapeNonAscii
final boolean escapeNonAscii
Definition: TextFormat.java:291
com.google.protobuf.TextFormat.TextGenerator.outdent
void outdent()
Definition: TextFormat.java:681
com.google.protobuf.TextFormat.Tokenizer.consumeUInt32
int consumeUInt32()
Definition: TextFormat.java:939
com.google.protobuf.TextFormat.Tokenizer.tryConsumeUInt64
boolean tryConsumeUInt64()
Definition: TextFormat.java:994
com.google.protobuf.TextFormat.Printer.printSingleField
void printSingleField(final FieldDescriptor field, final Object value, final TextGenerator generator)
Definition: TextFormat.java:548
com.google.protobuf.TextFormat.parse
static< T extends Message > T parse(final CharSequence input, final Class< T > protoClass)
Definition: TextFormat.java:1303
com.google.protobuf.TextFormat.ParseException.line
final int line
Definition: TextFormat.java:1207
com.google.protobuf.TextFormat.Printer.printToString
String printToString(final MessageOrBuilder message)
Definition: TextFormat.java:441
com.google.protobuf.Internal
Definition: Internal.java:54
com.google.protobuf.TextFormat.Printer.printUnknownField
static void printUnknownField(final int number, final int wireType, final List<?> values, final TextGenerator generator)
Definition: TextFormat.java:616
com.google.protobuf.TextFormat.ParseException.getLine
int getLine()
Definition: TextFormat.java:1231
com.google.protobuf.InvalidProtocolBufferException
Definition: InvalidProtocolBufferException.java:41
com.google.protobuf.UnknownFieldSet.parseFrom
static UnknownFieldSet parseFrom(final CodedInputStream input)
Definition: UnknownFieldSet.java:267
com.google.protobuf.TextFormat.Tokenizer.consumeByteString
void consumeByteString(List< ByteString > list)
Definition: TextFormat.java:1137
com.google.protobuf.TextFormat.Tokenizer.floatParseException
ParseException floatParseException(final NumberFormatException e)
Definition: TextFormat.java:1187
com.google.protobuf.TextFormat.Tokenizer
Definition: TextFormat.java:741
com.google.protobuf.TextFormat.Parser.parseInfoTreeBuilder
TextFormatParseInfoTree.Builder parseInfoTreeBuilder
Definition: TextFormat.java:1389
com.google.protobuf.TextFormat.ParseException.ParseException
ParseException(final int line, final int column, final String message)
Definition: TextFormat.java:1221
com.google.protobuf.TextFormat.printFieldValue
static void printFieldValue(final FieldDescriptor field, final Object value, final Appendable output)
Definition: TextFormat.java:223
com.google.protobuf.TextFormat.TextGenerator.indent
void indent()
Definition: TextFormat.java:676
com.google.protobuf.TextFormatParseInfoTree.Builder
Definition: TextFormatParseInfoTree.java:161
com.google.protobuf.TextFormat.Tokenizer.tryConsumeDouble
boolean tryConsumeDouble()
Definition: TextFormat.java:1032
com.google.protobuf.TextFormat.UnknownFieldParseException.unknownField
final String unknownField
Definition: TextFormat.java:1246
com.google.protobuf.Descriptors.EnumValueDescriptor
Definition: Descriptors.java:1774
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
com.google.protobuf.TextFormat.Printer.printToString
String printToString(final UnknownFieldSet fields)
Definition: TextFormat.java:451
com.google.protobuf.TextFormat.Parser.skipFieldMessage
void skipFieldMessage(Tokenizer tokenizer)
Definition: TextFormat.java:1988
Builder
struct Builder Builder
Definition: ruby/ext/google/protobuf_c/protobuf.h:67
com.google.protobuf.TextFormat.printUnicode
static void printUnicode(final UnknownFieldSet fields, final Appendable output)
Definition: TextFormat.java:101
com.google.protobuf.TextFormat.Printer.shortDebugString
String shortDebugString(final UnknownFieldSet fields)
Definition: TextFormat.java:493
com.google.protobuf.TextFormat.unsignedToString
static String unsignedToString(final long value)
Definition: TextFormat.java:638
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
com.google.protobuf.TextFormat.Tokenizer.consumeInt32
int consumeInt32()
Definition: TextFormat.java:925
com.google.protobuf.TextFormat.Tokenizer.consumeIdentifier
String consumeIdentifier()
Definition: TextFormat.java:889
com.google.protobuf.TextFormat.UnknownFieldParseException
Definition: TextFormat.java:1245
com.google.protobuf.TextFormat.Tokenizer.WHITESPACE
static final Pattern WHITESPACE
Definition: TextFormat.java:760
com.google.protobuf.TextFormat.Parser.Builder.allowUnknownFields
boolean allowUnknownFields
Definition: TextFormat.java:1411
com.google.protobuf.TextFormat.Printer.shortDebugString
String shortDebugString(final MessageOrBuilder message)
Definition: TextFormat.java:465
com.google.protobuf.TextFormat.ParseException.column
final int column
Definition: TextFormat.java:1208
com.google.protobuf.Descriptors.EnumDescriptor.getFullName
String getFullName()
Definition: Descriptors.java:1612
com.google.protobuf.TextFormat.Parser.merge
void merge(final Readable input, final ExtensionRegistry extensionRegistry, final Message.Builder builder)
Definition: TextFormat.java:1482
number
double number
Definition: cJSON.h:326
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
com.google.protobuf.Message
Definition: Message.java:50
com.google.protobuf.TextFormat.Parser.Builder.parseInfoTreeBuilder
TextFormatParseInfoTree.Builder parseInfoTreeBuilder
Definition: TextFormat.java:1416
com.google.protobuf.TextFormat.Parser
Definition: TextFormat.java:1363
com.google.protobuf.TextFormat
Definition: TextFormat.java:55
com.google.protobuf.TextFormat.merge
static void merge(final Readable input, final Message.Builder builder)
Definition: TextFormat.java:1288
com.google.protobuf.TextFormat.UnknownFieldParseException.UnknownFieldParseException
UnknownFieldParseException(final int line, final int column, final String unknownField, final String message)
Definition: TextFormat.java:1263
com.google.protobuf.TextFormat.Parser.allowUnknownFields
final boolean allowUnknownFields
Definition: TextFormat.java:1385
com.google.protobuf.Descriptors.FieldDescriptor
Definition: Descriptors.java:949
com.google.protobuf.ByteString
Definition: ByteString.java:67
com.google.protobuf.Descriptors.FieldDescriptor.JavaType.MESSAGE
MESSAGE
Definition: Descriptors.java:1271
com.google.protobuf.TextFormat.getParser
static Parser getParser()
Definition: TextFormat.java:1283


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00