jamesfredley commented on code in PR #15828:
URL: https://github.com/apache/grails-core/pull/15828#discussion_r3524949500
##########
grails-codecs-core/src/main/groovy/org/grails/plugins/codecs/HexCodecExtensionMethods.groovy:
##########
@@ -22,50 +22,45 @@ import java.nio.charset.StandardCharsets
import org.codehaus.groovy.runtime.NullObject
+import groovy.transform.CompileStatic
+
+@CompileStatic
class HexCodecExtensionMethods {
- static HEXDIGITS = '0123456789abcdef'
+ static Object HEXDIGITS = '0123456789abcdef'
// Expects an array/list of numbers
- static encodeAsHex(theTarget) {
+ static Object encodeAsHex(Object theTarget) {
if (theTarget == null || theTarget instanceof NullObject) {
return null
}
- def result = new StringBuilder()
- if (theTarget instanceof String) {
- theTarget = theTarget.getBytes(StandardCharsets.UTF_8)
- }
- theTarget.each() {
- result << HexCodecExtensionMethods.HEXDIGITS[(it & 0xF0) >> 4]
- result << HexCodecExtensionMethods.HEXDIGITS[it & 0x0F]
+ byte[] bytes = theTarget instanceof String ? ((String)
theTarget).getBytes(StandardCharsets.UTF_8) : DigestUtils.toByteArray(theTarget)
+ StringBuilder result = new StringBuilder(bytes.length * 2)
+ String hexDigits = (String) HEXDIGITS
+ for (byte value : bytes) {
+ int unsignedValue = value & 0xFF
+ result.append(hexDigits.charAt((unsignedValue & 0xF0) >> 4))
+ result.append(hexDigits.charAt(unsignedValue & 0x0F))
}
return result.toString()
}
- static decodeHex(theTarget) {
- if (!theTarget) return null
+ static Object decodeHex(Object theTarget) {
+ if (theTarget == null || theTarget instanceof NullObject ||
theTarget.toString().length() == 0) return null
Review Comment:
Fixed in 211a1ffae6. `decodeHex` now preserves the old Groovy truth behavior
under `@CompileStatic` by using `DefaultTypeTransformation.castToBoolean`, and
`HexCodecTests` covers falsy values including empty string, zero, false, empty
list, and empty byte array.
--
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]