Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
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

writeCodepoint

Private Function
std.zon.Serializer.writeCodepoint
fn writeCodepoint(
    self: *Serializer,
    codepoint: u21,
    options: WriteCodepointOptions,
) CodePointError!void

Write a Unicode codepoint to the writer using the given options.

Returns error.InvalidCodepoint if codepoint is not a valid Unicode codepoint.

Params

File

zon/Serializer.zig:373
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
fn writeCodepoint(
    self: *Serializer,
    codepoint: u21,
    options: WriteCodepointOptions,
) CodePointError!void {
    switch (codepoint) {
        // Printable ASCII
        ' ', '!', '#'...'&', '('...'[', ']'...'~' => try self.writer.writeByte(@intCast(codepoint)),
        // Unprintable ASCII
        0x00...0x08, 0x0B, 0x0C, 0x0E...0x1F, 0x7F => try self.writer.print("\\x{x:0>2}", .{codepoint}),
        // ASCII with special escapes
        '\n' => try self.writer.writeAll("\\n"),
        '\r' => try self.writer.writeAll("\\r"),
        '\t' => try self.writer.writeAll("\\t"),
        '\\' => try self.writer.writeAll("\\\\"),
        // Quotes need escaping if they conflict with the in-use quote character
        '\'' => if (options.quote_style == .single) try self.writer.writeAll("\\'") else try self.writer.writeByte('\''),
        '\"' => if (options.quote_style == .double) try self.writer.writeAll("\\\"") else try self.writer.writeByte('"'),

        // Surrogates can only be written with an escape
        0xD800...0xDFFF => try self.writer.print("\\u{{{x}}}", .{codepoint}),
        // Other valid codepoints
        0x80...0xD7FF, 0xE000...0x10FFFF => if (options.escape_non_ascii) {
            try self.writer.print("\\u{{{x}}}", .{codepoint});
        } else {
            var buf: [7]u8 = undefined;
            const len = std.unicode.utf8Encode(codepoint, &buf) catch unreachable;
            try self.writer.writeAll(buf[0..len]);
        },
        // Invalid codepoints
        0x110000...std.math.maxInt(u21) => return error.InvalidCodepoint,
    }
}