Skip to content

Commit 6c4c02e

Browse files
committed
DRY regex patterns
1 parent db462a6 commit 6c4c02e

63 files changed

Lines changed: 207 additions & 182 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,43 +249,67 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
249249
protected final static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application/json(;.*)?");
250250
protected final static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application/vnd.(.*)+json(;.*)?");
251251
private static final Pattern COMMON_PREFIX_ENUM_NAME = Pattern.compile("[a-zA-Z0-9]+\\z");
252-
/** Matches a trailing run of digits at the end of a name, used by {@link #generateNextName}. */
252+
/**
253+
* Matches a trailing run of digits at the end of a name, used by {@link #generateNextName}.
254+
*/
253255
protected static final Pattern TRAILING_DIGITS = Pattern.compile("\\d+\\z");
254-
/** Matches one or more non-word characters; used in {@link #toEnumVarName} and {@link #sanitizeName}. */
256+
/**
257+
* Matches one or more non-word characters; used in {@link #toEnumVarName} and {@link #sanitizeName}.
258+
*/
255259
protected static final Pattern NON_WORD_PLUS = Pattern.compile("\\W+");
256260
protected static final Pattern LEADING_UNDERSCORES = Pattern.compile("^_*");
257261
protected static final Pattern MULTI_UNDERSCORES = Pattern.compile("_+");
258262
protected static final Pattern MULTI_TRAILING_UNDERSCORES = Pattern.compile("_+$");
263+
protected static final Pattern MULTI_LEADING_UNDERSCORES = Pattern.compile("^_+");
259264
protected static final Pattern FIRST_LEADING_UNDERSCORE = Pattern.compile("^_");
260265
protected static final Pattern LAST_TRAILING_UNDERSCORE = Pattern.compile("_$");
266+
protected static final Pattern NEWLINE = Pattern.compile("\n");
267+
protected static final Pattern TRAILING_NEWLINE = Pattern.compile("\n$");
268+
protected static final Pattern LEFT_CURLY_BRACE = Pattern.compile("\\{");
269+
protected static final Pattern RIGHT_CURLY_BRACE = Pattern.compile("}");
261270

262271

263272
protected static final Pattern WHITESPACE = Pattern.compile("\\s+");
264273

265274
protected static final Pattern STARTS_WITH_SLASH = Pattern.compile("^/.*");
266275

267276
protected static final Pattern UNESCAPED_SLASH = Pattern.compile("(?<!\\\\)/");
277+
protected static final Pattern TRAILING_BACKSLASHES = Pattern.compile("\\\\+$");
278+
protected static final Pattern ENCLOSING_QUOTES = Pattern.compile("^\"|\"$");
268279

269-
/** Matches a string that starts with a digit (anchored); used across language generators. */
280+
281+
/**
282+
* Matches a string that starts with a digit (anchored); used across language generators.
283+
*/
270284
protected static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d.*");
271285

272286

273-
/** Matches a string consisting entirely of uppercase letters and underscores. */
287+
/**
288+
* Matches a string consisting entirely of uppercase letters and underscores.
289+
*/
274290
protected static final Pattern ALL_UPPER_UNDERSCORE = Pattern.compile("^[A-Z_]*$");
291+
protected static final Pattern NON_WORD_CHAR = Pattern.compile("[^a-zA-Z0-9_]");
292+
protected static final Pattern DASH_UNDERSCORE_SPACE_COLON_PARENTHESES = Pattern.compile("[-_ :()]");
275293

276294
protected static final Pattern MINUS = Pattern.compile("-");
277295
protected static final Pattern PLUS = Pattern.compile("\\+");
278296
protected static final Pattern DOT = Pattern.compile("\\.");
279297

280-
protected static final Pattern PATH_PARAMETER = Pattern.compile("\\{(.*?)}");
298+
protected static final Pattern PATH_PARAMETER = Pattern.compile("\\{([^}]+)}");
281299

282-
/** Matches a string consisting entirely of uppercase letters and underscores and digits. */
300+
/**
301+
* Matches a string consisting entirely of uppercase letters and underscores and digits.
302+
*/
283303
protected static final Pattern ALL_UPPER_UNDERSCORE_DIGITS = Pattern.compile("^[A-Z0-9_]*$");
284-
/** Matches tab, newline, or carriage-return; used in {@link #escapeText}. */
304+
/**
305+
* Matches tab, newline, or carriage-return; used in {@link #escapeText}.
306+
*/
285307
protected static final Pattern CONTROL_WHITESPACE = Pattern.compile("[\\t\\n\\r]");
286308

