Debian Codegraph @

debian/missing-sources/categories.rs

raw symbols SCIP
1 //! The code in this module provides a trait that is implemented against `char` that allows testing
2 //! or retrieving the Unicode category for the character as well as two `enum`s for identifying
3 //! character classes.
4
5 /// Trait to provide methods that provide boolean tests on most Unicode character categories.
6 ///
7 /// There is no `is_surrogate()` method since surrogate character codes are not valid values
8 /// for a Rust `char`.
9 ///
10 /// Importing the trait will provide the methods on the `char` type.
11 ///
12 /// Character codes are determined using a multistage table approach which allows constant-time
13 /// determination of character codes. Some special tricks are employed to enable fast determination
14 /// of composite classes (L, LC, M, N, P, S, Z, C) without requiring a check for each individual
15 /// sub-class.
16 ///
17
18 use crate::data::characters::{CAT_PAGES, CAT_TABLE};
19
20 pub trait CharacterCategories {
21 /// Determines whether a character is class L, letter (Lu, Ll, Lt, Lm, Lo). This includes all
22 /// characters used for word formation, both phonetic and ideograms. It does not include symbols
23 /// that look like letters but are really numbers, such as the encoded Roman numerals.
24 fn is_letter(self) -> bool;
25 /// Determines whether a character is class LC, cased letter (Lu, Ll, Lt). This will include
26 /// letters in the Adlam, Armenian,
27 /// Cherokee, Coptic, Cyrillic, Deseret,
28 /// Georgian (Mtavruli), Glagolitic, Greek, Medefaidrin, Old Hungarian, Osage,
29 /// Roman, Vithkuqi and Warang Citi alphabets (including their extensions) along with
30 /// special math versions of letters including the Angstrom symbol and Ohm symbol and other
31 /// such characters like ℇ, ℑ, ℕ, or Ↄ.
32 fn is_letter_cased(self) -> bool;
33 /// Determines whether a character is an upper-case letter (Lu)
34 fn is_letter_uppercase(self) -> bool;
35 /// Determines whether a character is a lower-case letter (Ll)
36 fn is_letter_lowercase(self) -> bool;
37 /// Determines whether a character is a title-case letter (Lt). This is composed of
38 /// Latin digraphs such as Dž and Greek capital letters with the iota subscript which when
39 /// uppercased and rendered with prosgegrammeni should be printed as, e.g., ΑΙ but in titlecase
40 /// would be Αι (although other possibilities for rendering both may occur depending on the typeface.
41 ///
42 /// **Inquiry** Is there a use for being able to verify that something is Lu *or* Lt? A bit-mask
43 /// is possible to avoid the logical or and would be mildly faster.
44 fn is_letter_titlecase(self) -> bool;
45 /// Determines whether a character is a modifier letter (Lm). These are letters or symbols typically
46 /// written adjacent to other letters which modify their usage in some way. They are mostly used
47 /// in phonetic contexts
48 fn is_letter_modifier(self) -> bool;
49 /// Determines whether a character is an other letter (Lo). This is all the characters from uncased
50 /// scripts alongside a handful of miscellaneous uncased letters such as ª or ƻ.
51 fn is_letter_other(self) -> bool;
52 /// Determines whether a character is a mark (M). These are characters that combine with other characters in
53 /// various ways (e.g., for accents or other modifiers). A character plus a mark is generally viewed as a single grapheme.
54 /// **Note** for most cases you will want to use grapheme segmentation to identify individual graphemes and
55 /// word segmentation to identify word boundaries rather than examining categories to find word/grapheme
56 /// boundaries.
57 fn is_mark(self) -> bool;
58 /// Determines whether a character is a nonspacing mark (Mn). This is largely diacritics which will
59 /// not change the width of the character even if they are placed before or after the base character
60 /// (as is the case with notation for Znamenny chant).
61 fn is_mark_nonspacing(self) -> bool;
62 /// Determines whether a character is a spacing mark (Mc).This is largely used by some scripts derived
63 /// from the classical Brahmic script where spacing modifiers are added to consonants to indicate
64 /// vowel sounds and for some musical symbols.
65 fn is_mark_spacing(self) -> bool;
66 /// Determines whether a character is an enclosing mark (Me). This is a small category of combining
67 /// marks like ㅤ⃠ which will surround their modified character, e.g., z⃠ which *may* be rendered
68 /// as a slashed circle over a z.
69 fn is_mark_enclosing(self) -> bool;
70 /// Determines if the category is either L or M (this is used by finl to determine whether to continue
71 /// a sequence for a named command. If you’re tempted to use this, ask whether you really want this
72 /// or if you’re better off using grapheme or word segmentation or UAX31 identifier and pattern
73 /// syntax.
74 fn is_letter_or_mark(self) -> bool;
75 /// Determines whether a character is a number (N). This includes digits in various scripts along with
76 /// special representations of numbers like Unicode encodings of Roman numerals or decorated digits
77 /// like 3︎⃣ and renderings of fractions.
78 fn is_number(self) -> bool;
79 /// Determines whether a character is a decimal digit (Nd). This includes the expected 0–9 digits
80 /// along with their equivalents in other scripts that use decimal digits, but potentially with
81 /// other forms plus some special representations of the digits for mathematics and legacy encodings
82 /// (the last include double-struck digits like 𝟚 and LED-style digits like 🯴 (U+1FBF4).
83 fn is_number_decimal(self) -> bool;
84 /// Determines whether a character is a letterlike numeric character (Nl). This includes Unicode
85 /// representation of Roman numerals as well as special characters for non-place value numbering
86 /// systems in other scripts (e.g., Greek letter-based numbers, Gothic, Old Persian, Cuneiform, etc.)
87 fn is_number_letter(self) -> bool;
88 /// Determines whether a character is an other numeric character (No). This includes fractions
89 /// in Hindu-Arabic (Western) and other scripts, super- and subscripted digits and decorated
90 /// digits along with digits from numbering systems which are not decimal but also not alphabetical
91 /// in their form like Old Hungarian or Sogdian.
92 fn is_number_other(self) -> bool;
93 /// Determines whether a character is a punctuation character (P)
94 fn is_punctuation(self) -> bool;
95 /// Determines whether a character is a connector punctuation character (Pc). These include
96 /// the underscore character and CJK variations on that character.
97 fn is_punctuation_connector(self) -> bool;
98 /// Determines whether a character is a dash punctuation character (Pd). This includes the hyphen,
99 /// typographical dashes and variations for non-Latin scripts.
100 fn is_punctuation_dash(self) -> bool;
101 /// Determines whether a character is an open punctuation character (Ps). This includes all the
102 /// variations on parentheses, braces and brackets in different scripts including mathematical
103 /// and computational special symbols along with the low-9
104 /// single and double quotation markes (‚ and „) but *not* other quotation marks.
105 fn is_punctuation_open(self) -> bool;
106 /// Determines whether a character is a close punctuation character (Pe). This includes all the
107 /// variations on parentheses, braces and brackets in different scripts including mathematical
108 /// and computational special symbols.
109 fn is_punctuation_close(self) -> bool;
110 /// Determines whether a character is an initial punctuation character (Pi). This includes variations
111 /// on opening quotation marks, functionally similar symbols from the New Testament editorial
112 /// symbols set and the left vertical bar with quill (which is apparently meant to be used to
113 /// build extended versions of ⁅ if I understand the reference in the Unicode character chart).
114 fn is_punctuation_initial(self) -> bool;
115 /// Determines whether a character is a final punctuation character (Pf). This includes variations
116 /// on closing quotation marks, functionally similar symbols from the New Testament editorial
117 /// symbols set and the left vertical bar with quill (which is apparently meant to be used to
118 /// build extended versions of ⁆ if I understand the reference in the Unicode character chart).
119 fn is_punctuation_final(self) -> bool;
120 /// Determines whether a character is an other punctuation character (Po). This is all the punctuation
121 /// symbols which are not part of the above set.
122 fn is_punctuation_other(self) -> bool;
123 /// Determines whether a character is a symbol (S). These are non-punctuation symbols including
124 /// currency and mathematical symbols as well as dingbats and emoji.
125 fn is_symbol(self) -> bool;
126 /// Determines whether a character is a math symbol (Sm).
127 fn is_symbol_math(self) -> bool;
128 /// Determines whether a character is a currency symbol (Sc)
129 fn is_symbol_currency(self) -> bool;
130 /// Determines whether a character is a modifier symbol (Sk). This is mostly stand-alone versions
131 /// of combining accents, along with the Emoji skintone modifiers.
132 fn is_symbol_modifier(self) -> bool;
133 /// Determines whether a character is an other symbol (So). This is all the symbols which neither
134 /// have mathematical uses nor are currency symbols.
135 fn is_symbol_other(self) -> bool;
136 /// Determines whether a character is a separator (Z). This is all spaces plus the Unicode line
137 /// and paragraph separators.
138 fn is_separator(self) -> bool;
139 /// Determines whether a character is a space separator (Zs). In addition to the standard space
140 /// and the non-breaking space (U+A0), there are various typographic spaces and the Ogham space
141 /// mark but printed spaces from other scripts such as the Ge'ez word space (፡).
142 fn is_separator_space(self) -> bool;
143 /// Determines whether a character is a line separator (Zl). This is the Unicode code point U+2028.
144 fn is_separator_line(self) -> bool;
145 /// Determines whether a character is a paragraph separator (Zp). This is the Unicode code point U+2029.
146 fn is_separator_paragraph(self) -> bool;
147 /// Determines whether a character is an other character (C). These consist of control characters,
148 /// format characters, surrogates (no test is provided for these since they will not appear in
149 /// valid UTF-8 text), unassigned characters and private use characters.
150 fn is_other(self) -> bool;
151 /// Determines whether a character is a control character (Cc). These are the 65 characters in the
152 /// ranges 0x00–0x1f and 0x7f–0x9f.
153 fn is_control(self) -> bool;
154 /// Determines whether a character is a format character (Cf). This is a mix of non-printing characters
155 /// like soft hyphen (U+00AD) and Zero Width Joiner (ZWJ—U+200D) and the tag block which is used
156 /// in Emoji sequences for flags along with some printing characters for non-Latin script (mostly
157 /// Arabic but also Mongolian).
158 fn is_format(self) -> bool;
159 /// Determines whether a character is a private use character (Co)
160 fn is_private_use(self) -> bool;
161 /// Determines whether a character is unassigned (Cn)
162 fn is_unassigned(self) -> bool;
163 /// Get the major category for a character (L, M, N, P, S, Z or C). Note that the “special” category
164 /// of LC cannot be returned by this method since it is not a mutually exclusive category.
165 fn get_major_category(self) -> MajorCategory;
166 /// get the minor category for a character (Lu, Ll, Lt, Lm, Lo,
167 /// Mn, Mc, Me,
168 /// Nd, Nl, No,
169 /// Pc, Pd, Ps, Pi, Pf, Po,
170 /// Sm, Sk, Sc, So,
171 /// Za, Zl, Zp,
172 /// Cc, Cf, Co, Cn). Note that the “special” category
173 /// of LC cannot be returned by this method since it is not a mutually exclusive category.
174 fn get_minor_category(self) -> MinorCategory;
175 }
176
177 /// enum for distinguishing Unicode minor categories of characters
178 #[derive(PartialEq, Debug)]
179 pub enum MinorCategory {
180 /// Uppercase letter
181 Lu,
182 /// Lowercase letter
183 Ll,
184 /// Titlecase letter
185 Lt,
186 /// Modifier letter
187 Lm,
188 /// Other letter
189 Lo,
190 /// Non-spacing mark
191 Mn,
192 /// Spacing mark
193 Mc,
194 /// Enclosing mark
195 Me,
196 /// Decimal number
197 Nd,
198 /// Letterlike number
199 Nl,
200 /// Other number
201 No,
202 /// Connector punctuation
203 Pc,
204 /// Dash punctuation
205 Pd,
206 /// Opening punctuation
207 Ps,
208 /// Closing punctuation
209 Pe,
210 /// Initial punctuation
211 Pi,
212 /// Final punctuation
213 Pf,
214 /// Other punctuation
215 Po,
216 /// Math symbol
217 Sm,
218 /// Modifier symbol
219 Sk,
220 /// Currency symbol
221 Sc,
222 /// Other symbol
223 So,
224 /// Space separator
225 Zs,
226 /// Line separator
227 Zl,
228 /// Paragraph separator
229 Zp,
230 /// Control character
231 Cc,
232 /// Format character
233 Cf,
234 /// Private use character
235 Co,
236 /// Unassigned character
237 Cn,
238 }
239
240 /// enum for distinguishing Unicode major categories of characters
241 #[derive(PartialEq, Debug)]
242 pub enum MajorCategory {
243 /// Letter
244 L,
245 /// Mark
246 M,
247 /// Number
248 N,
249 /// Punctuation
250 P,
251 /// Symbol
252 S,
253 /// Separator
254 Z,
255 /// Other character
256 C,
257 }
258
259 // Internally defined structure to allow symbolic identification of category codes as numeric values
260 // in the generated tables.
261 struct Cat;
262
263 impl Cat {
264 #![allow(non_upper_case_globals)]
265 const Lu: u8 = 0x90;
266 const Ll: u8 = 0x91;
267 const Lt: u8 = 0x92;
268 const LC: u8 = 0x90;
269 const Lm: u8 = 0x83;
270 const Lo: u8 = 0x84;
271 const L: u8 = 0x80;
272 const Mn: u8 = 0x10;
273 const Mc: u8 = 0x11;
274 const Me: u8 = 0x12;
275 const M: u8 = 0x10;
276 const Nd: u8 = 0x20;
277 const Nl: u8 = 0x21;
278 const No: u8 = 0x22;
279 const N: u8 = 0x20;
280 const Pc: u8 = 0x30;
281 const Pd: u8 = 0x31;
282 const Ps: u8 = 0x32;
283 const Pe: u8 = 0x33;
284 const Pi: u8 = 0x34;
285 const Pf: u8 = 0x35;
286 const Po: u8 = 0x36;
287 const P: u8 = 0x30;
288 const Sm: u8 = 0x40;
289 const Sc: u8 = 0x41;
290 const Sk: u8 = 0x42;
291 const So: u8 = 0x43;
292 const S: u8 = 0x40;
293 const Zs: u8 = 0x50;
294 const Zl: u8 = 0x51;
295 const Zp: u8 = 0x52;
296 const Z: u8 = 0x50;
297 const Cc: u8 = 0x61;
298 const Cf: u8 = 0x62;
299 // const Cs: u8 = 0x03;
300 const Co: u8 = 0x64;
301 const Cn: u8 = 0x60;
302 const C: u8 = 0x60;
303 }
304
305 #[inline]
306 fn get_code(c: char) -> u8 {
307 CAT_PAGES[usize::from(CAT_TABLE[(c as usize) >> 8])][(c as usize) & 0xff]
308 }
309
310 impl CharacterCategories for char {
311 #[inline]
312 fn get_major_category(self) -> MajorCategory {
313 match get_code(self) & 0xf0 {
314 Cat::L => MajorCategory::L,
315 Cat::LC => MajorCategory::L,
316 Cat::M => MajorCategory::M,
317 Cat::N => MajorCategory::N,
318 Cat::P => MajorCategory::P,
319 Cat::S => MajorCategory::S,
320 Cat::Z => MajorCategory::Z,
321 Cat::C => MajorCategory::C,
322 _ => {
323 panic!("Corrupt character data")
324 }
325 }
326 }
327
328 #[inline]
329 fn get_minor_category(self) -> MinorCategory {
330 match get_code(self) {
331 Cat::Lu => MinorCategory::Lu,
332 Cat::Ll => MinorCategory::Ll,
333 Cat::Lt => MinorCategory::Lt,
334 Cat::Lm => MinorCategory::Lm,
335 Cat::Lo => MinorCategory::Lo,
336 Cat::Mn => MinorCategory::Mn,
337 Cat::Mc => MinorCategory::Mc,
338 Cat::Me => MinorCategory::Me,
339 Cat::Nd => MinorCategory::Nd,
340 Cat::Nl => MinorCategory::Nl,
341 Cat::No => MinorCategory::No,
342 Cat::Pc => MinorCategory::Pc,
343 Cat::Pd => MinorCategory::Pd,
344 Cat::Ps => MinorCategory::Ps,
345 Cat::Pe => MinorCategory::Pe,
346 Cat::Pi => MinorCategory::Pi,
347 Cat::Pf => MinorCategory::Pf,
348 Cat::Po => MinorCategory::Po,
349 Cat::Sm => MinorCategory::Sm,
350 Cat::Sc => MinorCategory::Sc,
351 Cat::Sk => MinorCategory::Sk,
352 Cat::So => MinorCategory::So,
353 Cat::Zs => MinorCategory::Zs,
354 Cat::Zl => MinorCategory::Zl,
355 Cat::Zp => MinorCategory::Zp,
356 Cat::Cc => MinorCategory::Cc,
357 Cat::Cf => MinorCategory::Cf,
358 Cat::Co => MinorCategory::Co,
359 Cat::Cn => MinorCategory::Cn,
360 _ => {
361 panic!("Corrupt character data")
362 }
363 }
364 }
365 #[inline]
366 fn is_letter(self) -> bool {
367 get_code(self) & Cat::L == Cat::L
368 }
369
370 #[inline]
371 fn is_letter_cased(self) -> bool {
372 get_code(self) & 0xf0 == Cat::LC
373 }
374
375 #[inline]
376 fn is_letter_uppercase(self) -> bool {
377 get_code(self) == Cat::Lu
378 }
379
380 #[inline]
381 fn is_letter_lowercase(self) -> bool {
382 get_code(self) == Cat::Ll
383 }
384
385 #[inline]
386 fn is_letter_titlecase(self) -> bool {
387 get_code(self) == Cat::Lt
388 }
389
390 #[inline]
391 fn is_letter_modifier(self) -> bool {
392 get_code(self) == Cat::Lm
393 }
394
395 #[inline]
396 fn is_letter_other(self) -> bool {
397 get_code(self) == Cat::Lo
398 }
399
400 #[inline]
401 fn is_mark(self) -> bool {
402 get_code(self) & 0xf0 == Cat::M
403 }
404
405 #[inline]
406 fn is_mark_nonspacing(self) -> bool {
407 get_code(self) == Cat::Mn
408 }
409
410 #[inline]
411 fn is_mark_spacing(self) -> bool {
412 get_code(self) == Cat::Mc
413 }
414
415 #[inline]
416 fn is_mark_enclosing(self) -> bool {
417 get_code(self) == Cat::Me
418 }
419
420 #[inline]
421 fn is_letter_or_mark(self) -> bool {
422 return get_code(self) & 0x60 == 0x00
423 }
424
425 #[inline]
426 fn is_number(self) -> bool {
427 get_code(self) & 0xf0 == Cat::N
428 }
429
430 #[inline]
431 fn is_number_decimal(self) -> bool {
432 get_code(self) == Cat::Nd
433 }
434
435 #[inline]
436 fn is_number_letter(self) -> bool {
437 get_code(self) == Cat::Nl
438 }
439
440 #[inline]
441 fn is_number_other(self) -> bool {
442 get_code(self) == Cat::No
443 }
444
445 #[inline]
446 fn is_punctuation(self) -> bool {
447 get_code(self) & 0xf0 == Cat::P
448 }
449
450 #[inline]
451 fn is_punctuation_connector(self) -> bool {
452 get_code(self) == Cat::Pc
453 }
454
455 #[inline]
456 fn is_punctuation_dash(self) -> bool {
457 get_code(self) == Cat::Pd
458 }
459
460 #[inline]
461 fn is_punctuation_open(self) -> bool {
462 get_code(self) == Cat::Ps
463 }
464
465 #[inline]
466 fn is_punctuation_close(self) -> bool {
467 get_code(self) == Cat::Pe
468 }
469
470 #[inline]
471 fn is_punctuation_initial(self) -> bool {
472 get_code(self) == Cat::Pi
473 }
474
475 #[inline]
476 fn is_punctuation_final(self) -> bool {
477 get_code(self) == Cat::Pf
478 }
479
480 #[inline]
481 fn is_punctuation_other(self) -> bool {
482 get_code(self) == Cat::Po
483 }
484
485 #[inline]
486 fn is_symbol(self) -> bool {
487 get_code(self) & 0xf0 == Cat::S
488 }
489
490 #[inline]
491 fn is_symbol_math(self) -> bool {
492 get_code(self) == Cat::Sm
493 }
494
495 #[inline]
496 fn is_symbol_currency(self) -> bool {
497 get_code(self) == Cat::Sc
498 }
499
500 #[inline]
501 fn is_symbol_modifier(self) -> bool {
502 get_code(self) == Cat::Sk
503 }
504
505 #[inline]
506 fn is_symbol_other(self) -> bool {
507 get_code(self) == Cat::So
508 }
509
510 #[inline]
511 fn is_separator(self) -> bool {
512 get_code(self) & 0xf0 == Cat::Z
513 }
514
515 #[inline]
516 fn is_separator_space(self) -> bool {
517 get_code(self) == Cat::Zs
518 }
519
520 #[inline]
521 fn is_separator_line(self) -> bool {
522 get_code(self) == Cat::Zl
523 }
524
525 #[inline]
526 fn is_separator_paragraph(self) -> bool {
527 get_code(self) == Cat::Zp
528 }
529
530 #[inline]
531 fn is_other(self) -> bool {
532 get_code(self) & 0xf0 == Cat::C
533 }
534
535 #[inline]
536 fn is_control(self) -> bool {
537 get_code(self) == Cat::Cc
538 }
539
540 #[inline]
541 fn is_format(self) -> bool {
542 get_code(self) == Cat::Cf
543 }
544
545 #[inline]
546 fn is_private_use(self) -> bool {
547 get_code(self) == Cat::Co
548 }
549
550 #[inline]
551 fn is_unassigned(self) -> bool {
552 get_code(self) == Cat::Cn
553 }
554 }