diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-07-05 10:43:14 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-07-07 22:43:53 -0700 |
| commit | 5378fdb153bc76990105e3640e7725e434e8cdee (patch) | |
| tree | 9791e08538c10c521f5634ed79e3b369fcd004df /lib/std/http | |
| parent | 4ccc6f2b5777afd06f0fddbea4e0e0d0c92b007d (diff) | |
| download | zig-5378fdb153bc76990105e3640e7725e434e8cdee.tar.gz zig-5378fdb153bc76990105e3640e7725e434e8cdee.zip | |
std.fmt: fully remove format string from format methods
Introduces `std.fmt.alt` which is a helper for calling alternate format
methods besides one named "format".
Diffstat (limited to 'lib/std/http')
| -rw-r--r-- | lib/std/http/Client.zig | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/lib/std/http/Client.zig b/lib/std/http/Client.zig index 8f4dfe308e..b68f3291ab 100644 --- a/lib/std/http/Client.zig +++ b/lib/std/http/Client.zig @@ -832,7 +832,7 @@ pub const Request = struct { } fn sendAdapted(req: *Request, connection: *Connection, w: *std.io.Writer) !void { - try req.method.format(w, ""); + try req.method.format(w); try w.writeByte(' '); if (req.method == .CONNECT) { @@ -1290,26 +1290,32 @@ pub const basic_authorization = struct { } pub fn valueLengthFromUri(uri: Uri) usize { - var stream = std.io.countingWriter(std.io.null_writer); - try stream.writer().print("{fuser}", .{uri.user orelse Uri.Component.empty}); - const user_len = stream.bytes_written; - stream.bytes_written = 0; - try stream.writer().print("{fpassword}", .{uri.password orelse Uri.Component.empty}); - const password_len = stream.bytes_written; + const user: Uri.Component = uri.user orelse .empty; + const password: Uri.Component = uri.password orelse .empty; + + var w: std.io.Writer = .discarding(&.{}); + user.formatUser(&w) catch unreachable; // discarding + const user_len = w.count; + + w.count = 0; + password.formatPassword(&w) catch unreachable; // discarding + const password_len = w.count; + return valueLength(@intCast(user_len), @intCast(password_len)); } pub fn value(uri: Uri, out: []u8) []u8 { + const user: Uri.Component = uri.user orelse .empty; + const password: Uri.Component = uri.password orelse .empty; + var buf: [max_user_len + ":".len + max_password_len]u8 = undefined; - var stream = std.io.fixedBufferStream(&buf); - stream.writer().print("{fuser}", .{uri.user orelse Uri.Component.empty}) catch - unreachable; - assert(stream.pos <= max_user_len); - stream.writer().print(":{fpassword}", .{uri.password orelse Uri.Component.empty}) catch - unreachable; + var w: std.io.Writer = .fixed(&buf); + user.formatUser(&w) catch unreachable; // fixed + assert(w.count <= max_user_len); + password.formatPassword(&w) catch unreachable; // fixed @memcpy(out[0..prefix.len], prefix); - const base64 = std.base64.standard.Encoder.encode(out[prefix.len..], stream.getWritten()); + const base64 = std.base64.standard.Encoder.encode(out[prefix.len..], w.buffered()); return out[0 .. prefix.len + base64.len]; } }; |
