Skip to content
This repository was archived by the owner on Nov 22, 2025. It is now read-only.

Commit b80deb4

Browse files
filter c-style comments and empty lines, bug fix
1 parent e5eccc6 commit b80deb4

5 files changed

Lines changed: 113 additions & 85 deletions

File tree

src/textdiff/addHeaders.zig

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub fn main() !void {
1616

1717
if (i != 2) {
1818
std.debug.print("usage: addHeaders <dir>\n", .{});
19-
std.debug.print("adds headers from <dir> into uh_workspace\n", .{});
19+
std.debug.print("normalizes headers from <dir> into uh_norm/<dir>\n", .{});
20+
std.debug.print("adds headers from uh_norm/<dir> into uh_workspace\n", .{});
2021
return;
2122
}
2223
}
@@ -26,6 +27,11 @@ pub fn main() !void {
2627
const headerDir = args.next() orelse return;
2728

2829
var buf = try arena.alloc(u8, 1000);
30+
31+
const normalized_dir = try std.fmt.bufPrint(buf, "uh_norm/{s}", .{std.fs.path.basename(headerDir)});
32+
std.fs.cwd().makePath(normalized_dir) catch {};
33+
34+
buf = try arena.alloc(u8, 1000);
2935
const versionStr = try std.fmt.bufPrint(buf, "{s}|", .{std.fs.path.basename(headerDir)});
3036

3137
var dir = try std.fs.cwd().openIterableDir(headerDir, .{});
@@ -41,7 +47,7 @@ pub fn main() !void {
4147
continue;
4248
}
4349

44-
//if (!std.mem.eql(u8, entry.basename, "_ctype.h")) continue;
50+
//if (!std.mem.eql(u8, entry.basename, "ucontext.h")) continue;
4551

4652
//std.debug.print("entry: base {s} path {s}\n", .{ entry.basename, entry.path });
4753

@@ -50,6 +56,7 @@ pub fn main() !void {
5056

5157
var filepath = try std.fs.path.join(arena, &.{ headerDir, entry.path });
5258
var workpath = try std.fs.path.join(arena, &.{ "uh_workspace", entry.path });
59+
var normalized_path = try std.fs.path.join(arena, &.{ normalized_dir, entry.path });
5360

5461
// read all the lines of our work-in-progress file
5562
var worklines = std.ArrayList([]const u8).init(arena);
@@ -60,13 +67,15 @@ pub fn main() !void {
6067
}
6168
var file = try std.fs.cwd().createFile(workpath, .{ .read = true, .truncate = false });
6269
defer file.close();
63-
var contents = try file.reader().readAllAlloc(arena, 20 * 1024 * 1024);
70+
var contents = try file.reader().readAllAlloc(arena, 100 * 1024 * 1024);
71+
6472
while (contents.len > 0 and contents[contents.len - 1] == '\n') {
6573
contents = contents[0 .. contents.len - 1];
6674
}
6775

6876
if (contents.len > 0) {
6977
var it = std.mem.splitScalar(u8, contents, '\n');
78+
7079
while (it.next()) |line| {
7180
try worklines.append(line);
7281
}
@@ -80,7 +89,7 @@ pub fn main() !void {
8089
{
8190
var file = try std.fs.cwd().createFile(versionpath, .{ .read = true, .truncate = false });
8291
defer file.close();
83-
var contents = try file.reader().readAllAlloc(arena, 20 * 1024 * 1024);
92+
var contents = try file.reader().readAllAlloc(arena, 100 * 1024 * 1024);
8493
while (contents.len > 0 and contents[contents.len - 1] == '\n') {
8594
contents = contents[0 .. contents.len - 1];
8695
}
@@ -98,7 +107,37 @@ pub fn main() !void {
98107
{
99108
var file = try std.fs.cwd().openFile(filepath, .{});
100109
defer file.close();
101-
var contents = try file.reader().readAllAlloc(arena, 20 * 1024 * 1024);
110+
var contents = try file.reader().readAllAlloc(arena, 100 * 1024 * 1024);
111+
112+
// filter out comments
113+
var i: usize = 0;
114+
var k: usize = 0;
115+
var in_comment = false;
116+
while (i < contents.len) {
117+
if (i + 1 < contents.len) {
118+
if (!in_comment and std.mem.eql(u8, contents[i..][0..2], "/*")) {
119+
in_comment = true;
120+
i += 2;
121+
continue;
122+
}
123+
124+
if (in_comment and std.mem.eql(u8, contents[i..][0..2], "*/")) {
125+
in_comment = false;
126+
i += 2;
127+
continue;
128+
}
129+
}
130+
131+
if (!in_comment) {
132+
contents[k] = contents[i];
133+
k += 1;
134+
}
135+
136+
i += 1;
137+
}
138+
139+
contents.len = k;
140+
102141
while (contents.len > 0 and contents[contents.len - 1] == '\n') {
103142
contents = contents[0 .. contents.len - 1];
104143
}
@@ -109,7 +148,32 @@ pub fn main() !void {
109148
// normalize whitespace (replace tabs with spaces)
110149
buf = try arena.alloc(u8, line.len);
111150
_ = std.mem.replace(u8, line, "\t", " ", buf);
112-
try filelines.append(buf);
151+
152+
// filter out empty lines
153+
var empty = true;
154+
for (buf) |ch| {
155+
if (ch != ' ') {
156+
empty = false;
157+
break;
158+
}
159+
}
160+
161+
if (!empty) {
162+
try filelines.append(buf);
163+
}
164+
}
165+
}
166+
167+
// write out normalized file back to disk for debugging
168+
{
169+
if (std.fs.path.dirname(normalized_path)) |dirname| {
170+
try std.fs.cwd().makePath(dirname);
171+
}
172+
var nfile = try std.fs.cwd().createFile(normalized_path, .{});
173+
defer nfile.close();
174+
var writer = nfile.writer();
175+
for (filelines.items) |line| {
176+
try writer.print("{s}\n", .{line});
113177
}
114178
}
115179
}
@@ -339,7 +403,6 @@ pub fn addContext(arena: std.mem.Allocator, lines: *std.ArrayList([]const u8)) !
339403
for (seen_contexts.items) |sc| {
340404
if (ctx == sc) {
341405
ctx += 1;
342-
break;
343406
}
344407
}
345408
try context.append(ctx);
@@ -368,7 +431,6 @@ pub fn addContext(arena: std.mem.Allocator, lines: *std.ArrayList([]const u8)) !
368431
for (seen_contexts.items) |sc| {
369432
if (ctx == sc) {
370433
ctx += 1;
371-
break;
372434
}
373435
}
374436
try context.append(ctx);

src/textdiff/compile.bash

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# example: ./src/textdiff/compile.bash headers/m68k-linux*
3+
#
4+
# then run ./src/textdiff/test.bash headers/m68k-linux*
5+
6+
HEADER_LIST="$@"
7+
8+
if [ -d uh_workspace ];
9+
then
10+
echo "directory uh_workspace already exists, need to remove it - rm -r uh_* ?"
11+
exit 1
12+
fi
13+
14+
for i in $HEADER_LIST;
15+
do
16+
echo "addHeaders $i"
17+
./zig-out/bin/addHeaders $i || exit 1
18+
done
19+
20+
21+
echo "outputHeaders"
22+
./zig-out/bin/outputHeaders uh_headers || exit 1
23+
24+
25+
exit 0

src/textdiff/example.bash

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/textdiff/outputHeaders.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn main() !void {
5050
{
5151
var file = try std.fs.cwd().openFile(workpath, .{});
5252
defer file.close();
53-
var contents = try file.reader().readAllAlloc(arena, 20 * 1024 * 1024);
53+
var contents = try file.reader().readAllAlloc(arena, 100 * 1024 * 1024);
5454
while (contents.len > 0 and contents[contents.len - 1] == '\n') {
5555
contents = contents[0 .. contents.len - 1];
5656
}
@@ -70,7 +70,7 @@ pub fn main() !void {
7070
{
7171
var file = try std.fs.cwd().openFile(versionpath, .{});
7272
defer file.close();
73-
var contents = try file.reader().readAllAlloc(arena, 20 * 1024 * 1024);
73+
var contents = try file.reader().readAllAlloc(arena, 100 * 1024 * 1024);
7474
while (contents.len > 0 and contents[contents.len - 1] == '\n') {
7575
contents = contents[0 .. contents.len - 1];
7676
}

src/textdiff/test.bash

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
HEADER_LIST="$@"
3+
4+
for i in $HEADER_LIST;
5+
do
6+
echo "testHeaders $i"
7+
./zig-out/bin/testHeaders uh_headers uh_test $i
8+
diff -uBwr uh_norm/$(basename $i) uh_test | grep -v "Only in" > uh_diff
9+
if [ -s uh_diff ]
10+
then
11+
echo "failed: diff -uBwr uh_norm/$(basename $i) uh_test"
12+
exit 1
13+
fi
14+
done
15+
16+
exit 0

0 commit comments

Comments
 (0)