1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 macro_rules! try_opt {
21 use super::strcursor::StrCursor as Cur;
23 /// Represents a single `printf`-style substitution.
24 #[derive(Clone, Eq, PartialEq, Debug)]
25 pub enum Substitution<'a> {
26 /// A formatted output substitution.
28 /// A literal `%%` escape.
32 impl<'a> Substitution<'a> {
33 pub fn as_str(&self) -> &str {
35 Substitution::Format(ref fmt) => fmt.span,
36 Substitution::Escape => "%%",
40 /// Translate this substitution into an equivalent Rust formatting directive.
42 /// This ignores cases where the substitution does not have an exact equivalent, or where
43 /// the substitution would be unnecessary.
44 pub fn translate(&self) -> Option<String> {
46 Substitution::Format(ref fmt) => fmt.translate(),
47 Substitution::Escape => None,
52 #[derive(Clone, Eq, PartialEq, Debug)]
53 /// A single `printf`-style formatting directive.
54 pub struct Format<'a> {
55 /// The entire original formatting directive.
57 /// The (1-based) parameter to be converted.
58 pub parameter: Option<u16>,
61 /// Minimum width of the output.
62 pub width: Option<Num>,
63 /// Precision of the conversion.
64 pub precision: Option<Num>,
65 /// Length modifier for the conversion.
66 pub length: Option<&'a str>,
67 /// Type of parameter being converted.
72 /// Translate this directive into an equivalent Rust formatting directive.
74 /// Returns `None` in cases where the `printf` directive does not have an exact Rust
75 /// equivalent, rather than guessing.
76 pub fn translate(&self) -> Option<String> {
79 let (c_alt, c_zero, c_left, c_plus) = {
80 let mut c_alt = false;
81 let mut c_zero = false;
82 let mut c_left = false;
83 let mut c_plus = false;
84 for c in self.flags.chars() {
93 (c_alt, c_zero, c_left, c_plus)
96 // Has a special form in Rust for numbers.
97 let fill = if c_zero { Some("0") } else { None };
99 let align = if c_left { Some("<") } else { None };
101 // Rust doesn't have an equivalent to the `' '` flag.
102 let sign = if c_plus { Some("+") } else { None };
104 // Not *quite* the same, depending on the type...
107 let width = match self.width {
109 // NOTE: Rust doesn't support this.
112 w @ Some(Num::Arg(_)) => w,
113 w @ Some(Num::Num(_)) => w,
117 let precision = self.precision;
119 // NOTE: although length *can* have an effect, we can't duplicate the effect in Rust, so
120 // we just ignore it.
122 let (type_, use_zero_fill, is_int) = match self.type_ {
123 "d" | "i" | "u" => (None, true, true),
124 "f" | "F" => (None, false, false),
125 "s" | "c" => (None, false, false),
126 "e" | "E" => (Some(self.type_), true, false),
127 "x" | "X" | "o" => (Some(self.type_), true, true),
128 "p" => (Some(self.type_), false, true),
129 "g" => (Some("e"), true, false),
130 "G" => (Some("E"), true, false),
134 let (fill, width, precision) = match (is_int, width, precision) {
135 (true, Some(_), Some(_)) => {
136 // Rust can't duplicate this insanity.
139 (true, None, Some(p)) => (Some("0"), Some(p), None),
140 (true, w, None) => (fill, w, None),
141 (false, w, p) => (fill, w, p),
144 let align = match (self.type_, width.is_some(), align.is_some()) {
145 ("s", true, false) => Some(">"),
149 let (fill, zero_fill) = match (fill, use_zero_fill) {
150 (Some("0"), true) => (None, true),
151 (fill, _) => (fill, false),
154 let alt = match type_ {
155 Some("x") | Some("X") => alt,
159 let has_options = fill.is_some()
165 || precision.is_some()
169 // Initialise with a rough guess.
170 let cap = self.span.len() + if has_options { 2 } else { 0 };
171 let mut s = String::with_capacity(cap);
175 if let Some(arg) = self.parameter {
176 try_opt!(write!(s, "{}", try_opt!(arg.checked_sub(1))).ok());
182 let align = if let Some(fill) = fill {
189 if let Some(align) = align {
193 if let Some(sign) = sign {
205 if let Some(width) = width {
206 try_opt!(width.translate(&mut s).ok());
209 if let Some(precision) = precision {
211 try_opt!(precision.translate(&mut s).ok());
214 if let Some(type_) = type_ {
224 /// A general number used in a `printf` formatting directive.
225 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
227 // The range of these values is technically bounded by `NL_ARGMAX`... but, at least for GNU
228 // libc, it apparently has no real fixed limit. A `u16` is used here on the basis that it
229 // is *vanishingly* unlikely that *anyone* is going to try formatting something wider, or
230 // with more precision, than 32 thousand positions which is so wide it couldn't possibly fit
233 /// A specific, fixed value.
235 /// The value is derived from a positional argument.
237 /// The value is derived from the "next" unconverted argument.
242 fn from_str(s: &str, arg: Option<&str>) -> Self {
243 if let Some(arg) = arg {
244 Num::Arg(arg.parse().expect(&format!("invalid format arg `{:?}`", arg)))
248 Num::Num(s.parse().expect(&format!("invalid format num `{:?}`", s)))
252 fn translate(&self, s: &mut String) -> ::std::fmt::Result {
255 Num::Num(n) => write!(s, "{}", n),
257 let n = try!(n.checked_sub(1).ok_or(::std::fmt::Error));
260 Num::Next => write!(s, "*"),
265 /// Returns an iterator over all substitutions in a given string.
266 pub fn iter_subs(s: &str) -> Substitutions {
272 /// Iterator over substitutions in a string.
273 pub struct Substitutions<'a> {
277 impl<'a> Iterator for Substitutions<'a> {
278 type Item = Substitution<'a>;
279 fn next(&mut self) -> Option<Self::Item> {
280 match parse_next_substitution(self.s) {
281 Some((sub, tail)) => {
301 /// Parse the next substitution from the input string.
302 pub fn parse_next_substitution(s: &str) -> Option<(Substitution, &str)> {
306 let start = try_opt!(s.find('%'));
307 match s[start+1..].chars().next() {
308 Some('%') => return Some((Substitution::Escape, &s[start+2..])),
309 Some(_) => {/* fall-through */},
313 Cur::new_at_start(&s[start..])
316 // This is meant to be a translation of the following regex:
321 // (?: (?P<parameter> \d+) \$ )?
322 // (?P<flags> [-+ 0\#']* )
323 // (?P<width> \d+ | \* (?: (?P<widtha> \d+) \$ )? )?
324 // (?: \. (?P<precision> \d+ | \* (?: (?P<precisiona> \d+) \$ )? ) )?
327 // hh | h | ll | l | L | z | j | t
330 // | I32 | I64 | I | q
335 // Used to establish the full span at the end.
337 // The current position within the string.
338 let mut at = try_opt!(at.at_next_cp());
339 // `c` is the next codepoint, `next` is a cursor after it.
340 let (mut c, mut next) = try_opt!(at.next_cp());
342 // Update `at`, `c`, and `next`, exiting if we're out of input.
343 macro_rules! move_to {
347 let (c_, next_) = try_opt!(at.next_cp());
354 // Constructs a result when parsing fails.
356 // Note: `move` used to capture copies of the cursors as they are *now*.
357 let fallback = move || {
359 Substitution::Format(Format {
360 span: start.slice_between(next).unwrap(),
366 type_: at.slice_between(next).unwrap(),
372 // Next parsing state.
373 let mut state = Start;
375 // Sadly, Rust isn't *quite* smart enough to know these *must* be initialised by the end.
376 let mut parameter: Option<u16> = None;
377 let mut flags: &str = "";
378 let mut width: Option<Num> = None;
379 let mut precision: Option<Num> = None;
380 let mut length: Option<&str> = None;
381 let mut type_: &str = "";
384 if let Start = state {
387 let end = at_next_cp_while(next, is_digit);
388 match end.next_cp() {
389 // Yes, this *is* the parameter.
390 Some(('$', end2)) => {
392 parameter = Some(at.slice_between(end).unwrap().parse().unwrap());
395 // Wait, no, actually, it's the width.
400 width = Some(Num::from_str(at.slice_between(end).unwrap(), None));
403 // It's invalid, is what it is.
404 None => return fallback(),
415 if let Flags = state {
416 let end = at_next_cp_while(at, is_flag);
418 flags = at.slice_between(end).unwrap();
422 if let Width = state {
429 let end = at_next_cp_while(next, is_digit);
431 width = Some(Num::from_str(at.slice_between(end).unwrap(), None));
442 if let WidthArg = state {
443 let end = at_next_cp_while(at, is_digit);
444 match end.next_cp() {
445 Some(('$', end2)) => {
447 width = Some(Num::from_str("", Some(at.slice_between(end).unwrap())));
452 width = Some(Num::Next);
458 if let Prec = state {
472 if let PrecInner = state {
475 let end = at_next_cp_while(next, is_digit);
476 match end.next_cp() {
477 Some(('$', end2)) => {
479 precision = Some(Num::from_str("*", next.slice_between(end)));
484 precision = Some(Num::Next);
490 let end = at_next_cp_while(next, is_digit);
492 precision = Some(Num::from_str(at.slice_between(end).unwrap(), None));
495 _ => return fallback(),
499 if let Length = state {
500 let c1_next1 = next.next_cp();
501 match (c, c1_next1) {
502 ('h', Some(('h', next1)))
503 | ('l', Some(('l', next1)))
506 length = Some(at.slice_between(next1).unwrap());
510 ('h', _) | ('l', _) | ('L', _)
511 | ('z', _) | ('j', _) | ('t', _)
515 length = Some(at.slice_between(next).unwrap());
520 let end = next.at_next_cp()
521 .and_then(|end| end.at_next_cp())
522 .map(|end| (next.slice_between(end).unwrap(), end));
523 let end = match end {
524 Some(("32", end)) => end,
525 Some(("64", end)) => end,
529 length = Some(at.slice_between(end).unwrap());
541 if let Type = state {
543 type_ = at.slice_between(next).unwrap();
545 // Don't use `move_to!` here, as we *can* be at the end of the input.
555 span: start.slice_between(end).unwrap(),
556 parameter: parameter,
559 precision: precision,
563 Some((Substitution::Format(f), end.slice_after()))
566 fn at_next_cp_while<F>(mut cur: Cur, mut pred: F) -> Cur
567 where F: FnMut(char) -> bool {
569 match cur.next_cp() {
570 Some((c, next)) => if pred(c) {
580 fn is_digit(c: char) -> bool {
587 fn is_flag(c: char) -> bool {
589 '0' | '-' | '+' | ' ' | '#' | '\'' => true,
601 parse_next_substitution as pns,
604 macro_rules! assert_eq_pnsat {
605 ($lhs:expr, $rhs:expr) => {
607 pns($lhs).and_then(|(s, _)| s.translate()),
608 $rhs.map(<String as From<&str>>::from)
615 assert_eq!(pns("has no escapes"), None);
616 assert_eq!(pns("has no escapes, either %"), None);
617 assert_eq!(pns("*so* has a %% escape"), Some((S::Escape," escape")));
618 assert_eq!(pns("%% leading escape"), Some((S::Escape, " leading escape")));
619 assert_eq!(pns("trailing escape %%"), Some((S::Escape, "")));
624 macro_rules! assert_pns_eq_sub {
626 $param:expr, $flags:expr,
627 $width:expr, $prec:expr, $len:expr, $type_:expr,
630 pns(concat!($in_, "!")),
647 assert_pns_eq_sub!("%!",
648 { None, "", None, None, None, "!", });
649 assert_pns_eq_sub!("%c",
650 { None, "", None, None, None, "c", });
651 assert_pns_eq_sub!("%s",
652 { None, "", None, None, None, "s", });
653 assert_pns_eq_sub!("%06d",
654 { None, "0", Some(N::Num(6)), None, None, "d", });
655 assert_pns_eq_sub!("%4.2f",
656 { None, "", Some(N::Num(4)), Some(N::Num(2)), None, "f", });
657 assert_pns_eq_sub!("%#x",
658 { None, "#", None, None, None, "x", });
659 assert_pns_eq_sub!("%-10s",
660 { None, "-", Some(N::Num(10)), None, None, "s", });
661 assert_pns_eq_sub!("%*s",
662 { None, "", Some(N::Next), None, None, "s", });
663 assert_pns_eq_sub!("%-10.*s",
664 { None, "-", Some(N::Num(10)), Some(N::Next), None, "s", });
665 assert_pns_eq_sub!("%-*.*s",
666 { None, "-", Some(N::Next), Some(N::Next), None, "s", });
667 assert_pns_eq_sub!("%.6i",
668 { None, "", None, Some(N::Num(6)), None, "i", });
669 assert_pns_eq_sub!("%+i",
670 { None, "+", None, None, None, "i", });
671 assert_pns_eq_sub!("%08X",
672 { None, "0", Some(N::Num(8)), None, None, "X", });
673 assert_pns_eq_sub!("%lu",
674 { None, "", None, None, Some("l"), "u", });
675 assert_pns_eq_sub!("%Iu",
676 { None, "", None, None, Some("I"), "u", });
677 assert_pns_eq_sub!("%I32u",
678 { None, "", None, None, Some("I32"), "u", });
679 assert_pns_eq_sub!("%I64u",
680 { None, "", None, None, Some("I64"), "u", });
681 assert_pns_eq_sub!("%'d",
682 { None, "'", None, None, None, "d", });
683 assert_pns_eq_sub!("%10s",
684 { None, "", Some(N::Num(10)), None, None, "s", });
685 assert_pns_eq_sub!("%-10.10s",
686 { None, "-", Some(N::Num(10)), Some(N::Num(10)), None, "s", });
687 assert_pns_eq_sub!("%1$d",
688 { Some(1), "", None, None, None, "d", });
689 assert_pns_eq_sub!("%2$.*3$d",
690 { Some(2), "", None, Some(N::Arg(3)), None, "d", });
691 assert_pns_eq_sub!("%1$*2$.*3$d",
692 { Some(1), "", Some(N::Arg(2)), Some(N::Arg(3)), None, "d", });
693 assert_pns_eq_sub!("%-8ld",
694 { None, "-", Some(N::Num(8)), None, Some("l"), "d", });
699 let s = "The %d'th word %% is: `%.*s` %!\n";
700 let subs: Vec<_> = iter_subs(s).map(|sub| sub.translate()).collect();
702 subs.iter().map(|ms| ms.as_ref().map(|s| &s[..])).collect::<Vec<_>>(),
703 vec![Some("{}"), None, Some("{:.*}"), None]
707 /// Check that the translations are what we expect.
710 assert_eq_pnsat!("%c", Some("{}"));
711 assert_eq_pnsat!("%d", Some("{}"));
712 assert_eq_pnsat!("%u", Some("{}"));
713 assert_eq_pnsat!("%x", Some("{:x}"));
714 assert_eq_pnsat!("%X", Some("{:X}"));
715 assert_eq_pnsat!("%e", Some("{:e}"));
716 assert_eq_pnsat!("%E", Some("{:E}"));
717 assert_eq_pnsat!("%f", Some("{}"));
718 assert_eq_pnsat!("%g", Some("{:e}"));
719 assert_eq_pnsat!("%G", Some("{:E}"));
720 assert_eq_pnsat!("%s", Some("{}"));
721 assert_eq_pnsat!("%p", Some("{:p}"));
723 assert_eq_pnsat!("%06d", Some("{:06}"));
724 assert_eq_pnsat!("%4.2f", Some("{:4.2}"));
725 assert_eq_pnsat!("%#x", Some("{:#x}"));
726 assert_eq_pnsat!("%-10s", Some("{:<10}"));
727 assert_eq_pnsat!("%*s", None);
728 assert_eq_pnsat!("%-10.*s", Some("{:<10.*}"));
729 assert_eq_pnsat!("%-*.*s", None);
730 assert_eq_pnsat!("%.6i", Some("{:06}"));
731 assert_eq_pnsat!("%+i", Some("{:+}"));
732 assert_eq_pnsat!("%08X", Some("{:08X}"));
733 assert_eq_pnsat!("%lu", Some("{}"));
734 assert_eq_pnsat!("%Iu", Some("{}"));
735 assert_eq_pnsat!("%I32u", Some("{}"));
736 assert_eq_pnsat!("%I64u", Some("{}"));
737 assert_eq_pnsat!("%'d", None);
738 assert_eq_pnsat!("%10s", Some("{:>10}"));
739 assert_eq_pnsat!("%-10.10s", Some("{:<10.10}"));
740 assert_eq_pnsat!("%1$d", Some("{0}"));
741 assert_eq_pnsat!("%2$.*3$d", Some("{1:02$}"));
742 assert_eq_pnsat!("%1$*2$.*3$s", Some("{0:>1$.2$}"));
743 assert_eq_pnsat!("%-8ld", Some("{:<8}"));
749 use super::strcursor::StrCursor as Cur;
751 #[derive(Clone, Eq, PartialEq, Debug)]
752 pub enum Substitution<'a> {
758 impl<'a> Substitution<'a> {
759 pub fn as_str(&self) -> String {
761 Substitution::Ordinal(n) => format!("${}", n),
762 Substitution::Name(n) => format!("${}", n),
763 Substitution::Escape => "$$".into(),
767 pub fn translate(&self) -> Option<String> {
769 Substitution::Ordinal(n) => Some(format!("{{{}}}", n)),
770 Substitution::Name(n) => Some(format!("{{{}}}", n)),
771 Substitution::Escape => None,
776 /// Returns an iterator over all substitutions in a given string.
777 pub fn iter_subs(s: &str) -> Substitutions {
783 /// Iterator over substitutions in a string.
784 pub struct Substitutions<'a> {
788 impl<'a> Iterator for Substitutions<'a> {
789 type Item = Substitution<'a>;
790 fn next(&mut self) -> Option<Self::Item> {
791 match parse_next_substitution(self.s) {
792 Some((sub, tail)) => {
801 /// Parse the next substitution from the input string.
802 pub fn parse_next_substitution(s: &str) -> Option<(Substitution, &str)> {
804 let start = try_opt!(s.find('$'));
805 match s[start+1..].chars().next() {
806 Some('$') => return Some((Substitution::Escape, &s[start+2..])),
807 Some(c @ '0' ... '9') => {
808 let n = (c as u8) - b'0';
809 return Some((Substitution::Ordinal(n), &s[start+2..]));
811 Some(_) => {/* fall-through */},
815 Cur::new_at_start(&s[start..])
818 let at = try_opt!(at.at_next_cp());
820 Some((c, inner)) => {
821 if !is_ident_head(c) {
824 let end = at_next_cp_while(inner, is_ident_tail);
825 Some((Substitution::Name(at.slice_between(end).unwrap()), end.slice_after()))
832 fn at_next_cp_while<F>(mut cur: Cur, mut pred: F) -> Cur
833 where F: FnMut(char) -> bool {
835 match cur.next_cp() {
836 Some((c, next)) => if pred(c) {
846 fn is_ident_head(c: char) -> bool {
848 'a' ... 'z' | 'A' ... 'Z' | '_' => true,
853 fn is_ident_tail(c: char) -> bool {
856 c => is_ident_head(c)
864 parse_next_substitution as pns,
867 macro_rules! assert_eq_pnsat {
868 ($lhs:expr, $rhs:expr) => {
870 pns($lhs).and_then(|(f, _)| f.translate()),
871 $rhs.map(<String as From<&str>>::from)
878 assert_eq!(pns("has no escapes"), None);
879 assert_eq!(pns("has no escapes, either $"), None);
880 assert_eq!(pns("*so* has a $$ escape"), Some((S::Escape, " escape")));
881 assert_eq!(pns("$$ leading escape"), Some((S::Escape, " leading escape")));
882 assert_eq!(pns("trailing escape $$"), Some((S::Escape, "")));
887 macro_rules! assert_pns_eq_sub {
888 ($in_:expr, $kind:ident($arg:expr)) => {
889 assert_eq!(pns(concat!($in_, "!")), Some((S::$kind($arg.into()), "!")))
893 assert_pns_eq_sub!("$0", Ordinal(0));
894 assert_pns_eq_sub!("$1", Ordinal(1));
895 assert_pns_eq_sub!("$9", Ordinal(9));
896 assert_pns_eq_sub!("$N", Name("N"));
897 assert_pns_eq_sub!("$NAME", Name("NAME"));
902 use super::iter_subs;
903 let s = "The $0'th word $$ is: `$WORD` $!\n";
904 let subs: Vec<_> = iter_subs(s).map(|sub| sub.translate()).collect();
906 subs.iter().map(|ms| ms.as_ref().map(|s| &s[..])).collect::<Vec<_>>(),
907 vec![Some("{0}"), None, Some("{WORD}")]
913 assert_eq_pnsat!("$0", Some("{0}"));
914 assert_eq_pnsat!("$9", Some("{9}"));
915 assert_eq_pnsat!("$1", Some("{1}"));
916 assert_eq_pnsat!("$10", Some("{1}"));
917 assert_eq_pnsat!("$stuff", Some("{stuff}"));
918 assert_eq_pnsat!("$NAME", Some("{NAME}"));
919 assert_eq_pnsat!("$PREFIX/bin", Some("{PREFIX}"));
928 pub struct StrCursor<'a> {
933 impl<'a> StrCursor<'a> {
934 pub fn new_at_start(s: &'a str) -> StrCursor<'a> {
941 pub fn at_next_cp(mut self) -> Option<StrCursor<'a>> {
942 match self.try_seek_right_cp() {
948 pub fn next_cp(mut self) -> Option<(char, StrCursor<'a>)> {
949 let cp = match self.cp_after() {
953 self.seek_right(cp.len_utf8());
957 fn slice_before(&self) -> &'a str {
961 pub fn slice_after(&self) -> &'a str {
965 pub fn slice_between(&self, until: StrCursor<'a>) -> Option<&'a str> {
966 if !str_eq_literal(self.s, until.s) {
969 use std::cmp::{max, min};
970 let beg = min(self.at, until.at);
971 let end = max(self.at, until.at);
972 Some(&self.s[beg..end])
976 fn cp_after(&self) -> Option<char> {
977 self.slice_after().chars().next()
980 fn try_seek_right_cp(&mut self) -> bool {
981 match self.slice_after().chars().next() {
983 self.at += c.len_utf8();
990 fn seek_right(&mut self, bytes: usize) {
995 impl<'a> Copy for StrCursor<'a> {}
997 impl<'a> Clone for StrCursor<'a> {
998 fn clone(&self) -> StrCursor<'a> {
1003 impl<'a> std::fmt::Debug for StrCursor<'a> {
1004 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
1005 write!(fmt, "StrCursor({:?} | {:?})", self.slice_before(), self.slice_after())
1009 fn str_eq_literal(a: &str, b: &str) -> bool {
1010 a.as_bytes().as_ptr() == b.as_bytes().as_ptr()
1011 && a.len() == b.len()