Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slicing syntax which can obtain sentinel terminated types from non-sentinel-terminated types #3770

Closed
1 of 3 tasks
andrewrk opened this issue Nov 25, 2019 · 2 comments · Fixed by #3940
Closed
1 of 3 tasks
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Nov 25, 2019

Moved from #3731.

const std = @import("std");
const assert = std.debug.assert;

test "obtaining a null terminated slice" {
    // here we have a normal array
    var buf: [50]u8 = undefined;

    buf[0] = 'a';
    buf[1] = 'b';
    buf[2] = 'c';
    buf[3] = 0;

    // now we obtain a null terminated slice:
    const ptr = buf[0..3 :0];

    // ptr is a pointer to null-terminated array,
    // because the len was comptime known (See #863)
    comptime assert(@typeOf(ptr) == *[3:0]u8);

    var runtime_len: usize = 3;
    const ptr2 = buf[0..runtime_len :0];
    // ptr2 is a null-terminated slice
    comptime assert(@typeOf(ptr2) == [:0]u8);


    buf[3] += 1;
    _ = buf[0..3 :0]; // safety panic: slice sentinel assertion failed
}

Along with this:

There are already quite a few places this would be handy:

../lib/std/fs.zig:    // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
../lib/std/os.zig:        // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
../lib/std/os.zig:        // TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3731
../lib/std/os.zig:    // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
../lib/std/os.zig:            // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
../lib/std/os.zig:    // TODO avoid @ptrCast here using slice syntax with https://github.com/ziglang/zig/issues/3731
@andrewrk andrewrk added proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. accepted This proposal is planned. labels Nov 25, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone Nov 25, 2019
@JesseRMeyer
Copy link

Nit:

// ptr2 is a null-terminated slice
comptime assert(@typeOf(ptr) == [:0]u8);

Should be:

// ptr2 is a null-terminated slice
comptime assert(@typeOf(ptr2) == [:0]u8);

@daurnimator
Copy link
Collaborator

Would be interesting to use this for e.g. newline terminated strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants