@@ -77,6 +77,9 @@ pub fn camel_case_start(s: &str) -> StrIndex {
7777/// # use clippy_utils::str_utils::{camel_case_start_from_idx, StrIndex};
7878/// assert_eq!(camel_case_start_from_idx("AbcDef", 0), StrIndex::new(0, 0));
7979/// 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));
8083/// ```
8184pub fn camel_case_start_from_idx ( s : & str , start_idx : usize ) -> StrIndex {
8285 let char_count = s. chars ( ) . count ( ) ;
@@ -94,7 +97,7 @@ pub fn camel_case_start_from_idx(s: &str, start_idx: usize) -> StrIndex {
9497 let mut last_index = StrIndex :: new ( char_count, s. len ( ) ) ;
9598 for ( char_index, ( byte_index, c) ) in iter {
9699 if byte_index < start_idx {
97- continue ;
100+ break ;
98101 }
99102 if down {
100103 if c. is_uppercase ( ) {
@@ -120,12 +123,17 @@ pub fn camel_case_start_from_idx(s: &str, start_idx: usize) -> StrIndex {
120123/// Get the indexes of camel case components of a string `s`
121124///
122125/// ```
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+ /// );
127135/// ```
128- pub fn camel_case_indexes ( s : & str ) -> Vec < StrIndex > {
136+ pub fn camel_case_indices ( s : & str ) -> Vec < StrIndex > {
129137 let mut result = Vec :: new ( ) ;
130138 let mut str_idx = camel_case_start ( s) ;
131139
@@ -146,20 +154,15 @@ pub fn camel_case_indexes(s: &str) -> Vec<StrIndex> {
146154/// assert_eq!(camel_case_split("AbcDef"), vec!["Abc", "Def"]);
147155/// ```
148156pub 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+ }
161164
162- split_points . iter ( ) . map ( |( & start , & stop ) | & s[ start..stop ] ) . collect ( )
165+ offsets . windows ( 2 ) . map ( |w | & s[ w [ 0 ] ..w [ 1 ] ] ) . collect ( )
163166}
164167
165168/// Dealing with sting comparison can be complicated, this struct ensures that both the
@@ -298,11 +301,14 @@ mod test {
298301 assert_eq ! ( camel_case_start_from_idx( "AbcDef" , 0 ) , StrIndex :: new( 0 , 0 ) ) ;
299302 assert_eq ! ( camel_case_start_from_idx( "AbcDef" , 1 ) , StrIndex :: new( 3 , 3 ) ) ;
300303 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 ) ) ;
301307 }
302308
303309 #[ 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 ) ] ) ;
306312 }
307313
308314 #[ test]
0 commit comments