utils.zig (678B)
1 const std = @import("std"); 2 const log = std.log; 3 const mem = std.mem; 4 const meta = std.meta; 5 const os = std.os; 6 const unicode = std.unicode; 7 8 pub fn cast(comptime to: type) fn (*anyopaque) *to { 9 return (struct { 10 pub fn cast(module: *anyopaque) *to { 11 return @ptrCast(@alignCast(module)); 12 } 13 }).cast; 14 } 15 16 pub fn toUtf8(gpa: mem.Allocator, bytes: []const u8) ![]u32 { 17 const utf8 = try unicode.Utf8View.init(bytes); 18 var iter = utf8.iterator(); 19 20 var runes = try std.ArrayList(u32).initCapacity(gpa, bytes.len); 21 while (iter.nextCodepoint()) |rune| { 22 runes.appendAssumeCapacity(rune); 23 } 24 25 return runes.toOwnedSlice(); 26 }