50
50
import com .oracle .graal .python .builtins .objects .cext .CExtNodes .ToSulongNode ;
51
51
import com .oracle .graal .python .builtins .objects .cext .NativeCAPISymbols ;
52
52
import com .oracle .graal .python .builtins .objects .cext .PythonNativeObject ;
53
+ import com .oracle .graal .python .builtins .objects .cext .common .CExtCommonNodes .UnicodeFromWcharNode ;
53
54
import com .oracle .graal .python .builtins .objects .common .SequenceNodes ;
54
55
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes ;
55
56
import com .oracle .graal .python .builtins .objects .ints .PInt ;
@@ -111,10 +112,10 @@ static String doMaterializedNative(PString x) {
111
112
112
113
@ Specialization (guards = {"isNativeCharSequence(x)" }, replaces = "doMaterializedNative" )
113
114
static String doNative (PString x ,
114
- @ Cached PCallCapiFunction callCStringToStringNode ) {
115
+ @ Cached PCallCapiFunction callCStringToStringNode ,
116
+ @ Cached UnicodeFromWcharNode fromWcharNode ) {
115
117
// cast guaranteed by the guard
116
- NativeCharSequence nativeCharSequence = (NativeCharSequence ) x .getCharSequence ();
117
- String materialized = (String ) callCStringToStringNode .call (NativeCAPISymbols .FUN_PY_TRUFFLE_CSTR_TO_STRING , nativeCharSequence .getPtr ());
118
+ String materialized = materializeNativeCharSequence ((NativeCharSequence ) x .getCharSequence (), callCStringToStringNode , fromWcharNode );
118
119
x .setCharSequence (materialized );
119
120
return materialized ;
120
121
}
@@ -132,6 +133,29 @@ static String doMaterialized(PString x) {
132
133
// cast guaranteed by the guard
133
134
return (String ) x .getCharSequence ();
134
135
}
136
+
137
+ public static String materializeNativeCharSequence (NativeCharSequence nativeCharSequence ,
138
+ PCallCapiFunction callCStringToStringNode ,
139
+ UnicodeFromWcharNode fromWcharNode ) {
140
+ // cast guaranteed by the guard
141
+ String materialized ;
142
+ if (nativeCharSequence .isAsciiOnly ()) {
143
+ materialized = (String ) callCStringToStringNode .call (NativeCAPISymbols .FUN_PY_TRUFFLE_ASCII_TO_STRING , nativeCharSequence .getPtr ());
144
+ } else {
145
+ switch (nativeCharSequence .getElementSize ()) {
146
+ case 1 :
147
+ materialized = (String ) callCStringToStringNode .call (NativeCAPISymbols .FUN_PY_TRUFFLE_CSTR_TO_STRING , nativeCharSequence .getPtr ());
148
+ break ;
149
+ case 2 :
150
+ case 4 :
151
+ materialized = fromWcharNode .execute (nativeCharSequence .getPtr (), nativeCharSequence .getElementSize ());
152
+ break ;
153
+ default :
154
+ throw CompilerDirectives .shouldNotReachHere ("illegal element size" );
155
+ }
156
+ }
157
+ return materialized ;
158
+ }
135
159
}
136
160
137
161
@ GenerateUncached
0 commit comments