zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

poc_print_fn.zig (2560B) - Raw


      1 const Writer = struct {
      2     /// Calls print and then flushes the buffer.
      3     pub fn print(self: *Writer, comptime format: []const u8, args: anytype) anyerror!void {
      4         const State = enum {
      5             start,
      6             open_brace,
      7             close_brace,
      8         };
      9 
     10         comptime var start_index: usize = 0;
     11         comptime var state = State.start;
     12         comptime var next_arg: usize = 0;
     13 
     14         inline for (format, 0..) |c, i| {
     15             switch (state) {
     16                 State.start => switch (c) {
     17                     '{' => {
     18                         if (start_index < i) try self.write(format[start_index..i]);
     19                         state = State.open_brace;
     20                     },
     21                     '}' => {
     22                         if (start_index < i) try self.write(format[start_index..i]);
     23                         state = State.close_brace;
     24                     },
     25                     else => {},
     26                 },
     27                 State.open_brace => switch (c) {
     28                     '{' => {
     29                         state = State.start;
     30                         start_index = i;
     31                     },
     32                     '}' => {
     33                         try self.printValue(args[next_arg]);
     34                         next_arg += 1;
     35                         state = State.start;
     36                         start_index = i + 1;
     37                     },
     38                     's' => {
     39                         continue;
     40                     },
     41                     else => @compileError("Unknown format character: " ++ [1]u8{c}),
     42                 },
     43                 State.close_brace => switch (c) {
     44                     '}' => {
     45                         state = State.start;
     46                         start_index = i;
     47                     },
     48                     else => @compileError("Single '}' encountered in format string"),
     49                 },
     50             }
     51         }
     52         comptime {
     53             if (args.len != next_arg) {
     54                 @compileError("Unused arguments");
     55             }
     56             if (state != State.start) {
     57                 @compileError("Incomplete format string: " ++ format);
     58             }
     59         }
     60         if (start_index < format.len) {
     61             try self.write(format[start_index..format.len]);
     62         }
     63         try self.flush();
     64     }
     65 
     66     fn write(self: *Writer, value: []const u8) !void {
     67         _ = self;
     68         _ = value;
     69     }
     70     pub fn printValue(self: *Writer, value: anytype) !void {
     71         _ = self;
     72         _ = value;
     73     }
     74     fn flush(self: *Writer) !void {
     75         _ = self;
     76     }
     77 };
     78 
     79 // syntax