Debian Codegraph @

helix-core/src/chars.rs

raw symbols SCIP
1 //! Utility functions to categorize a `char`.
2
3 use crate::LineEnding;
4
5 #[derive(Debug, Eq, PartialEq)]
6 pub enum CharCategory {
7 Whitespace,
8 Eol,
9 Word,
10 Punctuation,
11 Unknown,
12 }
13
14 #[inline]
15 pub fn categorize_char(ch: char) -> CharCategory {
16 if char_is_line_ending(ch) {
17 CharCategory::Eol
18 } else if ch.is_whitespace() {
19 CharCategory::Whitespace
20 } else if char_is_word(ch) {
21 CharCategory::Word
22 } else if char_is_punctuation(ch) {
23 CharCategory::Punctuation
24 } else {
25 CharCategory::Unknown
26 }
27 }
28
29 /// Determine whether a character is a line ending.
30 #[inline]
31 pub fn char_is_line_ending(ch: char) -> bool {
32 LineEnding::from_char(ch).is_some()
33 }
34
35 /// Determine whether a character qualifies as (non-line-break)
36 /// whitespace.
37 #[inline]
38 pub fn char_is_whitespace(ch: char) -> bool {
39 // TODO: this is a naive binary categorization of whitespace
40 // characters. For display, word wrapping, etc. we'll need a better
41 // categorization based on e.g. breaking vs non-breaking spaces
42 // and whether they're zero-width or not.
43 match ch {
44 //'\u{1680}' | // Ogham Space Mark (here for completeness, but usually displayed as a dash, not as whitespace)
45 '\u{0009}' | // Character Tabulation
46 '\u{0020}' | // Space
47 '\u{00A0}' | // No-break Space
48 '\u{180E}' | // Mongolian Vowel Separator
49 '\u{202F}' | // Narrow No-break Space
50 '\u{205F}' | // Medium Mathematical Space
51 '\u{3000}' | // Ideographic Space
52 '\u{FEFF}' // Zero Width No-break Space
53 => true,
54
55 // En Quad, Em Quad, En Space, Em Space, Three-per-em Space,
56 // Four-per-em Space, Six-per-em Space, Figure Space,
57 // Punctuation Space, Thin Space, Hair Space, Zero Width Space.
58 ch if ('\u{2000}' ..= '\u{200B}').contains(&ch) => true,
59
60 _ => false,
61 }
62 }
63
64 #[inline]
65 pub fn char_is_punctuation(ch: char) -> bool {
66 use unicode_general_category::{get_general_category, GeneralCategory};
67
68 matches!(
69 get_general_category(ch),
70 GeneralCategory::OtherPunctuation
71 | GeneralCategory::OpenPunctuation
72 | GeneralCategory::ClosePunctuation
73 | GeneralCategory::InitialPunctuation
74 | GeneralCategory::FinalPunctuation
75 | GeneralCategory::ConnectorPunctuation
76 | GeneralCategory::DashPunctuation
77 | GeneralCategory::MathSymbol
78 | GeneralCategory::CurrencySymbol
79 | GeneralCategory::ModifierSymbol
80 )
81 }
82
83 #[inline]
84 pub fn char_is_word(ch: char) -> bool {
85 ch.is_alphanumeric() || ch == '_'
86 }
87
88 #[cfg(test)]
89 mod test {
90 use super::*;
91
92 #[test]
93 fn test_categorize() {
94 #[cfg(not(feature = "unicode-lines"))]
95 const EOL_TEST_CASE: &str = "\n";
96 #[cfg(feature = "unicode-lines")]
97 const EOL_TEST_CASE: &str = "\n\u{000B}\u{000C}\u{0085}\u{2028}\u{2029}";
98 const WORD_TEST_CASE: &str = "_hello_world_あいうえおー12345678901234567890";
99 const PUNCTUATION_TEST_CASE: &str =
100 "!\"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~!”#$%&’()*+、。:;<=>?@「」^`{|}~";
101 const WHITESPACE_TEST_CASE: &str = "      ";
102
103 for ch in EOL_TEST_CASE.chars() {
104 assert_eq!(CharCategory::Eol, categorize_char(ch));
105 }
106
107 for ch in WHITESPACE_TEST_CASE.chars() {
108 assert_eq!(
109 CharCategory::Whitespace,
110 categorize_char(ch),
111 "Testing '{}', but got `{:?}` instead of `Category::Whitespace`",
112 ch,
113 categorize_char(ch)
114 );
115 }
116
117 for ch in WORD_TEST_CASE.chars() {
118 assert_eq!(
119 CharCategory::Word,
120 categorize_char(ch),
121 "Testing '{}', but got `{:?}` instead of `Category::Word`",
122 ch,
123 categorize_char(ch)
124 );
125 }
126
127 for ch in PUNCTUATION_TEST_CASE.chars() {
128 assert_eq!(
129 CharCategory::Punctuation,
130 categorize_char(ch),
131 "Testing '{}', but got `{:?}` instead of `Category::Punctuation`",
132 ch,
133 categorize_char(ch)
134 );
135 }
136 }
137 }