clippy_utils/
str_utils.rs

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/// Dealing with sting indices can be hard, this struct ensures that both the
/// character and byte index are provided for correct indexing.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct StrIndex {
    pub char_index: usize,
    pub byte_index: usize,
}

impl StrIndex {
    pub fn new(char_index: usize, byte_index: usize) -> Self {
        Self { char_index, byte_index }
    }
}

/// Returns the index of the character after the first camel-case component of `s`.
///
/// ```no_run
/// # use clippy_utils::str_utils::{camel_case_until, StrIndex};
/// assert_eq!(camel_case_until("AbcDef"), StrIndex::new(6, 6));
/// assert_eq!(camel_case_until("ABCD"), StrIndex::new(0, 0));
/// assert_eq!(camel_case_until("AbcDD"), StrIndex::new(3, 3));
/// assert_eq!(camel_case_until("Abc\u{f6}\u{f6}DD"), StrIndex::new(5, 7));
/// ```
#[must_use]
pub fn camel_case_until(s: &str) -> StrIndex {
    let mut iter = s.char_indices().enumerate();
    if let Some((_char_index, (_, first))) = iter.next() {
        if !first.is_uppercase() {
            return StrIndex::new(0, 0);
        }
    } else {
        return StrIndex::new(0, 0);
    }
    let mut up = true;
    let mut last_index = StrIndex::new(0, 0);
    for (char_index, (byte_index, c)) in iter {
        if up {
            if c.is_lowercase() {
                up = false;
            } else {
                return last_index;
            }
        } else if c.is_uppercase() {
            up = true;
            last_index.byte_index = byte_index;
            last_index.char_index = char_index;
        } else if !c.is_lowercase() {
            return StrIndex::new(char_index, byte_index);
        }
    }

    if up {
        last_index
    } else {
        StrIndex::new(s.chars().count(), s.len())
    }
}

/// Returns index of the first camel-case component of `s`.
///
/// ```no_run
/// # use clippy_utils::str_utils::{camel_case_start, StrIndex};
/// assert_eq!(camel_case_start("AbcDef"), StrIndex::new(0, 0));
/// assert_eq!(camel_case_start("abcDef"), StrIndex::new(3, 3));
/// assert_eq!(camel_case_start("ABCD"), StrIndex::new(4, 4));
/// assert_eq!(camel_case_start("abcd"), StrIndex::new(4, 4));
/// assert_eq!(camel_case_start("\u{f6}\u{f6}cd"), StrIndex::new(4, 6));
/// ```
#[must_use]
pub fn camel_case_start(s: &str) -> StrIndex {
    camel_case_start_from_idx(s, 0)
}

/// Returns `StrIndex` of the last camel-case component of `s[idx..]`.
///
/// ```no_run
/// # use clippy_utils::str_utils::{camel_case_start_from_idx, StrIndex};
/// assert_eq!(camel_case_start_from_idx("AbcDef", 0), StrIndex::new(0, 0));
/// assert_eq!(camel_case_start_from_idx("AbcDef", 1), StrIndex::new(3, 3));
/// assert_eq!(camel_case_start_from_idx("AbcDefGhi", 0), StrIndex::new(0, 0));
/// assert_eq!(camel_case_start_from_idx("AbcDefGhi", 1), StrIndex::new(3, 3));
/// assert_eq!(camel_case_start_from_idx("Abcdefg", 1), StrIndex::new(7, 7));
/// ```
pub fn camel_case_start_from_idx(s: &str, start_idx: usize) -> StrIndex {
    let char_count = s.chars().count();
    let range = 0..char_count;
    let mut iter = range.rev().zip(s.char_indices().rev());
    if let Some((_, (_, first))) = iter.next() {
        if !first.is_lowercase() {
            return StrIndex::new(char_count, s.len());
        }
    } else {
        return StrIndex::new(char_count, s.len());
    }

    let mut down = true;
    let mut last_index = StrIndex::new(char_count, s.len());
    for (char_index, (byte_index, c)) in iter {
        if byte_index < start_idx {
            break;
        }
        if down {
            if c.is_uppercase() {
                down = false;
                last_index.byte_index = byte_index;
                last_index.char_index = char_index;
            } else if !c.is_lowercase() {
                return last_index;
            }
        } else if c.is_lowercase() {
            down = true;
        } else if c.is_uppercase() {
            last_index.byte_index = byte_index;
            last_index.char_index = char_index;
        } else {
            return last_index;
        }
    }

    last_index
}

