@@ -17,10 +17,10 @@ import option::none;
17
17
import option:: some;
18
18
19
19
/*
20
- * We have a CT (compile-time) module that parses format strings into a
20
+ * We have a 'ct' (compile-time) module that parses format strings into a
21
21
* sequence of conversions. From those conversions AST fragments are built
22
- * that call into properly-typed functions in the RT (run-time) module. Each
23
- * of those run-time conversion functions accepts another conversion
22
+ * that call into properly-typed functions in the 'rt' (run-time) module.
23
+ * Each of those run-time conversion functions accepts another conversion
24
24
* description that specifies how to format its output.
25
25
*
26
26
* The building of the AST is currently done in a module inside the compiler,
@@ -558,259 +558,6 @@ mod rt {
558
558
}
559
559
}
560
560
561
- // FIXME: This is a temporary duplication of the rt mod that only
562
- // needs to exist to accomodate the stage0 compiler until the next snapshot
563
- mod RT {
564
- tag flag {
565
- flag_left_justify;
566
- flag_left_zero_pad;
567
- flag_space_for_sign;
568
- flag_sign_always;
569
- flag_alternate;
570
- // FIXME: This is a hack to avoid creating 0-length vec exprs,
571
- // which have some difficulty typechecking currently. See
572
- // comments in front::extfmt::make_flags
573
- flag_none;
574
- }
575
-
576
- tag count {
577
- count_is( int) ;
578
- count_implied;
579
- }
580
-
581
- tag ty {
582
- ty_default;
583
- ty_bits;
584
- ty_hex_upper;
585
- ty_hex_lower;
586
- ty_octal;
587
- }
588
-
589
- // FIXME: May not want to use a vector here for flags;
590
- // instead just use a bool per flag
591
- type conv = rec ( vec[ flag] flags ,
592
- count width,
593
- count precision ,
594
- ty ty) ;
595
-
596
- fn conv_int ( & conv cv, int i) -> str {
597
- auto radix = 10 u;
598
- auto prec = get_int_precision ( cv) ;
599
- auto s = int_to_str_prec ( i, radix, prec) ;
600
- if ( 0 <= i) {
601
- if ( have_flag ( cv. flags , flag_sign_always) ) {
602
- s = "+" + s;
603
- } else if ( have_flag ( cv. flags , flag_space_for_sign) ) {
604
- s = " " + s;
605
- }
606
- }
607
- ret pad( cv, s, pad_signed) ;
608
- }
609
-
610
- fn conv_uint ( & conv cv, uint u) -> str {
611
- auto prec = get_int_precision ( cv) ;
612
- auto res;
613
- alt ( cv. ty ) {
614
- case ( ty_default) {
615
- res = uint_to_str_prec ( u, 10 u, prec) ;
616
- }
617
- case ( ty_hex_lower) {
618
- res = uint_to_str_prec ( u, 16 u, prec) ;
619
- }
620
- case ( ty_hex_upper) {
621
- res = _str:: to_upper ( uint_to_str_prec ( u, 16 u, prec) ) ;
622
- }
623
- case ( ty_bits) {
624
- res = uint_to_str_prec ( u, 2 u, prec) ;
625
- }
626
- case ( ty_octal) {
627
- res = uint_to_str_prec ( u, 8 u, prec) ;
628
- }
629
- }
630
- ret pad( cv, res, pad_unsigned) ;
631
- }
632
-
633
- fn conv_bool ( & conv cv, bool b) -> str {
634
- auto s;
635
- if ( b) {
636
- s = "true" ;
637
- } else {
638
- s = "false" ;
639
- }
640
- // run the boolean conversion through the string conversion logic,
641
- // giving it the same rules for precision, etc.
642
- ret conv_str( cv, s) ;
643
- }
644
-
645
- fn conv_char ( & conv cv, char c) -> str {
646
- ret pad ( cv, _str:: from_char ( c) , pad_nozero) ;
647
- }
648
-
649
- fn conv_str ( & conv cv, str s) -> str {
650
- auto unpadded = s;
651
- alt ( cv. precision ) {
652
- case ( count_implied) {
653
- }
654
- case ( count_is ( ?max) ) {
655
- // For strings, precision is the maximum characters displayed
656
- if ( max as uint < _str:: char_len ( s ) ) {
657
- // FIXME: substr works on bytes, not chars!
658
- unpadded = _str:: substr ( s, 0 u, max as uint ) ;
659
- }
660
- }
661
- }
662
- ret pad( cv, unpadded, pad_nozero) ;
663
- }
664
-
665
- // Convert an int to string with minimum number of digits. If precision is
666
- // 0 and num is 0 then the result is the empty string.
667
- fn int_to_str_prec ( int num, uint radix , uint prec) -> str {
668
- if ( num < 0 ) {
669
- ret "-" + uint_to_str_prec((-num) as uint, radix, prec);
670
- } else {
671
- ret uint_to_str_prec(num as uint, radix, prec);
672
- }
673
- }
674
-
675
- // Convert a uint to string with a minimum number of digits. If precision
676
- // is 0 and num is 0 then the result is the empty string. Could move this
677
- // to _uint: but it doesn't seem all that useful.
678
- fn uint_to_str_prec(uint num, uint radix, uint prec) -> str {
679
- auto s;
680
-
681
- if (prec == 0u && num == 0u) {
682
- s = " ";
683
- } else {
684
- s = _uint:: to_str ( num, radix) ;
685
- auto len = _str:: char_len ( s) ;
686
- if ( len < prec) {
687
- auto diff = prec - len;
688
- auto pad = str_init_elt ( '0' , diff) ;
689
- s = pad + s;
690
- }
691
- }
692
-
693
- ret s;
694
- }
695
-
696
- fn get_int_precision ( & conv cv) -> uint {
697
- alt ( cv. precision ) {
698
- case ( count_is ( ?c) ) {
699
- ret c as uint ;
700
- }
701
- case ( count_implied) {
702
- ret 1 u;
703
- }
704
- }
705
- }
706
-
707
- // FIXME: This might be useful in _str: but needs to be utf8 safe first
708
- fn str_init_elt ( char c, uint n_elts ) -> str {
709
- auto svec = _vec:: init_elt[ u8] ( c as u8 , n_elts) ;
710
- // FIXME: Using unsafe_from_bytes because rustboot
711
- // can't figure out the is_utf8 predicate on from_bytes?
712
- ret _str:: unsafe_from_bytes ( svec) ;
713
- }
714
-
715
- tag pad_mode {
716
- pad_signed;
717
- pad_unsigned;
718
- pad_nozero;
719
- }
720
-
721
- fn pad ( & conv cv, str s, pad_mode mode) -> str {
722
- auto uwidth;
723
- alt ( cv. width ) {
724
- case ( count_implied) {
725
- ret s;
726
- }
727
- case ( count_is ( ?width) ) {
728
- // FIXME: Maybe width should be uint
729
- uwidth = width as uint ;
730
- }
731
- }
732
-
733
- auto strlen = _str:: char_len ( s) ;
734
- if ( uwidth <= strlen) {
735
- ret s;
736
- }
737
-
738
- auto padchar = ' ' ;
739
- auto diff = uwidth - strlen;
740
- if ( have_flag ( cv. flags , flag_left_justify) ) {
741
- auto padstr = str_init_elt ( padchar, diff) ;
742
- ret s + padstr;
743
- }
744
-
745
- auto might_zero_pad = false ;
746
- auto signed = false ;
747
-
748
- alt ( mode) {
749
- case ( pad_nozero) {
750
- // fallthrough
751
- }
752
- case ( pad_signed) {
753
- might_zero_pad = true ;
754
- signed = true ;
755
- }
756
- case ( pad_unsigned) {
757
- might_zero_pad = true ;
758
- }
759
- }
760
-
761
- fn have_precision ( & conv cv) -> bool {
762
- alt ( cv. precision ) {
763
- case ( count_implied) {
764
- ret false ;
765
- }
766
- case ( _) {
767
- ret true ;
768
- }
769
- }
770
- }
771
-
772
- auto zero_padding = false ;
773
- if ( might_zero_pad
774
- && have_flag ( cv. flags , flag_left_zero_pad)
775
- && !have_precision ( cv) ) {
776
-
777
- padchar = '0' ;
778
- zero_padding = true ;
779
- }
780
-
781
- auto padstr = str_init_elt ( padchar, diff) ;
782
-
783
- // This is completely heinous. If we have a signed value then
784
- // potentially rip apart the intermediate result and insert some
785
- // zeros. It may make sense to convert zero padding to a precision
786
- // instead.
787
- if ( signed
788
- && zero_padding
789
- && _str:: byte_len ( s) > 0 u) {
790
-
791
- auto head = s. ( 0 ) ;
792
- if ( head == '+' as u8
793
- || head == '-' as u8
794
- || head == ' ' as u8 ) {
795
-
796
- auto headstr = _str:: unsafe_from_bytes ( [ head] ) ;
797
- auto bytelen = _str:: byte_len ( s) ;
798
- auto numpart = _str:: substr ( s, 1 u, bytelen - 1 u) ;
799
- ret headstr + padstr + numpart;
800
- }
801
- }
802
- ret padstr + s;
803
- }
804
-
805
- fn have_flag ( vec[ flag] flags , flag f) -> bool {
806
- for ( flag candidate in flags) {
807
- if ( candidate == f) {
808
- ret true ;
809
- }
810
- }
811
- ret false ;
812
- }
813
- }
814
561
815
562
// Local Variables:
816
563
// mode: rust;
0 commit comments