287309
protected static final Pattern MULTILINE_STRING = Pattern.compile("\r\n|\r|\n");
288-
/** Matches a callback path-expression parameter like {@code {$request.body#/id}}. */
310+
/**
311+
* Matches a callback path-expression parameter like {@code {$request.body#/id}}.
312+
*/
289313
private static final Pattern CALLBACK_EXPRESSION_PARAM = Pattern.compile("\\{\\$.*}");
290314
// Dynamic patterns keyed by user-supplied removeCharRegEx strings are cached via PatternCache.
291315

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ protected File processTemplateToFile(Map<String, Object> templateData, String te
14351435
private final Set<String> seenFilesLower = new HashSet<>();
14361436

14371437
private File processTemplateToFile(Map<String, Object> templateData, String templateName, String outputFilename, boolean shouldGenerate, String skippedByOption, String intendedOutputDir) throws IOException {
1438-
String adjustedOutputFilename = outputFilename.replaceAll("//", "/").replace('/', File.separatorChar);
1438+
String adjustedOutputFilename = outputFilename.replace("//", "/").replace('/', File.separatorChar);
14391439
File target = new File(adjustedOutputFilename);
14401440
if (ignoreProcessor.allowsFile(target)) {
14411441
if (shouldGenerate) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AdaCodegen.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void processOpts() {
125125
additionalProperties.put("httpClientGprName", httpClientPackageName.toLowerCase(Locale.ROOT));
126126
additionalProperties.put(CodegenConstants.PROJECT_NAME, projectName);
127127

128-
String[] names = this.modelPackage.split("\\.");
128+
String[] names = DOT.split(this.modelPackage);
129129
String pkgName = names[0];
130130
additionalProperties.put("packageLevel1", pkgName);
131131
supportingFiles.add(new SupportingFile("package-spec-level1.mustache", "src",
@@ -146,8 +146,8 @@ public void processOpts() {
146146
@Override
147147
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
148148
String content = fragment.execute();
149-
content = content.trim().replaceAll("\n$", "");
150-
writer.write(content.replaceAll("\n", "\n -- "));
149+
content = TRAILING_NEWLINE.matcher(content.trim()).replaceAll("");
150+
writer.write(NEWLINE.matcher(content).replaceAll("\n -- "));
151151
}
152152
});
153153
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AdaServerCodegen.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void processOpts() {
131131
additionalProperties.put("httpClientGprName", httpClientPackageName.toLowerCase(Locale.ROOT));
132132
additionalProperties.put(CodegenConstants.PROJECT_NAME, projectName);
133133

134-
String names[] = this.modelPackage.split("\\.");
134+
String names[] = DOT.split(this.modelPackage);
135135
String pkgName = names[0];
136136
additionalProperties.put("packageLevel1", pkgName);
137137
supportingFiles.add(new SupportingFile("package-spec-level1.mustache", "src",
@@ -152,8 +152,8 @@ public void processOpts() {
152152
@Override
153153
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
154154
String content = fragment.execute();
155-
content = content.trim().replaceAll("\n$", "");
156-
writer.write(content.replaceAll("\n", "\n -- "));
155+
content = TRAILING_NEWLINE.matcher(content.trim()).replaceAll("");
156+
writer.write(NEWLINE.matcher(content).replaceAll("\n -- "));
157157
}
158158
});
159159
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AndroidClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public String toVarName(String name) {
260260
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
261261

262262
// replace - with _ e.g. created-at => created_at
263-
name = name.replaceAll("-", "_"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
263+
name = MINUS.matcher(name).replaceAll("_"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
264264

265265
// if it's all upper case, do nothing
266266
if (ALL_UPPER_UNDERSCORE.matcher(name).matches()) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AsciidocDocumentationCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void execute(final Template.Fragment frag, final Writer out) throws IOExc
9898
}
9999

100100
private String escapeCurlyBrackets(String relativeFileName) {
101-
return relativeFileName.replaceAll("\\{", "\\\\{").replaceAll("\\}", "\\\\}");
101+
return RIGHT_CURLY_BRACE.matcher(LEFT_CURLY_BRACE.matcher(relativeFileName).replaceAll("\\\\{")).replaceAll("\\\\}");
102102
}
103103
}
104104

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AvroSchemaCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ protected List<Map<String, Object>> buildEnumVars(List<Object> values, String da
252252
*/
253253
private String sanitizeEnumValue(String value) {
254254
// Replace any non-alphanumeric characters with an underscore
255-
String sanitizedValue = value.replaceAll("[^A-Za-z0-9_]", "_");
255+
String sanitizedValue = NON_WORD_CHAR.matcher(value).replaceAll("_");
256256
// If the enum starts with a number, prefix it with an underscore
257257
sanitizedValue = sanitizedValue.replaceAll("^([0-9])", "_$1");
258258
return sanitizedValue;

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ public String toParamName(String name) {
630630
if (isReservedWord(name) || STARTS_WITH_DIGIT.matcher(name).matches()) {
631631
name = escapeReservedWord(name);
632632
}
633-
name = name.replaceAll("-", "_");
633+
name = MINUS.matcher(name).replaceAll("_");
634634
return name;
635635
}
636636

@@ -683,7 +683,7 @@ public String toModelDocFilename(String name) {
683683
@Override
684684
public String toApiFilename(String name) {
685685
// replace - with _ e.g. created-at => created_at
686-
name = name.replaceAll("-", "_"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
686+
name = MINUS.matcher(name).replaceAll("_"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
687687

688688
// e.g. PhoneNumberApi.rb => phone_number_api.rb
689689
return camelize(name) + "API";
@@ -715,7 +715,7 @@ public String toApiName(String name) {
715715

716716
@Override
717717
public String toEnumValue(String value, String datatype) {
718-
value = value.replaceAll("-", "_");
718+
value = MINUS.matcher(value).replaceAll("_");
719719
if (isReservedWord(value)) {
720720
value = escapeReservedWord(value);
721721
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ public String toEnumVarName(String value, String datatype) {
13231323
}
13241324

13251325
// string
1326-
String var = value.replaceAll(" ", "_");
1326+
String var = value.replace(" ", "_");
13271327
var = camelize(var);
13281328
var = NON_WORD_PLUS.matcher(var).replaceAll("");
13291329

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpReducedClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ public String toEnumVarName(String value, String datatype) {
847847
}
848848

849849
// string
850-
String var = value.replaceAll(" ", "_");
850+
String var = value.replace(" ", "_");
851851
var = camelize(var);
852852
var = NON_WORD_PLUS.matcher(var).replaceAll("");
853853

0 commit comments

Comments
 (0)