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 sentinel-terminated array or pointer with .. as end index should give a sentinel-terminated slice #3766

Closed
andrewrk opened this issue Nov 25, 2019 · 3 comments
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

Follow-up from #3728. Depends on #863.

test "slice to the end of a null terminated pointer" {
    const string_literal = "hello, world";
    comptime expect(@typeOf(string_literal[5..]) == *const [7:0]u8);
    const slice: [:0]const u8 = string_literal;
    comptime expect(@typeOf(slice[5..]) == [*:0]const u8);
}
@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
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Mar 31, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 9, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 20, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jun 29, 2023
@N00byEdge
Copy link
Contributor

N00byEdge commented Jan 21, 2024

Should it? ptr[std.mem.len(ptr) + 1..] could definitely be an unterminated pointer, while ptr[std.mem.len(ptr) + 1..:0] clearly is terminated, and only two characters longer to write.

@mikdusan
Copy link
Member

wait, title asks for sentinel-terminated slice
but the OP example tests for a sentinel-terminated pointer
and current master produces a sentinel-terminated slice

so.... maybe OP example actually intended to test for sentinel-terminated slice and master is already there?

const std = @import("std");

pub fn main() void {
    const string_literal = "hello, world";
    //comptime expect(@typeOf(string_literal[5..]) == *const [7:0]u8);
    std.log.debug("A: {}", .{@TypeOf(string_literal)});

    const slice: [:0]const u8 = string_literal;
    //comptime expect(@typeOf(slice[5..]) == [*:0]const u8);
    std.log.debug("B: {}", .{@TypeOf(slice)});

    std.log.debug("C: {}", .{@TypeOf(slice[5..])});

    rtime(slice);
}

fn rtime(s: [:0]const u8) void {
    std.log.debug("D: {}", .{@TypeOf(s[5..])});
}

output:

debug: A: *const [12:0]u8
debug: B: [:0]const u8
debug: C: *const [7:0]u8
debug: D: [:0]const u8

@mlugg
Copy link
Member

mlugg commented Feb 5, 2025

Yeah, the title of this issue seems a little confused but it definitely seems like what it proposes is implemented and has been for a while.

The second assertion in the OP fails because the slice is comptime-known so you get an array pointer. Plus, you'd never get a many-pointer from that operation; you'd get a slice. But you do indeed get a sentinel-terminated slice as was intended.

@mlugg mlugg closed this as completed Feb 5, 2025
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

No branches or pull requests

4 participants