5555import static com .oracle .graal .python .builtins .modules .CodecsModuleBuiltins .T_UTF_32_LE ;
5656import static com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiCallPath .Direct ;
5757import 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 ;
5860import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .CONST_WCHAR_PTR ;
5961import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .ConstCharPtr ;
6062import static com .oracle .graal .python .builtins .objects .cext .capi .transitions .ArgDescriptor .ConstCharPtrAsTruffleString ;
7577import static com .oracle .graal .python .nodes .ErrorMessages .SEPARATOR_EXPECTED_STR_INSTANCE_P_FOUND ;
7678import static com .oracle .graal .python .nodes .SpecialMethodNames .T___GETITEM__ ;
7779import static com .oracle .graal .python .nodes .StringLiterals .T_EMPTY_STRING ;
78- import static com .oracle .graal .python .nodes .StringLiterals .T_REPLACE ;
7980import static com .oracle .graal .python .nodes .StringLiterals .T_SPACE ;
8081import static com .oracle .graal .python .nodes .StringLiterals .T_STRICT ;
8182import static com .oracle .graal .python .nodes .StringLiterals .T_UTF8 ;
8788import static com .oracle .truffle .api .strings .TruffleString .Encoding .UTF_32LE ;
8889import static com .oracle .truffle .api .strings .TruffleString .Encoding .UTF_8 ;
8990
90- import java .nio .charset .Charset ;
91- import java .nio .charset .StandardCharsets ;
92-
9391import com .oracle .graal .python .PythonLanguage ;
9492import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
9593import com .oracle .graal .python .builtins .modules .BuiltinFunctions .ChrNode ;
109107import com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .CApiUnaryBuiltinNode ;
110108import com .oracle .graal .python .builtins .modules .codecs .ErrorHandlers ;
111109import com .oracle .graal .python .builtins .objects .PNone ;
112- import com .oracle .graal .python .builtins .objects .buffer .PythonBufferAccessLibrary ;
113110import com .oracle .graal .python .builtins .objects .bytes .PBytes ;
114111import com .oracle .graal .python .builtins .objects .cext .PythonAbstractNativeObject ;
115112import com .oracle .graal .python .builtins .objects .cext .capi .CApiContext ;
@@ -1072,9 +1069,10 @@ abstract static class PyUnicode_EncodeFSDefault extends CApiUnaryBuiltinNode {
10721069 static PBytes fromObject (Object s ,
10731070 @ Bind Node inliningTarget ,
10741071 @ 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 ));
10781076 }
10791077 }
10801078
@@ -1103,22 +1101,24 @@ Object doInt(Object arr, long size,
11031101 }
11041102
11051103 abstract static class NativeEncoderNode extends CApiBinaryBuiltinNode {
1106- private final Charset charset ;
1104+ private final TruffleString . Encoding encoding ;
11071105
1108- protected NativeEncoderNode (Charset charset ) {
1109- this .charset = charset ;
1106+ protected NativeEncoderNode (TruffleString . Encoding encoding ) {
1107+ this .encoding = encoding ;
11101108 }
11111109
11121110 @ Specialization (guards = "isNoValue(errors)" )
11131111 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 );
11161115 }
11171116
11181117 @ Specialization
11191118 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 ));
11221122 }
11231123
11241124 @ Fallback
@@ -1131,22 +1131,22 @@ static Object doUnicode(@SuppressWarnings("unused") Object s, @SuppressWarnings(
11311131 @ CApiBuiltin (ret = PyObjectTransfer , args = {PyObject , ConstCharPtrAsTruffleString }, call = Direct )
11321132 abstract static class _PyUnicode_AsLatin1String extends NativeEncoderNode {
11331133 protected _PyUnicode_AsLatin1String () {
1134- super (StandardCharsets .ISO_8859_1 );
1134+ super (TruffleString . Encoding .ISO_8859_1 );
11351135 }
11361136 }
11371137
11381138 @ CApiBuiltin (ret = PyObjectTransfer , args = {PyObject , ConstCharPtrAsTruffleString }, call = Direct )
11391139 abstract static class _PyUnicode_AsASCIIString extends NativeEncoderNode {
11401140 protected _PyUnicode_AsASCIIString () {
1141- super (StandardCharsets .US_ASCII );
1141+ super (TruffleString . Encoding .US_ASCII );
11421142 }
11431143 }
11441144
11451145 @ CApiBuiltin (ret = PyObjectTransfer , args = {PyObject , ConstCharPtrAsTruffleString }, call = Direct )
11461146 abstract static class _PyUnicode_AsUTF8String extends NativeEncoderNode {
11471147
11481148 protected _PyUnicode_AsUTF8String () {
1149- super (StandardCharsets .UTF_8 );
1149+ super (TruffleString . Encoding .UTF_8 );
11501150 }
11511151
11521152 @ NeverDefault
@@ -1190,15 +1190,14 @@ abstract static class GraalPyPrivate_Unicode_FillUtf8 extends CApiUnaryBuiltinNo
11901190 @ Specialization
11911191 static Object doNative (PythonAbstractNativeObject s ,
11921192 @ Cached CStructAccess .WriteLongNode writeLongNode ,
1193- @ Cached _PyUnicode_AsUTF8String asUTF8String ,
1194- @ CachedLibrary (limit = "1" ) PythonBufferAccessLibrary bufferLib ,
1193+ @ Cached EncodeNativeStringNode encodeNativeStringNode ,
11951194 @ Cached CStructAccess .WritePointerNode writePointerNode ,
11961195 @ 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 );
12001199 Object mem = allocateNode .alloc (len + 1 , true );
1201- writeByteNode . writeByteArray (mem , bufferLib . getInternalOrCopiedByteArray ( bytes ), len , 0 , 0 );
1200+ writeTruffleStringNode . write (mem , utf8Str , UTF_8 );
12021201 writePointerNode .writeToObj (s , CFields .PyCompactUnicodeObject__utf8 , mem );
12031202 writeLongNode .writeToObject (s , CFields .PyCompactUnicodeObject__utf8_length , len );
12041203 return 0 ;
@@ -1259,15 +1258,13 @@ abstract static class GraalPyPrivate_Unicode_FillUnicode extends CApiUnaryBuilti
12591258 static Object doNative (PythonAbstractNativeObject s ,
12601259 @ Bind Node inliningTarget ,
12611260 @ Cached CastToTruffleStringNode cast ,
1262- @ Cached UnicodeAsWideCharNode asWideCharNode ,
1263- @ CachedLibrary (limit = "1" ) PythonBufferAccessLibrary bufferLib ,
1261+ @ Cached TruffleString .SwitchEncodingNode switchEncodingNode ,
12641262 @ 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 );
12711268 return 0 ;
12721269 }
12731270 }
0 commit comments