diff options
author | Calin Juravle <calin@google.com> | 2014-04-24 10:46:44 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-04-24 10:46:44 +0000 |
commit | 41f4f5501af5f216e4cc232b1b9d7c5a28a70d2a (patch) | |
tree | f7396204ef7090fb86274ba25a92bdf1a03779f3 | |
parent | a6735a1f3cbe90f6f6555b4ebf10573a760bdb32 (diff) | |
parent | ae6e60bf18b455b1134b8b57a56b37cc588e179d (diff) | |
download | libcore-41f4f5501af5f216e4cc232b1b9d7c5a28a70d2a.zip libcore-41f4f5501af5f216e4cc232b1b9d7c5a28a70d2a.tar.gz libcore-41f4f5501af5f216e4cc232b1b9d7c5a28a70d2a.tar.bz2 |
Merge "Fix UUID#parseString in the presence of explicit + signs."
-rw-r--r-- | luni/src/main/java/java/lang/Long.java | 33 | ||||
-rw-r--r-- | luni/src/main/java/java/util/UUID.java | 25 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/lang/LongTest.java | 22 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/util/UUIDTest.java | 53 |
4 files changed, 115 insertions, 18 deletions
diff --git a/luni/src/main/java/java/lang/Long.java b/luni/src/main/java/java/lang/Long.java index 84169af..4ebc85b 100644 --- a/luni/src/main/java/java/lang/Long.java +++ b/luni/src/main/java/java/lang/Long.java @@ -378,6 +378,39 @@ public final class Long extends Number implements Comparable<Long> { return result; } + /** + * Equivalent to {@code parsePositiveLong(string, 10)}. + * + * @see #parsePositiveLong(String, int) + * + * @hide + */ + public static long parsePositiveLong(String string) throws NumberFormatException { + return parsePositiveLong(string, 10); + } + + /** + * Parses the specified string as a positive long value using the + * specified radix. 0 is considered a positive long. + * <p> + * This method behaves the same as {@link #parseLong(String, int)} except + * that it disallows leading '+' and '-' characters. See that method for + * error conditions. + * + * @see #parseLong(String, int) + * + * @hide + */ + public static long parsePositiveLong(String string, int radix) throws NumberFormatException { + if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { + throw new NumberFormatException("Invalid radix: " + radix); + } + if (string == null || string.length() == 0) { + throw invalidLong(string); + } + return parse(string, 0, radix, false); + } + @Override public short shortValue() { return (short) value; diff --git a/luni/src/main/java/java/util/UUID.java b/luni/src/main/java/java/util/UUID.java index 3594d87..020ac95 100644 --- a/luni/src/main/java/java/util/UUID.java +++ b/luni/src/main/java/java/util/UUID.java @@ -182,28 +182,17 @@ public final class UUID implements Serializable, Comparable<UUID> { throw new NullPointerException("uuid == null"); } - int[] position = new int[5]; - int lastPosition = 1; - int startPosition = 0; - - int i = 0; - for (; i < position.length && lastPosition > 0; i++) { - position[i] = uuid.indexOf("-", startPosition); - lastPosition = position[i]; - startPosition = position[i] + 1; - } - - // should have and only can have four "-" in UUID - if (i != position.length || lastPosition != -1) { + String[] parts = uuid.split("-"); + if (parts.length != 5) { throw new IllegalArgumentException("Invalid UUID: " + uuid); } - long m1 = Long.parseLong(uuid.substring(0, position[0]), 16); - long m2 = Long.parseLong(uuid.substring(position[0] + 1, position[1]), 16); - long m3 = Long.parseLong(uuid.substring(position[1] + 1, position[2]), 16); + long m1 = Long.parsePositiveLong(parts[0], 16); + long m2 = Long.parsePositiveLong(parts[1], 16); + long m3 = Long.parsePositiveLong(parts[2], 16); - long lsb1 = Long.parseLong(uuid.substring(position[2] + 1, position[3]), 16); - long lsb2 = Long.parseLong(uuid.substring(position[3] + 1), 16); + long lsb1 = Long.parsePositiveLong(parts[3], 16); + long lsb2 = Long.parsePositiveLong(parts[4], 16); long msb = (m1 << 32) | (m2 << 16) | m3; long lsb = (lsb1 << 48) | lsb2; diff --git a/luni/src/test/java/libcore/java/lang/LongTest.java b/luni/src/test/java/libcore/java/lang/LongTest.java index ce93145..cb95518 100644 --- a/luni/src/test/java/libcore/java/lang/LongTest.java +++ b/luni/src/test/java/libcore/java/lang/LongTest.java @@ -56,4 +56,26 @@ public class LongTest extends junit.framework.TestCase { assertEquals(1, Long.signum(Long.MAX_VALUE)); assertEquals(-1, Long.signum(Long.MIN_VALUE)); } + + public void test_parsePositiveLong() throws Exception { + assertEquals(0, Long.parsePositiveLong("0", 10)); + assertEquals(473, Long.parsePositiveLong("473", 10)); + assertEquals(255, Long.parsePositiveLong("FF", 16)); + + try { + Long.parsePositiveLong("-1", 10); + fail(); + } catch (NumberFormatException e) {} + + try { + Long.parsePositiveLong("+1", 10); + fail(); + } catch (NumberFormatException e) {} + + try { + Long.parsePositiveLong("+0", 16); + fail(); + } catch (NumberFormatException e) {} + } + } diff --git a/luni/src/test/java/libcore/java/util/UUIDTest.java b/luni/src/test/java/libcore/java/util/UUIDTest.java new file mode 100644 index 0000000..61e4ae0 --- /dev/null +++ b/luni/src/test/java/libcore/java/util/UUIDTest.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.java.util; + +import java.util.UUID; +import junit.framework.TestCase; + +// There are more tests in the harmony suite: +// harmony-tests/src/test/java/org/apache/harmony/tests/java/util/UUIDTest.java +public class UUIDTest extends TestCase { + + public void testFromStringInvalidValues() { + try { + UUID.fromString("+f81d4fae-7dec-11d0-a765-00a0c91e6bf6"); + fail(); + } catch (IllegalArgumentException expected) { } + + try { + UUID.fromString("f81d4fae-+7dec-11d0-a765-00a0c91e6bf6"); + fail(); + } catch (IllegalArgumentException expected) { } + + try { + UUID.fromString("f81d4fae-7dec-+11d0-a765-00a0c91e6bf6"); + fail(); + } catch (IllegalArgumentException expected) { } + + try { + UUID.fromString("f81d4fae-7dec-11d0-+a765-00a0c91e6bf6"); + fail(); + } catch (IllegalArgumentException expected) { } + + try { + UUID.fromString("f81d4fae-7dec-11d0-a765-+00a0c91e6bf6"); + fail(); + } catch (IllegalArgumentException expected) { } + } + +} |