Skip to content

Commit

Permalink
Use toUnsignedString() to convert uint32 and uint64 to strings.
Browse files Browse the repository at this point in the history
This method has existed since Java 8, released in 2014.

Also adjust the tests so that they verify unsigned integer values that are too big for the signed counterpart (for example, 4294967295 which fits in `uint32` but not `int32`).

We don't use the parallel `Long.parseUnsignedLong` because the existing code handles inputs like `"1.0"`.

PiperOrigin-RevId: 723945649
  • Loading branch information
eamonnmcmanus authored and copybara-github committed Feb 6, 2025
1 parent b8c3b60 commit aab6b1f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
24 changes: 2 additions & 22 deletions java/util/src/main/java/com/google/protobuf/util/JsonFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -1258,15 +1258,15 @@ private void printSingleFieldValue(
if (alwaysWithQuotes) {
generator.print("\"");
}
generator.print(unsignedToString((Integer) value));
generator.print(Integer.toUnsignedString((int) value));
if (alwaysWithQuotes) {
generator.print("\"");
}
break;

case UINT64:
case FIXED64:
generator.print("\"" + unsignedToString((Long) value) + "\"");
generator.print("\"" + Long.toUnsignedString((long) value) + "\"");
break;

case STRING:
Expand Down Expand Up @@ -1307,26 +1307,6 @@ private void printSingleFieldValue(
}
}

/** Convert an unsigned 32-bit integer to a string. */
private static String unsignedToString(final int value) {
if (value >= 0) {
return Integer.toString(value);
} else {
return Long.toString(value & 0x00000000FFFFFFFFL);
}
}

/** Convert an unsigned 64-bit integer to a string. */
private static String unsignedToString(final long value) {
if (value >= 0) {
return Long.toString(value);
} else {
// Pull off the most-significant bit so that BigInteger doesn't think
// the number is negative, then set it again using setBit().
return BigInteger.valueOf(value & Long.MAX_VALUE).setBit(Long.SIZE - 1).toString();
}
}

private static String getTypeName(String typeUrl) throws InvalidProtocolBufferException {
String[] parts = typeUrl.split("/");
if (parts.length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public static void resetLocale() {
private void setAllFields(TestAllTypes.Builder builder) {
builder.setOptionalInt32(1234);
builder.setOptionalInt64(1234567890123456789L);
builder.setOptionalUint32(5678);
builder.setOptionalUint64(2345678901234567890L);
builder.setOptionalUint32(-1);
builder.setOptionalUint64(-1L);
builder.setOptionalSint32(9012);
builder.setOptionalSint64(3456789012345678901L);
builder.setOptionalFixed32(3456);
Expand Down Expand Up @@ -190,8 +190,8 @@ public void testAllFields() throws Exception {
"{\n"
+ " \"optionalInt32\": 1234,\n"
+ " \"optionalInt64\": \"1234567890123456789\",\n"
+ " \"optionalUint32\": 5678,\n"
+ " \"optionalUint64\": \"2345678901234567890\",\n"
+ " \"optionalUint32\": 4294967295,\n"
+ " \"optionalUint64\": \"18446744073709551615\",\n"
+ " \"optionalSint32\": 9012,\n"
+ " \"optionalSint64\": \"3456789012345678901\",\n"
+ " \"optionalFixed32\": 3456,\n"
Expand Down Expand Up @@ -1611,8 +1611,8 @@ public void testOmittingInsignificantWhiteSpace() throws Exception {
"{"
+ "\"optionalInt32\":1234,"
+ "\"optionalInt64\":\"1234567890123456789\","
+ "\"optionalUint32\":5678,"
+ "\"optionalUint64\":\"2345678901234567890\","
+ "\"optionalUint32\":4294967295,"
+ "\"optionalUint64\":\"18446744073709551615\","
+ "\"optionalSint32\":9012,"
+ "\"optionalSint64\":\"3456789012345678901\","
+ "\"optionalFixed32\":3456,"
Expand Down

0 comments on commit aab6b1f

Please sign in to comment.