| ... | @@ -200,35 +200,42 @@ test "variadic functions" { | ... | @@ -200,35 +200,42 @@ test "variadic functions" { |
| 200 | if (builtin.cpu.arch.isSPARC() and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/23718 | 200 | if (builtin.cpu.arch.isSPARC() and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // https://github.com/ziglang/zig/issues/23718 |
| 201 | | 201 | |
| 202 | const S = struct { | 202 | const S = struct { |
| 203 | fn printf(list_ptr: *std.array_list.Managed(u8), format: [*:0]const u8, ...) callconv(.c) void { | 203 | fn printf(buffer: [*]u8, format: [*:0]const u8, ...) callconv(.c) void { |
| 204 | var ap = @cVaStart(); | 204 | var ap = @cVaStart(); |
| 205 | defer @cVaEnd(&ap); | 205 | defer @cVaEnd(&ap); |
| 206 | vprintf(list_ptr, format, &ap); | 206 | vprintf(buffer, format, &ap); |
| 207 | } | 207 | } |
| 208 | | 208 | |
| 209 | fn vprintf( | 209 | fn vprintf(buffer: [*]u8, format: [*:0]const u8, ap: *std.builtin.VaList) callconv(.c) void { |
| 210 | list: *std.array_list.Managed(u8), | 210 | var i: usize = 0; |
| 211 | format: [*:0]const u8, | 211 | for (format[0..3]) |byte| switch (byte) { |
| 212 | ap: *std.builtin.VaList, | | |
| 213 | ) callconv(.c) void { | | |
| 214 | for (std.mem.span(format)) |c| switch (c) { | | |
| 215 | 's' => { | 212 | 's' => { |
| 216 | const arg = @cVaArg(ap, [*:0]const u8); | 213 | const arg = @cVaArg(ap, [*:0]const u8); |
| 217 | list.writer().print("{s}", .{arg}) catch return; | 214 | buffer[i..][0..5].* = arg[0..5].*; |
| | 215 | i += 5; |
| 218 | }, | 216 | }, |
| 219 | 'd' => { | 217 | 'd' => { |
| 220 | const arg = @cVaArg(ap, c_int); | 218 | const arg = @cVaArg(ap, c_int); |
| 221 | list.writer().print("{d}", .{arg}) catch return; | 219 | switch (arg) { |
| | 220 | 1 => { |
| | 221 | buffer[i] = '1'; |
| | 222 | i += 1; |
| | 223 | }, |
| | 224 | 5 => { |
| | 225 | buffer[i] = '5'; |
| | 226 | i += 1; |
| | 227 | }, |
| | 228 | else => unreachable, |
| | 229 | } |
| 222 | }, | 230 | }, |
| 223 | else => unreachable, | 231 | else => unreachable, |
| 224 | }; | 232 | }; |
| 225 | } | 233 | } |
| 226 | }; | 234 | }; |
| 227 | | 235 | |
| 228 | var list = std.array_list.Managed(u8).init(std.testing.allocator); | 236 | var buffer: [7]u8 = undefined; |
| 229 | defer list.deinit(); | 237 | S.printf(&buffer, "dsd", @as(c_int, 1), @as([*:0]const u8, "hello"), @as(c_int, 5)); |
| 230 | S.printf(&list, "dsd", @as(c_int, 1), @as([*:0]const u8, "hello"), @as(c_int, 5)); | 238 | try expect(std.mem.eql(u8, &buffer, "1hello5")); |
| 231 | try std.testing.expectEqualStrings("1hello5", list.items); | | |
| 232 | } | 239 | } |
| 233 | | 240 | |
| 234 | test "copy VaList" { | 241 | test "copy VaList" { |