authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-09-21 15:50:43+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-05 16:10:34+01:00
log44533f10fee130498fb811eabb72e2afdc3c0f56
tree39abf25b0c8fcb43d7b819bde651b2bf889b7f14
parent6c4efab10611b9fe807f2517d2eec2ac60ae4f5c

std: Introduce std.unicode.utf8ValidCodepoint


1 files changed, 25 insertions(+), 0 deletions(-)

lib/std/unicode.zig+25
......@@ -153,6 +153,15 @@ pub fn utf8Decode4(bytes: []const u8) Utf8Decode4Error!u21 {
153153 return value;
154154}
155155
156/// Returns true if the given unicode codepoint can be encoded in UTF-8.
157pub fn utf8ValidCodepoint(value: u21) bool {
158 return switch (value) {
159 0xD800...0xDFFF => false, // Surrogates range
160 0x110000...0x1FFFFF => false, // Above the maximum codepoint value
161 else => true,
162 };
163}
164
156165/// Returns the length of a supplied UTF-8 string literal in terms of unicode
157166/// codepoints.
158167/// Asserts that the data is valid UTF-8.
......@@ -785,3 +794,19 @@ test "utf8 count codepoints" {
785794 try testUtf8CountCodepoints();
786795 comptime testUtf8CountCodepoints() catch unreachable;
787796}
797
798fn testUtf8ValidCodepoint() !void {
799 testing.expect(utf8ValidCodepoint('e'));
800 testing.expect(utf8ValidCodepoint('ë'));
801 testing.expect(utf8ValidCodepoint('は'));
802 testing.expect(utf8ValidCodepoint(0xe000));
803 testing.expect(utf8ValidCodepoint(0x10ffff));
804 testing.expect(!utf8ValidCodepoint(0xd800));
805 testing.expect(!utf8ValidCodepoint(0xdfff));
806 testing.expect(!utf8ValidCodepoint(0x110000));
807}
808
809test "utf8 valid codepoint" {
810 try testUtf8ValidCodepoint();
811 comptime testUtf8ValidCodepoint() catch unreachable;
812}