@@ -442,27 +442,38 @@ function allocate(size) {
442
442
}
443
443
444
444
function fromStringFast ( string , ops ) {
445
- const length = ops . byteLength ( string ) ;
445
+ const poolLength = Buffer . poolSize >>> 1 ;
446
+ const stringLength = string . length
446
447
447
- if ( length >= ( Buffer . poolSize >>> 1 ) )
448
+ if ( stringLength > poolLength )
449
+ return createFromString ( string , ops . encodingVal ) ;
450
+
451
+ let length = stringLength * 4 ; // max utf8 byte length
452
+
453
+ if ( length >= poolLength )
454
+ length = ops . byteLength ( string ) ;
455
+
456
+ if ( length >= poolLength )
448
457
return createFromString ( string , ops . encodingVal ) ;
449
458
450
459
if ( length > ( poolSize - poolOffset ) )
451
460
createPool ( ) ;
461
+
452
462
let b = new FastBuffer ( allocPool , poolOffset , length ) ;
453
463
const actual = ops . write ( b , string , 0 , length ) ;
454
464
if ( actual !== length ) {
455
- // byteLength() may overestimate. That's a rare case, though.
456
465
b = new FastBuffer ( allocPool , poolOffset , actual ) ;
457
466
}
467
+
458
468
poolOffset += actual ;
459
469
alignPool ( ) ;
470
+
460
471
return b ;
461
472
}
462
473
463
474
function fromString ( string , encoding ) {
464
475
let ops ;
465
- if ( typeof encoding !== 'string' || encoding . length === 0 ) {
476
+ if ( typeof encoding !== 'string' || encoding === 'utf8' || encoding . length === 0 ) {
466
477
if ( string . length === 0 )
467
478
return new FastBuffer ( ) ;
468
479
ops = encodingOps . utf8 ;
0 commit comments