# HG changeset patch # User Artem Tikhomirov # Date 1313605806 -7200 # Node ID 436bb5f65ce13e1a3a012fa999b19e65fca6a528 # Parent 61cb6724ff36e8696d252d7d75bf1cbc96166e92 Avoid redundant calls to library when converting a char to hex digit diff -r 61cb6724ff36 -r 436bb5f65ce1 src/org/tmatesoft/hg/core/Nodeid.java --- a/src/org/tmatesoft/hg/core/Nodeid.java Wed Aug 17 02:35:15 2011 +0200 +++ b/src/org/tmatesoft/hg/core/Nodeid.java Wed Aug 17 20:30:06 2011 +0200 @@ -165,7 +165,7 @@ // XXX is better impl for String possible? return fromAscii(asciiRepresentation.getBytes(), 0, 40); } - + /** * Parse encoded representation. Similar to {@link #fromAscii(String)}. */ @@ -176,10 +176,14 @@ byte[] data = new byte[20]; boolean zeroBytes = true; for (int i = 0, j = offset; i < data.length; i++) { - int hiNibble = Character.digit(asciiRepresentation[j++], 16); - int lowNibble = Character.digit(asciiRepresentation[j++], 16); - byte b = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); - data[i] = b; + int b = asciiRepresentation[j++] & 0xCF; // -0x30 to get decimal digit out from their char, and to uppercase if a letter + int hiNibble = b > 64 ? b - 55 : b; + b = asciiRepresentation[j++] & 0xCF; + int lowNibble = b > 64 ? b - 55 : b; + if (hiNibble >= 16 || lowNibble >= 16) { + throw new IllegalArgumentException(String.format("Characters '%c%c' (%1$d and %2$d) at index %d are not valid hex digits", asciiRepresentation[j-2], asciiRepresentation[j-1], j-2)); + } + data[i] = (byte) (((hiNibble << 4) | lowNibble) & 0xFF); zeroBytes = zeroBytes && b == 0; } if (zeroBytes) {