Skip to content

Fix Utf8View example to compile. #18288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2024
Merged

Conversation

davideger
Copy link
Contributor

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

@ianprime0509
Copy link
Contributor

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.

@davideger
Copy link
Contributor Author

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.

@thejoshwolfe thejoshwolfe merged commit e426ae4 into ziglang:master Jan 1, 2024
@thejoshwolfe
Copy link
Contributor

thanks for the contribution @davideger !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants