-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsp.zig
356 lines (336 loc) · 16.6 KB
/
zsp.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// MIT License
//
// Copyright (c) 2024 Alexei Kireev
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// zigStructPrint is available from https://github.com/Durobot/zigStructPrint
const std = @import("std");
pub fn printStruct(s: anytype, shorten_types: bool, indent: comptime_int) void
{
const s_type_info = @typeInfo(@TypeOf(s));
if (s_type_info != .@"struct")
@compileError("fn printStruct: `s` is " ++ @typeName(s) ++ " , expected a struct");
const ind_str = " " ** indent;
const ind_str_2 = " " ** (indent + 1);
std.debug.print("{s}{{\n", .{ ind_str });
var name_buf = [_]u8 { 0 } ** 100;
inline for (s_type_info.@"struct".fields) |fld|
switch (@typeInfo(fld.type))
{
.int, .float, .comptime_int, .comptime_float =>
std.debug.print("{s}{s}{s}: {s} = {d}\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, @typeName(fld.type), @field(s, fld.name) }),
.bool =>
std.debug.print("{s}{s}{s}: {s} = {}\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, @typeName(fld.type), @field(s, fld.name) }),
.@"struct" =>
{
std.debug.print("{s}{s}{s}: {s} =\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printStruct(@field(s, fld.name), shorten_types, indent + 1);
},
.array =>
{
std.debug.print("{s}{s}{s}: {s} = [ ",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printArray(@field(s, fld.name), shorten_types);
std.debug.print("]\n", .{});
},
.pointer => |ptr_type_info|
{
switch (ptr_type_info.size)
{
.One, .Many, .C =>
std.debug.print("{s}{s}{s}: {s} = {*}\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@field(s, fld.name) }),
.Slice =>
if (ptr_type_info.child == u8)
std.debug.print("{s}{s}{s}: {s} = \"{s}\"\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@field(s, fld.name) })
else
{
std.debug.print("{s}{s}{s}: {s} = [ ",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printArray(@field(s, fld.name), shorten_types);
std.debug.print("]\n", .{});
}
}
},
.@"enum" => std.debug.print("{s}{s}{s}: {s} = {s}\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@tagName(@field(s, fld.name)) }),
else => std.debug.print("{s}{s}{s}: {s} = -\n",
.{ ind_str_2, if (fld.is_comptime) "comptime " else "", fld.name,
typeName(@typeName(fld.type), &name_buf, shorten_types) }),
};
std.debug.print("{s}}}\n", .{ ind_str });
}
pub fn printStructInline(s: anytype, shorten_types: bool) void
{
const s_type_info = @typeInfo(@TypeOf(s));
if (s_type_info != .@"struct")
@compileError("fn printStructInline: `s` is " ++ @typeName(s) ++ " , expected a struct");
std.debug.print("{{ ", .{});
var name_buf = [_]u8 { 0 } ** 100;
inline for (s_type_info.@"struct".fields) |fld|
switch (@typeInfo(fld.type))
{
.int, .float, .comptime_int, .comptime_float =>
std.debug.print("{s}{s}: {s} = {d}, ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, @typeName(fld.type), @field(s, fld.name) }),
.bool =>
std.debug.print("{s}{s}: {s} = {}, ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, @typeName(fld.type), @field(s, fld.name) }),
.@"struct" =>
{
std.debug.print("{s}{s}: {s} = ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printStruct(@field(s, fld.name), 0, true);
},
.array =>
{
std.debug.print("{s}{s}: {s} = [ ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printArray(@field(s, fld.name), shorten_types);
std.debug.print("], ", .{});
},
.pointer => |ptr_type_info|
{
switch (ptr_type_info.size)
{
.One, .Many, .C =>
std.debug.print("{s}{s}: {s} = {*}, ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@field(s, fld.name) }),
.Slice =>
if (ptr_type_info.child == u8)
std.debug.print("{s}{s}: {s} = \"{s}\", ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@field(s, fld.name) })
else
{
std.debug.print("{s}{s}: {s} = [ ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types) });
printArray(@field(s, fld.name), shorten_types);
std.debug.print("], ", .{});
}
}
},
.@"enum" => std.debug.print("{s}{s}: {s} = {s}, ",
.{ if (fld.is_comptime) "comptime " else "",
fld.name, typeName(@typeName(fld.type), &name_buf, shorten_types),
@tagName(@field(s, fld.name)) }),
else => std.debug.print("{s}{s}: {s} = -, ",
.{ if (fld.is_comptime) "comptime " else "", fld.name,
typeName(@typeName(fld.type), &name_buf, shorten_types) }),
};
std.debug.print("}}, ", .{});
}
pub fn printArray(a: anytype, shorten_types: bool) void
{
const a_type_info = @typeInfo(@TypeOf(a));
if (a_type_info != .array and
(a_type_info != .pointer or a_type_info.pointer.size != .Slice))
@compileError("fn printArray: `a` is " ++ @typeName(a) ++ " , expected an array or a slice");
if (a_type_info == .pointer and a_type_info.pointer.child == u8 and a_type_info.pointer.is_const)
{
std.debug.print("\"{s}\" ", .{a});
return;
}
for (a) |e|
switch (@typeInfo(@TypeOf(e)))
{
.int, .float, .comptime_int, .comptime_float =>
std.debug.print("{d}, ", .{ e }),
.bool => std.debug.print("{}, ", .{ e }),
.array =>
{
std.debug.print("[ ", .{});
printArray(e, shorten_types);
std.debug.print("], ", .{});
},
.@"struct" => printStructInline(e, shorten_types),
.pointer => |ptr_info|
if (ptr_info.size == .Slice)
{
std.debug.print("[ ", .{});
printArray(e, shorten_types);
std.debug.print("], ", .{});
}
else
std.debug.print("-, ", .{}),
.@"enum" => std.debug.print("{s}, ", .{ @tagName(e) }),
else => std.debug.print("-, ", .{}),
};
}
/// if (shorten_name) copy short name to name_buf; return name_buf;
/// else return name;
fn typeName(name: []const u8, name_buf: []u8, shorten_name: bool) []const u8
{
if (!shorten_name) return name;
// type returned by a function, e.g.
// "[2]zon_get_fields.makeStructRetType(zon_get_fields.test.zonToStruct big test.TargetStruct.ArrStruct)"
// - must process function name part ("zon_get_fields.makeStructRetType")
// and argument part ("zon_get_fields.test.zonToStruct big test.TargetStruct.ArrStruct")
// separately, reconstructing the type in name_buf.
// "[2]makeStructRetType(ArrStruct)" is what we're aiming for in our example.
if (name.len > 0 and name[name.len - 1] == ')')
{
var first_free_name_buf_idx: usize = 0; // Index of the first free character in name_buf
// First opening bracket index, indicating the end of the function name
const open_bracket_idx = std.mem.indexOfScalar(u8, name, '(') orelse return name;
// Find the first letter in function name and copy everything up to it to name_buf (e.g. "[2]")
const fn_first_letter_idx =
blk1:
{
for (name[0..open_bracket_idx], 0..) |c, i|
if ((c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'z')) break :blk1 i;
return name; // First letter in fn name not found o_O
};
//if (fn_first_letter_idx >= open_bracket_idx) return name; // What? How?? o_O
if (fn_first_letter_idx > 0) // Copy this part into name_buf, if any
{
first_free_name_buf_idx = copyToBufAt(name_buf, name[0..fn_first_letter_idx], 0);
if (first_free_name_buf_idx >= name_buf.len) return name_buf; // increase constness
}
// Find the first letter in function name after the last dot ('.'),
// and copy everything starting with it up to the opening bracket, to name_buf
const fn_first_char_after_dot_idx =
blk2:
{
var i: usize = open_bracket_idx;
while (i != fn_first_letter_idx)
{
i -= 1;
if (name[i] == '.') break :blk2 i + 1;
}
break :blk2 fn_first_letter_idx; // '.' not found in fn name, pretend it's index is fn_first_letter_idx (?)
};
// Copy the rightmost part of the function name, including the opening bracket, to name_buf
first_free_name_buf_idx = copyToBufAt(name_buf,
name[fn_first_char_after_dot_idx..(open_bracket_idx+1)],
first_free_name_buf_idx);
if (first_free_name_buf_idx >= name_buf.len) return name_buf;
// Now copy the shortened version of the type name inside the brackets (function argument)
// to name_buf.
if (std.mem.lastIndexOfScalar(u8, name[open_bracket_idx..], '.')) |dot_pos| // if it contains a dot
{
// std.debug.print("open_bracket_idx = {}, dot_pos = {}\n", .{open_bracket_idx, dot_pos});
// std.debug.print("{s}\n", .{ name[open_bracket_idx..] });
first_free_name_buf_idx = copyToBufAt(name_buf, name[(open_bracket_idx + dot_pos + 1)..],
first_free_name_buf_idx);
}
else // no dot
first_free_name_buf_idx = copyToBufAt(name_buf, name[(open_bracket_idx+1)..],
first_free_name_buf_idx);
return name_buf[0..first_free_name_buf_idx];
}
// Something like [10] or * or whatever
if (name[0] < 'A' or (name[0] > 'Z' and name[0] < 'a') or name[0] > 'z')
{
var first_free_name_buf_idx: usize = 0; // Index of the first free character in name_buf
// Find the first letter in function name and copy everything up to it to name_buf (e.g. "[2]")
const first_letter_idx =
blk:
{
for (name, 0..) |c, i|
if ((c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'z')) break :blk i;
return name; // First letter in name not found o_O
};
if (first_letter_idx > 0) // Copy this part into name_buf, if any
{
first_free_name_buf_idx = copyToBufAt(name_buf, name[0..first_letter_idx], 0);
if (first_free_name_buf_idx >= name_buf.len) return name_buf;
}
// Now copy the shortened version of the type name into name_buf.
if (std.mem.lastIndexOfScalar(u8, name[first_letter_idx..], '.')) |dot_pos| // if it contains a dot
{
// std.debug.print("open_bracket_idx = {}, dot_pos = {}\n", .{open_bracket_idx, dot_pos});
// std.debug.print("{s}\n", .{ name[open_bracket_idx..] });
first_free_name_buf_idx = copyToBufAt(name_buf, name[(first_letter_idx + dot_pos + 1)..],
first_free_name_buf_idx);
}
else // no dot
first_free_name_buf_idx = copyToBufAt(name_buf, name[(first_letter_idx)..],
first_free_name_buf_idx);
return name_buf[0..first_free_name_buf_idx];
}
if (std.mem.lastIndexOfScalar(u8, name, '.')) |dot_pos|
{
var short_name: []u8 = @constCast(name);
if (name.len > dot_pos + 1) // name does not end with '.'
{
short_name.ptr += dot_pos + 1;
short_name.len -= dot_pos + 1;
if (short_name[short_name.len - 1] == ')') short_name.len -= 1;
}
return short_name;
}
else
return name;
}
fn ellipsis(buf: []u8) void
{
if (buf.len > 4)
{
buf[buf.len - 1] = '.';
buf[buf.len - 2] = '.';
buf[buf.len - 3] = '.';
}
else if (buf.len > 2)
{
buf[buf.len - 1] = '.';
buf[buf.len - 2] = '.';
}
}
/// Returns index of the next free character in dest, dest.len if no free characters
fn copyToBufAt(dest: []u8, src: []const u8, dest_pos: usize) usize
{
if (dest_pos >= dest.len)
{
std.log.err("fn copyToBufAt: dest_pos ({}) >= dest.len ({})", .{ dest_pos, dest.len });
return dest.len;
}
if (src.len >= (dest.len - dest_pos)) // fn name too long for the remainder of name_buf
{
const missing_chars = src.len - (dest.len - dest_pos);
std.mem.copyForwards(u8, dest[dest_pos..], src[0..(src.len - missing_chars)]);
ellipsis(dest);
return dest.len;
}
std.mem.copyForwards(u8, dest[dest_pos..], src);
return dest_pos + src.len;
}