1// A code point's Unicode properties, from the generated tables, and2// UTF-8 decoding.34import std::vector;5import std::option;67import tables;89// The packed value of the code point's properties (tables.alloy).10exp fn properties(c: u32) -> u32 {11 if (c >= 0x110000) {12 return properties(0xFFFD);13 }14 const chunk = TOP[(c >> 9) to u64] to u64;15 const block = MIDDLE[chunk * 32 + (c >> 4 & 31) to u64] to u64;16 return VALUES[BLOCKS[block * 16 + (c & 15) to u64] to u64];17}1819exp fn line_class(value: u32) -> u32 {20 return value & 63;21}2223exp fn grapheme_class(value: u32) -> u32 {24 return (value >> GCB_SHIFT) & 15;25}2627exp fn conjunct_class(value: u32) -> u32 {28 return (value >> INCB_SHIFT) & 3;29}3031// The code point's simple case folding (CaseFolding.txt's C and S): `A`32// is `a`, `Σ` and `ς` are `σ`.33exp fn fold(c: u32) -> u32 {34 // the last run starting at or before it35 var low: u64 = 0;36 var high = FOLD_RUNS.length();37 while (low < high) {38 const middle = (low + high) / 2;39 if (FOLD_RUNS[middle] >> 11 <= c) {40 low = middle + 1;41 } else {42 high = middle;43 }44 }45 if (low == 0) {46 return c;47 }48 const run = FOLD_RUNS[low - 1];49 const step = (run & 1) + 1;50 const offset = c - (run >> 11);51 if (offset < ((run >> 1) & 1023) * step && offset % step == 0) {52 return c +% FOLD_DELTAS[low - 1];53 }54 return c;55}5657// A code point decoded and the bytes it took: an ill-formed sequence is58// U+FFFD taking one byte.59exp type Decoded = struct {60 exp c: u32,61 exp length: u64,62};6364exp fn decode(text: &[u8], at: u64) -> Decoded {65 const b0 = text[at] to u32;66 if (b0 < 0x80) {67 return Decoded { .c = b0, .length = 1 };68 }69 var need: u64 = 0;70 var c: u32 = 0;71 var least: u32 = 0;72 if (b0 >= 0xC2 && b0 <= 0xDF) {73 need = 1;74 c = b0 & 0x1F;75 least = 0x80;76 } else if (b0 >= 0xE0 && b0 <= 0xEF) {77 need = 2;78 c = b0 & 0x0F;79 least = 0x800;80 } else if (b0 >= 0xF0 && b0 <= 0xF4) {81 need = 3;82 c = b0 & 0x07;83 least = 0x10000;84 } else {85 return Decoded { .c = 0xFFFD, .length = 1 };86 }87 if (at + need >= text.length()) {88 return Decoded { .c = 0xFFFD, .length = 1 };89 }90 for ([1..need + 1]) |k| {91 const b = text[at + k] to u32;92 if ((b & 0xC0) != 0x80) {93 return Decoded { .c = 0xFFFD, .length = 1 };94 }95 c = (c << 6) | (b & 0x3F);96 }97 if (c < least || c > 0x10FFFF || (c >= 0xD800 && c <= 0xDFFF)) {98 return Decoded { .c = 0xFFFD, .length = 1 };99 }100 return Decoded { .c = c, .length = need + 1 };101}102103// The code point as UTF-8, appended.104exp fn encode(out: &var Vector<u8>, c: u32) {105 if (c < 0x80) {106 out.push(c to u8);107 } else if (c < 0x800) {108 out.push((0xC0 | (c >> 6)) to u8);109 out.push((0x80 | (c & 0x3F)) to u8);110 } else if (c < 0x10000) {111 out.push((0xE0 | (c >> 12)) to u8);112 out.push((0x80 | ((c >> 6) & 0x3F)) to u8);113 out.push((0x80 | (c & 0x3F)) to u8);114 } else {115 out.push((0xF0 | (c >> 18)) to u8);116 out.push((0x80 | ((c >> 12) & 0x3F)) to u8);117 out.push((0x80 | ((c >> 6) & 0x3F)) to u8);118 out.push((0x80 | (c & 0x3F)) to u8);119 }120}121122// The code points the character canonically decomposes into, if it123// does: two, or one and 0.124exp fn decomposition(c: u32) -> Option<[u32 : 2]> {125 var low: u64 = 0;126 var high = DECOMPOSITIONS.length();127 while (low < high) {128 const middle = (low + high) / 2;129 const entry = DECOMPOSITIONS[middle];130 const found = (entry >> 42) to u32;131 if (found < c) {132 low = middle + 1;133 } else if (found > c) {134 high = middle;135 } else {136 return ::Some([((entry >> 21) & 0x1FFFFF) to u32, (entry & 0x1FFFFF) to u32]);137 }138 }139 return ::None;140}141142// The character the pair canonically composes into, if any (Hangul143// syllables left to composed_hangul).144exp fn composition(first: u32, second: u32) -> Option<u32> {145 var low: u64 = 0;146 var high = COMPOSE_ORDER.length();147 while (low < high) {148 const middle = (low + high) / 2;149 const entry = DECOMPOSITIONS[COMPOSE_ORDER[middle] to u64];150 const a = ((entry >> 21) & 0x1FFFFF) to u32;151 const b = (entry & 0x1FFFFF) to u32;152 if (a < first || (a == first && b < second)) {153 low = middle + 1;154 } else if (a == first && b == second) {155 return ::Some((entry >> 42) to u32);156 } else {157 high = middle;158 }159 }160 return ::None;161}162163// The code point's canonical combining class, 0 for a starter.164exp fn combining_class(c: u32) -> u32 => (properties(c) >> COMBINING_SHIFT) & 255;165166// The code point's Script (Unknown past Unicode's range).167exp fn script(c: u32) -> Script {168 if (c >= 0x110000) {169 return Script::Unknown;170 }171 const chunk = SCRIPT_TOP[(c >> 9) to u64] to u64;172 const block = SCRIPT_MIDDLE[chunk * 32 + (c >> 4 & 31) to u64] to u64;173 return SCRIPT_BLOCKS[block * 16 + (c & 15) to u64] to Script;174}175176// The scripts of the code point's Script_Extensions, when it has them177// (its value's SCRIPT_EXTENDED), as indices of Script; none otherwise.178exp fn script_extensions(c: u32) -> &[u8] {179 var low: u64 = 0;180 var high = SCRIPT_EXTENSION_RANGES.length() / 3;181 while (low < high) {182 const middle = (low + high) / 2;183 const at = middle * 3;184 if (c < SCRIPT_EXTENSION_RANGES[at]) {185 high = middle;186 } else if (c > SCRIPT_EXTENSION_RANGES[at + 1]) {187 low = middle + 1;188 } else {189 const set = SCRIPT_EXTENSION_RANGES[at + 2] to u64;190 const count = SCRIPT_EXTENSION_SETS[set] to u64;191 return &SCRIPT_EXTENSION_SETS[set + 1..set + 1 + count];192 }193 }194 return &SCRIPT_EXTENSION_SETS[..0];195}196197// A paired bracket (BidiBrackets.txt): the bracket it pairs with, and198// whether it is the closing one.199exp type Bracket = struct {200 exp other: u32,201 exp closing: bool,202};203204exp fn bracket(c: u32) -> Option<Bracket> {205 var low: u64 = 0;206 var high = BRACKETS.length() / 2;207 while (low < high) {208 const middle = (low + high) / 2;209 const found = BRACKETS[middle * 2];210 if (found < c) {211 low = middle + 1;212 } else if (found > c) {213 high = middle;214 } else {215 const pair = BRACKETS[middle * 2 + 1];216 return ::Some(Bracket {217 .other = pair & 0x7FFFFFFF,218 .closing = (pair & 0x80000000) != 0219 });220 }221 }222 return ::None;223}224225// The ISO 15924 code of the script, as `0x48616E69` for `Hani`.226exp fn script_code(s: Script) -> u32 => SCRIPT_CODES[s to u64];227228fn general_byte(c: u32) -> u8 {229 if (c >= 0x110000) {230 return 2; // Unassigned231 }232 const chunk = GENERAL_TOP[(c >> 9) to u64] to u64;233 const block = GENERAL_MIDDLE[chunk * 32 + (c >> 4 & 31) to u64] to u64;234 return GENERAL_BLOCKS[block * 16 + (c & 15) to u64];235}236237// The code point's General_Category.238exp fn general_category(c: u32) -> GeneralCategory => (general_byte(c) & 31) to GeneralCategory;239240// The code point's Joining_Type, X where ArabicShaping.txt lists none.241exp fn joining_type(c: u32) -> JoiningType => (general_byte(c) >> 5) to JoiningType;242243// The Hangul syllable a leading consonant and a vowel, or an LV syllable244// and a trailing consonant, compose into, if they do.245exp fn composed_hangul(a: u32, b: u32) -> Option<u32> {246 if (a >= 0x1100 && a < 0x1100 + 19 && b >= 0x1161 && b < 0x1161 + 21) {247 return ::Some(0xAC00 + ((a - 0x1100) * 21 + (b - 0x1161)) * 28);248 }249 if (250 a >= 0xAC00251 && a < 0xAC00 + 11172252 && (a - 0xAC00) % 28 == 0253 && b > 0x11A7254 && b < 0x11A7 + 28255 ) {256 return ::Some(a + (b - 0x11A7));257 }258 return ::None;259}