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.

ForcedOrdinal

res.ForcedOrdinal
pub const ForcedOrdinal = struct

File

Code

pub const ForcedOrdinal = struct {
    pub fn fromBytes(bytes: SourceBytes) u16 {
        var i: usize = 0;
        var result: u21 = 0;
        while (bytes.code_page.codepointAt(i, bytes.slice)) |codepoint| : (i += codepoint.byte_len) {
            const c = switch (codepoint.value) {
                // Codepoints that would need a surrogate pair in UTF-16 are
                // broken up into their UTF-16 code units and each code unit
                // is interpreted as a digit.
                0x10000...0x10FFFF => {
                    const high = @as(u16, @intCast((codepoint.value - 0x10000) >> 10)) + 0xD800;
                    if (result != 0) result *%= 10;
                    result +%= high -% '0';

                    const low = @as(u16, @intCast(codepoint.value & 0x3FF)) + 0xDC00;
                    if (result != 0) result *%= 10;
                    result +%= low -% '0';
                    continue;
                },
                Codepoint.invalid => 0xFFFD,
                else => codepoint.value,
            };
            if (result != 0) result *%= 10;
            result +%= c -% '0';
        }
        return @truncate(result);
    }

    pub fn fromUtf16Le(utf16: [:0]const u16) u16 {
        var result: u16 = 0;
        for (utf16) |code_unit| {
            if (result != 0) result *%= 10;
            result +%= std.mem.littleToNative(u16, code_unit) -% '0';
        }
        return result;
    }
}