Maksym Korshun created TEXT-242:
-----------------------------------
Summary: StringSubstitutorReader does not substitute variables
whose configured suffix is longer than two characters
Key: TEXT-242
URL: https://issues.apache.org/jira/browse/TEXT-242
Project: Commons Text
Issue Type: Bug
Reporter: Maksym Korshun
The bug appears when the suffix is longer than two characters.
{code:java}
Map<String, String> values = Map.of("name", "Maksym");
StringSubstitutor substitutor = new StringSubstitutor(values);
substitutor.setVariablePrefix("[[");
substitutor.setVariableSuffix("]]>");
StringReader stringReader = new StringReader("Hello [[name]]>!");
try (Reader reader = new StringSubstitutorReader(stringReader, substitutor);
StringWriter output = new StringWriter()) {
reader.transferTo(output);
System.out.println(output); // Hello [[name]]>!
} catch (IOException e) {
throw new RuntimeException(e);
}
{code}
in StringSubstitutorReader.java line 276 {{pos++}} advances only one character
and on line 295 {{endPos = pos + 1}}
Together, {{pos++}} and {{pos + 1}} make the replacement range include at most
two suffix characters.
Therefore:
- suffix length 1: works;
- suffix length 2: works accidentally;
- suffix length 3+: the suffix is cut off before substitution.
{{substitutor.setVariableSuffix("]]>");}} the suffix length is 3. The reader
recognizes {{]]>}} but then calculates a replacement range containing only
{{{}]]{}}}. The text passed to StringSubstitutor is effectively:
{{[[name]]}}
instead of:
{{[[name]]>}}
Since the configured suffix is {{]]>}}, {{[[name]]}} is not a complete
variable, so it remains unchanged.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)