Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,32 @@ const RAW_POINTERS = @embedFile("./pointers.bin");
const MAPPING = @embedFile("./mapping.txt");
var POINTERS: *[]const Ptr = @ptrCast(@alignCast(@constCast(&RAW_POINTERS)));

comptime {
if (@sizeOf(Ptr) != 3) {
@compileError("Ptr layout mismatch");
}
}

// Convert Unicode points to their ASCII representation. Write to the dest, and return the slice.
// If not found, return null. Additionally, the function can also write an empty string.
pub fn getReplacement(cp: u21) ?[]const u8 {
const ptr_size = @sizeOf(Ptr);
comptime {
if (RAW_POINTERS.len % ptr_size != 0) {
@compileError("pointers.bin size is not a multiple of Ptr");
}
}

const i = @as(usize, cp);
if (i >= POINTERS.*.len) {
// DO NOT USE POINTERS.*.len
const count = RAW_POINTERS.len / ptr_size;
if (i >= count) {
return null;
}

// Trick
POINTERS.*.len = count;

const p = POINTERS.*[i];

// if length is 1 or 2, then the "pointer" data is used to store the char
Expand Down