@@ -374,23 +374,23 @@ export function itoa32(value: i32, radix: i32): String {
374
374
}
375
375
if ( ! value ) return "0" ;
376
376
377
- var sign = value >>> 31 ;
377
+ var sign = ( value >>> 31 ) << 1 ;
378
378
if ( sign ) value = - value ;
379
379
var out : String ;
380
380
381
381
if ( radix == 10 ) {
382
- let decimals = decimalCount32 ( value ) + sign ;
383
- out = changetype < String > ( __new ( decimals << 1 , idof < String > ( ) ) ) ;
384
- utoa32_dec_core ( changetype < usize > ( out ) , value , decimals ) ;
382
+ let decimals = decimalCount32 ( value ) ;
383
+ out = changetype < String > ( __new ( ( decimals << 1 ) + sign , idof < String > ( ) ) ) ;
384
+ utoa32_dec_core ( changetype < usize > ( out ) + sign , value , decimals ) ;
385
385
} else if ( radix == 16 ) {
386
- let decimals = ( 31 - clz ( value ) >> 2 ) + 1 + sign ;
387
- out = changetype < String > ( __new ( decimals << 1 , idof < String > ( ) ) ) ;
388
- utoa32_hex_core ( changetype < usize > ( out ) , value , decimals ) ;
386
+ let decimals = ( 31 - clz ( value ) >> 2 ) + 1 ;
387
+ out = changetype < String > ( __new ( ( decimals << 1 ) + sign , idof < String > ( ) ) ) ;
388
+ utoa32_hex_core ( changetype < usize > ( out ) + sign , value , decimals ) ;
389
389
} else {
390
390
let val32 = u32 ( value ) ;
391
- let decimals = ulog_base ( val32 , radix ) + sign ;
392
- out = changetype < String > ( __new ( decimals << 1 , idof < String > ( ) ) ) ;
393
- utoa64_any_core ( changetype < usize > ( out ) , val32 , decimals , radix ) ;
391
+ let decimals = ulog_base ( val32 , radix ) ;
392
+ out = changetype < String > ( __new ( ( decimals << 1 ) + sign , idof < String > ( ) ) ) ;
393
+ utoa64_any_core ( changetype < usize > ( out ) + sign , val32 , decimals , radix ) ;
394
394
}
395
395
if ( sign ) store < u16 > ( changetype < usize > ( out ) , CharCode . MINUS ) ;
396
396
return out ;
@@ -432,29 +432,29 @@ export function itoa64(value: i64, radix: i32): String {
432
432
}
433
433
if ( ! value ) return "0" ;
434
434
435
- var sign = u32 ( value >>> 63 ) ;
435
+ var sign = u32 ( value >>> 63 ) << 1 ;
436
436
if ( sign ) value = - value ;
437
437
var out : String ;
438
438
439
439
if ( radix == 10 ) {
440
440
if ( < u64 > value <= < u64 > u32 . MAX_VALUE ) {
441
441
let val32 = < u32 > value ;
442
- let decimals = decimalCount32 ( val32 ) + sign ;
443
- out = changetype < String > ( __new ( decimals << 1 , idof < String > ( ) ) ) ;
444
- utoa32_dec_core ( changetype < usize > ( out ) , val32 , decimals ) ;
442
+ let decimals = decimalCount32 ( val32 ) ;
443
+ out = changetype < String > ( __new ( ( decimals << 1 ) + sign , idof < String > ( ) ) ) ;
444
+ utoa32_dec_core ( changetype < usize > ( out ) + sign , val32 , decimals ) ;
445
445
} else {
446
- let decimals = decimalCount64High ( value ) + sign ;
447
- out = changetype < String > ( __new ( decimals << 1 , idof < String > ( ) ) ) ;
448
- utoa64_dec_core ( changetype < usize > ( out ) , value , decimals ) ;
446
+ let decimals = decimalCount64High ( value ) ;
447
+ out = changetype < String > ( __new ( ( decimals << 1 ) + sign , idof < String > ( ) ) ) ;
448
+ utoa64_dec_core ( changetype < usize > ( out ) + sign , value , decimals ) ;
449
449
}
450
450
} else if ( radix == 16 ) {
451
- let decimals = ( 63 - u32 ( clz ( value ) ) >> 2 ) + 1 + sign ;
452
- out = changetype < String > ( __new ( decimals << 1 , idof < String > ( ) ) ) ;
453
- utoa64_hex_core ( changetype < usize > ( out ) , value , decimals ) ;
451
+ let decimals = ( 63 - u32 ( clz ( value ) ) >> 2 ) + 1 ;
452
+ out = changetype < String > ( __new ( ( decimals << 1 ) + sign , idof < String > ( ) ) ) ;
453
+ utoa64_hex_core ( changetype < usize > ( out ) + sign , value , decimals ) ;
454
454
} else {
455
- let decimals = ulog_base ( value , radix ) + sign ;
456
- out = changetype < String > ( __new ( decimals << 1 , idof < String > ( ) ) ) ;
457
- utoa64_any_core ( changetype < usize > ( out ) , value , decimals , radix ) ;
455
+ let decimals = ulog_base ( value , radix ) ;
456
+ out = changetype < String > ( __new ( ( decimals << 1 ) + sign , idof < String > ( ) ) ) ;
457
+ utoa64_any_core ( changetype < usize > ( out ) + sign , value , decimals , radix ) ;
458
458
}
459
459
if ( sign ) store < u16 > ( changetype < usize > ( out ) , CharCode . MINUS ) ;
460
460
return out ;
@@ -748,20 +748,50 @@ export function itoa_buffered<T extends number>(buffer: usize, value: T): u32 {
748
748
if ( isSigned < T > ( ) ) {
749
749
sign = u32 ( value < 0 ) ;
750
750
if ( sign ) {
751
- value = changetype < T > ( - value ) ;
751
+ if ( sizeof < T > ( ) == 1 ) {
752
+ if ( value == - 0x80 ) {
753
+ // -0x80 -> -128
754
+ store < u64 > ( buffer ,
755
+ < u64 > CharCode . MINUS |
756
+ < u64 > ( CharCode . _0 + 1 ) << 16 |
757
+ < u64 > ( CharCode . _0 + 2 ) << 32 |
758
+ < u64 > ( CharCode . _0 + 8 ) << 48
759
+ ) ;
760
+ return 4 ;
761
+ }
762
+ }
763
+ if ( sizeof < T > ( ) == 2 ) {
764
+ if ( value == - 0x8000 ) {
765
+ // -0x8000 -> -32768
766
+ store < u64 > ( buffer ,
767
+ < u64 > CharCode . MINUS |
768
+ < u64 > ( CharCode . _0 + 3 ) << 16 |
769
+ < u64 > ( CharCode . _0 + 2 ) << 32 |
770
+ < u64 > ( CharCode . _0 + 7 ) << 48
771
+ ) ; // -327
772
+ store < u32 > ( buffer + 8 ,
773
+ ( CharCode . _0 + 6 ) << 0 |
774
+ ( CharCode . _0 + 8 ) << 16
775
+ ) ; // 68
776
+ return 6 ;
777
+ }
778
+ }
752
779
store < u16 > ( buffer , CharCode . MINUS ) ;
780
+ // @ts -ignore
781
+ value = - value ;
753
782
}
754
783
}
784
+ var dest = buffer + ( sign << 1 ) ;
755
785
if ( ASC_SHRINK_LEVEL <= 1 ) {
756
786
if ( isSigned < T > ( ) ) {
757
787
if ( sizeof < T > ( ) <= 4 ) {
758
788
if ( < u32 > value < 10 ) {
759
- store < u16 > ( buffer + ( sign << 1 ) , value | CharCode . _0 ) ;
789
+ store < u16 > ( dest , value | CharCode . _0 ) ;
760
790
return 1 + sign ;
761
791
}
762
792
} else {
763
793
if ( < u64 > value < 10 ) {
764
- store < u16 > ( buffer + ( sign << 1 ) , value | CharCode . _0 ) ;
794
+ store < u16 > ( dest , value | CharCode . _0 ) ;
765
795
return 1 + sign ;
766
796
}
767
797
}
@@ -772,21 +802,23 @@ export function itoa_buffered<T extends number>(buffer: usize, value: T): u32 {
772
802
}
773
803
}
774
804
}
775
- var decimals = sign ;
805
+ var decimals : u32 = 0 ;
776
806
if ( sizeof < T > ( ) <= 4 ) {
777
- decimals += decimalCount32 ( value ) ;
778
- utoa32_dec_core ( buffer , value , decimals ) ;
807
+ let val32 = < u32 > value ;
808
+ decimals = decimalCount32 ( val32 ) ;
809
+ utoa32_dec_core ( dest , val32 , decimals ) ;
779
810
} else {
780
811
if ( < u64 > value <= < u64 > u32 . MAX_VALUE ) {
781
812
let val32 = < u32 > value ;
782
- decimals + = decimalCount32 ( val32 ) ;
783
- utoa32_dec_core ( buffer , val32 , decimals ) ;
813
+ decimals = decimalCount32 ( val32 ) ;
814
+ utoa32_dec_core ( dest , val32 , decimals ) ;
784
815
} else {
785
- decimals += decimalCount64High ( value ) ;
786
- utoa64_dec_core ( buffer , value , decimals ) ;
816
+ let val64 = < u64 > value ;
817
+ decimals = decimalCount64High ( val64 ) ;
818
+ utoa64_dec_core ( dest , val64 , decimals ) ;
787
819
}
788
820
}
789
- return decimals ;
821
+ return sign + decimals ;
790
822
}
791
823
792
824
export function dtoa_buffered ( buffer : usize , value : f64 ) : u32 {
0 commit comments