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.
pubconstLineHandler = struct {
line_number: usize = 1,
buffer: []constu8,
last_line_ending_index: ?usize = null,
/// Like incrementLineNumber but checks that the current char is a line ending first./// Returns the new line number if it was incremented, null otherwise.pubfnmaybeIncrementLineNumber(self: *LineHandler, cur_index: usize) ?usize {
constc = self.buffer[cur_index];
if (c == '\r'orc == '\n') {
returnself.incrementLineNumber(cur_index);
}
returnnull;
}
/// Increments line_number appropriately (handling line ending pairs)/// and returns the new line number if it was incremented, or null otherwise.pubfnincrementLineNumber(self: *LineHandler, cur_index: usize) ?usize {
if (self.currentIndexFormsLineEndingPair(cur_index)) {
self.last_line_ending_index = null;
returnnull;
} else {
self.line_number += 1;
self.last_line_ending_index = cur_index;
returnself.line_number;
}
}
/// \r\n and \n\r pairs are treated as a single line ending (but not \r\r \n\n)/// expects self.index and last_line_ending_index (if non-null) to contain line endings////// TODO: This is not really how the Win32 RC compiler handles line endings. Instead, it/// seems to drop all carriage returns during preprocessing and then replace all/// remaining line endings with well-formed CRLF pairs (e.g. `<CR>a<CR>b<LF>c` becomes `ab<CR><LF>c`)./// Handling this the same as the Win32 RC compiler would need control over the preprocessor,/// since Clang converts unpaired <CR> into unpaired <LF>.pubfncurrentIndexFormsLineEndingPair(self: *constLineHandler, cur_index: usize) bool {
if (self.last_line_ending_index == null) returnfalse;
// must immediately precede the current index, we know cur_index must
// be >= 1 since last_line_ending_index is non-null (so if the subtraction
// overflows it is a bug at the callsite of this function).
if (self.last_line_ending_index.? != cur_index - 1) returnfalse;
constcur_line_ending = self.buffer[cur_index];
constlast_line_ending = self.buffer[self.last_line_ending_index.?];
// sanity checkstd.debug.assert(cur_line_ending == '\r'orcur_line_ending == '\n');
std.debug.assert(last_line_ending == '\r'orlast_line_ending == '\n');
// can't be \n\n or \r\rif (last_line_ending == cur_line_ending) returnfalse;
returntrue;
}
}