MCPcopy Create free account
hub / github.com/apache/fory / to_snake_case

Function to_snake_case

rust/fory-core/src/util/string_util.rs:111–130  ·  view source on GitHub ↗

Converts a camelCase or PascalCase string to snake_case.

(name: &str)

Source from the content-addressed store, hash-verified

109
110/// Converts a camelCase or PascalCase string to snake_case.
111pub fn to_snake_case(name: &str) -> String {
112 let mut result = String::with_capacity(name.len() + 4);
113 let chars: Vec<char> = name.chars().collect();
114
115 for (i, &c) in chars.iter().enumerate() {
116 if c.is_ascii_uppercase() {
117 if i > 0 {
118 let prev_upper = chars.get(i - 1).is_some_and(|c| c.is_ascii_uppercase());
119 let next_upper_or_end = chars.get(i + 1).map_or(true, |c| c.is_ascii_uppercase());
120 if !prev_upper || !next_upper_or_end {
121 result.push('_');
122 }
123 }
124 result.push(c.to_ascii_lowercase());
125 } else {
126 result.push(c);
127 }
128 }
129 result
130}
131
132/// Converts a snake_case string to lowerCamelCase.
133pub fn to_camel_case(name: &str) -> String {

Callers 3

assign_remote_field_idsFunction · 0.50
group_fields_by_typeFunction · 0.50

Calls 2

lenMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected