You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "ecki (via GitHub)" <gi...@apache.org> on 2026/01/01 17:28:16 UTC
Re: [PR] [TEXT-XXX] Support ECMAScript \x unescaping in StringEscapeUtils [commons-text]
ecki commented on code in PR #589:
URL: https://github.com/apache/commons-text/pull/589#discussion_r2656506919
##########
src/main/java/org/apache/commons/text/StringEscapeUtils.java:
##########
@@ -375,36 +376,62 @@ public int translate(final CharSequence input, final int index, final Writer wri
}
/**
- * Translator object for unescaping escaped Java.
- *
- * While {@link #unescapeJava(String)} is the expected method of use, this
- * object allows the Java unescaping functionality to be used
- * as the foundation for a custom translator.
+ * Translator for octal escapes.
+ */
+ private static final CharSequenceTranslator OCTAL_JAVA_TRANSLATOR = new OctalUnescaper();
+
+ /**
+ * Translator for Unicode escapes.
+ */
+ private static final CharSequenceTranslator UNICODE_JAVA_TRANSLATOR = new UnicodeUnescaper();
+
+ /**
+ * Translator for Java control characters.
*/
- public static final CharSequenceTranslator UNESCAPE_JAVA;
+ private static final CharSequenceTranslator CTRL_CHARS_JAVA_TRANSLATOR = new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_UNESCAPE);
+
+ /**
+ * Translator for Java escapes that aren't control characters.
+ */
+ private static final CharSequenceTranslator UNESCAPE_JAVA_TRANSLATOR;
static {
final Map<CharSequence, CharSequence> unescapeJavaMap = new HashMap<>();
unescapeJavaMap.put("\\\\", "\\");
unescapeJavaMap.put("\\\"", "\"");
unescapeJavaMap.put("\\'", "'");
unescapeJavaMap.put("\\", StringUtils.EMPTY);
- UNESCAPE_JAVA = new AggregateTranslator(
- new OctalUnescaper(), // .between('\1', '\377'),
- new UnicodeUnescaper(),
- new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_UNESCAPE),
- new LookupTranslator(Collections.unmodifiableMap(unescapeJavaMap))
- );
+ UNESCAPE_JAVA_TRANSLATOR = new LookupTranslator(Collections.unmodifiableMap(unescapeJavaMap));
}
+ /**
+ * Translator object for unescaping escaped Java.
+ *
+ * While {@link #unescapeJava(String)} is the expected method of use, this
+ * object allows the Java unescaping functionality to be used
+ * as the foundation for a custom translator.
+ */
+ public static final CharSequenceTranslator UNESCAPE_JAVA = new AggregateTranslator(
+ OCTAL_JAVA_TRANSLATOR,
+ UNICODE_JAVA_TRANSLATOR,
+ CTRL_CHARS_JAVA_TRANSLATOR,
+ UNESCAPE_JAVA_TRANSLATOR
+ );
+
/**
* Translator object for unescaping escaped EcmaScript.
*
* While {@link #unescapeEcmaScript(String)} is the expected method of use, this
* object allows the EcmaScript unescaping functionality to be used
* as the foundation for a custom translator.
*/
- public static final CharSequenceTranslator UNESCAPE_ECMASCRIPT = UNESCAPE_JAVA;
+ public static final CharSequenceTranslator UNESCAPE_ECMASCRIPT = new AggregateTranslator(
+ new HexUnescaper(),
Review Comment:
For consistency should be a static constant as well?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org