Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3212,28 +3212,20 @@ protected void addParentContainer(CodegenModel m, String name, Property property

/**
* Underscore the given word.
* Copied from Twitter elephant bird
* https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
*
* @param word The word
* @return The underscored version of the word
*/
public static String underscore(String word) {
String firstPattern = "([A-Z]+)([A-Z][a-z][a-z]+)";
String secondPattern = "([a-z\\d])([A-Z])";
Pattern firstPattern = Pattern.compile("(?<=[A-Z])(?=[A-Z][a-z]{2,})");
Pattern secondPattern = Pattern.compile("(?<=[a-z\\d])(?=[A-Z])");
String replacementPattern = "$1_$2";
// Replace package separator with slash.
word = word.replaceAll("\\.", "/"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
// Replace $ with two underscores for inner classes.
word = word.replaceAll("\\$", "__");
// Replace capital letter with _ plus lowercase letter.
word = word.replaceAll(firstPattern, replacementPattern);
word = word.replaceAll(secondPattern, replacementPattern);
word = word.replace('-', '_');
// replace space with underscore
word = word.replace(' ', '_');
word = word.toLowerCase();
return word;

String replaced = word.replace('.', '/').replace("$", "__");
replaced = firstPattern.matcher(replaced).replaceAll("_");
replaced = secondPattern.matcher(replaced).replaceAll("_");
replaced = replaced.replace('-', '_').replace(' ', '_').toLowerCase();
return replaced;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void underscoreNamesTest() {

Assert.assertEquals(codegen.underscore("FooBar"), "foo_bar");
Assert.assertEquals(codegen.underscore("FooBarBaz"), "foo_bar_baz");
Assert.assertEquals(codegen.underscore("HTTPServer"), "http_server");
}

@Test(description = "test camelize")
Expand Down