@@ -59,7 +59,6 @@ const {
59
59
compare : _compare ,
60
60
compareOffset,
61
61
copy : _copy ,
62
- createFromString,
63
62
fill : bindingFill ,
64
63
isAscii : bindingIsAscii ,
65
64
isUtf8 : bindingIsUtf8 ,
@@ -442,21 +441,37 @@ function allocate(size) {
442
441
}
443
442
444
443
function fromStringFast ( string , ops ) {
445
- const length = ops . byteLength ( string ) ;
444
+ const maxLength = string . length * 4 ; // max utf8 length
446
445
447
- if ( length >= ( Buffer . poolSize >>> 1 ) )
448
- return createFromString ( string , ops . encodingVal ) ;
446
+ if ( maxLength >= ( Buffer . poolSize >>> 1 ) )
447
+ return fromStringSlow ( string , ops ) ;
449
448
450
- if ( length > ( poolSize - poolOffset ) )
449
+ if ( maxLength > ( poolSize - poolOffset ) )
451
450
createPool ( ) ;
452
- let b = new FastBuffer ( allocPool , poolOffset , length ) ;
451
+
452
+ let b = new FastBuffer ( allocPool , poolOffset , maxLength ) ;
453
+ const actual = ops . write ( b , string , 0 , maxLength ) ;
454
+
455
+ poolOffset += actual ;
456
+ alignPool ( ) ;
457
+
458
+ return new FastBuffer ( allocPool , poolOffset , actual ) ;
459
+ }
460
+
461
+ function fromStringSlow ( string , ops ) {
462
+ const length = ops . byteLength ( string ) ;
463
+
464
+ let b = createUnsafeBuffer ( length ) ;
465
+
453
466
const actual = ops . write ( b , string , 0 , length ) ;
454
467
if ( actual !== length ) {
455
468
// byteLength() may overestimate. That's a rare case, though.
456
469
b = new FastBuffer ( allocPool , poolOffset , actual ) ;
457
470
}
471
+
458
472
poolOffset += actual ;
459
473
alignPool ( ) ;
474
+
460
475
return b ;
461
476
}
462
477
0 commit comments