diff options
Diffstat (limited to 'java/src/main')
-rw-r--r-- | java/src/main/java/com/google/protobuf/TextFormat.java | 40 | ||||
-rw-r--r-- | java/src/main/java/com/google/protobuf/UnknownFieldSet.java | 2 |
2 files changed, 38 insertions, 4 deletions
diff --git a/java/src/main/java/com/google/protobuf/TextFormat.java b/java/src/main/java/com/google/protobuf/TextFormat.java index c4fdfe64..2d949b65 100644 --- a/java/src/main/java/com/google/protobuf/TextFormat.java +++ b/java/src/main/java/com/google/protobuf/TextFormat.java @@ -384,12 +384,24 @@ public final class TextFormat { private int previousLine = 0; private int previousColumn = 0; - private static Pattern WHITESPACE = Pattern.compile("(\\s|(#[^\n]*$))*"); + private static Pattern WHITESPACE = + Pattern.compile("(\\s|(#.*$))+", Pattern.MULTILINE); private static Pattern TOKEN = Pattern.compile( "[a-zA-Z_][0-9a-zA-Z_+-]*|" + // an identifier "[0-9+-][0-9a-zA-Z_.+-]*|" + // a number - "\"([^\"\n\\\\]|\\\\[^\n])*(\"|\\\\?$)|" + // a double-quoted string - "\'([^\"\n\\\\]|\\\\[^\n])*(\'|\\\\?$)"); // a single-quoted string + "\"([^\"\n\\\\]|\\\\.)*(\"|\\\\?$)|" + // a double-quoted string + "\'([^\"\n\\\\]|\\\\.)*(\'|\\\\?$)", // a single-quoted string + Pattern.MULTILINE); + + private static Pattern DOUBLE_INFINITY = Pattern.compile( + "-?inf(inity)?", + Pattern.CASE_INSENSITIVE); + private static Pattern FLOAT_INFINITY = Pattern.compile( + "-?inf(inity)?f?", + Pattern.CASE_INSENSITIVE); + private static Pattern FLOAT_NAN = Pattern.compile( + "nanf?", + Pattern.CASE_INSENSITIVE); /** Construct a tokenizer that parses tokens from the given text. */ public Tokenizer(CharSequence text) { @@ -570,6 +582,17 @@ public final class TextFormat { * Otherwise, throw a {@link ParseException}. */ public double consumeDouble() throws ParseException { + // We need to parse infinity and nan separately because + // Double.parseDouble() does not accept "inf", "infinity", or "nan". + if (DOUBLE_INFINITY.matcher(currentToken).matches()) { + boolean negative = currentToken.startsWith("-"); + nextToken(); + return negative ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY; + } + if (currentToken.equalsIgnoreCase("nan")) { + nextToken(); + return Double.NaN; + } try { double result = Double.parseDouble(currentToken); nextToken(); @@ -584,6 +607,17 @@ public final class TextFormat { * Otherwise, throw a {@link ParseException}. */ public float consumeFloat() throws ParseException { + // We need to parse infinity and nan separately because + // Float.parseFloat() does not accept "inf", "infinity", or "nan". + if (FLOAT_INFINITY.matcher(currentToken).matches()) { + boolean negative = currentToken.startsWith("-"); + nextToken(); + return negative ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY; + } + if (FLOAT_NAN.matcher(currentToken).matches()) { + nextToken(); + return Float.NaN; + } try { float result = Float.parseFloat(currentToken); nextToken(); diff --git a/java/src/main/java/com/google/protobuf/UnknownFieldSet.java b/java/src/main/java/com/google/protobuf/UnknownFieldSet.java index 6bcc4309..ba4dd5ad 100644 --- a/java/src/main/java/com/google/protobuf/UnknownFieldSet.java +++ b/java/src/main/java/com/google/protobuf/UnknownFieldSet.java @@ -387,7 +387,7 @@ public final class UnknownFieldSet { int number = WireFormat.getTagFieldNumber(tag); switch (WireFormat.getTagWireType(tag)) { case WireFormat.WIRETYPE_VARINT: - getFieldBuilder(number).addVarint(input.readInt32()); + getFieldBuilder(number).addVarint(input.readInt64()); return true; case WireFormat.WIRETYPE_FIXED64: getFieldBuilder(number).addFixed64(input.readFixed64()); |