]> Sergey Matveev's repositories - path-extractor.git/blob - validators.go
Shouldn't match //
[path-extractor.git] / validators.go
1 package pathextractor
2
3 import (
4         "regexp"
5         "strings"
6 )
7
8 func stringInSlice(str string, list []string) bool {
9         for _, v := range list {
10                 if v == str {
11                         return true
12                 }
13         }
14         return false
15 }
16
17 func isGitRange(input string) bool {
18         r := regexp.MustCompile("[0-9a-f]{3,}\\.\\.[0-9a-f]{3,}")
19         return r.Match([]byte(input))
20 }
21
22 func isGitPath(input string) bool {
23         r := regexp.MustCompile("^[ab]/")
24         return r.Match([]byte(input))
25 }
26
27 func isEmail(input string) bool {
28         r := regexp.MustCompile("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+([a-zA-Z]{2,4})")
29         result := r.FindSubmatch([]byte(input))
30         if result == nil {
31                 return false
32         }
33         fileExtensions := []string{"png", "bmp", "jpeg"}
34         return !stringInSlice(string(result[1]), fileExtensions)
35 }
36
37 func isDate(input string) bool {
38         r := regexp.MustCompile("^[0-9]+/[0-9]+/[0-9]+")
39         return r.Match([]byte(input))
40 }
41
42 func isGitInstruction(input string) bool {
43         r := regexp.MustCompile("\\.{3,}")
44         return r.Match([]byte(input))
45 }
46
47 func replaceGitPath(input string) string {
48         r := regexp.MustCompile("^[ab]/(.*)")
49         temp := [][]byte{}
50         temp = r.FindSubmatch([]byte(input))
51         return string(temp[1])
52 }
53
54 func isVersion(input string) bool {
55         r := regexp.MustCompile("[0-9x]\\.[0-9x]{1,2}(\\.[0-9x]{1,3})?")
56         return r.Match([]byte(input))
57 }
58
59 func endsWithInvalidString(input string) bool {
60         invalidEndings := []string{"."}
61         for _, s := range invalidEndings {
62                 if strings.LastIndex(input, s) == len(input)-len(s) {
63                         return true
64                 }
65         }
66         return false
67 }
68
69 func containsInvalidString(input string) bool {
70         invalidStrings := []string{"//", "()", "and/or", "origin/", "{", "}", "<", ">", "$", "*", "this."}
71         for _, s := range invalidStrings {
72                 if strings.Contains(input, s) {
73                         return true
74                 }
75         }
76         return false
77 }