Bye bye, fast compilation times

Walter Bright newshound2 at digitalmars.com
Tue Feb 6 20:11:56 UTC 2018


On 2/5/2018 9:35 PM, Dmitry Olshansky wrote:
> That’s really bad idea - isEmail is template so the burden of freaking slow ctRegex
> is paid on per instantiation basis. Could be horrible with separate compilation.

std.string.isEmail() in D1 was a simple function. Maybe regex is just the wrong 
solution for this problem.

---------------------- std.string.isEmail --------------------

/***************************
  * Does string s[] start with an email address?
  * Returns:
  *      null    it does not
  *      char[]  it does, and this is the slice of s[] that is that email address
  * References:
  *      RFC2822
  */
char[] isEmail(char[] s)
{   size_t i;

     if (!isalpha(s[0]))
         goto Lno;

     for (i = 1; 1; i++)
     {
         if (i == s.length)
             goto Lno;
         auto c = s[i];
         if (isalnum(c))
             continue;
         if (c == '-' || c == '_' || c == '.')
             continue;
         if (c != '@')
             goto Lno;
         i++;
         break;
     }
     //writefln("test1 '%s'", s[0 .. i]);

     /* Now do the part past the '@'
      */
     size_t lastdot;
     for (; i < s.length; i++)
     {
         auto c = s[i];
         if (isalnum(c))
             continue;
         if (c == '-' || c == '_')
             continue;
         if (c == '.')
         {
             lastdot = i;
             continue;
         }
         break;
     }
     if (!lastdot || (i - lastdot != 3 && i - lastdot != 4))
         goto Lno;

     return s[0 .. i];

Lno:
     return null;
}


More information about the Digitalmars-d mailing list