@@ -77,6 +77,9 @@ pub fn camel_case_start(s: &str) -> StrIndex {
77
77
/// # use clippy_utils::str_utils::{camel_case_start_from_idx, StrIndex};
78
78
/// assert_eq!(camel_case_start_from_idx("AbcDef", 0), StrIndex::new(0, 0));
79
79
/// assert_eq!(camel_case_start_from_idx("AbcDef", 1), StrIndex::new(3, 3));
80
+ /// assert_eq!(camel_case_start_from_idx("AbcDefGhi", 0), StrIndex::new(0, 0));
81
+ /// assert_eq!(camel_case_start_from_idx("AbcDefGhi", 1), StrIndex::new(3, 3));
82
+ /// assert_eq!(camel_case_start_from_idx("Abcdefg", 1), StrIndex::new(7, 7));
80
83
/// ```
81
84
pub fn camel_case_start_from_idx ( s : & str , start_idx : usize ) -> StrIndex {
82
85
let char_count = s. chars ( ) . count ( ) ;
@@ -94,7 +97,7 @@ pub fn camel_case_start_from_idx(s: &str, start_idx: usize) -> StrIndex {
94
97
let mut last_index = StrIndex :: new ( char_count, s. len ( ) ) ;
95
98
for ( char_index, ( byte_index, c) ) in iter {
96
99
if byte_index < start_idx {
97
- continue ;
100
+ break ;
98
101
}
99
102
if down {
100
103
if c. is_uppercase ( ) {
@@ -120,12 +123,17 @@ pub fn camel_case_start_from_idx(s: &str, start_idx: usize) -> StrIndex {
120
123
/// Get the indexes of camel case components of a string `s`
121
124
///
122
125
/// ```
123
- /// # use clippy_utils::str_utils::{camel_case_indexes, StrIndex};
124
- /// assert_eq!(camel_case_indexes("AbcDef"), vec![StrIndex::new(0, 0), StrIndex::new(3, 3),
125
- /// StrIndex::new(6, 6)]);
126
- /// assert_eq!(camel_case_indexes("abcDef"), vec![StrIndex::new(3, 3), StrIndex::new(6, 6)]);
126
+ /// # use clippy_utils::str_utils::{camel_case_indices, StrIndex};
127
+ /// assert_eq!(
128
+ /// camel_case_indices("AbcDef"),
129
+ /// vec![StrIndex::new(0, 0), StrIndex::new(3, 3), StrIndex::new(6, 6)]
130
+ /// );
131
+ /// assert_eq!(
132
+ /// camel_case_indices("abcDef"),
133
+ /// vec![StrIndex::new(3, 3), StrIndex::new(6, 6)]
134
+ /// );
127
135
/// ```
128
- pub fn camel_case_indexes ( s : & str ) -> Vec < StrIndex > {
136
+ pub fn camel_case_indices ( s : & str ) -> Vec < StrIndex > {
129
137
let mut result = Vec :: new ( ) ;
130
138
let mut str_idx = camel_case_start ( s) ;
131
139
@@ -146,20 +154,15 @@ pub fn camel_case_indexes(s: &str) -> Vec<StrIndex> {
146
154
/// assert_eq!(camel_case_split("AbcDef"), vec!["Abc", "Def"]);
147
155
/// ```
148
156
pub fn camel_case_split ( s : & str ) -> Vec < & str > {
149
- let offsets = camel_case_indexes ( s) ;
150
- let mut idxs_iter = offsets. iter ( ) . map ( |str_idx| str_idx. byte_index ) . peekable ( ) ;
151
- let idxs: Vec < usize > = if let Some ( & idx) = idxs_iter. peek ( ) {
152
- if idx == 0 {
153
- idxs_iter. collect ( )
154
- } else {
155
- Vec :: < usize > :: from ( [ 0 ] ) . into_iter ( ) . chain ( idxs_iter) . collect ( )
156
- }
157
- } else {
158
- return vec ! [ s] ;
159
- } ;
160
- let split_points: Vec < ( & usize , & usize ) > = idxs[ ..idxs. len ( ) - 1 ] . iter ( ) . zip ( & idxs[ 1 ..] ) . collect ( ) ;
157
+ let mut offsets = camel_case_indices ( s)
158
+ . iter ( )
159
+ . map ( |e| e. byte_index )
160
+ . collect :: < Vec < usize > > ( ) ;
161
+ if offsets[ 0 ] != 0 {
162
+ offsets. insert ( 0 , 0 ) ;
163
+ }
161
164
162
- split_points . iter ( ) . map ( |( & start , & stop ) | & s[ start..stop ] ) . collect ( )
165
+ offsets . windows ( 2 ) . map ( |w | & s[ w [ 0 ] ..w [ 1 ] ] ) . collect ( )
163
166
}
164
167
165
168
/// Dealing with sting comparison can be complicated, this struct ensures that both the
@@ -298,11 +301,14 @@ mod test {
298
301
assert_eq ! ( camel_case_start_from_idx( "AbcDef" , 0 ) , StrIndex :: new( 0 , 0 ) ) ;
299
302
assert_eq ! ( camel_case_start_from_idx( "AbcDef" , 1 ) , StrIndex :: new( 3 , 3 ) ) ;
300
303
assert_eq ! ( camel_case_start_from_idx( "AbcDef" , 4 ) , StrIndex :: new( 6 , 6 ) ) ;
304
+ assert_eq ! ( camel_case_start_from_idx( "AbcDefGhi" , 0 ) , StrIndex :: new( 0 , 0 ) ) ;
305
+ assert_eq ! ( camel_case_start_from_idx( "AbcDefGhi" , 1 ) , StrIndex :: new( 3 , 3 ) ) ;
306
+ assert_eq ! ( camel_case_start_from_idx( "Abcdefg" , 1 ) , StrIndex :: new( 7 , 7 ) ) ;
301
307
}
302
308
303
309
#[ test]
304
- fn camel_case_indexes_full ( ) {
305
- assert_eq ! ( camel_case_indexes ( "Abc\u{f6} \u{f6} DD" ) , vec![ StrIndex :: new( 7 , 9 ) ] ) ;
310
+ fn camel_case_indices_full ( ) {
311
+ assert_eq ! ( camel_case_indices ( "Abc\u{f6} \u{f6} DD" ) , vec![ StrIndex :: new( 7 , 9 ) ] ) ;
306
312
}
307
313
308
314
#[ test]
0 commit comments