/// Get the indexes of camel case components of a string `s`
///
/// ```no_run
/// # use clippy_utils::str_utils::{camel_case_indices, StrIndex};
/// assert_eq!(
///     camel_case_indices("AbcDef"),
///     vec![StrIndex::new(0, 0), StrIndex::new(3, 3), StrIndex::new(6, 6)]
/// );
/// assert_eq!(
///     camel_case_indices("abcDef"),
///     vec![StrIndex::new(3, 3), StrIndex::new(6, 6)]
/// );
/// ```
pub fn camel_case_indices(s: &str) -> Vec<StrIndex> {
    let mut result = Vec::new();
    let mut str_idx = camel_case_start(s);

    while str_idx.byte_index < s.len() {
        let next_idx = str_idx.byte_index + 1;
        result.push(str_idx);
        str_idx = camel_case_start_from_idx(s, next_idx);
    }
    result.push(str_idx);

    result
}

/// Split camel case string into a vector of its components
///
/// ```no_run
/// # use clippy_utils::str_utils::{camel_case_split, StrIndex};
/// assert_eq!(camel_case_split("AbcDef"), vec!["Abc", "Def"]);
/// ```
pub fn camel_case_split(s: &str) -> Vec<&str> {
    let mut offsets = camel_case_indices(s)
        .iter()
        .map(|e| e.byte_index)
        .collect::<Vec<usize>>();
    if offsets[0] != 0 {
        offsets.insert(0, 0);
    }

    offsets.windows(2).map(|w| &s[w[0]..w[1]]).collect()
}

/// Dealing with sting comparison can be complicated, this struct ensures that both the
/// character and byte count are provided for correct indexing.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct StrCount {
    pub char_count: usize,
    pub byte_count: usize,
}

impl StrCount {
    pub fn new(char_count: usize, byte_count: usize) -> Self {
        Self { char_count, byte_count }
    }
}

/// Returns the number of chars that match from the start
///
/// ```no_run
/// # use clippy_utils::str_utils::{count_match_start, StrCount};
/// assert_eq!(count_match_start("hello_mouse", "hello_penguin"), StrCount::new(6, 6));
/// assert_eq!(count_match_start("hello_clippy", "bye_bugs"), StrCount::new(0, 0));
/// assert_eq!(count_match_start("hello_world", "hello_world"), StrCount::new(11, 11));
/// assert_eq!(count_match_start("T\u{f6}ffT\u{f6}ff", "T\u{f6}ff"), StrCount::new(4, 5));
/// ```
#[must_use]
pub fn count_match_start(str1: &str, str2: &str) -> StrCount {
    // (char_index, char1)
    let char_count = str1.chars().count();
    let iter1 = (0..=char_count).zip(str1.chars());
    // (byte_index, char2)
    let iter2 = str2.char_indices();

    iter1
        .zip(iter2)
        .take_while(|((_, c1), (_, c2))| c1 == c2)
        .last()
        .map_or_else(StrCount::default, |((char_index, _), (byte_index, character))| {
            StrCount::new(char_index + 1, byte_index + character.len_utf8())
        })
}

/// Returns the number of chars and bytes that match from the end
///
/// ```no_run
/// # use clippy_utils::str_utils::{count_match_end, StrCount};
/// assert_eq!(count_match_end("hello_cat", "bye_cat"), StrCount::new(4, 4));
/// assert_eq!(count_match_end("if_item_thing", "enum_value"), StrCount::new(0, 0));
/// assert_eq!(count_match_end("Clippy", "Clippy"), StrCount::new(6, 6));
/// assert_eq!(count_match_end("MyT\u{f6}ff", "YourT\u{f6}ff"), StrCount::new(4, 5));
/// ```
#[must_use]
pub fn count_match_end(str1: &str, str2: &str) -> StrCount {
    let char_count = str1.chars().count();
    if char_count == 0 {
        return StrCount::default();
    }

    // (char_index, char1)
    let iter1 = (0..char_count).rev().zip(str1.chars().rev());
    // (byte_index, char2)
    let byte_count = str2.len();
    let iter2 = str2.char_indices().rev();

    iter1
        .zip(iter2)
        .take_while(|((_, c1), (_, c2))| c1 == c2)
        .last()
        .map_or_else(StrCount::default, |((char_index, _), (byte_index, _))| {
            StrCount::new(char_count - char_index, byte_count - byte_index)
        })
}

