gitcc_convco/
util.rs

1//! Misc utilities
2
3/// Additional methods for strings
4pub trait StringExt {
5    /// Checks if the string is all lowercase
6    fn is_lowercase(&self) -> bool;
7
8    /// Checks if a string starts with a lowercase character
9    fn starts_with_lowercase(&self) -> bool;
10
11    /// Returns a new string starting with a first character in lowercase
12    fn to_lowercase_first(&self) -> String;
13
14    /// Returns a new string starting with a first character in uppercase
15    fn to_uppercase_first(&self) -> String;
16}
17
18impl StringExt for str {
19    fn is_lowercase(&self) -> bool {
20        self.chars().all(|c| c.is_lowercase())
21    }
22
23    fn starts_with_lowercase(&self) -> bool {
24        match self.chars().next() {
25            None => false,
26            Some(c) => c.is_lowercase(),
27        }
28    }
29
30    fn to_lowercase_first(&self) -> String {
31        self.chars()
32            .enumerate()
33            .map(|(i, c)| {
34                if i == 0 {
35                    c.to_lowercase().collect::<String>()
36                } else {
37                    c.to_string()
38                }
39            })
40            .collect::<String>()
41    }
42
43    fn to_uppercase_first(&self) -> String {
44        self.chars()
45            .enumerate()
46            .map(|(i, c)| {
47                if i == 0 {
48                    c.to_uppercase().collect::<String>()
49                } else {
50                    c.to_string()
51                }
52            })
53            .collect::<String>()
54    }
55}