| 1 | const std = @import("std"); |
| 2 | |
| 3 | pub const ucd = @import("unicode-ucd"); |
| 4 | |
| 5 | pub fn hasDerivedCoreProperty(cp: u32, comptime prop: ucd.derived_core_properties.CoreProperty.Property) bool { |
| 6 | @setEvalBranchQuota(100_000); |
| 7 | inline for (ucd.derived_core_properties.data) |item| { |
| 8 | if (item.property == prop) { |
| 9 | if (cp >= item.from and cp <= item.to) { |
| 10 | return true; |
| 11 | } |
| 12 | } |
| 13 | } |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | pub fn hasProperty(cp: u32, comptime prop: ucd.prop_list.PropList.Property) bool { |
| 18 | @setEvalBranchQuota(100_000); |
| 19 | inline for (ucd.prop_list.data) |item| { |
| 20 | if (item.property == prop) { |
| 21 | if (cp >= item.from and cp <= item.to) { |
| 22 | return true; |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | pub fn hasEmojiCategory(cp: u32, comptime cat: ucd.emoji.Emoji.Category) bool { |
| 30 | @setEvalBranchQuota(100_000); |
| 31 | inline for (ucd.emoji.data) |item| { |
| 32 | if (item.category == cat) { |
| 33 | if (cp >= item.from and cp <= item.to) { |
| 34 | return true; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | /// {0,0} means not found |
| 42 | pub fn age(cp: u32) [2]u8 { |
| 43 | @setEvalBranchQuota(100_000); |
| 44 | inline for (ucd.derived_age.data) |item| { |
| 45 | if (cp >= item.from and cp <= item.to) { |
| 46 | return item.since; |
| 47 | } |
| 48 | } |
| 49 | return .{ 0, 0 }; |
| 50 | } |
| 51 | |
| 52 | pub fn eastAsianWidth(cp: u32) ?ucd.EastAsianWidth { |
| 53 | @setEvalBranchQuota(100_000); |
| 54 | inline for (ucd.east_asian_width.data) |item| { |
| 55 | if (cp >= item.from and cp <= item.to) { |
| 56 | return item.prop; |
| 57 | } |
| 58 | } |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | pub fn bidiPairedBracketType(cp: u32) ?ucd.bidi_brackets.BracketPairing.Type { |
| 63 | @setEvalBranchQuota(100_000); |
| 64 | inline for (ucd.bidi_brackets.data) |item| { |
| 65 | if (cp == item.codepoint) { |
| 66 | return item.type; |
| 67 | } |
| 68 | } |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | pub fn toLower(cp: u32) ?u32 { |
| 73 | if (cp > std.math.maxInt(u21)) return null; |
| 74 | return ucd.unicode_data.find(@intCast(cp))[11] orelse null; // https://github.com/ziglang/zig/issues/16765 |
| 75 | } |