#How to get standard input from cli ?
1 messages ยท Page 1 of 1 (latest)
arguements
natural numbers
The we have std.process.argsIterator
or std.process.args
both work fine
But you have to parse the elements
since the arguments are strings
do i need some kind of heap allocation for this. dont mind i never tried low level languages
In linux? no, in windows? yes
Generally languages do this for you, but zig is different regarding this, and ask you to explicitly to allocate
yeah after all its a C replacement
C also does this for you
so i need to allocate for this right?
Sure
for standard input oh okay
either way you can just pass an allocator and forget about it
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
var allocator = arena.allocator();
var args = try std.process.argsAlloc(allocator);
defer allocator.free(args);
This is my current code for a tool I use, this returns me a [][:0]u8 (A slice of strings of bytes)
so I can work with each string however I like
But there is also:
std.process.argsWithAllocator(allocator);
Which returns an iterator
var argsit = try std.process.argsWithAllocator(allocator);
defer argsit.deinit();
while (argsit.next()) |arg| {
// Use the args
}
defer arena.deinit();```
what does this mean?
An arena is a type of allocator that deallocates all memory when deinit
all frees are """"no-operation"""" (there is actual some, but let's ignore it) and it frees all memory at the end
I use it because it's a short lived program in my case and I did not care
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
var ally = gpa.allocator();
This right here is the general purposed one, that detect if you have a memory leak
does this have something to do with block scope? like scope gone variable freed
oh
Sure
it deallocates when scope ends right?
defer just means: "At the end of this block, execute this"
Well, I indicated as so
oh so when i create a allocater i should deinit it along with creating it
zig seems interesting
btw thanks mate
Not all of them, but is good looking into the allocator to see if it's worth it
generally whatever you init, you deinit, whatever you create you destroy and whatever you alloc you free
๐
while (argsit.next()) |arg| {
// Use the args
``` what does this `| arg |` implies
argsit.next() returns a ?[:0]const u8
? means the following type is "nullable" (can be asigned null)
while accept nullable values and boolean values
nullable values needs the syntax I wrote above and doing so, it "Captures" the value when not null, and asign it to arg
This works in all flow control that accept truth values (well, that is just if and while ig)
if(argsit.next()) |arg| {}
The equivalent would be:
var arg = argsit.next();
while(arg != null) {
var arg_payload = arg.?;
arg = argsit.next();
// Use the payload
}
or well the more succint syntax
ohk
var arg = argsit.next();
while(arg != null) : (arg = argsit.next()) {
var arg_payload = arg.?;
// Use the payload
}
and how to make it wait to take the input
Oh ok, then you don't need arguments
you need standard input ๐
Different concepts ๐
Here is how you would use this:
my_program 10 20
Oh
i need to build it okay
stdin still needs to build it, just that the program keeps waiting for something to read
yeah worked
got 2 args
is this a array var args = try std.process.argsAlloc(allocator);
i can get index of it ?
[][:0]u8
it shows
idk what that
yeah sure
is an "array" of sorts
I say of sorts because it's really a ptr you can access the offset of, but dw about details, know you have a .len field tellin you how long it is, and you can access via index with args[0], which will access the first argument
(Which is the program name btw)
yeah i saw that
pointer to 2 slices?
or pointer to array
to be exact:
[][:0]const u8 means: "A slice (ptr + len) that holds slices (ptr + len) with a 0 terminated separation (sentinel) of a bunch of bytes"
the ptr is a multiptr which implies multiple elements on contigous memory
which I guess COULD be defined as an array
In zig it would be correct at least, since arrays are contigous memory as value
in C is a bit more nebolous
Ah now i get it
@strong jetty when i access its index
it gives me [:0]u8
how can i parse this to u8
u8 would be a char
well a byte
You need to parse it to whatever the type you need
A string of 8 bit values (which nowadays are chars)
are there any parsers in std?
u8
https://ziglang.org/documentation/master/std/#A;std:fmt you can check parser for types from a string here
Requires more nuance ๐
and [:0]what does this :0 mean here
Sentinel, it means the last character is the end and is marked as the end by 0
"If you could see, at the end of this string there is a 0, like an actual byte 0"
If you would see the underlying bytes, the last char is a 0
Ah why this is needed?
to end it?
C did not have slices
so, how you know how long is a string?
hmm
for instance, when you interface with a C library, when it requests a string it usually wants a [*:0]const u8
something that terminates in a \0
Correction [*:0]
ah
@obsidian acorn so, since ptrs have no len attached, and actually goign arround with a len is wasteful (specially for the time C was written), 0 was decided as the "Here lies the end of the string"
Ok i got it
Having a len is wasteful in memory, relying on the 0 to end stuff basically means computational cycles getting to the end
๐
so uh what do i do to parse [:0]u8 to u8
like i checked it gives some { 49, 48, 48 } when i type 100 in console
Here is the deal
let's call things by the names and not the types: since a valid answer could be just take index 0 hahaha
In this case is: "How do I get a string to a value in between 0 and 255"
In this case std.fmt.parseInt is your friend
or doing it yourself
var my_u8 = try std.fmt.parseInt(u8,args[1],10);
or the manual method
var result = 0;
for(args[1], 0..) |val,i| {
result += (val - 48) * std.math.pow(u8, 10, args.len - i);
}
shouldn't you do val - 0x30 in this case?
you are right
This uses the fact that the nubers chars is from [48-57]
Ok
do i need to install something to debug zig programs?
like segfaults and etc
Well, zig provides a stacktrace
But you can use code-lldb, lldb, gdb, windbg (windows only) etc.
0kay
sir js src/main.zig:11:22: error: type 'main.Block' does not support array initialization syntax what does this means
Block is a struct
const std = @import("std");
const print = std.io.getStdOut().writer().print;
const Block = struct { user: block_state, filled: bool, index: i8 };
const block_state = enum { none, x, o };
pub fn main() !void {
var allocator = std.heap.page_allocator;
var board = std.ArrayList(Block).init(allocator);
defer board.deinit();
for (0..9, 0..) |_, index| {
board.append(Block{ block_state.none, false, index });
}
try print("{any}", .{board});
}```
this is the whole thing
of append function?
Block {.user = .none, .filled = false, .index = index};
this is the syntax to init a struct ?
ahh okay
.{ .field = val} works too
in rust it was different mb
(Y)
can i modify the type of
index
coming from the for loop
its gives usize
nvm i just changed u8 to usize
@strong jetty i saw two parameters in a function with
&
and *
what does these mean
fn handleConnection(allocator: *std.mem.Allocator, conn: *std.net.StreamServer.Connection) ```
here
and js try handleConnection(allocator, &conn);
here
are those references?
* means is a ptr to something
& is taking the address of something (getting the ptr)
If you wanna see them like that ,sure
explain & more briefly please
what does taking address mean in a function
things are memory, using & gets where they are in memory
oh address
&conn = address of conn
why we need to pass pointers ?
what is different in passing a variable and a pointer
i guess pointer also gives us the value stored there
in rust it was some ownership thing
whats the matter in zig?
i know my words dont any make sense :\
a pointer points to some value in memory
yeah i know that
you can modify the original value if you pass by pointer
if you just pass a value to a function and then try and change that, nothing will happen
well, you'll get a compile error
Oh
because you're changing a copy of the value
so:
fn foo(p: *i32) void {p.* = 69}
test "foo sets integer to 69" {
var x: i32 = 0;
foo(&x);
try std.testing.expectEqual(@as(i32, 69), x);
}
hmm makes sense
p.* = 69 what does this does
it seems it sets it to 69 but
what p.* means
.* means "dereference this pointer", "get the thing this pointer points to"
the & means "get a pointer to this thing"
then what would *x mean
maybe this helps (it's C syntax, I should update that)
we can have pointers to pointers
yes
lol
**i32
We got helpful people ๐
aribitrary int sizes in zig
u31 is 31 bits numbers
Which generally is u32 ignoring 1 bit
ah
is there any way to listen on a port
i figured that net.ServerStream can do that
but i dont know how to use it
oh, do you have it with zig syntax?
@strong jetty where can i get explaination of how to use standard library modules like net etc
Things you learn reading the docs
this doesnt have full explainations
The comments should.
if autodocs doesn't cut it you should read the source code/examples
If something isn't clear, please make a PR to add doc comments after reading the code.
tests are useful too
i found this so useful https://zigbyexample.github.io/tcp_connection
@strong jetty
i have come across this error many times
what does it mean
src/main.zig:11:36: error: array literal requires address-of operator (&) to coerce to slice type '[]const u8'
try conn.stream.write([4]u8{ 0, 12, 3, 23 });
Arrays are not slices
and cannot coerce to slices
but the address to an array is possible to make a slice
how to fix this then
its asks for an array
fn write(self: Stream, buffer: []const u8) WriteError!usize
TODO in evented I/O mode, this implementation incorrectly uses the event loop's file system thread instead of non-blocking. It needs to be reworked to properly use non-blocking I/O.
how to create slices?
oh strings?
slices are strings right?
like "test"
Slices are not strings, but strings can be slices
oh
Read the langref a bit more
no i just saw an example he
passed a string
so i thought slices are strings lol
my bad
noted thanks
@strong jetty what does comptime_int means?