Advertisement
Guest User

Untitled

a guest
Aug 4th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.70 KB | None | 0 0
  1. module vibe.core.events.validator;
  2. package:
  3. import std.regex : ctRegex, matchAll;
  4. /*
  5.  * ctRegex can compile Regex expressions into an optimal deterministic state machine
  6.  * However, compilation can take very long (2 minutes +) which is why it is preferable to separate it into another library
  7.  *
  8. */
  9.  
  10. // This is not exposed because the ctRegex defined here are inappropriate for capturing elements
  11. // Capturing should be done with Pegged, but it is not in the scope of this library
  12. private {
  13.     // The Regex code was taken from http://stackoverflow.com/questions/9208814/validate-ipv4-ipv6-and-hostname
  14.     enum IPv4Regex = ctRegex!(`^\s*((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\s*$`, ``);
  15.     enum IPv6Regex = ctRegex!(`^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$`, ``);
  16.    
  17.     // Hostnames are defined by RFC 1123 http://tools.ietf.org/html/rfc1123
  18.     enum HostRegex = ctRegex!(`^\s*((?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?)*\.?)\s*$`, ``);
  19.    
  20.     auto captureIPv4(string str){
  21.         return matchAll(str, IPv4Regex);
  22.     }
  23.    
  24.     private auto captureIPv6(string str){
  25.         return matchAll(str, IPv6Regex);
  26.     }
  27.    
  28.     private auto captureHost(string str){
  29.         return matchAll(str, HostRegex);
  30.     }
  31. }
  32.  
  33. // These insanely fast and accurate validation functions are the only features of this library
  34. public {
  35.     bool validateIPv4(string str){
  36.         return !captureIPv4(str).empty;
  37.     }
  38.    
  39.     bool validateIPv6(string str){
  40.         return !captureIPv6(str).empty;
  41.     }
  42.    
  43.     bool validateHost(string str){
  44.         return !captureHost(str).empty;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement