rollback escaping support in template, as it is global

This commit is contained in:
Jonathan Shook 2020-04-13 18:23:51 -05:00
parent 6e027ed3b7
commit 0405eb74fb
2 changed files with 44 additions and 44 deletions

View File

@ -94,7 +94,7 @@ public class Template implements LongFunction<String> {
pos = m.end(); pos = m.end();
} }
String partial = template.substring(pos); String partial = template.substring(pos);
partial = unescape(partial); // partial = unescape(partial);
literals.add(partial); literals.add(partial);
if (literals.size() != funcCount + 1) { if (literals.size() != funcCount + 1) {
throw new RuntimeException("The number of {} place holders in '" + template + "' should equal the number of functions."); throw new RuntimeException("The number of {} place holders in '" + template + "' should equal the number of functions.");
@ -105,28 +105,28 @@ public class Template implements LongFunction<String> {
} }
} }
// for testing // // for testing
public String unescape(String partial) { // public String unescape(String partial) {
StringBuilder unescaped = new StringBuilder(); // StringBuilder unescaped = new StringBuilder();
try { // try {
Pattern escapes = Pattern.compile("\\\\."); // Pattern escapes = Pattern.compile("\\\\.");
Matcher m = escapes.matcher(partial); // Matcher m = escapes.matcher(partial);
int pos = 0; // int pos = 0;
//
while (m.find()) { // while (m.find()) {
String prefix = partial.substring(pos,m.start()); // String prefix = partial.substring(pos,m.start());
unescaped.append(prefix); // unescaped.append(prefix);
String segment = partial.substring(m.start(),m.end()); // String segment = partial.substring(m.start(),m.end());
unescaped.append(segment.substring(1)); // unescaped.append(segment.substring(1));
pos = m.end(); // pos = m.end();
} // }
unescaped.append(partial.substring(pos)); // unescaped.append(partial.substring(pos));
//
} catch (Exception e) { // } catch (Exception e) {
throw new RuntimeException(e); // throw new RuntimeException(e);
} // }
return unescaped.toString(); // return unescaped.toString();
} // }
@Override @Override
public String apply(long value) { public String apply(long value) {

View File

@ -52,25 +52,25 @@ public class TemplateTest {
} }
} }
@Test // @Test
public void testUnescaping() { // public void testUnescaping() {
String escaped="front \\' back"; // String escaped="front \\' back";
LongFunction<String> func = String::valueOf; // LongFunction<String> func = String::valueOf;
Template template = new Template("{} extra", func); // Template template = new Template("{} extra", func);
String unescaped = template.unescape(escaped); // String unescaped = template.unescape(escaped);
assertThat(unescaped).isEqualTo("front ' back"); // assertThat(unescaped).isEqualTo("front ' back");
//
String unescaped2= template.unescape("\\' one \\' two \\'"); // String unescaped2= template.unescape("\\' one \\' two \\'");
assertThat(unescaped2).isEqualTo("' one ' two '"); // assertThat(unescaped2).isEqualTo("' one ' two '");
} // }
//
@Test // @Test
public void testBackslashUnescaping() { // public void testBackslashUnescaping() {
String escaped="front \\\\\" back"; // String escaped="front \\\\\" back";
LongFunction<String> func = String::valueOf; // LongFunction<String> func = String::valueOf;
Template template = new Template("{} extra", func); // Template template = new Template("{} extra", func);
String unescaped = template.unescape(escaped); // String unescaped = template.unescape(escaped);
assertThat(unescaped).isEqualTo("front \\\" back"); // assertThat(unescaped).isEqualTo("front \\\" back");
//
} // }
} }