~andreafeletto/levee

ref: f2b74950b7431f6024814ff54cbc0949e29e9a5d levee/src/utils.zig -rw-r--r-- 786 bytes
f2b74950Andrea Feletto add args for help and modules 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const std = @import("std");
const mem = std.mem;
const meta = std.meta;
const unicode = std.unicode;

pub fn cast(comptime to: type) fn (*anyopaque) *to {
    return (struct {
        pub fn cast(module: *anyopaque) *to {
            return @ptrCast(*to, @alignCast(@alignOf(to), module));
        }
    }).cast;
}

pub fn Mask(comptime container: type) type {
    const len = meta.fields(container).len;
    return [len]bool;
}

pub fn toUtf8(gpa: mem.Allocator, bytes: []const u8) ![]u32 {
    const utf8 = try unicode.Utf8View.init(bytes);
    var iter = utf8.iterator();

    var runes = try gpa.alloc(u32, bytes.len);
    var i: usize = 0;
    while (iter.nextCodepoint()) |rune| : (i += 1) {
        runes[i] = rune;
    }

    runes = gpa.resize(runes, i).?;
    return runes;
}