| 1 | const std = @import("std"); |
| 2 | const testing = std.testing; |
| 3 | |
| 4 | pub const Entity = struct { |
| 5 | entity: []const u8, |
| 6 | codepoints: Codepoints, |
| 7 | characters: []const u8, |
| 8 | }; |
| 9 | |
| 10 | pub const Codepoints = union(enum) { |
| 11 | Single: u32, |
| 12 | Double: [2]u32, |
| 13 | }; |
| 14 | |
| 15 | pub const ENTITIES = @import("entities.zig").ENTITIES; |
| 16 | |
| 17 | fn compare(lhs: Entity, rhs: Entity) std.math.Order { |
| 18 | return std.mem.order(u8, lhs.entity, rhs.entity); |
| 19 | } |
| 20 | |
| 21 | pub fn lookup(entity: []const u8) ?Entity { |
| 22 | const context: Entity = .{ .entity = entity, .codepoints = .{ .Single = 0 }, .characters = "" }; |
| 23 | const maybe_index = std.sort.binarySearch(Entity, &ENTITIES, context, compare); |
| 24 | |
| 25 | if (maybe_index) |index| { |
| 26 | return ENTITIES[index]; |
| 27 | } |
| 28 | return null; |
| 29 | } |