Skip to content

Instantly share code, notes, and snippets.

@matu3ba
Last active March 26, 2022 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matu3ba/fcd144a385d062b44882b2895e211c10 to your computer and use it in GitHub Desktop.
Save matu3ba/fcd144a385d062b44882b2895e211c10 to your computer and use it in GitHub Desktop.
toy bracket counter in zig
const std = @import("std");
const process = std.process;
const stdout = std.io.getStdOut();
//const log = std.log;
const usage: []const u8 =
\\Usage: bracket_count file1 file2 ..
;
pub fn main() !void {
// 1. read file names from cli args
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
const args: [][:0]u8 = try process.argsAlloc(arena);
defer process.argsFree(arena, args);
if (args.len <= 1) {
try stdout.writer().print("{s}\n", .{usage});
}
// 2. count {,} [,], (,) to check, if counter has become negative
// output usable for vim
var i: u64 = 1; // skip program name
while (i < args.len) : (i += 1) {
//log.debug("reading file {s}..\n", .{args[i]});
const read_contents = try std.fs.cwd().readFileAlloc(arena, args[i], 1024 * 1024);
defer arena.free(read_contents);
//log.debug("size: {d}\n", .{read_contents.len});
//log.debug("{s}\n", .{read_contents});
var line_nr: u64 = 0;
var line_offset: u64 = 0;
var counter_curly: i64 = 0;
var counter_edgy: i64 = 0;
var counter_round: i64 = 0;
for (read_contents) |char| {
if (char == '\n') {
line_nr += 1;
line_offset = 0;
}
line_offset += 1;
switch (char) {
'{' => {
counter_curly += 1;
},
'}' => {
counter_curly -= 1;
},
'[' => {
counter_edgy += 1;
},
']' => {
counter_edgy -= 1;
},
'(' => {
counter_round += 1;
},
')' => {
counter_round -= 1;
},
else => {},
}
// counter < 0 => increase counter to prevent spamming the same message
if (counter_curly < 0) {
try stdout.writer().print("too many closing {{}}-brackets\n{s}:{d}:{d}\n", .{ args[i], line_nr, line_offset });
counter_curly += 1;
}
if (counter_edgy < 0) {
try stdout.writer().print("too many closing []-brackets\n{s}:{d}:{d}\n", .{ args[i], line_nr, line_offset });
counter_edgy += 1;
}
if (counter_round < 0) {
try stdout.writer().print("too many closing ()-brackets\n{s}:{d}:{d}\n", .{ args[i], line_nr, line_offset });
counter_round += 1;
}
}
if (counter_curly < 0) try stdout.writer().print("too many closing {{}}-brackets\n{s}:{d}\n", .{ args[i], line_nr });
if (counter_curly > 0) try stdout.writer().print("too few closing {{}}-brackets\n{s}:{d}\n", .{ args[i], line_nr });
if (counter_edgy < 0) try stdout.writer().print("too many closing []-brackets\n{s}:{d}\n", .{ args[i], line_nr });
if (counter_edgy > 0) try stdout.writer().print("too few closing []-brackets\n{s}:{d}\n", .{ args[i], line_nr });
if (counter_round < 0) try stdout.writer().print("too many closing ()-brackets\n{s}:{d}\n", .{ args[i], line_nr });
if (counter_round > 0) try stdout.writer().print("too few closing ()-brackets\n{s}:{d}\n", .{ args[i], line_nr });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment