Skip to content

Commit

Permalink
further speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrodriges committed Dec 8, 2016
1 parent 664be17 commit 86be5ef
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 31 deletions.
90 changes: 83 additions & 7 deletions is.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,52 @@ func Hexadecimal(str string) bool {

// Hexcolor check if the string is a hexadecimal color.
func Hexcolor(str string) bool {
return rxHexcolor.MatchString(str)
if str == "" {
return false
}

if str[0] == '#' {
str = str[1:]
}

if len(str) != 3 && len(str) != 6 {
return false
}

for _, c := range str {
if ('F' < c || c < 'A') && ('f' < c || c < 'a') && ('9' < c || c < '0') {
return false
}
}

return true
}

// RGBcolor check if the string is a valid RGB color in form rgb(RRR, GGG, BBB).
func RGBcolor(str string) bool {
return rxRGBcolor.MatchString(str)
if str == "" || len(str) < 10 {
return false
}

if str[0:4] != "rgb(" || str[len(str)-1] != ')' {
return false
}

str = str[4 : len(str)-1]
str = strings.TrimSpace(str)

for _, p := range strings.Split(str, ",") {
if len(p) > 1 && p[0] == '0' {
return false
}

p = strings.TrimSpace(p)
if i, e := strconv.Atoi(p); (255 < i || i < 0) || e != nil {
return false
}
}

return true
}

// LowerCase check if the string is lowercase. Empty string is valid.
Expand Down Expand Up @@ -406,8 +446,8 @@ func Base64(s string) bool {
// FilePath check is a string is Win or Unix file path and returns it's type.
func FilePath(str string) (bool, int) {
if rxWinPath.MatchString(str) {
//check windows path limit see:
// http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#maxpath
// check windows path limit see:
// http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#maxpath
if len(str[3:]) > 32767 {
return false, Win
}
Expand Down Expand Up @@ -506,17 +546,53 @@ func MAC(str string) bool {

// MongoID check if the string is a valid hex-encoded representation of a MongoDB ObjectId.
func MongoID(str string) bool {
return rxMongoID.MatchString(str)
if str == "" || len(str) != 24 {
return false
}

for _, c := range str {
if ('F' < c || c < 'A') && ('f' < c || c < 'a') && ('9' < c || c < '0') {
return false
}
}

return true
}

// Latitude check if a string is valid latitude.
func Latitude(str string) bool {
return rxLatitude.MatchString(str)
if str == "" {
return false
}

f, err := strconv.ParseFloat(str, 64)
if err != nil {
return false
}

if 90.0 < f || f < -90.0 {
return false
}

return true
}

// Longitude check if a string is valid longitude.
func Longitude(str string) bool {
return rxLongitude.MatchString(str)
if str == "" {
return false
}

f, err := strconv.ParseFloat(str, 64)
if err != nil {
return false
}

if 180.0 < f || f < -180.0 {
return false
}

return true
}

// SSN will validate the given string as a U.S. Social Security Number
Expand Down
38 changes: 14 additions & 24 deletions patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,24 +275,19 @@ const (
// pInt string = "^(?:[-+]?(?:0|[1-9][0-9]*))$"
// pFloat string = "^(?:[-+]?(?:[0-9]+))?(?:\\.[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$"
// pHexadecimal string = "^[0-9a-fA-F]+$"
pMongoID string = "^[0-9a-fA-F]{24}$"
pHexcolor string = "^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$"
pRGBcolor string = "^rgb\\(\\s*(0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*\\)$"
// pASCII string = "^[\x00-\x7F]+$"
// pMultibyte string = "[^\x00-\x7F]"
pFullWidth string = "[^\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]"
pHalfWidth string = "[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]"
// pBase64 string = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
// pPrintableASCII string = "^[\x20-\x7E]+$"
pDataURI string = "^data:.+\\/(.+);base64$"
pLatitude string = "^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$"
pLongitude string = "^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$"
pDNSName string = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{1,62}){1}(.[a-zA-Z0-9]{1}[a-zA-Z0-9_-]{1,62})*$`
pURL string = `^((ftp|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`
pSSN string = `^\d{3}[- ]?\d{2}[- ]?\d{4}$`
pWinPath string = `^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$`
pUnixPath string = `^((?:\/[a-zA-Z0-9\.\:]+(?:_[a-zA-Z0-9\:\.]+)*(?:\-[\:a-zA-Z0-9\.]+)*)+\/?)$`
pSemver string = "^v?(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)(-(0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(\\.(0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\\+[0-9a-zA-Z-]+(\\.[0-9a-zA-Z-]+)*)?$"
pDataURI string = "^data:.+\\/(.+);base64$"
pDNSName string = `^([a-zA-Z0-9]{1}[a-zA-Z0-9_-]{1,62}){1}(.[a-zA-Z0-9]{1}[a-zA-Z0-9_-]{1,62})*$`
pURL string = `^((ftp|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`
pSSN string = `^\d{3}[- ]?\d{2}[- ]?\d{4}$`
pWinPath string = `^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$`
pUnixPath string = `^((?:\/[a-zA-Z0-9\.\:]+(?:_[a-zA-Z0-9\:\.]+)*(?:\-[\:a-zA-Z0-9\.]+)*)+\/?)$`
pSemver string = "^v?(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)(-(0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(\\.(0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\\+[0-9a-zA-Z-]+(\\.[0-9a-zA-Z-]+)*)?$"
)

// Used by IsFilePath func
Expand All @@ -317,22 +312,17 @@ var (
// rxInt = regexp.MustCompile(pInt)
// rxFloat = regexp.MustCompile(pFloat)
// rxHexadecimal = regexp.MustCompile(pHexadecimal)
rxMongoID = regexp.MustCompile(pMongoID)
rxHexcolor = regexp.MustCompile(pHexcolor)
rxRGBcolor = regexp.MustCompile(pRGBcolor)
// rxASCII = regexp.MustCompile(ASCII)
// rxPrintableASCII = regexp.MustCompile(PrintableASCII)
// rxMultibyte = regexp.MustCompile(pMultibyte)
rxFullWidth = regexp.MustCompile(pFullWidth)
rxHalfWidth = regexp.MustCompile(pHalfWidth)
// rxBase64 = regexp.MustCompile(Base64)
rxDataURI = regexp.MustCompile(pDataURI)
rxLatitude = regexp.MustCompile(pLatitude)
rxLongitude = regexp.MustCompile(pLongitude)
rxDNSName = regexp.MustCompile(pDNSName)
rxURL = regexp.MustCompile(pURL)
rxSSN = regexp.MustCompile(pSSN)
rxWinPath = regexp.MustCompile(pWinPath)
rxUnixPath = regexp.MustCompile(pUnixPath)
rxSemver = regexp.MustCompile(pSemver)
rxDataURI = regexp.MustCompile(pDataURI)
rxDNSName = regexp.MustCompile(pDNSName)
rxURL = regexp.MustCompile(pURL)
rxSSN = regexp.MustCompile(pSSN)
rxWinPath = regexp.MustCompile(pWinPath)
rxUnixPath = regexp.MustCompile(pUnixPath)
rxSemver = regexp.MustCompile(pSemver)
)

0 comments on commit 86be5ef

Please sign in to comment.