Skip to content

Commit

Permalink
fix(api): regex characters in StringTemplate variables should be es…
Browse files Browse the repository at this point in the history
…caped (#65)
  • Loading branch information
Kit-p authored Oct 1, 2022
1 parent 927539b commit 4553977
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public String format(final @Nullable String template)
if (format != null && !format.isBlank()) {
// String
if (value instanceof String) {
matcher.appendReplacement(builder, String.format(format, value));
matcher.appendReplacement(builder, Matcher.quoteReplacement(String.format(format, value)));
// Number
} else if (value instanceof Number) {
matcher.appendReplacement(builder, new DecimalFormat(format).format(value));
Expand Down Expand Up @@ -277,7 +277,7 @@ public String format(final @Nullable String template)
);
// Other
} else {
matcher.appendReplacement(builder, String.valueOf(value));
matcher.appendReplacement(builder, Matcher.quoteReplacement(String.valueOf(value)));
}
}
} catch (IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public void variables()
);
}

@Test
@DisplayName("Replace variables with special regex characters")
public void variablesWithSpecialCharacters()
{
st.add("special", "\\$\\\\$$");
assertEquals(
"_\\$\\\\$$",
st.format("_${special}")
);
}

@Test
@DisplayName("Resolve lazy variables")
public void lazyVariables()
Expand Down

0 comments on commit 4553977

Please sign in to comment.