Fix bug with 2-byte UTF-8 chars

This commit is contained in:
Richard Feldman 2022-07-01 22:11:37 -04:00
parent 598d03737c
commit 9803e3ab01
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798

View file

@ -557,6 +557,8 @@ pub fn strToScalars(string: RocStr) callconv(.C) RocList {
answer_index += 1;
}
answer.length = answer_index;
return answer;
}
@ -588,6 +590,34 @@ test "strToScalars: Multiple ASCII chars" {
try expect(RocList.eql(actual, expected));
}
test "strToScalars: One 2-byte UTF-8 character" {
const str = RocStr.fromSlice("é");
defer RocStr.deinit(str);
const expected_array = [_]u32{ 233 };
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
defer RocList.deinit(expected, u32);
const actual = strToScalars(str);
defer RocList.deinit(actual, u32);
try expect(RocList.eql(actual, expected));
}
test "strToScalars: Multiple 2-byte UTF-8 characters" {
const str = RocStr.fromSlice("Cäfés");
defer RocStr.deinit(str);
const expected_array = [_]u32{ 67, 228, 102, 233, 115 };
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
defer RocList.deinit(expected, u32);
const actual = strToScalars(str);
defer RocList.deinit(actual, u32);
try expect(RocList.eql(actual, expected));
}
// Str.fromInt
pub fn exportFromInt(comptime T: type, comptime name: []const u8) void {
comptime var f = struct {