File tree Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Expand file tree Collapse file tree 2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,13 @@ use std::borrow::Cow;
7
7
pub fn to_camel_case ( s : & ' _ str ) -> Cow < ' _ , str > {
8
8
let mut dest = Cow :: Borrowed ( s) ;
9
9
10
- for ( i, part) in s. split ( '_' ) . enumerate ( ) {
10
+ // handle '_' to be more friendly with the
11
+ // _var convention for unused variables
12
+ let s_iter = if s. starts_with ( '_' ) { & s[ 1 ..] } else { s }
13
+ . split ( '_' )
14
+ . enumerate ( ) ;
15
+
16
+ for ( i, part) in s_iter {
11
17
if i > 0 && part. len ( ) == 1 {
12
18
dest += Cow :: Owned ( part. to_uppercase ( ) ) ;
13
19
} else if i > 0 && part. len ( ) > 1 {
@@ -32,7 +38,7 @@ pub fn to_camel_case(s: &'_ str) -> Cow<'_, str> {
32
38
#[ test]
33
39
fn test_to_camel_case ( ) {
34
40
assert_eq ! ( & to_camel_case( "test" ) [ ..] , "test" ) ;
35
- assert_eq ! ( & to_camel_case( "_test" ) [ ..] , "Test " ) ;
41
+ assert_eq ! ( & to_camel_case( "_test" ) [ ..] , "test " ) ;
36
42
assert_eq ! ( & to_camel_case( "first_second" ) [ ..] , "firstSecond" ) ;
37
43
assert_eq ! ( & to_camel_case( "first_" ) [ ..] , "first" ) ;
38
44
assert_eq ! ( & to_camel_case( "a_b_c" ) [ ..] , "aBC" ) ;
Original file line number Diff line number Diff line change @@ -233,7 +233,13 @@ fn get_doc_attr(attrs: &[Attribute]) -> Option<Vec<MetaNameValue>> {
233
233
pub fn to_camel_case ( s : & str ) -> String {
234
234
let mut dest = String :: new ( ) ;
235
235
236
- for ( i, part) in s. split ( '_' ) . enumerate ( ) {
236
+ // handle '_' to be more friendly with the
237
+ // _var convention for unused variables
238
+ let s_iter = if s. starts_with ( '_' ) { & s[ 1 ..] } else { s }
239
+ . split ( '_' )
240
+ . enumerate ( ) ;
241
+
242
+ for ( i, part) in s_iter {
237
243
if i > 0 && part. len ( ) == 1 {
238
244
dest. push_str ( & part. to_uppercase ( ) ) ;
239
245
} else if i > 0 && part. len ( ) > 1 {
@@ -1874,7 +1880,7 @@ mod test {
1874
1880
#[ test]
1875
1881
fn test_to_camel_case ( ) {
1876
1882
assert_eq ! ( & to_camel_case( "test" ) [ ..] , "test" ) ;
1877
- assert_eq ! ( & to_camel_case( "_test" ) [ ..] , "Test " ) ;
1883
+ assert_eq ! ( & to_camel_case( "_test" ) [ ..] , "test " ) ;
1878
1884
assert_eq ! ( & to_camel_case( "first_second" ) [ ..] , "firstSecond" ) ;
1879
1885
assert_eq ! ( & to_camel_case( "first_" ) [ ..] , "first" ) ;
1880
1886
assert_eq ! ( & to_camel_case( "a_b_c" ) [ ..] , "aBC" ) ;
You can’t perform that action at this time.
0 commit comments