• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // copy-paste from Google Protobuf
2 // must be kept in sync with Google for JSON interop
3 #[doc(hidden)]
camel_case(input: &str) -> String4 pub fn camel_case(input: &str) -> String {
5     let mut capitalize_next = true;
6     let mut result = String::new();
7     result.reserve(input.len());
8 
9     for c in input.chars() {
10         if c == '_' {
11             capitalize_next = true;
12         } else if capitalize_next {
13             result.push(c.to_ascii_uppercase());
14             capitalize_next = false;
15         } else {
16             result.push(c);
17         }
18     }
19 
20     result
21 }
22 
23 #[doc(hidden)]
snake_case(input: &str) -> String24 pub fn snake_case(input: &str) -> String {
25     let mut result = String::new();
26 
27     let mut last_lower = false;
28 
29     for c in input.chars() {
30         if c.is_ascii_uppercase() && last_lower {
31             result.push('_');
32         }
33         result.push(c.to_ascii_lowercase());
34         last_lower = c.is_lowercase();
35     }
36 
37     result
38 }
39 
40 #[cfg(test)]
41 mod test {
42     use super::*;
43 
44     #[test]
test_camel_case()45     fn test_camel_case() {
46         assert_eq!("FooBarBazQuxQUUX", camel_case("foo_barBaz_QuxQUUX"));
47         assert_eq!("FooBarBazQuxQUUX", camel_case("Foo_barBaz_QuxQUUX"));
48     }
49 
50     #[test]
test_snake_case()51     fn test_snake_case() {
52         assert_eq!("foo_bar_baz_qux_quux", snake_case("foo_barBaz_QuxQUUX"));
53         assert_eq!("foo_bar_baz_qux_quux", snake_case("Foo_barBaz_QuxQUUX"));
54     }
55 }
56