mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-05-04 08:42:42 +03:00
fdac89d6cd
In preparation for its removal as accepted in https://github.com/ziglang/zig/issues/24738.
98 lines
3.6 KiB
Zig
98 lines
3.6 KiB
Zig
// zig run this file inside the test_parsing/ directory of this repo: https://github.com/nst/JSONTestSuite
|
|
|
|
const std = @import("std");
|
|
const Io = std.Io;
|
|
|
|
pub fn main(init: std.process.Init) !void {
|
|
const allocator = init.arena.allocator();
|
|
const io = init.io;
|
|
|
|
var stdout_buffer: [2000]u8 = undefined;
|
|
var stdout_writer = Io.File.stdout().writerStreaming(io, &stdout_buffer);
|
|
const output = &stdout_writer.interface;
|
|
try output.writeAll(
|
|
\\//! This file was generated by _generate_JSONTestSuite.zig
|
|
\\//! These test cases are sourced from: https://github.com/nst/JSONTestSuite
|
|
\\const ok = @import("./test.zig").ok;
|
|
\\const err = @import("./test.zig").err;
|
|
\\const any = @import("./test.zig").any;
|
|
\\
|
|
\\
|
|
);
|
|
|
|
var names = std.array_list.Managed([]const u8).init(allocator);
|
|
var cwd = try Io.Dir.cwd().openDir(io, ".", .{ .iterate = true });
|
|
var it = cwd.iterate();
|
|
while (try it.next(io)) |entry| {
|
|
try names.append(try allocator.dupe(u8, entry.name));
|
|
}
|
|
std.mem.sort([]const u8, names.items, {}, (struct {
|
|
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
|
|
return std.mem.lessThan(u8, a, b);
|
|
}
|
|
}).lessThan);
|
|
|
|
for (names.items) |name| {
|
|
const contents = try Io.Dir.cwd().readFileAlloc(io, name, allocator, .limited(300000));
|
|
try output.writeAll("test ");
|
|
try writeString(output, name);
|
|
try output.writeAll(" {\n try ");
|
|
switch (name[0]) {
|
|
'y' => try output.writeAll("ok"),
|
|
'n' => try output.writeAll("err"),
|
|
'i' => try output.writeAll("any"),
|
|
else => unreachable,
|
|
}
|
|
try output.writeByte('(');
|
|
try writeString(output, contents);
|
|
try output.writeAll(");\n}\n");
|
|
}
|
|
|
|
try output.flush();
|
|
}
|
|
|
|
const i_structure_500_nested_arrays = &(@as([500]u8, @splat('[')) ++ @as([500]u8, @splat(']')));
|
|
const n_structure_100000_opening_arrays: *const [100000]u8 = &@splat('[');
|
|
const n_structure_open_array_object = str: {
|
|
const part = "[{\"\":";
|
|
const buf: [50000][part.len]u8 = @splat(part.*);
|
|
const s: []const u8 = @ptrCast(&buf);
|
|
break :str s ++ "\n";
|
|
};
|
|
|
|
fn writeString(writer: anytype, s: []const u8) !void {
|
|
if (s.len > 200) {
|
|
// There are a few of these we can compress with Zig expressions.
|
|
if (std.mem.eql(u8, s, i_structure_500_nested_arrays)) {
|
|
return writer.writeAll("&@as([500]u8, @splat('[')) ++ &@as([500]u8, @splat(']'))");
|
|
} else if (std.mem.eql(u8, s, n_structure_100000_opening_arrays)) {
|
|
return writer.writeAll("&@as([100000]u8, @splat('['))");
|
|
} else if (std.mem.eql(u8, s, n_structure_open_array_object)) {
|
|
return writer.writeAll(
|
|
\\str: {
|
|
\\ const part = "[{\"\":";
|
|
\\ const buf: [50000][part.len]u8 = @splat(part.*);
|
|
\\ const s: []const u8 = @ptrCast(&buf);
|
|
\\ break :str s ++ "\n";
|
|
\\ }
|
|
);
|
|
} else {
|
|
@panic("unhandled long string literal");
|
|
}
|
|
}
|
|
try writer.writeByte('"');
|
|
for (s) |b| {
|
|
switch (b) {
|
|
0...('\n' - 1),
|
|
('\n' + 1)...0x1f,
|
|
0x7f...0xff,
|
|
=> try writer.print("\\x{x:0>2}", .{b}),
|
|
'\n' => try writer.writeAll("\\n"),
|
|
'"' => try writer.writeAll("\\\""),
|
|
'\\' => try writer.writeAll("\\\\"),
|
|
else => try writer.writeByte(b),
|
|
}
|
|
}
|
|
try writer.writeByte('"');
|
|
}
|