Skip to content

Commit

Permalink
Make the SnakeCase formatter not emit additional _
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zero authored and eqrion committed Nov 26, 2018
1 parent 57f7ec2 commit 5a9c8b1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/bindgen/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,25 @@ impl RenameRule {
RenameRule::PascalCase => text.to_owned(),
RenameRule::CamelCase => text[..1].to_lowercase() + &text[1..],
RenameRule::SnakeCase => {
// Do not add additional `_` if the string already contains `_` e.g. `__Field`
// Do not split consecutive capital letters
let mut result = String::new();
let mut add_separator = true;
let mut prev_uppercase = false;
for (i, c) in text.char_indices() {
if c.is_uppercase() && i != 0 {
result.push_str("_");
if c == '_' {
add_separator = false;
prev_uppercase = false;
}
if c.is_uppercase() {
if i != 0 && add_separator && !prev_uppercase {
result.push_str("_");
} else {
add_separator = true;
}
prev_uppercase = true;
} else {
prev_uppercase = false;
}
for x in c.to_lowercase() {
result.push(x);
Expand Down

0 comments on commit 5a9c8b1

Please sign in to comment.