Copilot commented on code in PR #15828:
URL: https://github.com/apache/grails-core/pull/15828#discussion_r3524190428
##########
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:
`decodeHex` used to treat any Groovy-falsy target (e.g. `''`, `0`, `false`,
empty collections/arrays) as `null` via `if (!theTarget) return null`. The new
guard only checks for `null`/`NullObject`/empty-string, which can turn
previously-non-throwing calls like `0.decodeHex()` or `[].decodeHex()` into
exceptions or garbage output. If the intent is to preserve the public
extension-method contract, restore the original Groovy-truthiness check.
--
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]