@@ -374,23 +374,23 @@ export function itoa32(value: i32, radix: i32): String {
374374 }
375375 if ( ! value ) return "0" ;
376376
377- var sign = value >>> 31 ;
377+ var sign = ( value >>> 31 ) << 1 ;
378378 if ( sign ) value = - value ;
379379 var out : String ;
380380
381381 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 ) ;
385385 } 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 ) ;
389389 } else {
390390 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 ) ;
394394 }
395395 if ( sign ) store < u16 > ( changetype < usize > ( out ) , CharCode . MINUS ) ;
396396 return out ;
@@ -432,29 +432,29 @@ export function itoa64(value: i64, radix: i32): String {
432432 }
433433 if ( ! value ) return "0" ;
434434
435- var sign = u32 ( value >>> 63 ) ;
435+ var sign = u32 ( value >>> 63 ) << 1 ;
436436 if ( sign ) value = - value ;
437437 var out : String ;
438438
439439 if ( radix == 10 ) {
440440 if ( < u64 > value <= < u64 > u32 . MAX_VALUE ) {
441441 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 ) ;
445445 } 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 ) ;
449449 }
450450 } 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 ) ;
454454 } 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 ) ;
458458 }
459459 if ( sign ) store < u16 > ( changetype < usize > ( out ) , CharCode . MINUS ) ;
460460 return out ;
@@ -748,20 +748,50 @@ export function itoa_buffered<T extends number>(buffer: usize, value: T): u32 {
748748 if ( isSigned < T > ( ) ) {
749749 sign = u32 ( value < 0 ) ;
750750 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+ }
752779 store < u16 > ( buffer , CharCode . MINUS ) ;
780+ // @ts -ignore
781+ value = - value ;
753782 }
754783 }
784+ var dest = buffer + ( sign << 1 ) ;
755785 if ( ASC_SHRINK_LEVEL <= 1 ) {
756786 if ( isSigned < T > ( ) ) {
757787 if ( sizeof < T > ( ) <= 4 ) {
758788 if ( < u32 > value < 10 ) {
759- store < u16 > ( buffer + ( sign << 1 ) , value | CharCode . _0 ) ;
789+ store < u16 > ( dest , value | CharCode . _0 ) ;
760790 return 1 + sign ;
761791 }
762792 } else {
763793 if ( < u64 > value < 10 ) {
764- store < u16 > ( buffer + ( sign << 1 ) , value | CharCode . _0 ) ;
794+ store < u16 > ( dest , value | CharCode . _0 ) ;
765795 return 1 + sign ;
766796 }
767797 }
@@ -772,21 +802,23 @@ export function itoa_buffered<T extends number>(buffer: usize, value: T): u32 {
772802 }
773803 }
774804 }
775- var decimals = sign ;
805+ var decimals : u32 = 0 ;
776806 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 ) ;
779810 } else {
780811 if ( < u64 > value <= < u64 > u32 . MAX_VALUE ) {
781812 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 ) ;
784815 } 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 ) ;
787819 }
788820 }
789- return decimals ;
821+ return sign + decimals ;
790822}
791823
792824export function dtoa_buffered ( buffer : usize , value : f64 ) : u32 {
0 commit comments