| ... | ... | @@ -8,6 +8,7 @@ const math = std.math; |
| 8 | 8 | const assert = std.debug.assert; |
| 9 | 9 | const mem = std.mem; |
| 10 | 10 | const unicode = std.unicode; |
| 11 | const meta = std.meta; |
| 11 | 12 | const builtin = @import("builtin"); |
| 12 | 13 | const errol = @import("fmt/errol.zig"); |
| 13 | 14 | const lossyCast = std.math.lossyCast; |
| ... | ... | @@ -84,43 +85,48 @@ pub fn format( |
| 84 | 85 | args: anytype, |
| 85 | 86 | ) !void { |
| 86 | 87 | const ArgSetType = u32; |
| 87 | | if (@typeInfo(@TypeOf(args)) != .Struct) { |
| 88 | | @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args))); |
| 88 | |
| 89 | const ArgsType = @TypeOf(args); |
| 90 | // XXX: meta.trait.is(.Struct)(ArgsType) doesn't seem to work... |
| 91 | if (@typeInfo(ArgsType) != .Struct) { |
| 92 | @compileError("Expected tuple or struct argument, found " ++ @typeName(ArgsType)); |
| 89 | 93 | } |
| 90 | | if (args.len > @typeInfo(ArgSetType).Int.bits) { |
| 94 | |
| 95 | const fields_info = meta.fields(ArgsType); |
| 96 | if (fields_info.len > @typeInfo(ArgSetType).Int.bits) { |
| 91 | 97 | @compileError("32 arguments max are supported per format call"); |
| 92 | 98 | } |
| 93 | 99 | |
| 94 | 100 | comptime var arg_state: struct { |
| 95 | 101 | next_arg: usize = 0, |
| 96 | | used_args: ArgSetType = 0, |
| 97 | | args_len: usize = args.len, |
| 102 | used_args: usize = 0, |
| 103 | args_len: usize = fields_info.len, |
| 98 | 104 | |
| 99 | 105 | fn hasUnusedArgs(comptime self: *@This()) bool { |
| 100 | | return (@popCount(ArgSetType, self.used_args) != self.args_len); |
| 106 | return @popCount(ArgSetType, self.used_args) != self.args_len; |
| 101 | 107 | } |
| 102 | 108 | |
| 103 | | fn nextArg(comptime self: *@This(), comptime pos_arg: ?usize) comptime_int { |
| 104 | | const next_idx = pos_arg orelse blk: { |
| 109 | fn nextArg(comptime self: *@This(), comptime arg_index: ?usize) comptime_int { |
| 110 | const next_index = arg_index orelse init: { |
| 105 | 111 | const arg = self.next_arg; |
| 106 | 112 | self.next_arg += 1; |
| 107 | | break :blk arg; |
| 113 | break :init arg; |
| 108 | 114 | }; |
| 109 | 115 | |
| 110 | | if (next_idx >= self.args_len) { |
| 116 | if (next_index >= self.args_len) { |
| 111 | 117 | @compileError("Too few arguments"); |
| 112 | 118 | } |
| 113 | 119 | |
| 114 | 120 | // Mark this argument as used |
| 115 | | self.used_args |= 1 << next_idx; |
| 121 | self.used_args |= 1 << next_index; |
| 116 | 122 | |
| 117 | | return next_idx; |
| 123 | return next_index; |
| 118 | 124 | } |
| 119 | 125 | } = .{}; |
| 120 | 126 | |
| 121 | 127 | comptime var parser: struct { |
| 122 | 128 | buf: []const u8 = undefined, |
| 123 | | pos: usize = 0, |
| 129 | pos: comptime_int = 0, |
| 124 | 130 | |
| 125 | 131 | // Returns a decimal number or null if the current character is not a |
| 126 | 132 | // digit |
| ... | ... | @@ -165,13 +171,21 @@ pub fn format( |
| 165 | 171 | return null; |
| 166 | 172 | } |
| 167 | 173 | |
| 174 | fn maybe(comptime self: *@This(), comptime val: u8) bool { |
| 175 | if (self.pos < self.buf.len and self.buf[self.pos] == val) { |
| 176 | self.pos += 1; |
| 177 | return true; |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | |
| 168 | 182 | // Returns the n-th next character or null if that's past the end |
| 169 | 183 | fn peek(comptime self: *@This(), comptime n: usize) ?u8 { |
| 170 | 184 | return if (self.pos + n < self.buf.len) self.buf[self.pos + n] else null; |
| 171 | 185 | } |
| 172 | 186 | } = .{}; |
| 173 | 187 | |
| 174 | | comptime var options: FormatOptions = .{}; |
| 188 | var options: FormatOptions = .{}; |
| 175 | 189 | |
| 176 | 190 | @setEvalBranchQuota(2000000); |
| 177 | 191 | |
| ... | ... | @@ -209,7 +223,7 @@ pub fn format( |
| 209 | 223 | if (i >= fmt.len) break; |
| 210 | 224 | |
| 211 | 225 | if (fmt[i] == '}') { |
| 212 | | @compileError("missing opening {"); |
| 226 | @compileError("Missing opening {"); |
| 213 | 227 | } |
| 214 | 228 | |
| 215 | 229 | // Get past the { |
| ... | ... | @@ -222,7 +236,7 @@ pub fn format( |
| 222 | 236 | comptime const fmt_end = i; |
| 223 | 237 | |
| 224 | 238 | if (i >= fmt.len) { |
| 225 | | @compileError("missing closing }"); |
| 239 | @compileError("Missing closing }"); |
| 226 | 240 | } |
| 227 | 241 | |
| 228 | 242 | // Get past the } |
| ... | ... | @@ -236,15 +250,29 @@ pub fn format( |
| 236 | 250 | parser.pos = 0; |
| 237 | 251 | |
| 238 | 252 | // Parse the positional argument number |
| 239 | | comptime var opt_pos_arg = comptime parser.number(); |
| 253 | comptime const opt_pos_arg = init: { |
| 254 | if (comptime parser.maybe('[')) { |
| 255 | comptime const arg_name = parser.until(']'); |
| 256 | |
| 257 | if (!comptime parser.maybe(']')) { |
| 258 | @compileError("Expected closing ]"); |
| 259 | } |
| 260 | |
| 261 | break :init comptime meta.fieldIndex(ArgsType, arg_name) orelse |
| 262 | @compileError("No argument with name '" ++ arg_name ++ "'"); |
| 263 | } else { |
| 264 | break :init comptime parser.number(); |
| 265 | } |
| 266 | }; |
| 240 | 267 | |
| 241 | 268 | // Parse the format specifier |
| 242 | | comptime var specifier_arg = comptime parser.until(':'); |
| 269 | comptime const specifier_arg = comptime parser.until(':'); |
| 243 | 270 | |
| 244 | 271 | // Skip the colon, if present |
| 245 | 272 | if (comptime parser.char()) |ch| { |
| 246 | | if (ch != ':') |
| 247 | | @compileError("expected : or }, found '" ++ [1]u8{ch} ++ "'"); |
| 273 | if (ch != ':') { |
| 274 | @compileError("Expected : or }, found '" ++ [1]u8{ch} ++ "'"); |
| 275 | } |
| 248 | 276 | } |
| 249 | 277 | |
| 250 | 278 | // Parse the fill character |
| ... | ... | @@ -276,26 +304,57 @@ pub fn format( |
| 276 | 304 | } |
| 277 | 305 | |
| 278 | 306 | // Parse the width parameter |
| 279 | | comptime var opt_width_arg = comptime parser.number(); |
| 280 | | options.width = opt_width_arg; |
| 307 | options.width = init: { |
| 308 | if (comptime parser.maybe('[')) { |
| 309 | comptime const arg_name = parser.until(']'); |
| 310 | |
| 311 | if (!comptime parser.maybe(']')) { |
| 312 | @compileError("Expected closing ]"); |
| 313 | } |
| 314 | |
| 315 | comptime const index = meta.fieldIndex(ArgsType, arg_name) orelse |
| 316 | @compileError("No argument with name '" ++ arg_name ++ "'"); |
| 317 | const arg_index = comptime arg_state.nextArg(index); |
| 318 | |
| 319 | break :init @field(args, fields_info[arg_index].name); |
| 320 | } else { |
| 321 | break :init comptime parser.number(); |
| 322 | } |
| 323 | }; |
| 281 | 324 | |
| 282 | 325 | // Skip the dot, if present |
| 283 | 326 | if (comptime parser.char()) |ch| { |
| 284 | | if (ch != '.') |
| 285 | | @compileError("expected . or }, found '" ++ [1]u8{ch} ++ "'"); |
| 327 | if (ch != '.') { |
| 328 | @compileError("Expected . or }, found '" ++ [1]u8{ch} ++ "'"); |
| 329 | } |
| 286 | 330 | } |
| 287 | 331 | |
| 288 | 332 | // Parse the precision parameter |
| 289 | | comptime var opt_precision_arg = comptime parser.number(); |
| 290 | | options.precision = opt_precision_arg; |
| 333 | options.precision = init: { |
| 334 | if (comptime parser.maybe('[')) { |
| 335 | comptime const arg_name = parser.until(']'); |
| 336 | |
| 337 | if (!comptime parser.maybe(']')) { |
| 338 | @compileError("Expected closing ]"); |
| 339 | } |
| 340 | |
| 341 | comptime const arg_i = meta.fieldIndex(ArgsType, arg_name) orelse |
| 342 | @compileError("No argument with name '" ++ arg_name ++ "'"); |
| 343 | const arg_to_use = comptime arg_state.nextArg(arg_i); |
| 344 | |
| 345 | break :init @field(args, fields_info[arg_to_use].name); |
| 346 | } else { |
| 347 | break :init comptime parser.number(); |
| 348 | } |
| 349 | }; |
| 291 | 350 | |
| 292 | 351 | if (comptime parser.char()) |ch| { |
| 293 | | @compileError("extraneous trailing character '" ++ [1]u8{ch} ++ "'"); |
| 352 | @compileError("Extraneous trailing character '" ++ [1]u8{ch} ++ "'"); |
| 294 | 353 | } |
| 295 | 354 | |
| 296 | 355 | const arg_to_print = comptime arg_state.nextArg(opt_pos_arg); |
| 297 | 356 | try formatType( |
| 298 | | args[arg_to_print], |
| 357 | @field(args, fields_info[arg_to_print].name), |
| 299 | 358 | specifier_arg, |
| 300 | 359 | options, |
| 301 | 360 | writer, |
| ... | ... | @@ -1992,3 +2051,22 @@ test "null" { |
| 1992 | 2051 | const inst = null; |
| 1993 | 2052 | try testFmt("null", "{}", .{inst}); |
| 1994 | 2053 | } |
| 2054 | |
| 2055 | test "named arguments" { |
| 2056 | try testFmt("hello world!", "{} world{c}", .{ "hello", '!' }); |
| 2057 | try testFmt("hello world!", "{[greeting]} world{[punctuation]c}", .{ .punctuation = '!', .greeting = "hello" }); |
| 2058 | try testFmt("hello world!", "{[1]} world{[0]c}", .{ '!', "hello" }); |
| 2059 | } |
| 2060 | |
| 2061 | test "runtime width specifier" { |
| 2062 | var width: usize = 9; |
| 2063 | try testFmt("~~hello~~", "{:~^[1]}", .{ "hello", width }); |
| 2064 | try testFmt("~~hello~~", "{:~^[width]}", .{ .string = "hello", .width = width }); |
| 2065 | } |
| 2066 | |
| 2067 | test "runtime precision specifier" { |
| 2068 | var number: f32 = 3.1415; |
| 2069 | var precision: usize = 2; |
| 2070 | try testFmt("3.14e+00", "{:1.[1]}", .{ number, precision }); |
| 2071 | try testFmt("3.14e+00", "{:1.[precision]}", .{ .number = number, .precision = precision }); |
| 2072 | } |