#Getting non-undefined values from an array

1 messages · Page 1 of 1 (latest)

alpine raptor
#

I have this code:

var input_buf_long: [100]u8 = undefined;
pub fn main() !void {
    const stdout_file = std.io.getStdOut().writer();
    var bw = std.io.bufferedWriter(stdout_file);
    const stdout = bw.writer();
    const stdin = std.io.getStdIn().reader();
    _ = try stdin.readUntilDelimiterOrEof(input_buf_long[0..], '\n');

And i need to put actual characters in some other constant variable. Do i need to manually allocate another array with length of read symbols?

alpine raptor
#

Hacked this solution, seems okayish.

fn getNonUndefStr(buf_str: []const u8) []const u8 {
    var iter_val: u8 = buf_str[0];
    var list_l: usize = 0;
    while (iter_val != 0) {
        list_l += 1;
        iter_val = buf_str[list_l];
    }
    list_l -= 2;
    return buf_str[0..list_l];
}
meager widget
#
  1. readUntilDelimiterOrEof returns the slice of input_buf_long with the read symbols. You can also pass in &array here as array pointers coerce to slices.
  2. Stirngs don't end in a 0 byte in zig. They're slices which contain a length. You can represent null terminated strings using sentinel-terminated ptrs/slices if youd like: https://ziglang.org/documentation/master/#Sentinel-Terminated-Pointers but outside interacting with C code, its rare to need it