#How do I properly create new slice from std.mem.trim()?

1 messages · Page 1 of 1 (latest)

bright badge
#
var file_name = [_:0]u8{0} ** 255;
    _ = @import("bindings/win32.zig").GetModuleFileNameA(null, &file_name, 255);
    const trimmed_file_name = std.mem.trimRight(u8, &file_name, " ");
    std.log.info("File name (len {d}): {s}", .{ trimmed_file_name.len, trimmed_file_name });

The trimmed file name is still 255 length and still has null bytes on the right

timber blade
bright badge
#

what does that do

#

and where should i put that?

timber blade
#
/// Takes a sentinel-terminated pointer and returns a slice, iterating over the
/// memory to find the sentinel and determine the length.
/// Pointer attributes such as const are preserved.
/// `[*c]` pointers are assumed to be non-null and 0-terminated.
#
const file_name_slice = std.mem.span(&file_name);
std.log.info("File name (len {d}): {s}", .{ file_name_slice.len, file_name_slice });
#

also worth noting that you might just want to use std.fs.selfExePath or std.fs.selfExePathAlloc if i understand what you're trying to do correctly

bright badge
#

oh

#

i need the exe name

#

so i was gonna use std.mem.split or whatever its called

#

on the /

timber blade
#

std.fs.path.basename would be better

bright badge
#

basenameWindows better if im only working with windows?

#

i didnt know all these helper methods existed

timber blade