#Cast a []const u8 to []const u21
1 messages · Page 1 of 1 (latest)
Maybe std.ArrayListUnmanaged has a method to do the cast, because what I am doing is:
try line.appendSlice(allocator, s);```
where s is the []const u8 and line a std.ArrayList(u21)
there is no cast that applies here because u8 and u21 are different sizes
and what exactly are you looking for here, because u21 is a very strange integer
@as(u21, @intCast(c))
where c = u8
unicode character
and the []const u8 is UTF8?
uhhhh not sure
because what you likely want here is a unicode iterator; the slice of u8 and the slice of unicode codepoints are very likely different lengths
because utf-8 is a multi-byte encoding
no []const u8 is a single line of file
var split = std.mem.splitScalar(u8, data, '\n');
while (split.next()) |s| {
var line = Line{};
errdefer line.deinit(allocator);
try line.appendSlice(allocator, s);
try tab.lines.append(line);
}```
and the file is using what encoding?
oh wait I think I get what you're saying
because the encoding determines how each codepoint is encoded in those bytes
yeah...
var iter = std.unicode.Utf8Iterator{ .bytes = s, .i = 0 };
while (iter.nextCodepoint()) |unicode_char| {
try line.append(allocator, unicode_char);
}
try tab.lines.append(line);
like this?
where s is []const u8
you're intended to obtain a Utf8Iterator from a Utf8View
but otherwise yes
because Utf8Iterator requires validated utf8
hmmm okay
so
var view = try std.unicode.Utf8View.init(s);
var iter = view.iterator()
view can be const, but yes
@glacial mantle sorry for the ping, but is there a way to convert []const u21 to []u8?
utf8Encode
usually in this form: https://github.com/ziglang/zig/blob/master/lib/std/unicode.zig#L866-L869