You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/12/10 11:24:26 UTC
(commons-text) 01/04: Reduce nesting
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git
commit 4f366ec330cb4d48cc8b0433ce3ba57d11569c2a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Dec 10 06:16:37 2024 -0500
Reduce nesting
Remove unnecessary keyword
---
.../org/apache/commons/text/StrSubstitutor.java | 238 ++++++++++-----------
1 file changed, 118 insertions(+), 120 deletions(-)
diff --git a/src/main/java/org/apache/commons/text/StrSubstitutor.java b/src/main/java/org/apache/commons/text/StrSubstitutor.java
index 721d6745..085751dc 100644
--- a/src/main/java/org/apache/commons/text/StrSubstitutor.java
+++ b/src/main/java/org/apache/commons/text/StrSubstitutor.java
@@ -329,11 +329,11 @@ public class StrSubstitutor {
*/
public StrSubstitutor(final StrLookup<?> variableResolver, final String prefix, final String suffix,
final char escape) {
- this.setVariableResolver(variableResolver);
- this.setVariablePrefix(prefix);
- this.setVariableSuffix(suffix);
- this.setEscapeChar(escape);
- this.setValueDelimiterMatcher(DEFAULT_VALUE_DELIMITER);
+ setVariableResolver(variableResolver);
+ setVariablePrefix(prefix);
+ setVariableSuffix(suffix);
+ setEscapeChar(escape);
+ setValueDelimiterMatcher(DEFAULT_VALUE_DELIMITER);
}
/**
@@ -348,11 +348,11 @@ public class StrSubstitutor {
*/
public StrSubstitutor(final StrLookup<?> variableResolver, final String prefix, final String suffix,
final char escape, final String valueDelimiter) {
- this.setVariableResolver(variableResolver);
- this.setVariablePrefix(prefix);
- this.setVariableSuffix(suffix);
- this.setEscapeChar(escape);
- this.setValueDelimiter(valueDelimiter);
+ setVariableResolver(variableResolver);
+ setVariablePrefix(prefix);
+ setVariableSuffix(suffix);
+ setEscapeChar(escape);
+ setValueDelimiter(valueDelimiter);
}
/**
@@ -383,11 +383,11 @@ public class StrSubstitutor {
public StrSubstitutor(
final StrLookup<?> variableResolver, final StrMatcher prefixMatcher, final StrMatcher suffixMatcher,
final char escape, final StrMatcher valueDelimiterMatcher) {
- this.setVariableResolver(variableResolver);
- this.setVariablePrefixMatcher(prefixMatcher);
- this.setVariableSuffixMatcher(suffixMatcher);
- this.setEscapeChar(escape);
- this.setValueDelimiterMatcher(valueDelimiterMatcher);
+ setVariableResolver(variableResolver);
+ setVariablePrefixMatcher(prefixMatcher);
+ setVariableSuffixMatcher(suffixMatcher);
+ setEscapeChar(escape);
+ setValueDelimiterMatcher(valueDelimiterMatcher);
}
/**
@@ -1142,123 +1142,121 @@ public class StrSubstitutor {
bufEnd);
if (startMatchLen == 0) {
pos++;
+ } else // found variable start marker
+ if (pos > offset && chars[pos - 1] == escape) {
+ // escaped
+ if (preserveEscapes) {
+ pos++;
+ continue;
+ }
+ buf.deleteCharAt(pos - 1);
+ chars = buf.buffer; // in case buffer was altered
+ lengthChange--;
+ altered = true;
+ bufEnd--;
} else {
- // found variable start marker
- if (pos > offset && chars[pos - 1] == escape) {
- // escaped
- if (preserveEscapes) {
- pos++;
+ // find suffix
+ final int startPos = pos;
+ pos += startMatchLen;
+ int endMatchLen = 0;
+ int nestedVarCount = 0;
+ while (pos < bufEnd) {
+ if (substitutionInVariablesEnabled
+ && pfxMatcher.isMatch(chars,
+ pos, offset, bufEnd) != 0) {
+ // found a nested variable start
+ endMatchLen = pfxMatcher.isMatch(chars,
+ pos, offset, bufEnd);
+ nestedVarCount++;
+ pos += endMatchLen;
continue;
}
- buf.deleteCharAt(pos - 1);
- chars = buf.buffer; // in case buffer was altered
- lengthChange--;
- altered = true;
- bufEnd--;
- } else {
- // find suffix
- final int startPos = pos;
- pos += startMatchLen;
- int endMatchLen = 0;
- int nestedVarCount = 0;
- while (pos < bufEnd) {
- if (substitutionInVariablesEnabled
- && pfxMatcher.isMatch(chars,
- pos, offset, bufEnd) != 0) {
- // found a nested variable start
- endMatchLen = pfxMatcher.isMatch(chars,
- pos, offset, bufEnd);
- nestedVarCount++;
- pos += endMatchLen;
- continue;
- }
- endMatchLen = suffMatcher.isMatch(chars, pos, offset,
- bufEnd);
- if (endMatchLen == 0) {
- pos++;
- } else {
- // found variable end marker
- if (nestedVarCount == 0) {
- String varNameExpr = new String(chars, startPos
- + startMatchLen, pos - startPos
- - startMatchLen);
- if (substitutionInVariablesEnabled) {
- final StrBuilder bufName = new StrBuilder(varNameExpr);
- substitute(bufName, 0, bufName.length());
- varNameExpr = bufName.toString();
- }
- pos += endMatchLen;
- final int endPos = pos;
-
- String varName = varNameExpr;
- String varDefaultValue = null;
-
- if (valueDelimMatcher != null) {
- final char[] varNameExprChars = varNameExpr.toCharArray();
- int valueDelimiterMatchLen = 0;
- for (int i = 0; i < varNameExprChars.length; i++) {
- // if there's any nested variable when nested variable substitution disabled,
- // then stop resolving name and default value.
- if (!substitutionInVariablesEnabled
- && pfxMatcher.isMatch(varNameExprChars,
- i,
- i,
- varNameExprChars.length) != 0) {
- break;
- }
- if (valueDelimMatcher.isMatch(varNameExprChars, i) != 0) {
- valueDelimiterMatchLen = valueDelimMatcher.isMatch(varNameExprChars, i);
- varName = varNameExpr.substring(0, i);
- varDefaultValue = varNameExpr.substring(i + valueDelimiterMatchLen);
- break;
- }
+ endMatchLen = suffMatcher.isMatch(chars, pos, offset,
+ bufEnd);
+ if (endMatchLen == 0) {
+ pos++;
+ } else {
+ // found variable end marker
+ if (nestedVarCount == 0) {
+ String varNameExpr = new String(chars, startPos
+ + startMatchLen, pos - startPos
+ - startMatchLen);
+ if (substitutionInVariablesEnabled) {
+ final StrBuilder bufName = new StrBuilder(varNameExpr);
+ substitute(bufName, 0, bufName.length());
+ varNameExpr = bufName.toString();
+ }
+ pos += endMatchLen;
+ final int endPos = pos;
+
+ String varName = varNameExpr;
+ String varDefaultValue = null;
+
+ if (valueDelimMatcher != null) {
+ final char[] varNameExprChars = varNameExpr.toCharArray();
+ int valueDelimiterMatchLen = 0;
+ for (int i = 0; i < varNameExprChars.length; i++) {
+ // if there's any nested variable when nested variable substitution disabled,
+ // then stop resolving name and default value.
+ if (!substitutionInVariablesEnabled
+ && pfxMatcher.isMatch(varNameExprChars,
+ i,
+ i,
+ varNameExprChars.length) != 0) {
+ break;
+ }
+ if (valueDelimMatcher.isMatch(varNameExprChars, i) != 0) {
+ valueDelimiterMatchLen = valueDelimMatcher.isMatch(varNameExprChars, i);
+ varName = varNameExpr.substring(0, i);
+ varDefaultValue = varNameExpr.substring(i + valueDelimiterMatchLen);
+ break;
}
}
+ }
- // on the first call initialize priorVariables
- if (priorVariables == null) {
- priorVariables = new ArrayList<>();
- priorVariables.add(new String(chars,
- offset, length));
- }
+ // on the first call initialize priorVariables
+ if (priorVariables == null) {
+ priorVariables = new ArrayList<>();
+ priorVariables.add(new String(chars,
+ offset, length));
+ }
- // handle cyclic substitution
- checkCyclicSubstitution(varName, priorVariables);
- priorVariables.add(varName);
+ // handle cyclic substitution
+ checkCyclicSubstitution(varName, priorVariables);
+ priorVariables.add(varName);
- // resolve the variable
- String varValue = resolveVariable(varName, buf,
- startPos, endPos);
- if (varValue == null) {
- varValue = varDefaultValue;
- }
- if (varValue != null) {
- final int varLen = varValue.length();
- buf.replace(startPos, endPos, varValue);
- altered = true;
- int change = 0;
- if (!substitutionInValuesDisabled) { // recursive replace
- change = substitute(buf, startPos,
- varLen, priorVariables);
- }
- change = change
- + varLen - (endPos - startPos);
- pos += change;
- bufEnd += change;
- lengthChange += change;
- chars = buf.buffer; // in case buffer was
- // altered
+ // resolve the variable
+ String varValue = resolveVariable(varName, buf,
+ startPos, endPos);
+ if (varValue == null) {
+ varValue = varDefaultValue;
+ }
+ if (varValue != null) {
+ final int varLen = varValue.length();
+ buf.replace(startPos, endPos, varValue);
+ altered = true;
+ int change = 0;
+ if (!substitutionInValuesDisabled) { // recursive replace
+ change = substitute(buf, startPos,
+ varLen, priorVariables);
}
-
- // remove variable from the cyclic stack
- priorVariables
- .remove(priorVariables.size() - 1);
- break;
+ change = change
+ + varLen - (endPos - startPos);
+ pos += change;
+ bufEnd += change;
+ lengthChange += change;
+ chars = buf.buffer; // in case buffer was
+ // altered
}
- nestedVarCount--;
- pos += endMatchLen;
+
+ // remove variable from the cyclic stack
+ priorVariables
+ .remove(priorVariables.size() - 1);
+ break;
}
+ nestedVarCount--;
+ pos += endMatchLen;
}
}
}