From 1c2cbcecbb3c718f9e8b4de7f701d8c17a0e2c68 Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Thu, 28 Nov 2024 14:53:29 +0100 Subject: [PATCH] Apply regex to check if name isn't pascal case already --- .../csharp/service/CSharpNamingService.java | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/rewrite-csharp/src/main/java/org/openrewrite/csharp/service/CSharpNamingService.java b/rewrite-csharp/src/main/java/org/openrewrite/csharp/service/CSharpNamingService.java index f53a44b..52bd794 100644 --- a/rewrite-csharp/src/main/java/org/openrewrite/csharp/service/CSharpNamingService.java +++ b/rewrite-csharp/src/main/java/org/openrewrite/csharp/service/CSharpNamingService.java @@ -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; } }