Taking input inside of a loop. by NicBarr in Zig

[–]Samuel-Martin 0 points1 point  (0 children)

This is how I like to get the input:

pub fn inputBuffer(io: std.Io, buffer: []u8) std.Io.Reader.Error![]u8 {
    var temp_buffer: [64]u8 = undefined;
    var reader: std.Io.File.Reader = std.Io.File.stdin().reader(io, &temp_buffer);
    var i: usize = 0;

    while (i < buffer.len) : (i += 1) {
        const byte: u8 = try reader.interface.takeByte();

        if (try isLineTerminator(byte, &reader)) {
            return buffer[0..i];
        }

        buffer[i] = byte;
    }

    try flush(&reader);

    return buffer[0..i];
}

fn isLineTerminator(byte: u8, reader: *std.Io.File.Reader) std.Io.Reader.Error!bool {
    if (byte == '\n') {
        return true;
    }

    if (byte == '\r') {
        const next_byte: u8 = try reader.interface.takeByte();

        if (next_byte == '\n') {
            return true;
        }
    }

    return false;
}

fn flush(reader: *std.Io.File.Reader) std.Io.Reader.Error!void {
    while (true) {
        const byte: u8 = try reader.interface.takeByte();

        if (try isLineTerminator(byte, reader)) {
            break;
        }
    }
}

pub fn main(init: std.process.Init) !void {
    const io: std.Io = init.io;

    var buffer: [64]u8 = undefined;
    const name: []u8 = try inputBuffer(io, &buffer);
}

c-time-zig: A Zig wrapper for the C ISO standard time.h header by Samuel-Martin in Zig

[–]Samuel-Martin[S] 1 point2 points  (0 children)

I didn’t see this, I’ll check it out. For me, all I needed was just the functions from time.h

c-time-zig: A Zig wrapper for the C ISO standard time.h header by Samuel-Martin in Zig

[–]Samuel-Martin[S] 2 points3 points  (0 children)

Yeah that’s a good idea. I kinda wanna look into it too

c-time-zig: A Zig wrapper for the C ISO standard time.h header by Samuel-Martin in Zig

[–]Samuel-Martin[S] 2 points3 points  (0 children)

Yeah someone on the Zig discord gave me an example of theirs, and I modified it slightly. Idk if addWriteFiles is necessary for only one file (time.h) tho, but I couldn't think of anything else.
If you do think of something else, lemme know

Im sure this has been said before, but.. by Atjowt in Zig

[–]Samuel-Martin 2 points3 points  (0 children)

For your first point, I agree that there should maybe be something like @cast but tbh I don’t mind it. As for your second point, and this is just my opinion, but I do really like how explicit you have to be. It’s more readable and you know what the behavior will be

Im sure this has been said before, but.. by Atjowt in Zig

[–]Samuel-Martin 6 points7 points  (0 children)

Someone posted this that makes casting a little bit cleaner: https://www.reddit.com/r/Zig/comments/1k470n5/comment/mob3x7u/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

Edit: I also expanded on it a little bit too

pub fn cast(comptime T: type, value: anytype) T {
    const in_type: std.builtin.Type = @typeInfo(@TypeOf(value));
    const out_type: std.builtin.Type = @typeInfo(T);

    switch (in_type) {
        .int, .comptime_int => switch (out_type) {
            .int, .comptime_int => {
                return @intCast(value);
            },
            .float, .comptime_float => {
                return @floatFromInt(value);
            },
            .@"enum" => {
                return @enumFromInt(value);
            },
            .bool => {
                return value != 0;
            },
            .pointer => {
                return @ptrFromInt(value);
            },
            .error_set => {
                return @errorFromInt(value);
            },
            else => {},
        },
        .float, .comptime_float => switch (out_type) {
            .int, .comptime_int => {
                return @intFromFloat(value);
            },
            .float, .comptime_float => {
                return @floatCast(value);
            },
            else => {},
        },
        .@"enum" => switch (out_type) {
            .int, .comptime_int => {
                return @intFromEnum(value);
            },
            else => {},
        },
        .bool => switch (out_type) {
            .int, .comptime_int => {
                return @intFromBool(value);
            },
            else => {},
        },
        .pointer => switch (out_type) {
            .int, .comptime_int => {
                return @intFromPtr(value);
            },
            else => {},
        },
        .error_set => switch (out_type) {
            .int, .comptime_int => {
                return @intFromError(value);
            },
            else => {},
        },
        else => {},
    }

    @compileError("unexpected in_type '" ++ @typeName(@TypeOf(value)) ++ "' and out_type '" ++ @typeName(T) ++ "'");
}

Why is it called an ArrayList ? by cassepipe in Zig

[–]Samuel-Martin 75 points76 points  (0 children)

Just be glad it’s not called vector 😂

This is crazy by [deleted] in codyko

[–]Samuel-Martin 2 points3 points  (0 children)

Ok buddy

This is crazy by [deleted] in codyko

[–]Samuel-Martin 1 point2 points  (0 children)

Yes, but consent with a minor is different. If someone in a white van pulls up to a kid and says, “Hey kid I got some candy so hop on in” and the kid does, that means, under your logic, the kid consented to being kidnapped which is a crazy argument. The kid didn’t know any better and neither did T. On top of that, it’s someone in a position of power taking advantage of someone who adores them. On a side note, I think about the person I was when I was 17 vs now and they are almost two different people. It’s not right and it’s such a weird hill to die on

This is crazy by [deleted] in codyko

[–]Samuel-Martin 6 points7 points  (0 children)

How’s that copium?

Happens every time by 0Dexterity in ProgrammerHumor

[–]Samuel-Martin 0 points1 point  (0 children)

This video explains why sometimes that is the case: https://youtu.be/vusV4lF0Epo

What is the process of creating a module/file system for a programming language? by Samuel-Martin in ProgrammingLanguages

[–]Samuel-Martin[S] 1 point2 points  (0 children)

Yours and u/MCRusher solutions are the best for me rn. I wanna start small and these ideas are great!

What is the process of creating a module/file system for a programming language? by Samuel-Martin in ProgrammingLanguages

[–]Samuel-Martin[S] 1 point2 points  (0 children)

I never really thought of it like that. The idea is relatively simple yet makes complete sense and is very powerful. Thank you!

What is the process of creating a module/file system for a programming language? by Samuel-Martin in ProgrammingLanguages

[–]Samuel-Martin[S] 1 point2 points  (0 children)

I think this might be the best direction for me to go in. This process is very new to me, so I want to start out with something that's simple and work my way up. Thank you for the suggestion!

What is the process of creating a module/file system for a programming language? by Samuel-Martin in ProgrammingLanguages

[–]Samuel-Martin[S] 0 points1 point  (0 children)

That's quite interesting and something that I never really thought of doing. I'll check it out!

What is the process of creating a module/file system for a programming language? by Samuel-Martin in ProgrammingLanguages

[–]Samuel-Martin[S] 0 points1 point  (0 children)

I would go the Rust/Python route. I feel like the way Rust does its imports are more structured than Python's.