#std.mem.copy doesn't copy sentinel?

1 messages · Page 1 of 1 (latest)

umbral acorn
#

like the title suggests

var dest: [256]u8 = undefined;
var source: [:0]const u8 = "Test";

std.mem.copy(u8, &dest, source);

using step-by-step debugging, the sentinel of source isn't copied in dest, is that normal?

late scaffold
#

Yes that is normal

#

source has length 4, that doesn't include the sentinel

umbral acorn
#

so there isn't a std function to copy with the sentinel?

#

it's kinda annoying to have to setup the sentinel manually afterward

#

especially when it's in nested structures like that x)

late scaffold
#

You could try std.mem.copy(u8, &dest, source[0..source.len + 1])? idk if that'll work but it might

umbral acorn
#

i think it will trigger safety check, no?

late scaffold
#

Try it and see

umbral acorn
#

technically it will access out of bounds of the source

late scaffold
#

You're allowed to access the sentinel

umbral acorn
#

it works indeed, thx