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 2020/07/02 02:00:03 UTC
[commons-text] branch master updated (1b59b4b -> 3ac1bb5)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-text.git.
from 1b59b4b Add org.apache.commons.text.TextStringBuilder.wrap(char[], int).
new 248af06 Resue builder APIs for cleaner separation.
new e0e07f9 Remove some inline comments.
new 3ac1bb5 Add org.apache.commons.text.StringSubstitutor.StringSubstitutor(StringSubstitutor).
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 1 +
.../org/apache/commons/text/StringSubstitutor.java | 108 ++++++++++-----------
.../text/matcher/AbstractStringMatcher.java | 10 ++
.../apache/commons/text/StringSubstitutorTest.java | 26 +++++
4 files changed, 91 insertions(+), 54 deletions(-)
[commons-text] 03/03: Add
org.apache.commons.text.StringSubstitutor.StringSubstitutor(StringSubstitutor).
Posted by gg...@apache.org.
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 3ac1bb583a5cb171ade5d5fc467d1d12e95432ba
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jul 1 21:59:55 2020 -0400
Add org.apache.commons.text.StringSubstitutor.StringSubstitutor(StringSubstitutor).
---
src/changes/changes.xml | 1 +
.../org/apache/commons/text/StringSubstitutor.java | 55 +++++++++++++++-------
.../text/matcher/AbstractStringMatcher.java | 10 ++++
.../apache/commons/text/StringSubstitutorTest.java | 26 ++++++++++
4 files changed, 74 insertions(+), 18 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2ef515f..6a50798 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -64,6 +64,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.matcher.StringMatcherFactory.stringMatcher(char...).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.wrap(char[]).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.TextStringBuilder.wrap(char[], int).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add org.apache.commons.text.StringSubstitutor.StringSubstitutor(StringSubstitutor).</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] junit-jupiter 5.5.1 -> 5.5.2.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[test] org.assertj:assertj-core 3.13.2 -> 3.16.1.</action>
<action type="update" dev="ggregory" due-to="Gary Gregory">[build] com.puppycrawl.tools:checkstyle 8.23 -> 8.34.</action>
diff --git a/src/main/java/org/apache/commons/text/StringSubstitutor.java b/src/main/java/org/apache/commons/text/StringSubstitutor.java
index 4a3b316..8acf438 100644
--- a/src/main/java/org/apache/commons/text/StringSubstitutor.java
+++ b/src/main/java/org/apache/commons/text/StringSubstitutor.java
@@ -350,49 +350,49 @@ public class StringSubstitutor {
}
/**
- * Stores the escape character.
+ * The flag whether substitution in variable values is disabled.
*/
- private char escapeChar;
+ private boolean disableSubstitutionInValues;
/**
- * Stores the variable prefix.
+ * The flag whether substitution in variable names is enabled.
*/
- private StringMatcher prefixMatcher;
+ private boolean enableSubstitutionInVariables;
/**
- * Stores the variable suffix.
+ * The flag whether exception should be thrown on undefined variable.
*/
- private StringMatcher suffixMatcher;
+ private boolean enableUndefinedVariableException;
/**
- * Stores the default variable value delimiter.
+ * Stores the escape character.
*/
- private StringMatcher valueDelimiterMatcher;
+ private char escapeChar;
/**
- * Variable resolution is delegated to an implementor of {@link StringLookup}.
+ * Stores the variable prefix.
*/
- private StringLookup variableResolver;
+ private StringMatcher prefixMatcher;
/**
- * The flag whether substitution in variable names is enabled.
+ * Whether escapes should be preserved. Default is false;
*/
- private boolean enableSubstitutionInVariables;
+ private boolean preserveEscapes;
/**
- * Whether escapes should be preserved. Default is false;
+ * Stores the variable suffix.
*/
- private boolean preserveEscapes;
+ private StringMatcher suffixMatcher;
/**
- * The flag whether substitution in variable values is disabled.
+ * Stores the default variable value delimiter.
*/
- private boolean disableSubstitutionInValues;
+ private StringMatcher valueDelimiterMatcher;
/**
- * The flag whether exception should be thrown on undefined variable.
+ * Variable resolution is delegated to an implementor of {@link StringLookup}.
*/
- private boolean enableUndefinedVariableException;
+ private StringLookup variableResolver;
/**
* Creates a new instance with defaults for variable prefix and suffix and the escaping character.
@@ -536,6 +536,25 @@ public class StringSubstitutor {
}
/**
+ * Creates a new instance based on the given StringSubstitutor.
+ *
+ * @param other The StringSubstitutor is use as the source.
+ *
+ * @since 1.9
+ */
+ public StringSubstitutor(final StringSubstitutor other) {
+ disableSubstitutionInValues = other.isDisableSubstitutionInValues();
+ enableSubstitutionInVariables = other.isEnableSubstitutionInVariables();
+ enableUndefinedVariableException = other.isEnableUndefinedVariableException();
+ escapeChar = other.getEscapeChar();
+ prefixMatcher = other.getVariablePrefixMatcher();
+ preserveEscapes = other.isPreserveEscapes();
+ suffixMatcher = other.getVariableSuffixMatcher();
+ valueDelimiterMatcher = other.getValueDelimiterMatcher();
+ variableResolver = other.getStringLookup();
+ }
+
+ /**
* Checks if the specified variable is already in the stack (list) of variables.
*
* @param varName the variable name to check
diff --git a/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java b/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java
index 7e12665..cf1a460 100644
--- a/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java
+++ b/src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java
@@ -84,6 +84,11 @@ abstract class AbstractStringMatcher implements StringMatcher {
public int size() {
return 1;
}
+
+ @Override
+ public String toString() {
+ return super.toString() + "['" + ch + "']";
+ }
}
/**
@@ -143,6 +148,11 @@ abstract class AbstractStringMatcher implements StringMatcher {
return 1;
}
+ @Override
+ public String toString() {
+ return super.toString() + Arrays.toString(chars);
+ }
+
}
/**
diff --git a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java
index 6382845..327d466 100644
--- a/src/test/java/org/apache/commons/text/StringSubstitutorTest.java
+++ b/src/test/java/org/apache/commons/text/StringSubstitutorTest.java
@@ -186,6 +186,32 @@ public class StringSubstitutorTest {
values = null;
}
+ @Test
+ public void testConstructorStringSubstitutor() {
+ final StringSubstitutor source = new StringSubstitutor();
+ source.setDisableSubstitutionInValues(true);
+ source.setEnableSubstitutionInVariables(true);
+ source.setEnableUndefinedVariableException(true);
+ source.setEscapeChar('e');
+ source.setValueDelimiter('d');
+ source.setVariablePrefix('p');
+ source.setVariableResolver(StringLookupFactory.INSTANCE.nullStringLookup());
+ source.setVariableSuffix('s');
+ //
+ final StringSubstitutor target = new StringSubstitutor(source);
+ //
+ assertTrue(target.isDisableSubstitutionInValues());
+ assertTrue(target.isEnableSubstitutionInVariables());
+ assertTrue(target.isEnableUndefinedVariableException());
+ assertEquals('e', target.getEscapeChar());
+ assertTrue(target.getValueDelimiterMatcher().toString().endsWith("['d']"),
+ target.getValueDelimiterMatcher().toString());
+ assertTrue(target.getVariablePrefixMatcher().toString().endsWith("['p']"),
+ target.getValueDelimiterMatcher().toString());
+ assertTrue(target.getVariableSuffixMatcher().toString().endsWith("['s']"),
+ target.getValueDelimiterMatcher().toString());
+ }
+
/**
* Tests get set.
*/
[commons-text] 02/03: Remove some inline comments.
Posted by gg...@apache.org.
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 e0e07f98f3601c2fa044273e77df9ead8119977a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jul 1 21:37:35 2020 -0400
Remove some inline comments.
---
.../org/apache/commons/text/StringSubstitutor.java | 19 -------------------
1 file changed, 19 deletions(-)
diff --git a/src/main/java/org/apache/commons/text/StringSubstitutor.java b/src/main/java/org/apache/commons/text/StringSubstitutor.java
index 66d0616..4a3b316 100644
--- a/src/main/java/org/apache/commons/text/StringSubstitutor.java
+++ b/src/main/java/org/apache/commons/text/StringSubstitutor.java
@@ -394,7 +394,6 @@ public class StringSubstitutor {
*/
private boolean enableUndefinedVariableException;
- // -----------------------------------------------------------------------
/**
* Creates a new instance with defaults for variable prefix and suffix and the escaping character.
*/
@@ -555,7 +554,6 @@ public class StringSubstitutor {
}
// Escape
- // -----------------------------------------------------------------------
/**
* Returns the escape character.
*
@@ -566,7 +564,6 @@ public class StringSubstitutor {
}
// Resolver
- // -----------------------------------------------------------------------
/**
* Gets the StringLookup that is used to lookup variables.
*
@@ -577,7 +574,6 @@ public class StringSubstitutor {
}
// Variable Default Value Delimiter
- // -----------------------------------------------------------------------
/**
* Gets the variable default value delimiter matcher currently in use.
* <p>
@@ -594,8 +590,6 @@ public class StringSubstitutor {
return valueDelimiterMatcher;
}
- // Prefix
- // -----------------------------------------------------------------------
/**
* Gets the variable prefix matcher currently in use.
* <p>
@@ -609,8 +603,6 @@ public class StringSubstitutor {
return prefixMatcher;
}
- // Suffix
- // -----------------------------------------------------------------------
/**
* Gets the variable suffix matcher currently in use.
* <p>
@@ -649,8 +641,6 @@ public class StringSubstitutor {
return disableSubstitutionInValues;
}
- // Substitution support in variable names
- // -----------------------------------------------------------------------
/**
* Returns a flag whether substitution is done in variable names.
*
@@ -678,7 +668,6 @@ public class StringSubstitutor {
return preserveEscapes;
}
- // -----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables with their matching values from the resolver using the given source
* array as a template. The array is not altered by this method.
@@ -757,7 +746,6 @@ public class StringSubstitutor {
return buf.toString();
}
- // -----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables in the given source object with their matching values from the
* resolver. The input source object is converted to a string using {@code toString} and is not altered.
@@ -775,7 +763,6 @@ public class StringSubstitutor {
return buf.toString();
}
- // -----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables with their matching values from the resolver using the given source
* string as a template.
@@ -820,7 +807,6 @@ public class StringSubstitutor {
return buf.toString();
}
- // -----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables with their matching values from the resolver using the given source
* buffer as a template. The buffer is not altered by this method.
@@ -861,7 +847,6 @@ public class StringSubstitutor {
return buf.toString();
}
- // -----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables with their matching values from the resolver using the given source
* builder as a template. The builder is not altered by this method.
@@ -902,7 +887,6 @@ public class StringSubstitutor {
return buf.toString();
}
- // -----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables within the given source buffer with their matching values from the
* resolver. The buffer is updated with the result.
@@ -943,7 +927,6 @@ public class StringSubstitutor {
return true;
}
- // -----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables within the given source buffer with their matching values from the
* resolver. The buffer is updated with the result.
@@ -984,7 +967,6 @@ public class StringSubstitutor {
return true;
}
- // -----------------------------------------------------------------------
/**
* Replaces all the occurrences of variables within the given source builder with their matching values from the
* resolver.
@@ -1270,7 +1252,6 @@ public class StringSubstitutor {
return this;
}
- // -----------------------------------------------------------------------
/**
* Internal method that substitutes the variables.
* <p>
[commons-text] 01/03: Resue builder APIs for cleaner separation.
Posted by gg...@apache.org.
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 248af06171e14648e00ce0873c5f95e03041a6c7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Jul 1 21:34:41 2020 -0400
Resue builder APIs for cleaner separation.
---
.../org/apache/commons/text/StringSubstitutor.java | 34 +++++++++++-----------
1 file changed, 17 insertions(+), 17 deletions(-)
diff --git a/src/main/java/org/apache/commons/text/StringSubstitutor.java b/src/main/java/org/apache/commons/text/StringSubstitutor.java
index 47196e1..66d0616 100644
--- a/src/main/java/org/apache/commons/text/StringSubstitutor.java
+++ b/src/main/java/org/apache/commons/text/StringSubstitutor.java
@@ -1318,23 +1318,23 @@ public class StringSubstitutor {
final boolean top = priorVariables == null;
boolean altered = false;
int lengthChange = 0;
- char[] chars = builder.getBuffer();
int bufEnd = offset + length;
int pos = offset;
while (pos < bufEnd) {
- final int startMatchLen = prefixMatcher.isMatch(chars, pos, offset, bufEnd);
+ final int startMatchLen = prefixMatcher.isMatch(builder, pos, offset, bufEnd);
if (startMatchLen == 0) {
pos++;
} else {
// found variable start marker
- if (pos > offset && chars[pos - 1] == escapeCh) {
- // escaped
+ if (pos > offset && builder.charAt(pos - 1) == escapeCh) {
+ // escape detected
if (preserveEscapes) {
+ // keep escape
pos++;
continue;
}
+ // delete escape
builder.deleteCharAt(pos - 1);
- chars = builder.getBuffer(); // in case buffer was altered
lengthChange--;
altered = true;
bufEnd--;
@@ -1345,22 +1345,23 @@ public class StringSubstitutor {
int endMatchLen = 0;
int nestedVarCount = 0;
while (pos < bufEnd) {
- if (substitutionInVariablesEnabled && prefixMatcher.isMatch(chars, pos, offset, bufEnd) != 0) {
+ if (substitutionInVariablesEnabled
+ && prefixMatcher.isMatch(builder, pos, offset, bufEnd) != 0) {
// found a nested variable start
- endMatchLen = prefixMatcher.isMatch(chars, pos, offset, bufEnd);
+ endMatchLen = prefixMatcher.isMatch(builder, pos, offset, bufEnd);
nestedVarCount++;
pos += endMatchLen;
continue;
}
- endMatchLen = suffixMatcher.isMatch(chars, pos, offset, bufEnd);
+ endMatchLen = suffixMatcher.isMatch(builder, 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);
+ String varNameExpr = builder.toString(startPos + startMatchLen,
+ pos - startPos - startMatchLen);
if (substitutionInVariablesEnabled) {
final TextStringBuilder bufName = new TextStringBuilder(varNameExpr);
substitute(bufName, 0, bufName.length());
@@ -1379,13 +1380,13 @@ public class StringSubstitutor {
// if there's any nested variable when nested variable substitution disabled,
// then stop resolving name and default value.
if (!substitutionInVariablesEnabled && prefixMatcher.isMatch(varNameExprChars,
- i, i, varNameExprChars.length) != 0) {
+ i, i, varNameExprChars.length) != 0) {
break;
}
if (valueDelimMatcher.isMatch(varNameExprChars, i, 0,
- varNameExprChars.length) != 0) {
+ varNameExprChars.length) != 0) {
valueDelimiterMatchLen = valueDelimMatcher.isMatch(varNameExprChars, i, 0,
- varNameExprChars.length);
+ varNameExprChars.length);
varName = varNameExpr.substring(0, i);
varDefaultValue = varNameExpr.substring(i + valueDelimiterMatchLen);
break;
@@ -1396,7 +1397,7 @@ public class StringSubstitutor {
// on the first call initialize priorVariables
if (priorVariables == null) {
priorVariables = new ArrayList<>();
- priorVariables.add(new String(chars, offset, length));
+ priorVariables.add(builder.toString(offset, length));
}
// handle cyclic substitution
@@ -1420,11 +1421,10 @@ public class StringSubstitutor {
pos += change;
bufEnd += change;
lengthChange += change;
- chars = builder.getBuffer(); // in case buffer was altered
} else if (undefinedVariableException) {
throw new IllegalArgumentException(String.format(
- "Cannot resolve variable '%s' (enableSubstitutionInVariables=%s).", varName,
- substitutionInVariablesEnabled));
+ "Cannot resolve variable '%s' (enableSubstitutionInVariables=%s).", varName,
+ substitutionInVariablesEnabled));
}
// remove variable from the cyclic stack