| ... | ... | @@ -21,17 +21,6 @@ pub const FormatOptions = struct { |
| 21 | 21 | fill: u8 = ' ', |
| 22 | 22 | }; |
| 23 | 23 | |
| 24 | | fn nextArg(comptime used_pos_args: *u32, comptime maybe_pos_arg: ?comptime_int, comptime next_arg: *comptime_int) comptime_int { |
| 25 | | if (maybe_pos_arg) |pos_arg| { |
| 26 | | used_pos_args.* |= 1 << pos_arg; |
| 27 | | return pos_arg; |
| 28 | | } else { |
| 29 | | const arg = next_arg.*; |
| 30 | | next_arg.* += 1; |
| 31 | | return arg; |
| 32 | | } |
| 33 | | } |
| 34 | | |
| 35 | 24 | fn peekIsAlign(comptime fmt: []const u8) bool { |
| 36 | 25 | // Should only be called during a state transition to the format segment. |
| 37 | 26 | comptime assert(fmt[0] == ':'); |
| ... | ... | @@ -113,12 +102,36 @@ pub fn format( |
| 113 | 102 | |
| 114 | 103 | comptime var start_index = 0; |
| 115 | 104 | comptime var state = State.Start; |
| 116 | | comptime var next_arg = 0; |
| 117 | 105 | comptime var maybe_pos_arg: ?comptime_int = null; |
| 118 | | comptime var used_pos_args: ArgSetType = 0; |
| 119 | 106 | comptime var specifier_start = 0; |
| 120 | 107 | comptime var specifier_end = 0; |
| 121 | 108 | comptime var options = FormatOptions{}; |
| 109 | comptime var arg_state: struct { |
| 110 | next_arg: usize = 0, |
| 111 | used_args: ArgSetType = 0, |
| 112 | args_len: usize = args.len, |
| 113 | |
| 114 | fn hasUnusedArgs(comptime self: *@This()) bool { |
| 115 | return (@popCount(ArgSetType, self.used_args) != self.args_len); |
| 116 | } |
| 117 | |
| 118 | fn nextArg(comptime self: *@This(), comptime pos_arg: ?comptime_int) comptime_int { |
| 119 | const next_idx = pos_arg orelse blk: { |
| 120 | const arg = self.next_arg; |
| 121 | self.next_arg += 1; |
| 122 | break :blk arg; |
| 123 | }; |
| 124 | |
| 125 | if (next_idx >= self.args_len) { |
| 126 | @compileError("Too few arguments"); |
| 127 | } |
| 128 | |
| 129 | // Mark this argument as used |
| 130 | self.used_args |= 1 << next_idx; |
| 131 | |
| 132 | return next_idx; |
| 133 | } |
| 134 | } = .{}; |
| 122 | 135 | |
| 123 | 136 | inline for (fmt) |c, i| { |
| 124 | 137 | switch (state) { |
| ... | ... | @@ -166,11 +179,7 @@ pub fn format( |
| 166 | 179 | } |
| 167 | 180 | }, |
| 168 | 181 | '}' => { |
| 169 | | const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg); |
| 170 | | |
| 171 | | if (arg_to_print >= args.len) { |
| 172 | | @compileError("Too few arguments"); |
| 173 | | } |
| 182 | const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg); |
| 174 | 183 | |
| 175 | 184 | try formatType( |
| 176 | 185 | args[arg_to_print], |
| ... | ... | @@ -203,7 +212,7 @@ pub fn format( |
| 203 | 212 | state = if (comptime peekIsAlign(fmt[i..])) State.FormatFillAndAlign else State.FormatWidth; |
| 204 | 213 | }, |
| 205 | 214 | '}' => { |
| 206 | | const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg); |
| 215 | const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg); |
| 207 | 216 | |
| 208 | 217 | try formatType( |
| 209 | 218 | args[arg_to_print], |
| ... | ... | @@ -250,7 +259,7 @@ pub fn format( |
| 250 | 259 | state = .FormatPrecision; |
| 251 | 260 | }, |
| 252 | 261 | '}' => { |
| 253 | | const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg); |
| 262 | const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg); |
| 254 | 263 | |
| 255 | 264 | try formatType( |
| 256 | 265 | args[arg_to_print], |
| ... | ... | @@ -278,7 +287,7 @@ pub fn format( |
| 278 | 287 | options.precision.? += c - '0'; |
| 279 | 288 | }, |
| 280 | 289 | '}' => { |
| 281 | | const arg_to_print = comptime nextArg(&used_pos_args, maybe_pos_arg, &next_arg); |
| 290 | const arg_to_print = comptime arg_state.nextArg(maybe_pos_arg); |
| 282 | 291 | |
| 283 | 292 | try formatType( |
| 284 | 293 | args[arg_to_print], |
| ... | ... | @@ -299,13 +308,7 @@ pub fn format( |
| 299 | 308 | } |
| 300 | 309 | } |
| 301 | 310 | comptime { |
| 302 | | // All arguments must have been printed but we allow mixing positional and fixed to achieve this. |
| 303 | | var i: usize = 0; |
| 304 | | inline while (i < next_arg) : (i += 1) { |
| 305 | | used_pos_args |= 1 << i; |
| 306 | | } |
| 307 | | |
| 308 | | if (@popCount(ArgSetType, used_pos_args) != args.len) { |
| 311 | if (comptime arg_state.hasUnusedArgs()) { |
| 309 | 312 | @compileError("Unused arguments"); |
| 310 | 313 | } |
| 311 | 314 | if (state != State.Start) { |