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.
pubfnparseFloat(comptimeT: type, s: []constu8) ParseFloatError!T {
if (@typeInfo(T) != .float) {
@compileError("Cannot parse a float into a non-floating point type.");
}
if (s.len == 0) {
returnerror.InvalidCharacter;
}
vari: usize = 0;
constnegative = s[i] == '-';
if (s[i] == '-'ors[i] == '+') {
i += 1;
}
if (s.len == i) {
returnerror.InvalidCharacter;
}
constn = parse.parseNumber(T, s[i..], negative) orelse {
returnparse.parseInfOrNan(T, s[i..], negative) orelseerror.InvalidCharacter;
};
if (n.hex) {
returnconvertHex(T, n);
}
if (convertFast(T, n)) |f| {
returnf;
}
if (T == f16orT == f32orT == f64) {
// If significant digits were truncated, then we can have rounding error
// only if `mantissa + 1` produces a different result. We also avoid
// redundantly using the Eisel-Lemire algorithm if it was unable to
// correctly round on the first pass.
if (convertEiselLemire(T, n.exponent, n.mantissa)) |bf| {
if (!n.many_digits) {
returnbf.toFloat(T, n.negative);
}
if (convertEiselLemire(T, n.exponent, n.mantissa + 1)) |bf2| {
if (bf.eql(bf2)) {
returnbf.toFloat(T, n.negative);
}
}
}
}
// Unable to correctly round the float using the Eisel-Lemire algorithm.
// Fallback to a slower, but always correct algorithm.
returnconvertSlow(T, s[i..]).toFloat(T, negative);
}