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 › Io › Threaded › lookupHostsReader Private Function lookupHostsReader Doc Source Private Function std.Io.Threaded.lookupHostsReader
fn lookupHostsReader (
t : *Threaded ,
host_name : HostName ,
resolved : *Io .Queue (HostName .LookupResult ),
options : HostName .LookupOptions ,
reader : *Io .Reader ,
) error Params File Io/Threaded.zig: 14875
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943 fn lookupHostsReader (
t : *Threaded ,
host_name : HostName ,
resolved : *Io .Queue (HostName .LookupResult ),
options : HostName .LookupOptions ,
reader : *Io .Reader ,
) error { ReadFailed , Canceled , UnknownHostName , Closed }!void {
const t_io = io (t );
var addresses_len : usize = 0 ;
var canonical_name : ?HostName = null ;
while (true ) {
const line = reader .takeDelimiterExclusive ('\n' ) catch |err | switch (err ) {
error .StreamTooLong => {
// Skip lines that are too long.
_ = reader .discardDelimiterInclusive ('\n' ) catch |er | switch (er ) {
error .EndOfStream => break ,
error .ReadFailed => |e | return e ,
};
continue ;
},
error .ReadFailed => |e | return e ,
error .EndOfStream => break ,
};
reader .toss (@min (1 , reader .bufferedLen ()));
var split_it = std .mem .splitScalar (u8 , if (is_windows and std .mem .endsWith (u8 , line , "\r" ))
line [0 .. line .len - 1 ]
else
line , '#' );
const no_comment_line = split_it .first ();
var line_it = std .mem .tokenizeAny (u8 , no_comment_line , " \t" );
const ip_text = line_it .next () orelse continue ;
var first_name_text : ?[]const u8 = null ;
while (line_it .next ()) |name_text | {
if (std .ascii .eqlIgnoreCase (name_text , host_name .bytes )) {
if (first_name_text == null ) first_name_text = name_text ;
break ;
}
} else continue ;
if (canonical_name == null ) {
if (options .canonical_name_buffer ) |buf | {
if (HostName .init (first_name_text .?)) |name_text | {
if (name_text .bytes .len <= buf .len ) {
const canonical_name_dest = buf [0 ..name_text .bytes .len ];
@memcpy (canonical_name_dest , name_text .bytes );
canonical_name = .{ .bytes = canonical_name_dest };
}
} else |_ | {}
}
}
if (options .family != .ip6 ) {
if (IpAddress .parseIp4 (ip_text , options .port )) |addr | {
try resolved .putOne (t_io , .{ .address = addr });
addresses_len += 1 ;
} else |_ | {}
}
if (options .family != .ip4 ) {
if (IpAddress .parseIp6 (ip_text , options .port )) |addr | {
try resolved .putOne (t_io , .{ .address = addr });
addresses_len += 1 ;
} else |_ | {}
}
}
if (canonical_name ) |canon_name | try resolved .putOne (t_io , .{ .canonical_name = canon_name });
if (addresses_len == 0 ) return error .UnknownHostName ;
}