From 6c561b518b28920ad1433c12c16d0c991a228c91 Mon Sep 17 00:00:00 2001 From: Baron Roberts Date: Wed, 3 Apr 2024 22:14:19 -0700 Subject: [PATCH] Eliminate testing for whether to escape and just escape. --- .../java/org/cthing/xmlwriter/XmlWriter.java | 33 +++---------------- .../org/cthing/xmlwriter/XmlWriterTest.java | 23 ------------- 2 files changed, 4 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/cthing/xmlwriter/XmlWriter.java b/src/main/java/org/cthing/xmlwriter/XmlWriter.java index a09a40d..37c5971 100755 --- a/src/main/java/org/cthing/xmlwriter/XmlWriter.java +++ b/src/main/java/org/cthing/xmlwriter/XmlWriter.java @@ -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 '&', '<', and '>' characters using * the standard XML escape sequences and escaping any character above the ASCII range using a numeric character @@ -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) { @@ -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()); diff --git a/src/test/java/org/cthing/xmlwriter/XmlWriterTest.java b/src/test/java/org/cthing/xmlwriter/XmlWriterTest.java index b958c8b..6e6c384 100755 --- a/src/test/java/org/cthing/xmlwriter/XmlWriterTest.java +++ b/src/test/java/org/cthing/xmlwriter/XmlWriterTest.java @@ -87,29 +87,6 @@ void testWriteRawChar() throws Exception { assertThat(this.stringWriter).hasToString(testString); } - public static Stream needsEscapingProvider() { - return Stream.of( - arguments("", false), - arguments("abc", false), - arguments("", 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 {