|
1 | 1 | #[doc(hidden)];
|
| 2 | +// NB: transitionary, de-mode-ing. |
| 3 | +#[forbid(deprecated_mode)]; |
| 4 | +#[forbid(deprecated_pattern)]; |
2 | 5 |
|
3 | 6 | /*
|
4 | 7 | Syntax Extension: fmt
|
@@ -463,202 +466,6 @@ pub mod rt {
|
463 | 466 | }
|
464 | 467 | }
|
465 | 468 |
|
466 |
| -// Remove after snapshot |
467 |
| - |
468 |
| -// Functions used by the fmt extension at runtime. For now there are a lot of |
469 |
| -// decisions made a runtime. If it proves worthwhile then some of these |
470 |
| -// conditions can be evaluated at compile-time. For now though it's cleaner to |
471 |
| -// implement it 0this way, I think. |
472 |
| -pub mod rt2 { |
473 |
| - |
474 |
| - pub const flag_none : u32 = 0u32; |
475 |
| - pub const flag_left_justify : u32 = 0b00000000000001u32; |
476 |
| - pub const flag_left_zero_pad : u32 = 0b00000000000010u32; |
477 |
| - pub const flag_space_for_sign : u32 = 0b00000000000100u32; |
478 |
| - pub const flag_sign_always : u32 = 0b00000000001000u32; |
479 |
| - pub const flag_alternate : u32 = 0b00000000010000u32; |
480 |
| - |
481 |
| - pub enum Count { CountIs(int), CountImplied, } |
482 |
| - pub enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, } |
483 |
| - |
484 |
| - pub type Conv = {flags: u32, width: Count, precision: Count, ty: Ty}; |
485 |
| - |
486 |
| - pub pure fn conv_int(cv: Conv, i: int) -> ~str { |
487 |
| - let radix = 10; |
488 |
| - let prec = get_int_precision(cv); |
489 |
| - let mut s : ~str = int_to_str_prec(i, radix, prec); |
490 |
| - if 0 <= i { |
491 |
| - if have_flag(cv.flags, flag_sign_always) { |
492 |
| - unsafe { str::unshift_char(&mut s, '+') }; |
493 |
| - } else if have_flag(cv.flags, flag_space_for_sign) { |
494 |
| - unsafe { str::unshift_char(&mut s, ' ') }; |
495 |
| - } |
496 |
| - } |
497 |
| - return unsafe { pad(cv, s, PadSigned) }; |
498 |
| - } |
499 |
| - pub pure fn conv_uint(cv: Conv, u: uint) -> ~str { |
500 |
| - let prec = get_int_precision(cv); |
501 |
| - let mut rs = |
502 |
| - match cv.ty { |
503 |
| - TyDefault => uint_to_str_prec(u, 10u, prec), |
504 |
| - TyHexLower => uint_to_str_prec(u, 16u, prec), |
505 |
| - TyHexUpper => str::to_upper(uint_to_str_prec(u, 16u, prec)), |
506 |
| - TyBits => uint_to_str_prec(u, 2u, prec), |
507 |
| - TyOctal => uint_to_str_prec(u, 8u, prec) |
508 |
| - }; |
509 |
| - return unsafe { pad(cv, rs, PadUnsigned) }; |
510 |
| - } |
511 |
| - pub pure fn conv_bool(cv: Conv, b: bool) -> ~str { |
512 |
| - let s = if b { ~"true" } else { ~"false" }; |
513 |
| - // run the boolean conversion through the string conversion logic, |
514 |
| - // giving it the same rules for precision, etc. |
515 |
| - return conv_str(cv, s); |
516 |
| - } |
517 |
| - pub pure fn conv_char(cv: Conv, c: char) -> ~str { |
518 |
| - let mut s = str::from_char(c); |
519 |
| - return unsafe { pad(cv, s, PadNozero) }; |
520 |
| - } |
521 |
| - pub pure fn conv_str(cv: Conv, s: &str) -> ~str { |
522 |
| - // For strings, precision is the maximum characters |
523 |
| - // displayed |
524 |
| - let mut unpadded = match cv.precision { |
525 |
| - CountImplied => s.to_unique(), |
526 |
| - CountIs(max) => if max as uint < str::char_len(s) { |
527 |
| - str::substr(s, 0u, max as uint) |
528 |
| - } else { |
529 |
| - s.to_unique() |
530 |
| - } |
531 |
| - }; |
532 |
| - return unsafe { pad(cv, unpadded, PadNozero) }; |
533 |
| - } |
534 |
| - pub pure fn conv_float(cv: Conv, f: float) -> ~str { |
535 |
| - let (to_str, digits) = match cv.precision { |
536 |
| - CountIs(c) => (float::to_str_exact, c as uint), |
537 |
| - CountImplied => (float::to_str, 6u) |
538 |
| - }; |
539 |
| - let mut s = unsafe { to_str(f, digits) }; |
540 |
| - if 0.0 <= f { |
541 |
| - if have_flag(cv.flags, flag_sign_always) { |
542 |
| - s = ~"+" + s; |
543 |
| - } else if have_flag(cv.flags, flag_space_for_sign) { |
544 |
| - s = ~" " + s; |
545 |
| - } |
546 |
| - } |
547 |
| - return unsafe { pad(cv, s, PadFloat) }; |
548 |
| - } |
549 |
| - pub pure fn conv_poly<T>(cv: Conv, v: &T) -> ~str { |
550 |
| - let s = sys::log_str(v); |
551 |
| - return conv_str(cv, s); |
552 |
| - } |
553 |
| -
|
554 |
| - // Convert an int to string with minimum number of digits. If precision is |
555 |
| - // 0 and num is 0 then the result is the empty string. |
556 |
| - pub pure fn int_to_str_prec(num: int, radix: uint, prec: uint) -> ~str { |
557 |
| - return if num < 0 { |
558 |
| - ~"-" + uint_to_str_prec(-num as uint, radix, prec) |
559 |
| - } else { uint_to_str_prec(num as uint, radix, prec) }; |
560 |
| - } |
561 |
| -
|
562 |
| - // Convert a uint to string with a minimum number of digits. If precision |
563 |
| - // is 0 and num is 0 then the result is the empty string. Could move this |
564 |
| - // to uint: but it doesn't seem all that useful. |
565 |
| - pub pure fn uint_to_str_prec(num: uint, radix: uint, |
566 |
| - prec: uint) -> ~str { |
567 |
| - return if prec == 0u && num == 0u { |
568 |
| - ~"" |
569 |
| - } else { |
570 |
| - let s = uint::to_str(num, radix); |
571 |
| - let len = str::char_len(s); |
572 |
| - if len < prec { |
573 |
| - let diff = prec - len; |
574 |
| - let pad = str::from_chars(vec::from_elem(diff, '0')); |
575 |
| - pad + s |
576 |
| - } else { move s } |
577 |
| - }; |
578 |
| - } |
579 |
| - pub pure fn get_int_precision(cv: Conv) -> uint { |
580 |
| - return match cv.precision { |
581 |
| - CountIs(c) => c as uint, |
582 |
| - CountImplied => 1u |
583 |
| - }; |
584 |
| - } |
585 |
| - |
586 |
| - pub enum PadMode { PadSigned, PadUnsigned, PadNozero, PadFloat } |
587 |
| - |
588 |
| - pub impl PadMode : Eq { |
589 |
| - pure fn eq(other: &PadMode) -> bool { |
590 |
| - match (self, (*other)) { |
591 |
| - (PadSigned, PadSigned) => true, |
592 |
| - (PadUnsigned, PadUnsigned) => true, |
593 |
| - (PadNozero, PadNozero) => true, |
594 |
| - (PadFloat, PadFloat) => true, |
595 |
| - (PadSigned, _) => false, |
596 |
| - (PadUnsigned, _) => false, |
597 |
| - (PadNozero, _) => false, |
598 |
| - (PadFloat, _) => false |
599 |
| - } |
600 |
| - } |
601 |
| - pure fn ne(other: &PadMode) -> bool { !self.eq(other) } |
602 |
| - } |
603 |
| - |
604 |
| - pub fn pad(cv: Conv, s: ~str, mode: PadMode) -> ~str { |
605 |
| - let mut s = move s; // sadtimes |
606 |
| - let uwidth : uint = match cv.width { |
607 |
| - CountImplied => return s, |
608 |
| - CountIs(width) => { |
609 |
| - // FIXME: width should probably be uint (see Issue #1996) |
610 |
| - width as uint |
611 |
| - } |
612 |
| - }; |
613 |
| - let strlen = str::char_len(s); |
614 |
| - if uwidth <= strlen { return s; } |
615 |
| - let mut padchar = ' '; |
616 |
| - let diff = uwidth - strlen; |
617 |
| - if have_flag(cv.flags, flag_left_justify) { |
618 |
| - let padstr = str::from_chars(vec::from_elem(diff, padchar)); |
619 |
| - return s + padstr; |
620 |
| - } |
621 |
| - let {might_zero_pad, signed} = match mode { |
622 |
| - PadNozero => {might_zero_pad:false, signed:false}, |
623 |
| - PadSigned => {might_zero_pad:true, signed:true }, |
624 |
| - PadFloat => {might_zero_pad:true, signed:true}, |
625 |
| - PadUnsigned => {might_zero_pad:true, signed:false} |
626 |
| - }; |
627 |
| - pure fn have_precision(cv: Conv) -> bool { |
628 |
| - return match cv.precision { CountImplied => false, _ => true }; |
629 |
| - } |
630 |
| - let zero_padding = { |
631 |
| - if might_zero_pad && have_flag(cv.flags, flag_left_zero_pad) && |
632 |
| - (!have_precision(cv) || mode == PadFloat) { |
633 |
| - padchar = '0'; |
634 |
| - true |
635 |
| - } else { |
636 |
| - false |
637 |
| - } |
638 |
| - }; |
639 |
| - let padstr = str::from_chars(vec::from_elem(diff, padchar)); |
640 |
| - // This is completely heinous. If we have a signed value then |
641 |
| - // potentially rip apart the intermediate result and insert some |
642 |
| - // zeros. It may make sense to convert zero padding to a precision |
643 |
| - // instead. |
644 |
| - |
645 |
| - if signed && zero_padding && s.len() > 0 { |
646 |
| - let head = str::shift_char(&mut s); |
647 |
| - if head == '+' || head == '-' || head == ' ' { |
648 |
| - let headstr = str::from_chars(vec::from_elem(1u, head)); |
649 |
| - return headstr + padstr + s; |
650 |
| - } |
651 |
| - else { |
652 |
| - str::unshift_char(&mut s, head); |
653 |
| - } |
654 |
| - } |
655 |
| - return padstr + s; |
656 |
| - } |
657 |
| - pub pure fn have_flag(flags: u32, f: u32) -> bool { |
658 |
| - flags & f != 0 |
659 |
| - } |
660 |
| -} |
661 |
| - |
662 | 469 | #[cfg(test)]
|
663 | 470 | mod test {
|
664 | 471 | #[test]
|
|
0 commit comments