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 standard library.
std › unicode › utf8ToUtf16LeArrayListImpl Private Function utf8ToUtf16LeArrayListImpl Doc Source Private Function std.unicode.utf8ToUtf16LeArrayListImpl
fn utf8ToUtf16LeArrayListImpl (result : *std .array_list .Managed (u16 ), utf8 : []const u8 , comptime surrogates : Surrogates ) !void Params File unicode.zig: 1152
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188 fn utf8ToUtf16LeArrayListImpl (result : *std .array_list .Managed (u16 ), utf8 : []const u8 , comptime surrogates : Surrogates ) !void {
assert (result .unusedCapacitySlice ().len >= utf8 .len );
var remaining = utf8 ;
vectorized : {
const chunk_len = std .simd .suggestVectorLength (u16 ) orelse break :vectorized ;
const Chunk = @Vector (chunk_len , u8 );
// Fast path. Check for and encode ASCII characters at the start of the input.
while (remaining .len >= chunk_len ) {
const chunk : Chunk = remaining [0 ..chunk_len ].*;
const mask : Chunk = @splat (0x80 );
if (@reduce (.Or , chunk & mask == mask )) {
// found a non ASCII code unit
break ;
}
const utf16_chunk = mem .nativeToLittle (@Vector (chunk_len , u16 ), chunk );
result .addManyAsArrayAssumeCapacity (chunk_len ).* = utf16_chunk ;
remaining = remaining [chunk_len ..];
}
}
const view = switch (surrogates ) {
.cannot_encode_surrogate_half => try Utf8View .init (remaining ),
.can_encode_surrogate_half => try Wtf8View .init (remaining ),
};
var it = view .iterator ();
while (it .nextCodepoint ()) |codepoint | {
if (codepoint < 0x10000 ) {
try result .append (mem .nativeToLittle (u16 , @intCast (codepoint )));
} else {
const high = @as (u16 , @intCast ((codepoint - 0x10000 ) >> 10 )) + 0xD800 ;
const low = @as (u16 , @intCast (codepoint & 0x3FF )) + 0xDC00 ;
try result .appendSlice (&.{ mem .nativeToLittle (u16 , high ), mem .nativeToLittle (u16 , low ) });
}
}
}