@@ -29,9 +29,7 @@ pub fn split_by_case(s: &str, separators: Option<&[char]>) -> Vec<String> {
2929 for c in s. chars ( ) {
3030 let is_splitter = splitters. contains ( & c) ;
3131 if is_splitter {
32- if !buff. is_empty ( ) {
33- parts. push ( std:: mem:: take ( & mut buff) ) ;
34- }
32+ parts. push ( std:: mem:: take ( & mut buff) ) ;
3533 previous_upper = None ;
3634 previous_splitter = Some ( true ) ;
3735 continue ;
@@ -54,9 +52,10 @@ pub fn split_by_case(s: &str, separators: Option<&[char]>) -> Vec<String> {
5452 let char_count = buff. chars ( ) . count ( ) ;
5553 if char_count > 1 {
5654 let last_char = buff. chars ( ) . last ( ) . unwrap ( ) ;
55+ // Byte index of the start of the last character
5756 let new_len = buff
5857 . char_indices ( )
59- . nth ( char_count - 2 )
58+ . nth ( char_count - 1 )
6059 . map ( |( i, _) | i)
6160 . unwrap_or ( 0 ) ;
6261 let rest = buff[ ..new_len] . to_string ( ) ;
@@ -91,8 +90,43 @@ pub fn hello(name: &str) -> String {
9190#[ cfg( test) ]
9291mod tests {
9392 use super :: * ;
93+
9494 #[ test]
9595 fn test_hello ( ) {
9696 assert_eq ! ( hello( "world" ) , "Hello, world!" ) ;
9797 }
98+
99+ #[ test]
100+ fn test_split_by_case ( ) {
101+ // Default separators: ['-', '_', '/', '.']
102+ assert_eq ! ( split_by_case( "" , None ) , Vec :: <String >:: new( ) ) ;
103+ assert_eq ! ( split_by_case( "foo" , None ) , [ "foo" ] ) ;
104+ assert_eq ! ( split_by_case( "fooBar" , None ) , [ "foo" , "Bar" ] ) ;
105+ assert_eq ! ( split_by_case( "FooBarBaz" , None ) , [ "Foo" , "Bar" , "Baz" ] ) ;
106+ assert_eq ! ( split_by_case( "FooBARb" , None ) , [ "Foo" , "BA" , "Rb" ] ) ;
107+ assert_eq ! (
108+ split_by_case( "foo_bar-baz/qux" , None ) ,
109+ [ "foo" , "bar" , "baz" , "qux" ]
110+ ) ;
111+ assert_eq ! (
112+ split_by_case( "foo--bar-Baz" , None ) ,
113+ [ "foo" , "" , "bar" , "Baz" ]
114+ ) ;
115+ assert_eq ! ( split_by_case( "FOO_BAR" , None ) , [ "FOO" , "BAR" ] ) ;
116+ assert_eq ! ( split_by_case( "foo123-bar" , None ) , [ "foo123" , "bar" ] ) ;
117+ assert_eq ! ( split_by_case( "FOOBar" , None ) , [ "FOO" , "Bar" ] ) ;
118+ assert_eq ! ( split_by_case( "ALink" , None ) , [ "A" , "Link" ] ) ;
119+
120+ // Custom separators: ['\\', '.', '-']
121+ assert_eq ! (
122+ split_by_case( r"foo\Bar.fuzz-FIZz" , Some ( & [ '\\' , '.' , '-' ] ) ) ,
123+ [ "foo" , "Bar" , "fuzz" , "FI" , "Zz" ]
124+ ) ;
125+
126+ // Custom separator: only ['_']
127+ assert_eq ! (
128+ split_by_case( "new-name-value" , Some ( & [ '_' ] ) ) ,
129+ [ "new-name-value" ]
130+ ) ;
131+ }
98132}
0 commit comments