Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tryParse methods to UnsignedBytes, UnsignedInts, and UnsignedLongs. #2180

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ public void testParseUnsignedByte() {
assertParseFails("256");
}

private static void assertTryParseFalse(String value) {
assertFalse(UnsignedBytes.tryParseUnsignedByte(value));
}

public void testTryParseUnsignedByte() {
// We can easily afford to test this exhaustively.
for (int i = 0; i <= 0xff; i++) {
assertTrue(UnsignedBytes.tryParseUnsignedByte(Integer.toString(i)));
}
assertTryParseFalse("1000");
assertTryParseFalse("-1");
assertTryParseFalse("-128");
assertTryParseFalse("256");
}

public void testMaxValue() {
assertTrue(UnsignedBytes
.compare(UnsignedBytes.MAX_VALUE, (byte) (UnsignedBytes.MAX_VALUE + 1)) > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ public void testParseIntFail() {
} catch (NumberFormatException expected) {}
}

public void testTryParseInt() {
for (long a : UNSIGNED_INTS) {
assertTrue(UnsignedInts.tryParseUnsignedInt(Long.toString(a)));
}
}

public void testParseIntFalse() {
assertFalse(UnsignedInts.tryParseUnsignedInt(Long.toString(1L << 32)));
}

public void testParseIntWithRadix() {
for (long a : UNSIGNED_INTS) {
for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
Expand Down Expand Up @@ -219,6 +229,38 @@ public void testParseIntThrowsExceptionForInvalidRadix() {
} catch (NumberFormatException expected) {}
}

public void testTryParseIntWithRadix() {
for (long a : UNSIGNED_INTS) {
for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
assertTrue(UnsignedInts.tryParseUnsignedInt(Long.toString(a, radix), radix));
}
}
}

public void testTryParseIntWithRadixLimits() {
// loops through all legal radix values.
for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
// tests can successfully parse a number string with this radix.
String maxAsString = Long.toString((1L << 32) - 1, radix);
assertTrue(UnsignedInts.tryParseUnsignedInt(maxAsString, radix));

// tests that we get exception whre an overflow would occur.
long overflow = 1L << 32;
String overflowAsString = Long.toString(overflow, radix);
assertFalse(UnsignedInts.tryParseUnsignedInt(overflowAsString, radix));
}
}

public void testTryParseIntFalseForInvalidRadix() {
// Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX,
// inclusive.
assertFalse(UnsignedInts.tryParseUnsignedInt("0", Character.MIN_RADIX - 1));
assertFalse(UnsignedInts.tryParseUnsignedInt("0", Character.MAX_RADIX + 1));

// The radix is used as an array index, so try a negative value.
assertFalse(UnsignedInts.tryParseUnsignedInt("0", -1));
}

public void testDecodeInt() {
assertEquals(0xffffffff, UnsignedInts.decode("0xffffffff"));
assertEquals(01234567, UnsignedInts.decode("01234567")); // octal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ public void testParseLongFails() {
}
}

private static void assertTryParseFalse(String value) {
assertFalse(UnsignedBytes.tryParseUnsignedByte(value));
}

public void testTryParseUnsignedByte() {
// We can easily afford to test this exhaustively.
for (int i = 0; i <= 0xff; i++) {
assertTrue(UnsignedBytes.tryParseUnsignedByte(Integer.toString(i)));
}
assertTryParseFalse("1000");
assertTryParseFalse("-1");
assertTryParseFalse("-128");
assertTryParseFalse("256");
}

public void testDecodeLong() {
assertEquals(0xffffffffffffffffL, UnsignedLongs.decode("0xffffffffffffffff"));
assertEquals(01234567, UnsignedLongs.decode("01234567")); // octal
Expand Down Expand Up @@ -256,6 +271,33 @@ public void testParseLongThrowsExceptionForInvalidRadix() {
}
}

private static void assertTryParseFalse(String value, int radix) {
assertFalse(UnsignedBytes.tryParseUnsignedByte(value, radix));
}

public void testTryParseUnsignedByteWithRadix() throws NumberFormatException {
// We can easily afford to test this exhaustively.
for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
for (int i = 0; i <= 0xff; i++) {
assertTrue(UnsignedBytes.tryParseUnsignedByte(Integer.toString(i, radix), radix));
}
assertTryParseFalse(Integer.toString(1000, radix), radix);
assertTryParseFalse(Integer.toString(-1, radix), radix);
assertTryParseFalse(Integer.toString(-128, radix), radix);
assertTryParseFalse(Integer.toString(256, radix), radix);
}
}

public void testTryParseUnsignedByteFalseForInvalidRadix() {
// Valid radix values are Character.MIN_RADIX to Character.MAX_RADIX,
// inclusive.
assertTryParseFalse("0", Character.MIN_RADIX - 1);
assertTryParseFalse("0", Character.MAX_RADIX + 1);

// The radix is used as an array index, so try a negative value.
assertTryParseFalse("0", -1);
}

