@@ -345,55 +345,27 @@ npy_string_newsize(const char *init, size_t size,
345
345
npy_packed_static_string * to_init ,
346
346
npy_string_allocator * allocator )
347
347
{
348
- if (size > NPY_MAX_STRING_SIZE ) {
348
+ if (npy_string_newemptysize ( size , to_init , allocator ) < 0 ) {
349
349
return -1 ;
350
350
}
351
351
352
- _npy_static_string_u * to_init_u = ((_npy_static_string_u * )to_init );
353
-
354
- unsigned char flags = VSTRING_FLAGS (to_init_u );
355
-
356
352
if (size == 0 ) {
357
- * to_init = * NPY_EMPTY_STRING ;
358
- to_init_u -> direct_buffer .flags_and_size |= flags ;
359
353
return 0 ;
360
354
}
361
355
362
- if (size > NPY_SHORT_STRING_MAX_SIZE ) {
363
- int on_heap = 0 ;
364
- char * ret_buf =
365
- heap_or_arena_allocate (allocator , to_init_u , size , & on_heap );
366
-
367
- if (ret_buf == NULL ) {
368
- return -1 ;
369
- }
370
-
371
- set_vstring_size (to_init_u , size );
356
+ _npy_static_string_u * to_init_u = ((_npy_static_string_u * )to_init );
372
357
373
- memcpy ( ret_buf , init , size ) ;
358
+ char * buf = NULL ;
374
359
375
- if (on_heap ) {
376
- to_init_u -> vstring .offset = (size_t )ret_buf ;
377
- }
378
- else {
379
- npy_string_arena * arena = & allocator -> arena ;
380
- if (arena == NULL ) {
381
- return -1 ;
382
- }
383
- to_init_u -> vstring .offset =
384
- (size_t )ret_buf - (size_t )arena -> buffer ;
385
- }
360
+ if (size > NPY_SHORT_STRING_MAX_SIZE ) {
361
+ buf = vstring_buffer (& allocator -> arena , to_init_u );
386
362
}
387
363
else {
388
- // Size can be no larger than 7 or 15, depending on CPU architecture.
389
- // In either case, the size data is in at most the least significant 4
390
- // bits of the byte so it's safe to | with one of 0x10, 0x20, 0x40, or
391
- // 0x80.
392
- to_init_u -> direct_buffer .flags_and_size =
393
- NPY_STRING_SHORT | flags | size ;
394
- memcpy (& (to_init_u -> direct_buffer .buf ), init , size );
364
+ buf = to_init_u -> direct_buffer .buf ;
395
365
}
396
366
367
+ memcpy (buf , init , size );
368
+
397
369
return 0 ;
398
370
}
399
371
@@ -437,6 +409,10 @@ npy_string_newemptysize(size_t size, npy_packed_static_string *out,
437
409
set_vstring_size (out_u , size );
438
410
}
439
411
else {
412
+ // Size can be no larger than 7 or 15, depending on CPU architecture.
413
+ // In either case, the size data is in at most the least significant 4
414
+ // bits of the byte so it's safe to | with one of 0x10, 0x20, 0x40, or
415
+ // 0x80.
440
416
out_u -> direct_buffer .flags_and_size = NPY_STRING_SHORT | flags | size ;
441
417
}
442
418
0 commit comments