|
79 | 79 | import java.nio.charset.StandardCharsets; |
80 | 80 | import java.util.*; |
81 | 81 | import java.util.Map.Entry; |
| 82 | +import java.util.concurrent.ConcurrentHashMap; |
82 | 83 | import java.util.concurrent.ConcurrentSkipListSet; |
83 | 84 | import java.util.concurrent.TimeUnit; |
84 | 85 | import java.util.function.Consumer; |
@@ -248,6 +249,14 @@ apiTemplateFiles are for API outputs only (controllers/handlers). |
248 | 249 | protected final static Pattern JSON_MIME_PATTERN = Pattern.compile("(?i)application/json(;.*)?"); |
249 | 250 | protected final static Pattern JSON_VENDOR_MIME_PATTERN = Pattern.compile("(?i)application/vnd.(.*)+json(;.*)?"); |
250 | 251 | 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}. */ |
| 253 | + private static final Pattern TRAILING_DIGITS = Pattern.compile("\\d+\\z"); |
| 254 | + /** |
| 255 | + * Cache of removeCharRegEx string → compiled {@link Pattern} with {@link Pattern#UNICODE_CHARACTER_CLASS}. |
| 256 | + * {@code sanitizeName} is called once per unique (name, regex, exceptions) tuple, so the regex string |
| 257 | + * (almost always {@code "\\W"}) would otherwise be recompiled for every unique property/model name. |
| 258 | + */ |
| 259 | + private static final ConcurrentHashMap<String, Pattern> REMOVE_CHAR_UNICODE_PATTERN_CACHE = new ConcurrentHashMap<>(); |
251 | 260 |
|
252 | 261 | /** |
253 | 262 | * True if the code generator supports multiple class inheritance. |
@@ -6009,8 +6018,7 @@ protected void addParentContainer(CodegenModel model, String name, Schema schema |
6009 | 6018 | * @return The next name for the base name |
6010 | 6019 | */ |
6011 | 6020 | private static String generateNextName(String name) { |
6012 | | - Pattern pattern = Pattern.compile("\\d+\\z"); |
6013 | | - Matcher matcher = pattern.matcher(name); |
| 6021 | + Matcher matcher = TRAILING_DIGITS.matcher(name); |
6014 | 6022 | if (matcher.find()) { |
6015 | 6023 | String numStr = matcher.group(); |
6016 | 6024 | int num = Integer.parseInt(numStr) + 1; |
@@ -6755,7 +6763,10 @@ public String sanitizeName(final String name, String removeCharRegEx, ArrayList< |
6755 | 6763 | // remove everything else other than word, number and _ |
6756 | 6764 | // $php_variable => php_variable |
6757 | 6765 | if (allowUnicodeIdentifiers) { //could be converted to a single line with ?: operator |
6758 | | - modifiable = Pattern.compile(sanitizeNameOptions.getRemoveCharRegEx(), Pattern.UNICODE_CHARACTER_CLASS).matcher(modifiable).replaceAll(""); |
| 6766 | + modifiable = REMOVE_CHAR_UNICODE_PATTERN_CACHE |
| 6767 | + .computeIfAbsent(sanitizeNameOptions.getRemoveCharRegEx(), |
| 6768 | + regex -> Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS)) |
| 6769 | + .matcher(modifiable).replaceAll(""); |
6759 | 6770 | } else { |
6760 | 6771 | modifiable = modifiable.replaceAll(sanitizeNameOptions.getRemoveCharRegEx(), ""); |
6761 | 6772 | } |
|
0 commit comments