#arg cmp str

1 messages · Page 1 of 1 (latest)

swift gyro
#
const std = @import("std");

pub fn main() !void {
    for (std.os.argv) |arg| {
        if (std.mem.eql(u8, arg, "")) {}
        std.debug.print("{s}\n", .{arg});
    }
}
#
expected type '[]const u8', found '[*:0]u8'
muted lynx
#

you need to first run std.mem.span on the arg to get a slice

#

it's stored as a null terminated pointer

swift gyro
#

why

dire wave
#

a null terminated pointer is a pointer with a null value (value of 0) at the end

#

this is used to indicate the length

swift gyro
#

i know

dire wave
#

whereas a Zig slice has a ptr + a separate numeric length

#

you need to convert between the two

#

span will find the sentinel and produce a slice with that found length

muted lynx
# swift gyro why

because that's what the value is stored as 🤷‍♂️
it's equivalent to C's argv

swift gyro
#

why isnt os.argv already slices

#

is there a helper function

dire wave
#

yes

muted lynx
#

std.process.argsAlloc for example

#

or std.mem.span as said earlier to convert from [*:0] -> []

dire wave
#

I'd strongly recommend argsAlloc or a close sibling though

#

std.os.argv is not cross-platform (windows specifically comes to mind)

swift gyro
#

why cant you iterate using a for loop

dire wave
#

you can

#

just letting you know why you shouldn't in this case

swift gyro
#

std.process.args()

dire wave
#

well that's what argsAlloc is

#

it just takes an allocator because a memory allocation is potentially required to produce the argument list :)

muted lynx
#

yeah, argsAlloc literally just wraps the ArgIterator

dire wave
#

oh i didn't know that, so yeah the allocator is 100% required then lol

swift gyro
#

i wonder if it could be done inplace where the os allocs the args on the stack

muted lynx
dire wave
#

but not on windows (and wasi ig)

#

thus the alloc variants

swift gyro
#

then for loop isnt a for loop?

vagrant hornet
#

std.os.argv is precisely a pointer to the arguments on the stack, which is why it can't contain slices

swift gyro
#

yea could it be convcerted into slices inplace

#

need to offset the chars by one and place the length

vagrant hornet
#

there is no space to store the length

swift gyro
#

yea

#

can you compare strings without having to do this

crisp hawk
#

You can, but process arguments aren't zig strings (except on some theoretical zig operating system).

swift gyro
#

is there a way to check if im missing defer deinit

muted lynx
#

use a allocator that reports leaks

#

such as GPA

#

std.heap.GeneralPurposeAllocator

swift gyro
#

expected type 'mem.Allocator', found 'type'

muted lynx
#

you need to do std.heap.GeneralPurposeAllocator(.{}){} because it's a function that returns a type that needs to be initialized.

swift gyro
#
    comptime var alloc = std.heap.GeneralPurposeAllocator(.{});
    var args = try std.process.argsWithAllocator(alloc);
#
const std = @import("std");

pub fn main() !void {
    var alloc = std.heap.GeneralPurposeAllocator(.{}){};
    var args = try std.process.argsWithAllocator(alloc.allocator());
    while (args.next()) |arg| {
        if (std.mem.eql(u8, arg, "--help")) {
            std.debug.print("{s}\n", .{"Usage: echo [STRING]..."});
            return;
        }
        std.debug.print("{s}\n", .{arg});
    }
    _ = alloc.detectLeaks();
}
#

its not dping nyaything

muted lynx
#

how are you running it?

#

it looks fine to me

swift gyro
#

zig build run -- --help

muted lynx
#

are you adding the args to to the run step in the build.zig?

swift gyro
#

whats a build.zig

muted lynx
#

it's the zig file used for managing the build system. if you dont know what that is, then you're running it wrong.

you want to use zig run file.zig -- --help for example, or zig build-exe...

swift gyro
#

it does work but the detectLeaks does nothing

#

im in a init-exe

muted lynx
#

oh sure then

#

that's because when you have then --help arg, you're returning

#

and it never reaches the detect leaks

swift gyro
#

ah copilot messed it up

#

um its still not working

#

does detect leaks need to be called outside main function?

muted lynx
#

no

#

you're doing it right

#

on an OS that supports std.os.argv there is no allocation that happens

#

so there's nothing to leak

#

you should still run deinit, to be cross platform

swift gyro
#

does it not have to convert to slices?

muted lynx
#

so?

swift gyro
#

that requiers alloc right?

muted lynx
#

no it doesn't

swift gyro
#

argv is c strings right

muted lynx
#

sure

swift gyro
#

can defer deinit be enforced?

muted lynx
#

no

swift gyro
#

alright thannks

#

can you infer types for generic functions?

#
std.mem.eql(u8, arg, "--help")

not having to specify u8