[geary/mjog/dovecot-envelope-mailbox-quirk: 2/8] Geary.RFC822.MailboxAddress: Compile is-valid regex only once




commit 747df10b9dec2609ee8e11ba2a1e0525313825f2
Author: Michael Gratton <mike vee net>
Date:   Tue Aug 18 11:15:27 2020 +1000

    Geary.RFC822.MailboxAddress: Compile is-valid regex only once

 src/engine/rfc822/rfc822-mailbox-address.vala | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
---
diff --git a/src/engine/rfc822/rfc822-mailbox-address.vala b/src/engine/rfc822/rfc822-mailbox-address.vala
index 68663b4bd..80cb01122 100644
--- a/src/engine/rfc822/rfc822-mailbox-address.vala
+++ b/src/engine/rfc822/rfc822-mailbox-address.vala
@@ -22,6 +22,8 @@ public class Geary.RFC822.MailboxAddress :
     Gee.Hashable<MailboxAddress>,
     DecodedMessageData {
 
+    private static Regex? email_regex = null;
+
     private static unichar[] ATEXT = {
         '!', '#', '$', '%', '&', '\'', '*', '+', '-',
         '/', '=', '?', '^', '_', '`', '{', '|', '}', '~'
@@ -29,17 +31,20 @@ public class Geary.RFC822.MailboxAddress :
 
     /** Determines if a string contains a valid RFC822 mailbox address. */
     public static bool is_valid_address(string address) {
-        try {
-            // http://www.regular-expressions.info/email.html
-            // matches john dep aol.museum not john aol   com
-            Regex email_regex =
-                new Regex("[A-Z0-9._%+-]+@((?:[A-Z0-9-]+\\.)+[A-Z]{2}|localhost)",
-                    RegexCompileFlags.CASELESS);
-            return email_regex.match(address);
-        } catch (RegexError e) {
-            debug("Regex error validating email address: %s", e.message);
-            return false;
+        if (MailboxAddress.email_regex == null) {
+            try {
+                // http://www.regular-expressions.info/email.html
+                // matches john dep aol.museum not john aol   com
+                MailboxAddress.email_regex = new Regex(
+                    "[A-Z0-9._%+-]+@((?:[A-Z0-9-]+\\.)+[A-Z]{2}|localhost)",
+                    RegexCompileFlags.CASELESS
+                );
+            } catch (RegexError e) {
+                warning("Regex error validating email address: %s", e.message);
+                return false;
+            }
         }
+        return MailboxAddress.email_regex.match(address);
     }
 
     private static string decode_name(string name) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]