/// Returns a `snake_case` version of the input
/// ```no_run
/// use clippy_utils::str_utils::to_snake_case;
/// assert_eq!(to_snake_case("AbcDef"), "abc_def");
/// assert_eq!(to_snake_case("ABCD"), "a_b_c_d");
/// assert_eq!(to_snake_case("AbcDD"), "abc_d_d");
/// assert_eq!(to_snake_case("Abc1DD"), "abc1_d_d");
/// ```
pub fn to_snake_case(name: &str) -> String {
    let mut s = String::new();
    for (i, c) in name.chars().enumerate() {
        if c.is_uppercase() {
            // characters without capitalization are considered lowercase
            if i != 0 {
                s.push('_');
            }
            s.extend(c.to_lowercase());
        } else {
            s.push(c);
        }
    }
    s
}
/// Returns a `CamelCase` version of the input
/// ```no_run
/// use clippy_utils::str_utils::to_camel_case;
/// assert_eq!(to_camel_case("abc_def"), "AbcDef");
/// assert_eq!(to_camel_case("a_b_c_d"), "ABCD");
/// assert_eq!(to_camel_case("abc_d_d"), "AbcDD");
/// assert_eq!(to_camel_case("abc1_d_d"), "Abc1DD");
/// ```
pub fn to_camel_case(item_name: &str) -> String {
    let mut s = String::new();
    let mut up = true;
    for c in item_name.chars() {
        if c.is_uppercase() {
            // we only turn snake case text into CamelCase
            return item_name.to_string();
        }
        if c == '_' {
            up = true;
            continue;
        }
        if up {
            up = false;
            s.extend(c.to_uppercase());
        } else {
            s.push(c);
        }
    }
    s
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn camel_case_start_full() {
        assert_eq!(camel_case_start("AbcDef"), StrIndex::new(0, 0));
        assert_eq!(camel_case_start("Abc"), StrIndex::new(0, 0));
        assert_eq!(camel_case_start("ABcd"), StrIndex::new(0, 0));
        assert_eq!(camel_case_start("ABcdEf"), StrIndex::new(0, 0));
        assert_eq!(camel_case_start("AabABcd"), StrIndex::new(0, 0));
    }

    #[test]
    fn camel_case_start_partial() {
        assert_eq!(camel_case_start("abcDef"), StrIndex::new(3, 3));
        assert_eq!(camel_case_start("aDbc"), StrIndex::new(1, 1));
        assert_eq!(camel_case_start("aabABcd"), StrIndex::new(3, 3));
        assert_eq!(camel_case_start("\u{f6}\u{f6}AabABcd"), StrIndex::new(2, 4));
    }

    #[test]
    fn camel_case_start_not() {
        assert_eq!(camel_case_start("AbcDef_"), StrIndex::new(7, 7));
        assert_eq!(camel_case_start("AbcDD"), StrIndex::new(5, 5));
        assert_eq!(camel_case_start("all_small"), StrIndex::new(9, 9));
        assert_eq!(camel_case_start("\u{f6}_all_small"), StrIndex::new(11, 12));
    }

    #[test]
    fn camel_case_start_caps() {
        assert_eq!(camel_case_start("ABCD"), StrIndex::new(4, 4));
    }

    #[test]
    fn camel_case_until_full() {
        assert_eq!(camel_case_until("AbcDef"), StrIndex::new(6, 6));
        assert_eq!(camel_case_until("Abc"), StrIndex::new(3, 3));
        assert_eq!(camel_case_until("Abc\u{f6}\u{f6}\u{f6}"), StrIndex::new(6, 9));
    }

    #[test]
    fn camel_case_until_not() {
        assert_eq!(camel_case_until("abcDef"), StrIndex::new(0, 0));
        assert_eq!(camel_case_until("aDbc"), StrIndex::new(0, 0));
    }

    #[test]
    fn camel_case_until_partial() {
        assert_eq!(camel_case_until("AbcDef_"), StrIndex::new(6, 6));
        assert_eq!(camel_case_until("CallTypeC"), StrIndex::new(8, 8));
        assert_eq!(camel_case_until("AbcDD"), StrIndex::new(3, 3));
        assert_eq!(camel_case_until("Abc\u{f6}\u{f6}DD"), StrIndex::new(5, 7));
    }

    #[test]
    fn until_caps() {
        assert_eq!(camel_case_until("ABCD"), StrIndex::new(0, 0));
    }

    #[test]
    fn camel_case_start_from_idx_full() {
        assert_eq!(camel_case_start_from_idx("AbcDef", 0), StrIndex::new(0, 0));
        assert_eq!(camel_case_start_from_idx("AbcDef", 1), StrIndex::new(3, 3));
        assert_eq!(camel_case_start_from_idx("AbcDef", 4), StrIndex::new(6, 6));
        assert_eq!(camel_case_start_from_idx("AbcDefGhi", 0), StrIndex::new(0, 0));
        assert_eq!(camel_case_start_from_idx("AbcDefGhi", 1), StrIndex::new(3, 3));
        assert_eq!(camel_case_start_from_idx("Abcdefg", 1), StrIndex::new(7, 7));
    }

    #[test]
    fn camel_case_indices_full() {
        assert_eq!(camel_case_indices("Abc\u{f6}\u{f6}DD"), vec![StrIndex::new(7, 9)]);
    }

    #[test]
    fn camel_case_split_full() {
        assert_eq!(camel_case_split("A"), vec!["A"]);
        assert_eq!(camel_case_split("AbcDef"), vec!["Abc", "Def"]);
        assert_eq!(camel_case_split("Abc"), vec!["Abc"]);
        assert_eq!(camel_case_split("abcDef"), vec!["abc", "Def"]);
        assert_eq!(camel_case_split("\u{f6}\u{f6}AabABcd"), vec![
            "\u{f6}\u{f6}",
            "Aab",
            "A",
            "Bcd"
        ]);
    }
}