aboutsummaryrefslogtreecommitdiffhomepage
path: root/java/src/main
diff options
context:
space:
mode:
authorGravatar temporal <temporal@630680e5-0e50-0410-840e-4b1c322b438d>2008-07-23 01:19:07 +0000
committerGravatar temporal <temporal@630680e5-0e50-0410-840e-4b1c322b438d>2008-07-23 01:19:07 +0000
commitf206351d1469970230caa5f61eaa54797194bee1 (patch)
tree3b1b9ea37171fdb231bca2fe6c74492eeea2abd7 /java/src/main
parentcc930432c2823c3d82e0b8dd2ae4f446c82f4fce (diff)
Sync code with Google-internal branch. Changes:
Protoc (parser) - Improved error message when an enum value's name conflicts with another symbol defined in the enum type's scope, e.g. if two enum types declared in the same scope have values with the same name. This is disallowed for compatibility with C++, but this wasn't clear from the error. C++ - Restored the set_foo(const char*) accessor for "bytes" type because some code inside Google depends on it. However, set_foo(const char*, int) is still there (and actually is changed to take const void*). - Fixed TokenizerTest when compiling with -DNDEBUG on Linux. - Other irrelevant tweaks. Java - Fixed UnknownFieldSet's parsing of varints larger than 32 bits. - Fixed TextFormat's parsing of "inf" and "nan". - Fixed TextFormat's parsing of comments. Python - Fixed text_format_test on Windows where floating-point exponents sometimes contain extra zeros.
Diffstat (limited to 'java/src/main')
-rw-r--r--java/src/main/java/com/google/protobuf/TextFormat.java40
-rw-r--r--java/src/main/java/com/google/protobuf/UnknownFieldSet.java2
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());