1// Extended grapheme clusters, by UAX #29's rules (Unicode 16.0): what a2// caret steps over and a selection takes whole.34import std::vector;56import tables;7import properties;89// The byte offsets where clusters start, then the text's length: every10// boundary, the first and last included (none for empty text).11exp fn grapheme_boundaries(text: &[u8]) -> Vector<u64> {12 var out = Vector<u64>::empty();13 if (text.length() == 0) {14 return move out;15 }16 out.push(0);17 var state = GraphemeState::start();18 var at: u64 = 0;19 while (at < text.length()) {20 const d = decode(&text, at);21 const value = properties(d.c);22 if (at > 0 && state.breaks_before(value)) {23 out.push(at);24 }25 state.advance(value);26 at += d.length;27 }28 out.push(text.length());29 return move out;30}3132// The boundary after `at`, itself a boundary, or the text's length.33exp fn next_grapheme(text: &[u8], at: u64) -> u64 {34 var state = GraphemeState::start();35 var position = at;36 while (position < text.length()) {37 const d = decode(&text, position);38 const value = properties(d.c);39 if (position > at && state.breaks_before(value)) {40 return position;41 }42 state.advance(value);43 position += d.length;44 }45 return text.length();46}4748// The boundary before `at`, itself a boundary, or `from`: scanning from49// `from`, a boundary known to come before `at`.50exp fn previous_grapheme_from(text: &[u8], from: u64, at: u64) -> u64 {51 var last = from;52 var position = from;53 while (position < at) {54 last = position;55 position = next_grapheme(&text, position);56 }57 return last;58}5960// Where an emoji sequence stands (GB11).61type Emoji = enum {62 Outside,63 Pictographic, // after Extended_Pictographic Extend*64 Joined, // after that and a ZWJ65};6667// Where an Indic conjunct stands (GB9c).68type Conjunct = enum {69 Outside,70 Consonant, // after an InCB consonant, then its extends71 Linked, // once a linker followed72};7374// What the rules need to know of the text before a position.75exp type GraphemeState = struct {76 previous: u32, // the last code point's properties77 regional: u64, // regional indicators ending there78 emoji: Emoji,79 conjunct: Conjunct,80};8182exp fn GraphemeState::start() -> GraphemeState {83 return GraphemeState {84 .previous = 0,85 .regional = 0,86 .emoji = Emoji::Outside,87 .conjunct = Conjunct::Outside88 };89}9091exp fn breaks_before(self s: &GraphemeState, value: u32) -> bool {92 const a = grapheme_class(s.previous);93 const b = grapheme_class(value);94 if (a == GCB_CR && b == GCB_LF) {95 return false; // GB396 }97 if (a == GCB_CONTROL || a == GCB_CR || a == GCB_LF) {98 return true; // GB499 }100 if (b == GCB_CONTROL || b == GCB_CR || b == GCB_LF) {101 return true; // GB5102 }103 if (a == GCB_L && (b == GCB_L || b == GCB_V || b == GCB_LV || b == GCB_LVT)) {104 return false; // GB6105 }106 if ((a == GCB_LV || a == GCB_V) && (b == GCB_V || b == GCB_T)) {107 return false; // GB7108 }109 if ((a == GCB_LVT || a == GCB_T) && b == GCB_T) {110 return false; // GB8111 }112 if (b == GCB_EXTEND || b == GCB_ZWJ || b == GCB_SPACINGMARK || a == GCB_PREPEND) {113 return false; // GB9, GB9a, GB9b114 }115 if (conjunct_class(value) == INCB_CONSONANT && s.conjunct is ::Linked) {116 return false; // GB9c117 }118 if ((value & PICTOGRAPHIC) != 0 && s.emoji is ::Joined) {119 return false; // GB11120 }121 if (a == GCB_REGIONAL_INDICATOR && b == GCB_REGIONAL_INDICATOR && s.regional % 2 == 1) {122 return false; // GB12, GB13123 }124 return true;125}126127exp fn advance(self s: &var GraphemeState, value: u32) {128 const g = grapheme_class(value);129 s.regional = if (g == GCB_REGIONAL_INDICATOR) s.regional + 1 else 0;130 if ((value & PICTOGRAPHIC) != 0) {131 s.emoji = ::Pictographic;132 } else if (s.emoji is ::Pictographic && g == GCB_EXTEND) {133 s.emoji = ::Pictographic;134 } else if (s.emoji is ::Pictographic && g == GCB_ZWJ) {135 s.emoji = ::Joined;136 } else {137 s.emoji = ::Outside;138 }139 const c = conjunct_class(value);140 if (c == INCB_CONSONANT) {141 s.conjunct = ::Consonant;142 } else if (c == INCB_LINKER && !(s.conjunct is ::Outside)) {143 s.conjunct = ::Linked;144 } else if (c != INCB_EXTEND) {145 s.conjunct = ::Outside;146 }147 s.previous = value;148}