public void testToString() {
String[] tests = {
"ffffffffffffffff",
Expand Down
38 changes: 38 additions & 0 deletions guava/src/com/google/common/primitives/UnsignedBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,24 @@ public static byte parseUnsignedByte(String string) {
return parseUnsignedByte(string, 10);
}

/**
* Returns true if the unsigned {@code byte} value can be represented by the given decimal string.
*
* @throws NullPointerException if {@code s} is null
* (in contrast to {@link Byte#parseByte(String)})
* @since 19.0
*/
@Beta
@SuppressWarnings("CheckReturnValue")
public static boolean tryParseUnsignedByte(String string) {
try {
parseUnsignedByte(string, 10);
return true;
} catch (NumberFormatException e) {
return false;
}
}

/**
* Returns the unsigned {@code byte} value represented by a string with the given radix.
*
Expand All @@ -241,6 +259,26 @@ public static byte parseUnsignedByte(String string, int radix) {
}
}

/**
* Returns true if the unsigned {@code byte} value can be represented by a string with the given radix.
*
* @param string the string containing the unsigned {@code byte} representation to be parsed.
* @param radix the radix to use while parsing {@code string}
* @throws NullPointerException if {@code s} is null
* (in contrast to {@link Byte#parseByte(String)})
* @since 19.0
*/
@Beta
@SuppressWarnings("CheckReturnValue")
public static boolean tryParseUnsignedByte(String string, int radix) {
try {
parseUnsignedByte(string, radix);
return true;
} catch (NumberFormatException e) {
return false;
}
}

/**
* Returns a string containing the supplied {@code byte} values separated by
* {@code separator}. For example, {@code join(":", (byte) 1, (byte) 2,
Expand Down
35 changes: 35 additions & 0 deletions guava/src/com/google/common/primitives/UnsignedInts.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,22 @@ public static int parseUnsignedInt(String s) {
return parseUnsignedInt(s, 10);
}

/**
* Returns true if the unsigned {@code int} value can be represented by the given decimal string.
*
* @throws NullPointerException if {@code s} is null
* (in contrast to {@link Integer#parseInt(String)})
*/
@SuppressWarnings("CheckReturnValue")
public static boolean tryParseUnsignedInt(String s) {
try {
parseUnsignedInt(s, 10);
return true;
} catch (NumberFormatException e) {
return false;
}
}

/**
* Returns the unsigned {@code int} value represented by a string with the given radix.
*
Expand All @@ -263,6 +279,25 @@ public static int parseUnsignedInt(String string, int radix) {
return (int) result;
}

/**
* Returns true if the unsigned {@code int} value can be represented by a string with the given radix.
*
* @param string the string containing the unsigned integer representation to be parsed.
* @param radix the radix to use while parsing {@code s}; must be between
* {@link Character#MIN_RADIX} and {@link Character#MAX_RADIX}.
* @throws NullPointerException if {@code s} is null
* (in contrast to {@link Integer#parseInt(String)})
*/
@SuppressWarnings("CheckReturnValue")
public static boolean tryParseUnsignedInt(String string, int radix) {
try {
parseUnsignedInt(string, radix);
return true;
} catch (NumberFormatException e) {
return false;
}
}

/**
* Returns a string representation of x, where x is treated as unsigned.
*/
Expand Down
34 changes: 34 additions & 0 deletions guava/src/com/google/common/primitives/UnsignedLongs.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,22 @@ public static long parseUnsignedLong(String s) {
return parseUnsignedLong(s, 10);
}

/**
* Returns true if the unsigned {@code long} value can be represented by the given decimal string.
*
* @throws NullPointerException if {@code s} is null
* (in contrast to {@link Long#parseLong(String)})
*/
@SuppressWarnings("CheckReturnValue")
public static boolean tryParseUnsignedLong(String s) {
try {
parseUnsignedLong(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}

/**
* Returns the unsigned {@code long} value represented by the given string.
*
Expand Down Expand Up @@ -324,6 +340,24 @@ public static long parseUnsignedLong(String s, int radix) {
return value;
}

/**
* Returns true if the unsigned {@code long} value can be represented by a string with the given radix.
*
* @param s the string containing the unsigned {@code long} representation to be parsed.
* @param radix the radix to use while parsing {@code s}
* @throws NullPointerException if {@code s} is null
* (in contrast to {@link Long#parseLong(String)})
*/
@SuppressWarnings("CheckReturnValue")
public static boolean tryParseUnsignedLong(String s, int radix) {
try {
parseUnsignedLong(s, radix);
return true;
} catch (NumberFormatException e) {
return false;
}
}

/**
* Returns true if (current * radix) + digit is a number too large to be represented by an
* unsigned long. This is useful for detecting overflow while parsing a string representation of
Expand Down