#Array to Slice

1 messages · Page 1 of 1 (latest)

crystal slate
#

For the life of me I've tried so many variations, I just can't manage to concatenate the slices during comptime. With this particular example I'm getting the error error: expected type '[]comptime-regex.PathElement', found '*const [1]comptime-regex.PathElement'. I would like to keep using ++ if possible just because I want to understand how to use it.

const std = @import("std");
const PathElementType = enum { element, attribute };
const PathElement = struct { type: PathElementType, value: []const u8 };

fn tokenizeUserPath(comptime userpath: []const u8) []PathElement {
    comptime var all_elements: []PathElement = &.{};
    comptime var tokenizer = std.mem.tokenizeAny(u8, userpath, "/#");

    comptime var idx: comptime_int = 0;

    comptime {
        while (tokenizer.next()) |content| {
            const new_token: [1]PathElement = [1]PathElement{PathElement{ .type = PathElementType.element, .value = content }};

            all_elements = all_elements ++ new_token[0..1];
            idx += 1;
        }
    }
    return all_elements;
}

test {
    const pathTokens: []PathElement = tokenizeUserPath("/categories/messages/unique#5");
    _ = pathTokens;
}

willow bone
#

you need to make all_elements a const slice for that to work

comptime var all_elements: []const PathElement = &.{};
crystal slate
#

dang, I can't believe that was it

#

I was trying all sorts of voodoo magic like ptrcast and constcast and all that

willow bone
#

the issue with mutable slices ([]T) is that all temporaries in zig are constant and const won't coerce to mutable

crystal slate
#

ok, lesson learned! thank you 🙂

#

if I may, are you aware of any places in the docs or even a blog post that might dig into that a bit?

#

I don't know if this thread is about the same issue... seems similar. https://ziggit.dev/t/comptime-mutable-memory-changes/3702

willow bone
#

does that help answer or are you referring more to the const/mut coercion rules?

#

and those comptime mutable memory changes were a bit of a different topic.

#

but related.

crystal slate
#

That will definitely help. I appreciate it!

willow bone
#

for sure. oh just noticed you're using tokenizeAny() which splits on any of those characters, not the exact match. pretty sure you want tokenizeSequence() or maybe splitSequence() if thats not a thing.

crystal slate
#

Oh ok, I'll have a look at those. Is there a function that would do a similar thing but also give me which of the separators was found for the match? Otherwise I'll probably have to iterate through every character and manually check what kind of token it is.

willow bone
#

oic nevermind. seems like thats what you wanted. i misunderstood.

crystal slate
#

what I'd really like for is to get the token "type" and its value

willow bone
#

i forget but think you might be able to check the iterator state something like iter.buf[iter.index]

crystal slate
#

oooh wow ok! I'll dig into that as well then. 🙂

willow bone
#

and you might want to use @compileLog() for logging those