@Test void shouldCorrectlyEscapeEmptyStrings() { for (String[] strings : new String[][] { { "", "" }, { " \t ", " \t " }, { "Nothing to escape", "Nothing to escape" }, { "' \" '", "\\' \\\" \\'" } }) { String string = strings[0]; String expectedEscapedString = strings[1]; assertThat(StringLiteral.escapeString(string)).hasValue(expectedEscapedString); } }
static Optional<String> escapeString(CharSequence unescapedString) { if (unescapedString == null) { return Optional.empty(); } final StringBuffer stringBuffer = new StringBuffer(); Matcher matcher = RESERVED_CHARS.matcher(unescapedString); while (matcher.find()) { matcher.appendReplacement(stringBuffer, "\\\\\\" + matcher.group(1)); } matcher.appendTail(stringBuffer); return Optional.of(stringBuffer.toString()); }
StringLiteral extends Literal<CharSequence> { static Optional<String> escapeString(CharSequence unescapedString) { if (unescapedString == null) { return Optional.empty(); } final StringBuffer stringBuffer = new StringBuffer(); Matcher matcher = RESERVED_CHARS.matcher(unescapedString); while (matcher.find()) { matcher.appendReplacement(stringBuffer, "\\\\\\" + matcher.group(1)); } matcher.appendTail(stringBuffer); return Optional.of(stringBuffer.toString()); } }
StringLiteral extends Literal<CharSequence> { static Optional<String> escapeString(CharSequence unescapedString) { if (unescapedString == null) { return Optional.empty(); } final StringBuffer stringBuffer = new StringBuffer(); Matcher matcher = RESERVED_CHARS.matcher(unescapedString); while (matcher.find()) { matcher.appendReplacement(stringBuffer, "\\\\\\" + matcher.group(1)); } matcher.appendTail(stringBuffer); return Optional.of(stringBuffer.toString()); } StringLiteral(CharSequence content); }
StringLiteral extends Literal<CharSequence> { static Optional<String> escapeString(CharSequence unescapedString) { if (unescapedString == null) { return Optional.empty(); } final StringBuffer stringBuffer = new StringBuffer(); Matcher matcher = RESERVED_CHARS.matcher(unescapedString); while (matcher.find()) { matcher.appendReplacement(stringBuffer, "\\\\\\" + matcher.group(1)); } matcher.appendTail(stringBuffer); return Optional.of(stringBuffer.toString()); } StringLiteral(CharSequence content); @Override String asString(); }
StringLiteral extends Literal<CharSequence> { static Optional<String> escapeString(CharSequence unescapedString) { if (unescapedString == null) { return Optional.empty(); } final StringBuffer stringBuffer = new StringBuffer(); Matcher matcher = RESERVED_CHARS.matcher(unescapedString); while (matcher.find()) { matcher.appendReplacement(stringBuffer, "\\\\\\" + matcher.group(1)); } matcher.appendTail(stringBuffer); return Optional.of(stringBuffer.toString()); } StringLiteral(CharSequence content); @Override String asString();  }