Skip to content

Commit a3244e1

Browse files
committed
CodecsModuleBuiltins: reuse BytesUtils helpers
1 parent 49d3dce commit a3244e1

File tree

2 files changed

+8
-27
lines changed

2 files changed

+8
-27
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/CodecsModuleBuiltins.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*/
4141
package com.oracle.graal.python.builtins.modules;
4242

43+
import static com.oracle.graal.python.builtins.objects.bytes.BytesUtils.HEXDIGITS;
44+
import static com.oracle.graal.python.builtins.objects.bytes.BytesUtils.digitValue;
4345
import static com.oracle.graal.python.nodes.ErrorMessages.BYTESLIKE_OBJ_REQUIRED;
4446
import static com.oracle.graal.python.nodes.ErrorMessages.ENCODING_ERROR_WITH_CODE;
4547
import static com.oracle.graal.python.nodes.ErrorMessages.INVALID_ESCAPE_AT;
@@ -64,6 +66,7 @@
6466
import com.oracle.graal.python.builtins.CoreFunctions;
6567
import com.oracle.graal.python.builtins.PythonBuiltins;
6668
import com.oracle.graal.python.builtins.objects.PNone;
69+
import com.oracle.graal.python.builtins.objects.bytes.ByteArrayBuffer;
6770
import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
6871
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
6972
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
@@ -80,7 +83,6 @@
8083
import com.oracle.graal.python.nodes.expression.CoerceToBooleanNode;
8184
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
8285
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
83-
import com.oracle.graal.python.builtins.objects.bytes.ByteArrayBuffer;
8486
import com.oracle.graal.python.nodes.util.CannotCastException;
8587
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
8688
import com.oracle.graal.python.nodes.util.CastToJavaStringNodeGen;
@@ -636,25 +638,6 @@ private void handleDecodingError(TruffleDecoder encoder, String errorAction, Obj
636638
@Builtin(name = "escape_decode", minNumOfPositionalArgs = 1, parameterNames = {"data", "errors"})
637639
@GenerateNodeFactory
638640
abstract static class CodecsEscapeDecodeNode extends EncodeBaseNode {
639-
private static final int[] _PyLong_DigitValue = {
640-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
641-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
642-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
643-
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 37, 37, 37, 37, 37, 37,
644-
37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
645-
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
646-
37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
647-
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
648-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
649-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
650-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
651-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
652-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
653-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
654-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
655-
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
656-
};
657-
658641
enum Errors {
659642
ERR_STRICT,
660643
ERR_IGNORE,
@@ -761,8 +744,8 @@ Object decode(Object data, String errors,
761744
break;
762745
case 'x':
763746
if (i + 2 < bytes.length) {
764-
int digit1 = _PyLong_DigitValue[bytes[i + 1] & 0xff];
765-
int digit2 = _PyLong_DigitValue[bytes[i + 2] & 0xff];
747+
int digit1 = digitValue(bytes[i + 1]);
748+
int digit2 = digitValue(bytes[i + 2]);
766749
if (digit1 < 16 && digit2 < 16) {
767750
buffer.append((digit1 << 4) + digit2);
768751
i += 2;
@@ -810,8 +793,6 @@ private static boolean isHexDigit(char digit) {
810793
@Builtin(name = "escape_encode", minNumOfPositionalArgs = 1, parameterNames = {"data", "errors"})
811794
@GenerateNodeFactory
812795
abstract static class CodecsEscapeEncodeNode extends EncodeBaseNode {
813-
private static final String Py_hexdigits = "0123456789abcdef";
814-
815796
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
816797
Object encode(PBytes data, @SuppressWarnings("unused") PNone errors,
817798
@CachedLibrary(value = "data") PythonObjectLibrary pol) {
@@ -844,8 +825,8 @@ Object encode(PBytes data, @SuppressWarnings("unused") String errors,
844825
} else if (c < ' ' || c >= 0x7f) {
845826
buffer.append('\\');
846827
buffer.append('x');
847-
buffer.append(Py_hexdigits.charAt((c & 0xf0) >> 4));
848-
buffer.append(Py_hexdigits.charAt(c & 0xf));
828+
buffer.append(HEXDIGITS[(c & 0xf0) >> 4]);
829+
buffer.append(HEXDIGITS[c & 0xf]);
849830
} else {
850831
buffer.append(c);
851832
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
public final class BytesUtils {
4747

48-
static final byte[] HEXDIGITS = "0123456789abcdef".getBytes();
48+
public static final byte[] HEXDIGITS = "0123456789abcdef".getBytes();
4949

5050
// tables are copied from CPython/Python/pyctype.c
5151
static final byte PY_CTF_LOWER = 0x01;

0 commit comments

Comments
 (0)