Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-core/src/text/words.rs
1/// The default word-boundary characters.
2pub const DEFAULT_WORD_BOUNDARY_CHARS: [char; 33] = [
3 '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '=', '+', '[', '{', ']', '}',
4 '\\', '|', ';', ':', '\'', '"', ',', '.', '<', '>', '/', '?', '«', '»',
5];
6 
7/// Default subword-boundary characters: basically just underscores for now (snake_case)
8pub const SUBWORD_BOUNDARY_CHARS: [char; 1] = ['_'];
9 
10/// Split a string slice at the next word boundary, returning before and after the word boundary
11///
12/// The next word boundary is the transition from not in a word (i.e. in a separator) to
13/// in a word. The first slice returned goes from start of the input slice to the word boundary.
14/// The second slice returns goes from the start of the new word to the end of the input slice.
15pub fn split_at_next_word_start(text: &str) -> (&str, &str) {
16 let mut in_word = true;
17 let mut byte_index = 0;
18 for c in text.chars() {
19 if in_word {
20 if is_default_word_boundary(c) {
21 in_word = false;
22 }
23 } else if !is_default_word_boundary(c) {
24 break;
25 }
26 byte_index += c.len_utf8();
27 }
28 
29 text.split_at(byte_index)
30}
31 
32/// Default logic for determining if a character is a word separator. Word separators are
33/// whitespace or a specific set of punctuation characters.
34pub fn is_default_word_boundary(c: char) -> bool {
35 c.is_whitespace() || DEFAULT_WORD_BOUNDARY_CHARS.contains(&c)
36}
37 
38/// Logic for determining if a character is a subword separator.
39/// Subword separators include all the default word separators
40/// (whitespace or a specific set of punctuation characters)
41/// and subword-specific separators (underscores, for snake_case).
42pub fn is_subword_boundary_char(c: char) -> bool {
43 is_default_word_boundary(c) || SUBWORD_BOUNDARY_CHARS.contains(&c)
44}
45