#concatenate 3 **comptime-unknown** strings but can't afford to use try catch or if
1 messages · Page 1 of 1 (latest)
?
catch the errors in your own code before sending it to the C function
you cant concatenate without the possibility of errors because allocation can always fail
hmm, let me give my code example
pub fn send(x: header_with_body) []const u8 {
const allocator = std.heap.page_allocator;
const dz = [_][]const u8{ x.header, "\n\n", x.body };
const res = try std.mem.concat(allocator, u8, &dz);
return res;
}
error: Function expected to return []const u8, but is returning whatever![]const u8
how can I make this function use std.mem.concat(allocator, u8, &dz); but not return an error
youd just do const res = std.mem.concat(allocator, u8, &dz) catch return "server_error"
but there's something wrong with that too
now the function can either return a heap allocated string or a statically allocated string
which is bad
since you have to free heap allocated strings but cant free statically failed allocated strings
I dont understand what issue youre trying to solve either
this function can return an error, there's no reason it shouldnt be able to
you only cant return an error from a function that follows the C calling convention
ya, this function is used in some other function which returns something to C. and I get error callConv(.C) type returning functoin can't return errors
yeah so the issue isnt with this function
the issue is in that function which is marked callconv(.C) and is calling this function
show me that function
uh, its very complex, like then that function is inside a wrapper which is called by another function, then passed as argument to other function.... its huge code!
Now, if I go and add ! to this it will take me through all my source code reaching the c file.
so I was trying if it was possible we can make this function not return an error and catch it in some way
well either way
just simply dont return an error from the callconv(.C) function lol
catch it in the body and do something else
yes, let me try and I'll get back
that error does seems vanished, but maybe taken over by another error:
can you just quickly please tell me the code to do this correctly:
pub fn path(of: []const u8, is: []const u8) bool {
const res = std.mem.tokenizeAny(u8, of, " ");
if (std.mem.eql(u8, res.index(3), is)) {
return true;
}
return false;
}
like, I want to get the 3rd index in zig, similar code in python:
of.split(" ")[3]
TokenIterator doesnt have a .index() method
and res needs to be mutable
hmm, can you please tell me a way to split string with delimiter " " like the code and get the 3rd index of it.
youd do something like
_ = res.next().?;
_ = res.next().?;
third_index = res.next().?;
note that this asserts that there are at least three items in the iterator
🥹 we are back with !
and the reason you cant just take an index is because it's lazy
it's not a list
you cant get the third item without computing the second item and you cant get the second item without computing the first one
the function containing it won't give an an error right?
no
.next() reutrns a ?[]const u8
but again
if there arent at least three entries in that iterator, this will crash your program on Debug/ReleaseSafe builds and cause illegal behavior on ReleaseFast builds
so if you arent 100% sure that's the case, you should handle those cases
error: expected type '*mem.TokenIterator(u8,.any)', found '*const mem.TokenIterator(u8,.any)'
I am getting this for some reason
yeah
because you declared res as a constant
even though it needs to be mutated to get the next token
no problem