zig icon indicating copy to clipboard operation
zig copied to clipboard

Fix Utf8View example to compile.

Open davideger opened this issue 1 year ago • 2 comments

Updated Utf8View example to format the single codepoint UTF-8 slice with {s}

davideger avatar Dec 15 '23 21:12 davideger

This is a good fix, although in the long run I wonder if it would be better to replace such code examples in doc comments with doctests, such as

test Utf8View {
    var utf8 = (try std.unicode.Utf8View.init("test")).iterator();
    try std.testing.expectEqual(@as(?u21, 't'), utf8.nextCodepoint());
    try std.testing.expectEqual(@as(?u21, 'e'), utf8.nextCodepoint());
    try std.testing.expectEqual(@as(?u21, 's'), utf8.nextCodepoint());
    try std.testing.expectEqual(@as(?u21, 't'), utf8.nextCodepoint());
    try std.testing.expectEqual(@as(?u21, null), utf8.nextCodepoint());
}

(this example would be much nicer with #17431, or alternatively for now, try std.testing.expect(utf8.nextCodepoint() == 't'), etc.)

In addition to being full tests in their own right (ensuring they remain valid over time), doctests will also show up in the generated documentation, with one existing example being std.mem.readInt.

ianprime0509 avatar Dec 19 '23 05:12 ianprime0509

I think your test is a great addition to the library independent of this fix.

As a beginner at zig, I find it nice to have a small snippet of idiomatic zig that I can copy and paste into a new function.

davideger avatar Dec 19 '23 06:12 davideger

thanks for the contribution @davideger !

thejoshwolfe avatar Jan 01 '24 23:01 thejoshwolfe