Feedback about my learning project

Hi guys, once again I want to thank people who helped me with my previous post, so just now i want to share what I’ve been working on:
https://github.com/walltime1/xxz

I will be grateful for any feedback, and I am most interested in developing an understanding about writing really productive code and making this app ass fast as possible. Also I’d like to rewrite as much code as possible to comptime, but for now I really am not getting this topic at all.

1 Like

First, this project looks really cool! I also really like your argv parsing strategy (the enum with stringToEnum).

I guess you can remove the src/root.zig file and everything that uses that file in the build.zig file.

Also, your test.txt file is there two times…

Comptime isn’t that hard. There is a really old blog post by Loris Cro about comptime. Not all code will still run, but some will probably do.

1 Like

Cool, however, I think that the file name should be able to be passed directly without the ‘-f’ flag, as is usual in Unix.

1 Like

That is WIP, I am looking for a way to drop reading from the stdin if a valid file can be read, either from -f or as a last parameter. Currently the stdin has the upper hand, because I haven’t managed to handle the case with it’s being empty.

Edit: done.

Thanks, will sort that out!

By the way thanks for Ziglings, that was my entry point :slightly_smiling_face:

1 Like

You could also remove reading file as input and only support stdin.
You can call your binary like this then:

xzz < test.txt

Makes it a lot simpler, imo.

It already works that way, reading and parsing of args was part of a learning.

Another issues that came out – the buffer contains data from previous chunk, and I need to sort out how to flush it and avoid double printing.

Can we somehow know how many bytes were read into the buffer?

Just look for Michael Kerrisk’s book, The Linux Programming Interface, you will surely find this information there.

1 Like

In zig, operations that write to a buffer usually return a slice of the written portion (with the correct length, of course). Be mindful to use the slice, and not the buffer, to use the witten data.

Before I figure out slices I manages to find that readAtLeast returns N read bytes which fits my purpose, but for now I’m stuck with casting usize to u8. Any tips on that?

There are a slew of casting builtin functions, particularly around integers. Which you use would depend on the use case. Where are you getting a usize that you want to be a u8?

there is @truncate(), but that will be lossy if you have values > 255

I have this piece of code

fn printHexdump2(buffer: []const u8, bufferLen: u8, wordLength: u8, offset: i64) !void {

Which I intended to use here

const bytesRead = try bufRead.reader().readAtLeast(buffer, lineStop);
try printHexdump2(buffer, bytesRead, wordLength, offset);

And got an error

src/main.zig:94:35: error: expected type 'u8', found 'usize'
        try printHexdump2(buffer, bytesRead, wordLength, offset);

Turns out I could change the type of bufferLen to usize to solve this issue, which seems to me like a crutch but I will leave it for how

An alternate approach is to not pass in the bufferLen value at all but just the slice. Slices contain both a pointer and a length. You would get something like this:

const bytesRead = try bufRead.reader().readAtLeast(buffer, lineStop);
try printHexdump2(buffer[0..bytesRead], wordLength, offset);
1 Like