1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/// Split `s` into a vec of things that look like words and individual
/// non-word characters.
///
/// "foo..bar23" -> vec!["foo", ".", ".", "bar23"]
///
/// See also `split_words_and_numbers`. Both these functions are hot,
/// so they are separate implementations rather than passing a bool to
/// customise number handling.
pub(crate) fn split_words(s: &str) -> Vec<&str> {
    let mut words = vec![];
    let mut word_start: Option<usize> = None;
    for (idx, c) in s.char_indices() {
        match word_start {
            Some(start) => {
                if c.is_alphanumeric() || c == '-' || c == '_' {
                    // Just carry on in this word.
                } else {
                    // Push the previous word, then this non-word character.
                    words.push(&s[start..idx]);
                    words.push(&s[idx..idx + c.len_utf8()]);
                    word_start = None;
                }
            }
            None => {
                if c.is_alphanumeric() || c == '-' || c == '_' {
                    word_start = Some(idx);
                } else {
                    words.push(&s[idx..idx + c.len_utf8()]);
                }
            }
        }
    }

    if let Some(start) = word_start {
        words.push(&s[start..]);
    }
    words
}

/// Split `s` into a vec of things that look like words and individual
/// non-word characters.
///
/// "foo..bar23" -> vec!["foo", ".", ".", "bar23"]
pub(crate) fn split_words_and_numbers(s: &str) -> Vec<&str> {
    let mut words = vec![];
    let mut word_start: Option<(usize, char)> = None;
    for (idx, c) in s.char_indices() {
        match word_start {
            Some((start, start_c)) => {
                if c.is_alphanumeric() || c == '_' {
                    // Word character, add to the current word if it's
                    // not a number.
                    if c.is_ascii_digit() == start_c.is_ascii_digit() {
                        // Just carry on in this word.
                    } else {
                        // Finish previous word, start a new one.
                        words.push(&s[start..idx]);
                        word_start = Some((idx, c));
                    }
                } else {
                    // Push the previous word, then this non-word character.
                    words.push(&s[start..idx]);
                    words.push(&s[idx..idx + c.len_utf8()]);
                    word_start = None;
                }
            }
            None => {
                if c.is_alphanumeric() || c == '-' || c == '_' {
                    word_start = Some((idx, c));
                } else {
                    words.push(&s[idx..idx + c.len_utf8()]);
                }
            }
        }
    }

    if let Some((start, _)) = word_start {
        words.push(&s[start..]);
    }
    words
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn test_split_words() {
        let s = "example.com";
        let res = split_words(s);
        assert_eq!(res, vec!["example", ".", "com"])
    }

    #[test]
    fn test_split_words_punctuation() {
        let s = "example..";
        let res = split_words(s);
        assert_eq!(res, vec!["example", ".", "."])
    }

    #[test]
    fn test_split_words_numbers() {
        let s = "foo123bar";
        let res = split_words(s);
        assert_eq!(res, vec!["foo123bar"])
    }

    #[test]
    fn test_split_words_treats_newline_separately() {
        let s = "example.\ncom";
        let res = split_words(s);
        assert_eq!(res, vec!["example", ".", "\n", "com"])
    }

    #[test]
    fn test_split_words_single_unicode() {
        let s = "a ö b";
        let res = split_words(s);
        assert_eq!(res, vec!["a", " ", "ö", " ", "b"])
    }

    #[test]
    fn test_split_words_single_unicode_not_alphabetic() {
        let s = "a 💝 b";
        let res = split_words(s);
        assert_eq!(res, vec!["a", " ", "💝", " ", "b"])
    }

    #[test]
    fn test_split_words_unicode() {
        let s = "a xöy b";
        let res = split_words(s);
        assert_eq!(res, vec!["a", " ", "xöy", " ", "b"])
    }

    #[test]
    fn test_split_words_and_numbers() {
        let s = "a123b";
        let res = split_words_and_numbers(s);
        assert_eq!(res, vec!["a", "123", "b"])
    }

    #[test]
    fn test_split_words_and_numbers_spaces() {
        let s = "foo bar";
        let res = split_words_and_numbers(s);
        assert_eq!(res, vec!["foo", " ", "bar"])
    }
}