]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/camel_case.rs
Auto merge of #3598 - xfix:apply-cargo-fix-edition-idioms, r=phansch
[rust.git] / clippy_lints / src / utils / camel_case.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 /// Return the index of the character after the first camel-case component of
11 /// `s`.
12 pub fn until(s: &str) -> usize {
13     let mut iter = s.char_indices();
14     if let Some((_, first)) = iter.next() {
15         if !first.is_uppercase() {
16             return 0;
17         }
18     } else {
19         return 0;
20     }
21     let mut up = true;
22     let mut last_i = 0;
23     for (i, c) in iter {
24         if up {
25             if c.is_lowercase() {
26                 up = false;
27             } else {
28                 return last_i;
29             }
30         } else if c.is_uppercase() {
31             up = true;
32             last_i = i;
33         } else if !c.is_lowercase() {
34             return i;
35         }
36     }
37     if up {
38         last_i
39     } else {
40         s.len()
41     }
42 }
43
44 /// Return index of the last camel-case component of `s`.
45 pub fn from(s: &str) -> usize {
46     let mut iter = s.char_indices().rev();
47     if let Some((_, first)) = iter.next() {
48         if !first.is_lowercase() {
49             return s.len();
50         }
51     } else {
52         return s.len();
53     }
54     let mut down = true;
55     let mut last_i = s.len();
56     for (i, c) in iter {
57         if down {
58             if c.is_uppercase() {
59                 down = false;
60                 last_i = i;
61             } else if !c.is_lowercase() {
62                 return last_i;
63             }
64         } else if c.is_lowercase() {
65             down = true;
66         } else {
67             return last_i;
68         }
69     }
70     last_i
71 }
72
73 #[cfg(test)]
74 mod test {
75     use super::{from, until};
76
77     #[test]
78     fn from_full() {
79         assert_eq!(from("AbcDef"), 0);
80         assert_eq!(from("Abc"), 0);
81     }
82
83     #[test]
84     fn from_partial() {
85         assert_eq!(from("abcDef"), 3);
86         assert_eq!(from("aDbc"), 1);
87     }
88
89     #[test]
90     fn from_not() {
91         assert_eq!(from("AbcDef_"), 7);
92         assert_eq!(from("AbcDD"), 5);
93     }
94
95     #[test]
96     fn from_caps() {
97         assert_eq!(from("ABCD"), 4);
98     }
99
100     #[test]
101     fn until_full() {
102         assert_eq!(until("AbcDef"), 6);
103         assert_eq!(until("Abc"), 3);
104     }
105
106     #[test]
107     fn until_not() {
108         assert_eq!(until("abcDef"), 0);
109         assert_eq!(until("aDbc"), 0);
110     }
111
112     #[test]
113     fn until_partial() {
114         assert_eq!(until("AbcDef_"), 6);
115         assert_eq!(until("CallTypeC"), 8);
116         assert_eq!(until("AbcDD"), 3);
117     }
118
119     #[test]
120     fn until_caps() {
121         assert_eq!(until("ABCD"), 0);
122     }
123 }