@@ -62,6 +62,12 @@ public class StdLibTest extends NativeTestHelper {
62
62
63
63
final static Charset nativeCharset = Charset .forName (System .getProperty ("native.encoding" ));
64
64
65
+ static MemorySegment allocateSegmentForString (Arena arena , String str ) {
66
+ // Allocate a memory segment using the native-encoded bytes.
67
+ byte [] nativeBytes = str .getBytes (nativeCharset );
68
+ return arena .allocateFrom (ValueLayout .JAVA_BYTE , Arrays .copyOf (nativeBytes , nativeBytes .length + 1 ));
69
+ }
70
+
65
71
private StdLibHelper stdLibHelper = new StdLibHelper ();
66
72
67
73
@ Test (dataProvider = "stringPairs" )
@@ -315,7 +321,7 @@ int rand() throws Throwable {
315
321
316
322
int printf (String format , List <PrintfArg > args ) throws Throwable {
317
323
try (var arena = Arena .ofConfined ()) {
318
- MemorySegment formatStr = arena . allocateFrom ( format , nativeCharset );
324
+ MemorySegment formatStr = allocateSegmentForString ( arena , format );
319
325
return (int )specializedPrintf (args ).invokeExact (formatStr ,
320
326
args .stream ().map (a -> a .nativeValue (arena )).toArray ());
321
327
}
@@ -396,7 +402,7 @@ enum PrintfArg {
396
402
INT (int .class , C_INT , "%d" , "%d" , arena -> 42 , 42 ),
397
403
LONG (long .class , C_LONG_LONG , "%lld" , "%d" , arena -> 84L , 84L ),
398
404
DOUBLE (double .class , C_DOUBLE , "%.4f" , "%.4f" , arena -> 1.2345d , 1.2345d ),
399
- STRING (MemorySegment .class , C_POINTER , "%s" , "%s" , arena -> arena . allocateFrom ( "str" , nativeCharset ), "str" );
405
+ STRING (MemorySegment .class , C_POINTER , "%s" , "%s" , arena -> allocateSegmentForString ( arena , "str" ), "str" );
400
406
401
407
final Class <?> carrier ;
402
408
final ValueLayout layout ;
0 commit comments