summaryrefslogtreecommitdiff
path: root/src/util.zig
blob: 41c35849bcef53f1c7ef3094fbbcd61638abbcfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const std = @import("std");

/// Returns an slice of all the fields in the given type replacing all '_' with '-'.
pub fn enumFieldsToStringSlice(comptime E: type) []const []const u8 {
    @setEvalBranchQuota(10000);
    var names: []const []const u8 = &[_][]const u8{};
    for (std.meta.fields(E)) |field| {
        var name: [field.name.len]u8 = undefined;
        _ = std.mem.replace(u8, field.name, &[_]u8{'_'}, &[_]u8{'-'}, &name);
        names = names ++ &[_][]const u8{&name};
    }
    return names;
}

pub fn enumFieldsToSlice(comptime T: type) []const T {
    var fields: []const T = &[_]T{};
    inline for (@typeInfo(T).Enum.fields) |enumField| {
        fields = fields ++ &[_]T{
            @field(T, enumField.name),
        };
    }
    return fields;
}