Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

tagToId

lang.tagToId
pub fn tagToId(tag: []const u8) error

File

Code

pub fn tagToId(tag: []const u8) error{InvalidLanguageTag}!?LanguageId {
    const parsed = try parse(tag);
    // There are currently no language tags with assigned IDs that have
    // multiple suffixes, so we can skip the lookup.
    if (parsed.multiple_suffixes) return null;
    const longest_known_tag = comptime blk: {
        var len = 0;
        for (@typeInfo(LanguageId).@"enum".field_names) |field_name| {
            if (field_name.len > len) len = field_name.len;
        }
        break :blk len;
    };
    // If the tag is longer than the longest tag that has an assigned ID,
    // then we can skip the lookup.
    if (tag.len > longest_known_tag) return null;
    var normalized_buf: [longest_known_tag]u8 = undefined;
    // To allow e.g. `de-de_phoneb` to get looked up as `de-de`, we need to
    // omit the suffix, but only if the tag contains a valid alternate sort order.
    const tag_to_normalize = if (parsed.isSuffixValidSortOrder()) tag[0 .. tag.len - (parsed.suffix.?.len + 1)] else tag;
    const normalized_tag = normalizeTag(tag_to_normalize, &normalized_buf);
    return std.meta.stringToEnum(LanguageId, normalized_tag) orelse {
        // special case for a tag that has been mapped to the same ID
        // twice.
        if (std.mem.eql(u8, "ff_latn_ng", normalized_tag)) {
            return LanguageId.ff_ng;
        }
        return null;
    };
}