55
55
import static com .oracle .graal .python .builtins .modules .CodecsModuleBuiltins .T_UTF_32_LE ;
56
56
import static com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiCallPath .Direct ;
57
57
import static com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiCallPath .Ignored ;
58
+ import static com .oracle .graal .python .builtins .modules .ctypes .CtypesNodes .WCHAR_T_ENCODING ;
59
+ import static com .oracle .graal .python .builtins .modules .ctypes .CtypesNodes .WCHAR_T_SIZE ;
58
60
import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .CONST_WCHAR_PTR ;
59
61
import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .ConstCharPtr ;
60
62
import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .ConstCharPtrAsTruffleString ;
75
77
import static com .oracle .graal .python .nodes .ErrorMessages .SEPARATOR_EXPECTED_STR_INSTANCE_P_FOUND ;
76
78
import static com .oracle .graal .python .nodes .SpecialMethodNames .T___GETITEM__ ;
77
79
import static com .oracle .graal .python .nodes .StringLiterals .T_EMPTY_STRING ;
78
- import static com .oracle .graal .python .nodes .StringLiterals .T_REPLACE ;
79
80
import static com .oracle .graal .python .nodes .StringLiterals .T_SPACE ;
80
81
import static com .oracle .graal .python .nodes .StringLiterals .T_STRICT ;
81
82
import static com .oracle .graal .python .nodes .StringLiterals .T_UTF8 ;
87
88
import static com .oracle .truffle .api .strings .TruffleString .Encoding .UTF_32LE ;
88
89
import static com .oracle .truffle .api .strings .TruffleString .Encoding .UTF_8 ;
89
90
90
- import java .nio .charset .Charset ;
91
- import java .nio .charset .StandardCharsets ;
92
-
93
91
import com .oracle .graal .python .PythonLanguage ;
94
92
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
95
93
import com .oracle .graal .python .builtins .modules .BuiltinFunctions .ChrNode ;
109
107
import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiUnaryBuiltinNode ;
110
108
import com .oracle .graal .python .builtins .modules .codecs .ErrorHandlers ;
111
109
import com .oracle .graal .python .builtins .objects .PNone ;
112
- import com .oracle .graal .python .builtins .objects .buffer .PythonBufferAccessLibrary ;
113
110
import com .oracle .graal .python .builtins .objects .bytes .PBytes ;
114
111
import com .oracle .graal .python .builtins .objects .cext .PythonAbstractNativeObject ;
115
112
import com .oracle .graal .python .builtins .objects .cext .capi .CApiContext ;
@@ -1072,9 +1069,10 @@ abstract static class PyUnicode_EncodeFSDefault extends CApiUnaryBuiltinNode {
1072
1069
static PBytes fromObject (Object s ,
1073
1070
@ Bind Node inliningTarget ,
1074
1071
@ Cached CastToTruffleStringNode castStr ,
1075
- @ Cached EncodeNativeStringNode encode ) {
1076
- byte [] array = encode .execute (StandardCharsets .UTF_8 , castStr .execute (inliningTarget , s ), T_REPLACE );
1077
- return PFactory .createBytes (PythonLanguage .get (inliningTarget ), array );
1072
+ @ Cached TruffleString .SwitchEncodingNode switchEncodingNode ,
1073
+ @ Cached TruffleString .CopyToByteArrayNode copyToByteArrayNode ) {
1074
+ TruffleString utf8Str = switchEncodingNode .execute (castStr .execute (inliningTarget , s ), TruffleString .Encoding .UTF_8 );
1075
+ return PFactory .createBytes (PythonLanguage .get (inliningTarget ), copyToByteArrayNode .execute (utf8Str , TruffleString .Encoding .UTF_8 ));
1078
1076
}
1079
1077
}
1080
1078
@@ -1103,22 +1101,24 @@ Object doInt(Object arr, long size,
1103
1101
}
1104
1102
1105
1103
abstract static class NativeEncoderNode extends CApiBinaryBuiltinNode {
1106
- private final Charset charset ;
1104
+ private final TruffleString . Encoding encoding ;
1107
1105
1108
- protected NativeEncoderNode (Charset charset ) {
1109
- this .charset = charset ;
1106
+ protected NativeEncoderNode (TruffleString . Encoding encoding ) {
1107
+ this .encoding = encoding ;
1110
1108
}
1111
1109
1112
1110
@ Specialization (guards = "isNoValue(errors)" )
1113
1111
Object doUnicode (Object s , @ SuppressWarnings ("unused" ) PNone errors ,
1114
- @ Shared ("encodeNode" ) @ Cached EncodeNativeStringNode encodeNativeStringNode ) {
1115
- return doUnicode (s , T_STRICT , encodeNativeStringNode );
1112
+ @ Shared ("encodeNode" ) @ Cached EncodeNativeStringNode encodeNativeStringNode ,
1113
+ @ Shared ("copyNode" ) @ Cached TruffleString .CopyToByteArrayNode copyToByteArrayNode ) {
1114
+ return doUnicode (s , T_STRICT , encodeNativeStringNode , copyToByteArrayNode );
1116
1115
}
1117
1116
1118
1117
@ Specialization
1119
1118
Object doUnicode (Object s , TruffleString errors ,
1120
- @ Shared ("encodeNode" ) @ Cached EncodeNativeStringNode encodeNativeStringNode ) {
1121
- return PFactory .createBytes (PythonLanguage .get (this ), encodeNativeStringNode .execute (charset , s , errors ));
1119
+ @ Shared ("encodeNode" ) @ Cached EncodeNativeStringNode encodeNativeStringNode ,
1120
+ @ Shared ("copyNode" ) @ Cached TruffleString .CopyToByteArrayNode copyToByteArrayNode ) {
1121
+ return PFactory .createBytes (PythonLanguage .get (this ), copyToByteArrayNode .execute (encodeNativeStringNode .execute (encoding , s , errors ), encoding ));
1122
1122
}
1123
1123
1124
1124
@ Fallback
@@ -1131,22 +1131,22 @@ static Object doUnicode(@SuppressWarnings("unused") Object s, @SuppressWarnings(
1131
1131
@ CApiBuiltin (ret = PyObjectTransfer , args = {PyObject , ConstCharPtrAsTruffleString }, call = Direct )
1132
1132
abstract static class _PyUnicode_AsLatin1String extends NativeEncoderNode {
1133
1133
protected _PyUnicode_AsLatin1String () {
1134
- super (StandardCharsets .ISO_8859_1 );
1134
+ super (TruffleString . Encoding .ISO_8859_1 );
1135
1135
}
1136
1136
}
1137
1137
1138
1138
@ CApiBuiltin (ret = PyObjectTransfer , args = {PyObject , ConstCharPtrAsTruffleString }, call = Direct )
1139
1139
abstract static class _PyUnicode_AsASCIIString extends NativeEncoderNode {
1140
1140
protected _PyUnicode_AsASCIIString () {
1141
- super (StandardCharsets .US_ASCII );
1141
+ super (TruffleString . Encoding .US_ASCII );
1142
1142
}
1143
1143
}
1144
1144
1145
1145
@ CApiBuiltin (ret = PyObjectTransfer , args = {PyObject , ConstCharPtrAsTruffleString }, call = Direct )
1146
1146
abstract static class _PyUnicode_AsUTF8String extends NativeEncoderNode {
1147
1147
1148
1148
protected _PyUnicode_AsUTF8String () {
1149
- super (StandardCharsets .UTF_8 );
1149
+ super (TruffleString . Encoding .UTF_8 );
1150
1150
}
1151
1151
1152
1152
@ NeverDefault
@@ -1190,15 +1190,14 @@ abstract static class GraalPyPrivate_Unicode_FillUtf8 extends CApiUnaryBuiltinNo
1190
1190
@ Specialization
1191
1191
static Object doNative (PythonAbstractNativeObject s ,
1192
1192
@ Cached CStructAccess .WriteLongNode writeLongNode ,
1193
- @ Cached _PyUnicode_AsUTF8String asUTF8String ,
1194
- @ CachedLibrary (limit = "1" ) PythonBufferAccessLibrary bufferLib ,
1193
+ @ Cached EncodeNativeStringNode encodeNativeStringNode ,
1195
1194
@ Cached CStructAccess .WritePointerNode writePointerNode ,
1196
1195
@ Cached CStructAccess .AllocateNode allocateNode ,
1197
- @ Cached CStructAccess .WriteByteNode writeByteNode ) {
1198
- PBytes bytes = ( PBytes ) asUTF8String .execute (s , T_STRICT );
1199
- int len = bufferLib . getBufferLength ( bytes );
1196
+ @ Cached CStructAccess .WriteTruffleStringNode writeTruffleStringNode ) {
1197
+ TruffleString utf8Str = encodeNativeStringNode .execute (UTF_8 , s , T_STRICT );
1198
+ int len = utf8Str . byteLength ( UTF_8 );
1200
1199
Object mem = allocateNode .alloc (len + 1 , true );
1201
- writeByteNode . writeByteArray (mem , bufferLib . getInternalOrCopiedByteArray ( bytes ), len , 0 , 0 );
1200
+ writeTruffleStringNode . write (mem , utf8Str , UTF_8 );
1202
1201
writePointerNode .writeToObj (s , CFields .PyCompactUnicodeObject__utf8 , mem );
1203
1202
writeLongNode .writeToObject (s , CFields .PyCompactUnicodeObject__utf8_length , len );
1204
1203
return 0 ;
@@ -1259,15 +1258,13 @@ abstract static class GraalPyPrivate_Unicode_FillUnicode extends CApiUnaryBuilti
1259
1258
static Object doNative (PythonAbstractNativeObject s ,
1260
1259
@ Bind Node inliningTarget ,
1261
1260
@ Cached CastToTruffleStringNode cast ,
1262
- @ Cached UnicodeAsWideCharNode asWideCharNode ,
1263
- @ CachedLibrary (limit = "1" ) PythonBufferAccessLibrary bufferLib ,
1261
+ @ Cached TruffleString .SwitchEncodingNode switchEncodingNode ,
1264
1262
@ Cached CStructAccess .AllocateNode allocateNode ,
1265
- @ Cached CStructAccess .WriteByteNode writeByteNode ) {
1266
- int wcharSize = CStructs .wchar_t .size ();
1267
- PBytes bytes = asWideCharNode .executeNativeOrder (inliningTarget , cast .castKnownString (inliningTarget , s ), wcharSize );
1268
- int len = bufferLib .getBufferLength (bytes );
1269
- Object mem = allocateNode .alloc (len + wcharSize , true );
1270
- writeByteNode .writeByteArray (mem , bufferLib .getInternalOrCopiedByteArray (bytes ), len , 0 , 0 );
1263
+ @ Cached CStructAccess .WriteTruffleStringNode writeTruffleStringNode ) {
1264
+ TruffleString str = switchEncodingNode .execute (cast .castKnownString (inliningTarget , s ), WCHAR_T_ENCODING );
1265
+ int len = str .byteLength (WCHAR_T_ENCODING );
1266
+ Object mem = allocateNode .alloc (len + WCHAR_T_SIZE , true );
1267
+ writeTruffleStringNode .write (mem , str , WCHAR_T_ENCODING );
1271
1268
return 0 ;
1272
1269
}
1273
1270
}
0 commit comments