Skip to content

Commit

Permalink
Apply regex to check if name isn't pascal case already
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Nov 28, 2024
1 parent 06c492c commit 1c2cbce
Showing 1 changed file with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,41 @@

public class CSharpNamingService implements NamingService {

private static final Pattern STANDARD_METHOD_NAME = Pattern.compile("^[A-Z][a-zA-Z0-9]*$");
private static final Pattern SNAKE_CASE = Pattern.compile("^[a-zA-Z0-9]+_\\w+$");

@Override
public String getMethodName(String oldMethodName) {
StringBuilder result = new StringBuilder();
if (SNAKE_CASE.matcher(oldMethodName).matches()) {
result.append(NameCaseConvention.format(NameCaseConvention.UPPER_CAMEL, oldMethodName));
} else {
int nameLength = oldMethodName.length();
for (int i = 0; i < nameLength; i++) {
char c = oldMethodName.charAt(i);
public String standardizeMethodName(String oldMethodName) {
if (!STANDARD_METHOD_NAME.matcher(oldMethodName).matches()) {
StringBuilder result = new StringBuilder();
if (SNAKE_CASE.matcher(oldMethodName).matches()) {
result.append(NameCaseConvention.format(NameCaseConvention.UPPER_CAMEL, oldMethodName));
} else {
int nameLength = oldMethodName.length();
for (int i = 0; i < nameLength; i++) {
char c = oldMethodName.charAt(i);

if (i == 0) {
// the java specification requires identifiers to start with [a-zA-Z$_]
if (c != '$' && c != '_') {
result.append(Character.toUpperCase(c));
}
} else {
if (!Character.isLetterOrDigit(c)) {
while (i < nameLength && (!Character.isLetterOrDigit(c) || c > 'z')) {
c = oldMethodName.charAt(i++);
}
if (i < nameLength) {
if (i == 0) {
// the java specification requires identifiers to start with [a-zA-Z$_]
if (c != '$' && c != '_') {
result.append(Character.toUpperCase(c));
}
} else {
result.append(c);
if (!Character.isLetterOrDigit(c)) {
while (i < nameLength && (!Character.isLetterOrDigit(c) || c > 'z')) {
c = oldMethodName.charAt(i++);
}
if (i < nameLength) {
result.append(Character.toUpperCase(c));
}
} else {
result.append(c);
}
}
}
}
return result.toString();
}
return result.toString();
return oldMethodName;
}
}

0 comments on commit 1c2cbce

Please sign in to comment.