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 standard library. Private Function
fnutf16LeToUtf8Impl(utf8: []u8, utf16le: []constu16, comptimesurrogates: Surrogates) (switch (surrogates) {
.cannot_encode_surrogate_half => Utf16LeToUtf8Error,
.can_encode_surrogate_half => error{},
})!usize {
vardest_index: usize = 0;
varremaining = utf16le;
vectorized: {
constchunk_len = std.simd.suggestVectorLength(u16) orelsebreak :vectorized;
constChunk = @Vector(chunk_len, u16);
// Fast path. Check for and encode ASCII characters at the start of the input.while (remaining.len >= chunk_len) {
constchunk: Chunk = remaining[0..chunk_len].*;
constmask: Chunk = @splat(mem.nativeToLittle(u16, 0x7F));
if (@reduce(.Or, chunk | mask != mask)) {
// found a non ASCII code unitbreak;
}
constascii_chunk: @Vector(chunk_len, u8) = @truncate(mem.nativeToLittle(Chunk, chunk));
utf8[dest_index..][0..chunk_len].* = ascii_chunk;
dest_index += chunk_len;
remaining = remaining[chunk_len..];
}
}
switch (surrogates) {
.cannot_encode_surrogate_half => {
varit = Utf16LeIterator.init(remaining);
while (tryit.nextCodepoint()) |codepoint| {
dest_index += utf8Encode(codepoint, utf8[dest_index..]) catch |err| switch (err) {
// The maximum possible codepoint encoded by UTF-16 is U+10FFFF,// which is within the valid codepoint range.error.CodepointTooLarge => unreachable,
// We know the codepoint was valid in UTF-16, meaning it is not// an unpaired surrogate codepoint.error.Utf8CannotEncodeSurrogateHalf => unreachable,
};
}
},
.can_encode_surrogate_half => {
varit = Wtf16LeIterator.init(remaining);
while (it.nextCodepoint()) |codepoint| {
dest_index += wtf8Encode(codepoint, utf8[dest_index..]) catch |err| switch (err) {
// The maximum possible codepoint encoded by UTF-16 is U+10FFFF,// which is within the valid codepoint range.error.CodepointTooLarge => unreachable,
};
}
},
}
returndest_index;
}
Asserts that the output buffer is big enough. Returns end byte index into utf8.