SHARE
TWEET

Untitled

a guest Jul 18th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <regex>
  4. #include <chrono>
  5. #include <iostream>
  6.  
  7. char mail[] = "pippo.paperino234@some.contoso.com";
  8.  
  9. bool isValidEmail(char* mail) {
  10.     int len = strlen(mail);
  11.     char c;
  12.     int position = 0;
  13.     // check first character
  14.     c = mail[position++];
  15.     if(!((c >= 'A' && c <= 'z') || (c >= '0' && c <= '9'))) return false;
  16.  
  17.     // check first group before '@'
  18.     c = mail[position++];
  19.     do {
  20.         if(!((c >= 'A' && c <= 'z') || (c >= '0' && c <= '9') || c == '.'))  return false;
  21.         c = mail[position++];
  22.     } while (c != '@' && position < len);
  23.  
  24.     if(position >= len)  return false;
  25.  
  26.     // do the second group
  27.     c = mail[position++];
  28.     if(!((c >= 'A' && c <= 'z') || (c >= '0' && c <= '9'))) return false;
  29.     c = mail[position++];
  30.     do {
  31.         if(!((c >= 'A' && c <= 'z') || (c >= '0' && c <= '9'))) return false;
  32.         c = mail[position++];
  33.     } while (c != '.' && position < len);
  34.     if(position >= len)  return false;
  35.  
  36.  
  37.     // now we could have something that ends in only letters
  38.     do {
  39.     c = mail[position++];
  40.     if(!((c >= 'A' && c <= 'z') || (c >= '0' && c <= '9')))  return false;
  41.     do {
  42.         if(!((c >= 'A' && c <= 'z') || (c >= '0' && c <= '9')))  return false;
  43.         c = mail[position++];
  44.     } while (c != '.' && position < len);
  45.     if(position >= len && c == '.')  return false;
  46.     } while(position < len);
  47.  
  48.     return true;
  49.    
  50. }
  51. int main(int argc, char** argv) {  
  52.     auto start = std::chrono::system_clock::now();
  53.  
  54. {
  55.     bool valid;
  56.     for(int i = 0; i < 1000000; i++) valid = isValidEmail(mail);
  57.  
  58.     auto end = std::chrono::system_clock::now();
  59.     auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  60.     std::cout << "Time for sequential version: " << elapsed.count() << " ms, value: " << valid << std::endl;   
  61. }
  62.  
  63.  
  64.     const std::regex mail_regex("^[A-z0-9][A-z0-9\\.]+@([A-z0-9]+\\.)+[A-z]+$");
  65.    
  66. {
  67.     bool valid;
  68.     for(int i = 0; i < 1000000; i++) valid = std::regex_match(mail, mail_regex);
  69.  
  70.     auto end = std::chrono::system_clock::now();
  71.     auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  72.     std::cout << "Time for regex version: " << elapsed.count() << " ms, value: " << valid << std::endl;
  73. }
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top