]> git.lizzy.rs Git - rust.git/blob - clippy_utils/src/camel_case.rs
Merge commit '928e72dd10749875cbd412f74bfbfd7765dbcd8a' into clippyup
[rust.git] / clippy_utils / src / camel_case.rs
1 /// Returns the index of the character after the first camel-case component of `s`.
2 #[must_use]
3 pub fn until(s: &str) -> usize {
4     let mut iter = s.char_indices();
5     if let Some((_, first)) = iter.next() {
6         if !first.is_uppercase() {
7             return 0;
8         }
9     } else {
10         return 0;
11     }
12     let mut up = true;
13     let mut last_i = 0;
14     for (i, c) in iter {
15         if up {
16             if c.is_lowercase() {
17                 up = false;
18             } else {
19                 return last_i;
20             }
21         } else if c.is_uppercase() {
22             up = true;
23             last_i = i;
24         } else if !c.is_lowercase() {
25             return i;
26         }
27     }
28     if up {
29         last_i
30     } else {
31         s.len()
32     }
33 }
34
35 /// Returns index of the last camel-case component of `s`.
36 #[must_use]
37 pub fn from(s: &str) -> usize {
38     let mut iter = s.char_indices().rev();
39     if let Some((_, first)) = iter.next() {
40         if !first.is_lowercase() {
41             return s.len();
42         }
43     } else {
44         return s.len();
45     }
46     let mut down = true;
47     let mut last_i = s.len();
48     for (i, c) in iter {
49         if down {
50             if c.is_uppercase() {
51                 down = false;
52                 last_i = i;
53             } else if !c.is_lowercase() {
54                 return last_i;
55             }
56         } else if c.is_lowercase() {
57             down = true;
58         } else if c.is_uppercase() {
59             last_i = i;
60         } else {
61             return last_i;
62         }
63     }
64     last_i
65 }
66
67 #[cfg(test)]
68 mod test {
69     use super::{from, until};
70
71     #[test]
72     fn from_full() {
73         assert_eq!(from("AbcDef"), 0);
74         assert_eq!(from("Abc"), 0);
75         assert_eq!(from("ABcd"), 0);
76         assert_eq!(from("ABcdEf"), 0);
77         assert_eq!(from("AabABcd"), 0);
78     }
79
80     #[test]
81     fn from_partial() {
82         assert_eq!(from("abcDef"), 3);
83         assert_eq!(from("aDbc"), 1);
84         assert_eq!(from("aabABcd"), 3);
85     }
86
87     #[test]
88     fn from_not() {
89         assert_eq!(from("AbcDef_"), 7);
90         assert_eq!(from("AbcDD"), 5);
91     }
92
93     #[test]
94     fn from_caps() {
95         assert_eq!(from("ABCD"), 4);
96     }
97
98     #[test]
99     fn until_full() {
100         assert_eq!(until("AbcDef"), 6);
101         assert_eq!(until("Abc"), 3);
102     }
103
104     #[test]
105     fn until_not() {
106         assert_eq!(until("abcDef"), 0);
107         assert_eq!(until("aDbc"), 0);
108     }
109
110     #[test]
111     fn until_partial() {
112         assert_eq!(until("AbcDef_"), 6);
113         assert_eq!(until("CallTypeC"), 8);
114         assert_eq!(until("AbcDD"), 3);
115     }
116
117     #[test]
118     fn until_caps() {
119         assert_eq!(until("ABCD"), 0);
120     }
121 }