| 1 | //! Parse printf format strings |
| 2 | |
| 3 | use craterust-analyzer cargo sprintf 0.4.2 crate/::{PrintfErrorrust-analyzer cargo sprintf 0.4.2 PrintfError#, Resultrust-analyzer cargo sprintf 0.4.2 Result#}; |
| 4 | |
| 5 | /// A part of a format string: either a string of characters to be included |
| 6 | /// verbatim, or a format specifier that should be replaced based on an argument |
| 7 | /// to the [vsprintf](crate::vsprintf) call. |
| 8 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 9 | pub enum FormatElementrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#<'a> { |
| 10 | /// Some characters that are copied to the output as-is |
| 11 | Verbatimrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#Verbatim#(&'a str), |
| 12 | /// A format specifier |
| 13 | Formatrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#Format#(ConversionSpecifierrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#), |
| 14 | } |
| 15 | |
| 16 | /// Parsed printf conversion specifier |
| 17 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 18 | pub struct ConversionSpecifierrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier# { |
| 19 | /// flag `#`: use `0x`, etc? |
| 20 | pub alt_formrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#alt_form.: bool, |
| 21 | /// flag `0`: left-pad with zeros? |
| 22 | pub zero_padrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#zero_pad.: bool, |
| 23 | /// flag `-`: left-adjust (pad with spaces on the right) |
| 24 | pub left_adjrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#left_adj.: bool, |
| 25 | /// flag `' '` (space): indicate sign with a space? |
| 26 | pub space_signrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#space_sign.: bool, |
| 27 | /// flag `+`: Always show sign? (for signed numbers) |
| 28 | pub force_signrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#force_sign.: bool, |
| 29 | /// field width |
| 30 | pub widthrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#width.: NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#, |
| 31 | /// floating point field precision |
| 32 | pub precisionrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#precision.: NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#, |
| 33 | /// data type |
| 34 | pub conversion_typerust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#conversion_type.: ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#, |
| 35 | } |
| 36 | |
| 37 | /// Width / precision parameter |
| 38 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 39 | pub enum NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam# { |
| 40 | /// The literal width |
| 41 | Literalrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#Literal#(i32), |
| 42 | /// Get the width from the previous argument |
| 43 | /// |
| 44 | /// This should never be passed to [Printf::format()][crate::Printf::format()]. |
| 45 | FromArgumentrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#FromArgument#, |
| 46 | } |
| 47 | |
| 48 | /// Printf data type |
| 49 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 50 | pub enum ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType# { |
| 51 | /// `d`, `i`, or `u` |
| 52 | DecIntrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#DecInt#, |
| 53 | /// `o` |
| 54 | OctIntrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#OctInt#, |
| 55 | /// `x` or `p` |
| 56 | HexIntLowerrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#HexIntLower#, |
| 57 | /// `X` |
| 58 | HexIntUpperrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#HexIntUpper#, |
| 59 | /// `e` |
| 60 | SciFloatLowerrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#SciFloatLower#, |
| 61 | /// `E` |
| 62 | SciFloatUpperrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#SciFloatUpper#, |
| 63 | /// `f` |
| 64 | DecFloatLowerrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#DecFloatLower#, |
| 65 | /// `F` |
| 66 | DecFloatUpperrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#DecFloatUpper#, |
| 67 | /// `g` |
| 68 | CompactFloatLowerrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#CompactFloatLower#, |
| 69 | /// `G` |
| 70 | CompactFloatUpperrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#CompactFloatUpper#, |
| 71 | /// `c` |
| 72 | Charrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#Char#, |
| 73 | /// `s` |
| 74 | Stringrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#String#, |
| 75 | /// `%` |
| 76 | PercentSignrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#PercentSign#, |
| 77 | } |
| 78 | |
| 79 | /// Parses a string to a vector of [FormatElement] |
| 80 | /// |
| 81 | /// Takes a printf-style format string `fmt` |
| 82 | /// |
| 83 | /// use sprintf::parser::{ |
| 84 | /// parse_format_string, ConversionSpecifier, ConversionType, FormatElement, NumericParam, |
| 85 | /// }; |
| 86 | /// let fmt = "Hello %#06x"; |
| 87 | /// let parsed = parse_format_string(fmt).unwrap(); |
| 88 | /// assert_eq!(parsed[0], FormatElement::Verbatim("Hello ")); |
| 89 | /// assert_eq!( |
| 90 | /// parsed[1], |
| 91 | /// FormatElement::Format(ConversionSpecifier { |
| 92 | /// alt_form: true, |
| 93 | /// zero_pad: true, |
| 94 | /// left_adj: false, |
| 95 | /// space_sign: false, |
| 96 | /// force_sign: false, |
| 97 | /// width: NumericParam::Literal(6), |
| 98 | /// precision: NumericParam::Literal(6), |
| 99 | /// conversion_type: ConversionType::HexIntLower, |
| 100 | /// }) |
| 101 | /// ); |
| 102 | /// |
| 103 | pub fn parse_format_stringrust-analyzer cargo sprintf 0.4.2 parser/parse_format_string().(fmt: &str) -> Resultrust-analyzer cargo sprintf 0.4.2 Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<FormatElementrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#>> { |
| 104 | // find the first % |
| 105 | let mut res = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 106 | |
| 107 | let mut rem = fmt; |
| 108 | |
| 109 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().rem.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 110 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((verbatim_prefix, rest)) = rem.split_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_once().('%') { |
| 111 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().verbatim_prefix.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 112 | res.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(FormatElementrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#::Verbatimrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#Verbatim#(verbatim_prefix)); |
| 113 | } |
| 114 | let (spec, rest) = take_conversion_specifierrust-analyzer cargo sprintf 0.4.2 parser/take_conversion_specifier().(rest)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 115 | res.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(FormatElementrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#::Formatrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#Format#(spec)); |
| 116 | rem = rest; |
| 117 | } else { |
| 118 | res.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(FormatElementrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#::Verbatimrust-analyzer cargo sprintf 0.4.2 parser/FormatElement#Verbatim#(rem)); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(res) |
| 124 | } |
| 125 | |
| 126 | fn take_conversion_specifierrust-analyzer cargo sprintf 0.4.2 parser/take_conversion_specifier().(s: &str) -> Resultrust-analyzer cargo sprintf 0.4.2 Result#<(ConversionSpecifierrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#, &str)> { |
| 127 | let mut spec = ConversionSpecifierrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier# { |
| 128 | alt_formrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#alt_form.: false, |
| 129 | zero_padrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#zero_pad.: false, |
| 130 | left_adjrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#left_adj.: false, |
| 131 | space_signrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#space_sign.: false, |
| 132 | force_signrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#force_sign.: false, |
| 133 | widthrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#width.: NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#::Literalrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#Literal#(0), |
| 134 | precisionrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#precision.: NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#::FromArgumentrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#FromArgument#, // Placeholder - must not be returned! |
| 135 | // ignore length modifier |
| 136 | conversion_typerust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#conversion_type.: ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::DecIntrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#DecInt#, |
| 137 | }; |
| 138 | |
| 139 | let mut s = s; |
| 140 | |
| 141 | // parse flags |
| 142 | loop { |
| 143 | match s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() { |
| 144 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('#') => { |
| 145 | spec.alt_formrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#alt_form. = true; |
| 146 | } |
| 147 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('0') => { |
| 148 | spec.zero_padrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#zero_pad. = true; |
| 149 | } |
| 150 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('-') => { |
| 151 | spec.left_adjrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#left_adj. = true; |
| 152 | } |
| 153 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(' ') => { |
| 154 | spec.space_signrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#space_sign. = true; |
| 155 | } |
| 156 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('+') => { |
| 157 | spec.force_signrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#force_sign. = true; |
| 158 | } |
| 159 | _ => { |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | s = &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 164 | } |
| 165 | // parse width |
| 166 | let (w, mut s) = take_numeric_paramrust-analyzer cargo sprintf 0.4.2 parser/take_numeric_param().(s); |
| 167 | spec.widthrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#width. = w; |
| 168 | // parse precision |
| 169 | if matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().(), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('.')) { |
| 170 | s = &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 171 | let (p, s2) = take_numeric_paramrust-analyzer cargo sprintf 0.4.2 parser/take_numeric_param().(s); |
| 172 | spec.precisionrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#precision. = p; |
| 173 | s = s2; |
| 174 | } |
| 175 | // check length specifier |
| 176 | for len_spec in ["hh", "h", "ll", "l", "q", "L", "j", "z", "Z", "t"] { |
| 177 | if s.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(len_spec) { |
| 178 | s = s.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().(len_spec).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(PrintfErrorrust-analyzer cargo sprintf 0.4.2 PrintfError#::ParseErrorrust-analyzer cargo sprintf 0.4.2 PrintfError#ParseError#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 179 | break; // only allow one length specifier |
| 180 | } |
| 181 | } |
| 182 | // parse conversion type |
| 183 | spec.conversion_typerust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#conversion_type. = match s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() { |
| 184 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('i') | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('d') | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('u') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::DecIntrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#DecInt#, |
| 185 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('o') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::OctIntrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#OctInt#, |
| 186 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('x') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::HexIntLowerrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#HexIntLower#, |
| 187 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('X') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::HexIntUpperrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#HexIntUpper#, |
| 188 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('e') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::SciFloatLowerrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#SciFloatLower#, |
| 189 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('E') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::SciFloatUpperrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#SciFloatUpper#, |
| 190 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('f') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::DecFloatLowerrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#DecFloatLower#, |
| 191 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('F') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::DecFloatUpperrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#DecFloatUpper#, |
| 192 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('g') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::CompactFloatLowerrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#CompactFloatLower#, |
| 193 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('G') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::CompactFloatUpperrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#CompactFloatUpper#, |
| 194 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('c') | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('C') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::Charrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#Char#, |
| 195 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('s') | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('S') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::Stringrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#String#, |
| 196 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('p') => { |
| 197 | spec.alt_formrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#alt_form. = true; |
| 198 | ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::HexIntLowerrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#HexIntLower# |
| 199 | } |
| 200 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('%') => ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::PercentSignrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#PercentSign#, |
| 201 | _ => { |
| 202 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PrintfErrorrust-analyzer cargo sprintf 0.4.2 PrintfError#::ParseErrorrust-analyzer cargo sprintf 0.4.2 PrintfError#ParseError#); |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | if spec.precisionrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#precision. rust-analyzer cargo sprintf 0.4.2 parser/impl#[NumericParam][`PartialEq<Self>`]eq().==rust-analyzer cargo sprintf 0.4.2 parser/impl#[NumericParam][`PartialEq<Self>`]eq(). rust-analyzer cargo sprintf 0.4.2 parser/impl#[NumericParam][`PartialEq<Self>`]eq().NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#::FromArgumentrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#FromArgument# { |
| 207 | // If precision is not specified, set to default value |
| 208 | let p = if spec.conversion_typerust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#conversion_type. rust-analyzer cargo sprintf 0.4.2 parser/impl#[ConversionType][`PartialEq<Self>`]eq().==rust-analyzer cargo sprintf 0.4.2 parser/impl#[ConversionType][`PartialEq<Self>`]eq(). rust-analyzer cargo sprintf 0.4.2 parser/impl#[ConversionType][`PartialEq<Self>`]eq().ConversionTyperust-analyzer cargo sprintf 0.4.2 parser/ConversionType#::Stringrust-analyzer cargo sprintf 0.4.2 parser/ConversionType#String# { |
| 209 | // Default to max limit (aka no limit) for strings |
| 210 | i32::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]MAX. |
| 211 | } else { |
| 212 | // Default to 6 for all other types |
| 213 | 6 |
| 214 | }; |
| 215 | spec.precisionrust-analyzer cargo sprintf 0.4.2 parser/ConversionSpecifier#precision. = NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#::Literalrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#Literal#(p); |
| 216 | } |
| 217 | |
| 218 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((spec, &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)) |
| 219 | } |
| 220 | |
| 221 | fn take_numeric_paramrust-analyzer cargo sprintf 0.4.2 parser/take_numeric_param().(s: &str) -> (NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#, &str) { |
| 222 | match s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() { |
| 223 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#('*') => (NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#::FromArgumentrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#FromArgument#, &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().), |
| 224 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(digit) if ('1'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'9').containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]contains().(&digit) => { |
| 225 | let mut s = s; |
| 226 | let mut w = 0; |
| 227 | loop { |
| 228 | match s.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().() { |
| 229 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(digit) if ('0'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#'9').containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]contains().(&digit) => { |
| 230 | w = 10 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Mul<Self>`]mul().w rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Add<Self>`]add().(digit as i32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`Sub<Self>`]sub().'0' as i32); |
| 231 | } |
| 232 | _ => { |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | s = &s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 237 | } |
| 238 | (NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#::Literalrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#Literal#(w), s) |
| 239 | } |
| 240 | _ => (NumericParamrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#::Literalrust-analyzer cargo sprintf 0.4.2 parser/NumericParam#Literal#(0), s), |
| 241 | } |
| 242 | } |