Copilot commented on code in PR #2627:
URL: https://github.com/apache/groovy/pull/2627#discussion_r3478217008
##########
src/main/java/org/codehaus/groovy/runtime/EncodingGroovyMethods.java:
##########
@@ -372,12 +372,19 @@ public static byte[] decodeHex(final String value) {
byte[] bytes = new byte[value.length() / 2];
for (int i = 0; i < value.length(); i += 2) {
- bytes[i / 2] = (byte) Integer.parseInt(value.substring(i, i + 2),
16);
+ bytes[i / 2] = (byte) ((hexToNibble(value.charAt(i)) << 4) |
hexToNibble(value.charAt(i + 1)));
}
return bytes;
}
+ private static int hexToNibble(final char c) {
+ if (c >= '0' && c <= '9') return c - '0';
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
+ throw new NumberFormatException("illegal hexadecimal character " + c +
" in hex string");
Review Comment:
The NumberFormatException message interpolates the raw character without any
quoting, which can be ambiguous (e.g. whitespace) and harder to read in
failures. Quoting the character makes the error clearer without changing
behavior.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]