Skip to content

Commit

Permalink
Eliminate testing for whether to escape and just escape.
Browse files Browse the repository at this point in the history
  • Loading branch information
baron1405 committed Apr 4, 2024
1 parent c9f1b5a commit 6c561b5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 52 deletions.
33 changes: 4 additions & 29 deletions src/main/java/org/cthing/xmlwriter/XmlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2604,31 +2604,6 @@ void writeQuoted(final char[] carr, final int start, final int length) throws SA
writeRaw('"');
}

/**
* Determines whether the specified portion of the character array requires escaping.
*
* @param carr Character array to test
* @param start Starting index in the array
* @param length Number of characters to test
* @return {@code true} if the specified character array requires escaping.
*/
@AccessForTesting
static boolean needsEscaping(final char[] carr, final int start, final int length) {
int end = start + length;
while (--end >= start) {
final char c = carr[end];

if (c == '<' || c == '>' || c == '&' || c == '\n') {
return true;
}
if ((c <= '\u001F' || c >= '\u007F') && c != '\t' && c != '\r') {
return true;
}
}

return false;
}

/**
* Writes the specified character array to the output escaping the '&amp;', '&lt;', and '&gt;' characters using
* the standard XML escape sequences and escaping any character above the ASCII range using a numeric character
Expand All @@ -2641,7 +2616,7 @@ static boolean needsEscaping(final char[] carr, final int start, final int lengt
*/
@AccessForTesting
void writeEscaped(final char[] carr, final int start, final int length) throws SAXException {
if (this.escaping && needsEscaping(carr, start, length)) {
if (this.escaping) {
final int end = start + length;
int i = start;
while (i < end) {
Expand Down Expand Up @@ -2671,10 +2646,10 @@ void writeEscaped(final int c) throws SAXException {
case '\n' -> writeNewline();
case '\t', '\r' -> writeRaw((char)c);
default -> {
if (c > '\u001F' && c < '\u007F') {
if (c > 0x001F && c < 0x007F) {
writeRaw((char)c);
} else if ((c >= '\u007F' && c <= '\uD7FF')
|| (c >= '\uE000' && c <= '\uFFFD')
} else if ((c >= 0x007F && c <= 0xD7FF)
|| (c >= 0xE000 && c <= 0xFFFD)
|| (c >= 0x10000 && c <= 0x10FFFF)) {
writeRaw("&#x");
writeRaw(Integer.toHexString(c).toUpperCase());
Expand Down
23 changes: 0 additions & 23 deletions src/test/java/org/cthing/xmlwriter/XmlWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,6 @@ void testWriteRawChar() throws Exception {
assertThat(this.stringWriter).hasToString(testString);
}

public static Stream<Arguments> needsEscapingProvider() {
return Stream.of(
arguments("", false),
arguments("abc", false),
arguments("<abc", true),
arguments("abc>", true),
arguments("a&bc", true),
arguments("a\nbc", true),
arguments("a\tbc", false),
arguments("a\rbc", false),
arguments("a\u008Abc", true),
arguments("a\uE08Abc", true),
arguments("a\uD83D\uDE03bc", true)
);
}

@ParameterizedTest
@MethodSource("needsEscapingProvider")
@DisplayName("Determine whether an array requires escaping")
void testNeedsEscaping(final String str, final boolean needsEscaping) {
assertThat(XmlWriter.needsEscaping(str.toCharArray(), 0, str.length())).isEqualTo(needsEscaping);
}

@Test
@DisplayName("Write an array with escaping")
void testWriteEscapedArray() throws Exception {
Expand Down

0 comments on commit 6c561b5

Please sign in to comment.