Function wtf8ToUtf8LossyAllocZ [src]

Prototype

pub fn wtf8ToUtf8LossyAllocZ(allocator: mem.Allocator, wtf8: []const u8) error{ InvalidWtf8, OutOfMemory }![:0]u8

Parameters

allocator: mem.Allocatorwtf8: []const u8

Possible Errors

InvalidWtf8
OutOfMemory

Example

test wtf8ToUtf8LossyAllocZ { const invalid_utf8 = "\xff"; try testing.expectError(error.InvalidWtf8, wtf8ToUtf8LossyAllocZ(testing.allocator, invalid_utf8)); { const ascii = "abcd"; const utf8 = try wtf8ToUtf8LossyAllocZ(testing.allocator, ascii); defer testing.allocator.free(utf8); try testing.expectEqualStrings("abcd", utf8); } { const surrogate_half = "ab\xed\xa0\xbdcd"; const utf8 = try wtf8ToUtf8LossyAllocZ(testing.allocator, surrogate_half); defer testing.allocator.free(utf8); try testing.expectEqualStrings("ab\u{FFFD}cd", utf8); } { // If the WTF-8 is not well-formed, each surrogate half is converted into a separate // replacement character instead of being interpreted as a surrogate pair. const encoded_surrogate_pair = "ab\xed\xa0\xbd\xed\xb2\xa9cd"; const utf8 = try wtf8ToUtf8LossyAllocZ(testing.allocator, encoded_surrogate_pair); defer testing.allocator.free(utf8); try testing.expectEqualStrings("ab\u{FFFD}\u{FFFD}cd", utf8); } }

Source

pub fn wtf8ToUtf8LossyAllocZ(allocator: mem.Allocator, wtf8: []const u8) error{ InvalidWtf8, OutOfMemory }![:0]u8 { const utf8 = try allocator.allocSentinel(u8, wtf8.len, 0); errdefer allocator.free(utf8); try wtf8ToUtf8Lossy(utf8, wtf8); return utf8; }