This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-mime4j.git

commit 31018e82be121ee801d51803c1c10b2297692c34
Author: Benoit Tellier <[email protected]>
AuthorDate: Mon May 24 18:47:53 2021 +0700

    [REFACTORING] Pre-compile EncoderUtil::quote REGEX
    
    Using String::replaceAll leads to recompile the regex on each calls.
---
 core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java 
b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
index e883646..3b3690c 100644
--- a/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
+++ b/core/src/main/java/org/apache/james/mime4j/codec/EncoderUtil.java
@@ -26,6 +26,7 @@ import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.util.BitSet;
 import java.util.Locale;
+import java.util.regex.Pattern;
 
 import org.apache.james.mime4j.Charsets;
 import org.apache.james.mime4j.util.CharsetUtil;
@@ -55,6 +56,8 @@ public class EncoderUtil {
 
     private static final BitSet ATEXT_CHARS = initChars("()<>@.,;:\\\"[]");
 
+    private static final Pattern QUOTE = Pattern.compile("[\\\\\"]");
+
     private static BitSet initChars(String specials) {
         BitSet bs = new BitSet(128);
         for (char ch = 33; ch < 127; ch++) {
@@ -526,8 +529,7 @@ public class EncoderUtil {
         // VCHAR = %x21-7E
         // DQUOTE = %x22
 
-        String escaped = str.replaceAll("[\\\\\"]", "\\\\$0");
-        return "\"" + escaped + "\"";
+        return '"' + QUOTE.matcher(str).replaceAll("\\\\$0") + '"';
     }
 
     private static String encodeB(String prefix, String text,

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to