mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-27 19:09:47 +03:00
Fix fmt UTF-8 characters as fill (#18533)
Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com>
This commit is contained in:
@@ -69,6 +69,19 @@ pub fn utf8Encode(c: u21, out: []u8) !u3 {
|
||||
return length;
|
||||
}
|
||||
|
||||
pub inline fn utf8EncodeComptime(comptime c: u21) [
|
||||
utf8CodepointSequenceLength(c) catch |err|
|
||||
@compileError(@errorName(err))
|
||||
]u8 {
|
||||
comptime var result: [
|
||||
utf8CodepointSequenceLength(c) catch
|
||||
unreachable
|
||||
]u8 = undefined;
|
||||
comptime assert((utf8Encode(c, &result) catch |err|
|
||||
@compileError(@errorName(err))) == result.len);
|
||||
return result;
|
||||
}
|
||||
|
||||
const Utf8DecodeError = Utf8Decode2Error || Utf8Decode3Error || Utf8Decode4Error;
|
||||
|
||||
/// Decodes the UTF-8 codepoint encoded in the given slice of bytes.
|
||||
@@ -525,6 +538,13 @@ fn testUtf8Encode() !void {
|
||||
try testing.expect(array[3] == 0b10001000);
|
||||
}
|
||||
|
||||
test "utf8 encode comptime" {
|
||||
try testing.expectEqualSlices(u8, "€", &utf8EncodeComptime('€'));
|
||||
try testing.expectEqualSlices(u8, "$", &utf8EncodeComptime('$'));
|
||||
try testing.expectEqualSlices(u8, "¢", &utf8EncodeComptime('¢'));
|
||||
try testing.expectEqualSlices(u8, "𐍈", &utf8EncodeComptime('𐍈'));
|
||||
}
|
||||
|
||||
test "utf8 encode error" {
|
||||
try comptime testUtf8EncodeError();
|
||||
try testUtf8EncodeError();
|
||||
|
||||
Reference in New Issue
Block a user