Skip to content

Commit

Permalink
NameCaseConvention: Optimize matches()
Browse files Browse the repository at this point in the history
For some recipes it can be quite expensive to first format every variable name only to check if it was already compliant.
  • Loading branch information
knutwannheden committed Mar 14, 2024
1 parent 22eef9a commit c88fe60
Showing 1 changed file with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,64 @@ public boolean matches(String str) {
}

public static boolean matches(NameCaseConvention convention, String str) {
return str.equals(format(convention, str));
switch (convention) {
case LOWER_CAMEL:
if (!Character.isLowerCase(str.charAt(0)) && str.charAt(0) != '$') {
return false;
}
for (int i = 1; i < str.length(); i++) {
char p = str.charAt(i - 1);
char c = str.charAt(i);
if (Character.isUpperCase(c) && Character.isUpperCase(p)) {
return false;
} else if (c == '_') {
return false;
}
}
return true;
case UPPER_CAMEL:
if (!Character.isUpperCase(str.charAt(0)) && str.charAt(0) != '$') {
return false;
}
for (int i = 1; i < str.length(); i++) {
char p = str.charAt(i - 1);
char c = str.charAt(i);
if (Character.isUpperCase(c) && Character.isUpperCase(p)) {
return false;
} else if (c == '_') {
return false;
}
}
return true;
case LOWER_UNDERSCORE:
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isAlphabetic(c) && Character.isUpperCase(c)) {
return false;
}
}
return true;
case UPPER_UNDERSCORE:
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isAlphabetic(c) && Character.isLowerCase(c)) {
return false;
}
}
return true;
case LOWER_HYPHEN:
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isAlphabetic(c) && Character.isUpperCase(c)) {
return false;
} else if (c == '_') {
return false;
}
}
return true;
default:
return str.equals(format(convention, str));
}
}

/**
Expand Down

0 comments on commit c88fe60

Please sign in to comment.