Merge branch '124-branch'
Conflicts: src/changes/changes.xml LANG-1208: StrSubstitutor can preserve escapes This closes #124 from github. Thanks to Samuel Karp.
This commit is contained in:
commit
59c28bb25f
|
@ -22,6 +22,7 @@
|
|||
<body>
|
||||
|
||||
<release version="3.5" date="tba" description="tba">
|
||||
<action issue="LANG-1208" type="update" dev="bayard" due-to="Samuel Karp">StrSubstitutor can preserve escapes</action>
|
||||
<action issue="LANG-1175" type="fix" dev="wikier" due-to="Benedikt Ritter">Remove Ant-based build</action>
|
||||
<action issue="LANG-1192" type="add" dev="chas" due-to="Dominik Stadler">FastDateFormat support of the week-year component (uppercase 'Y')</action>
|
||||
<action issue="LANG-1194" type="fix" dev="chas">Limit max heap memory for consistent Travis CI build</action>
|
||||
|
|
|
@ -165,6 +165,10 @@ public class StrSubstitutor {
|
|||
* The flag whether substitution in variable names is enabled.
|
||||
*/
|
||||
private boolean enableSubstitutionInVariables;
|
||||
/**
|
||||
* Whether escapes should be preserved. Default is false;
|
||||
*/
|
||||
private boolean preserveEscapes = false;
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
|
@ -768,6 +772,10 @@ public class StrSubstitutor {
|
|||
// 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--;
|
||||
|
@ -1192,4 +1200,29 @@ public class StrSubstitutor {
|
|||
final boolean enableSubstitutionInVariables) {
|
||||
this.enableSubstitutionInVariables = enableSubstitutionInVariables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the flag controlling whether escapes are preserved during
|
||||
* substitution.
|
||||
*
|
||||
* @return the preserve escape flag
|
||||
*/
|
||||
public boolean isPreserveEscapes() {
|
||||
return preserveEscapes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a flag controlling whether escapes are preserved during
|
||||
* substitution. If set to <b>true</b>, the escape character is retained
|
||||
* during substitution (e.g. <code>$${this-is-escaped}</code> remains
|
||||
* <code>$${this-is-escaped}</code>). If set to <b>false</b>, the escape
|
||||
* character is removed during substitution (e.g.
|
||||
* <code>$${this-is-escaped}</code> becomes
|
||||
* <code>${this-is-escaped}</code>). The default value is <b>false</b>
|
||||
*
|
||||
* @param preserveEscapes
|
||||
*/
|
||||
public void setPreserveEscapes(final boolean preserveEscapes) {
|
||||
this.preserveEscapes = preserveEscapes;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -623,6 +623,21 @@ public class StrSubstitutorTest {
|
|||
assertEquals("Hello there commons!", StrSubstitutor.replace("@greeting@ there @name@!", map, "@", "@"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubstitutePreserveEscape() {
|
||||
final String org = "${not-escaped} $${escaped}";
|
||||
final Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("not-escaped", "value");
|
||||
|
||||
StrSubstitutor sub = new StrSubstitutor(map, "${", "}", '$');
|
||||
assertFalse(sub.isPreserveEscapes());
|
||||
assertEquals("value ${escaped}", sub.replace(org));
|
||||
|
||||
sub.setPreserveEscapes(true);
|
||||
assertTrue(sub.isPreserveEscapes());
|
||||
assertEquals("value $${escaped}", sub.replace(org));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
private void doTestReplace(final String expectedResult, final String replaceTemplate, final boolean substring) {
|
||||
final String expectedShortResult = expectedResult.substring(1, expectedResult.length() - 1);
|
||||
|
|
Loading…
Reference in New Issue