Skip to content

Commit

Permalink
[substitution] better escaping support - to enable mustache syntax in…
Browse files Browse the repository at this point in the history
… configuration
  • Loading branch information
dependabot-ci committed Jul 4, 2023
1 parent db1a085 commit a4c3982
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,45 @@ public String replace(final String source) {
final var previous = current;
current = substitute(current, 0);
if (previous.equals(current)) {
return previous;
return unescape(previous);
}
} while (true);
}

protected String unescape(final String previous) {
final var copy = new StringBuilder(previous.length());
boolean forceAppend = false;
for (int i = 0; i < previous.length(); i++) {
final var c = previous.charAt(i);
if (!forceAppend && c == ESCAPE) {
forceAppend = true;
continue;
} else if (forceAppend) {
forceAppend = false;
}
copy.append(c);
}
return copy.toString();
}

private String substitute(final String input, int iteration) {
if (iteration > maxIterations) {
return input;
}

int from = 0;
int start = -1;
while (start < 0 && from < input.length()) {
while (from < input.length()) {
start = input.indexOf(PREFIX, from);
if (start < 0) {
return input;
}
if (start != 0 && input.charAt(start - 1) != ESCAPE) {
if (start == 0 || input.charAt(start - 1) != ESCAPE) {
break;
}
from = start + 1;
}

final var keyStart = start + PREFIX.length();
final var end = input.indexOf(SUFFIX, keyStart);
if (end < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ void fallback() {
assertEquals("foo or dummy", new Substitutor(k -> null).replace("foo {{key:-or}} dummy"));
}

@Test
void escaped() {
assertEquals("foo {{key:-or}} dummy", new Substitutor(k -> null).replace("foo \\{{key:-or}} dummy"));
assertEquals("foo before {{key:-or}} / {{key:-or2}} after dummy", new Substitutor(k -> {
switch (k) {
case "suffix":
return "after";
case "prefix":
return "before";
default:
return null;
}
}).replace("foo {{prefix}} \\{{key:-or}\\} / {{test:-\\{\\{key:-or2\\}\\}}} {{suffix}} dummy"));
}

@Test
void nested() {
assertEquals("foo replaced dummy", new Substitutor(k -> "key".equals(k) ? "replaced" : null).replace("foo {{k{{missing:-e}}y}} dummy"));
Expand Down

0 comments on commit a4c3982

Please sign in to comment.