]> Sergey Matveev's repositories - path-extractor.git/blob - extractor.go
2a3b2cd5723d89593b5782e6782fa768b495c596
[path-extractor.git] / extractor.go
1 package pathextractor
2
3 import "regexp"
4 import "fmt"
5
6 type MatchOptions struct {
7         format string
8 }
9
10 func pathExtractor(input string) [][][]byte {
11         surroundRegex := "[^][ \\t:'\"]*"
12         r := regexp.MustCompile("(" + surroundRegex + "[\\./]" + surroundRegex + ")")
13         temp := [][][]byte{}
14         temp = r.FindAllSubmatch([]byte(input), -1)
15         return temp
16 }
17
18 func stripParens(input string) string {
19         r := regexp.MustCompile("^\\((.*)\\)$")
20         temp := [][]byte{}
21         temp = r.FindSubmatch([]byte(input))
22         if len(temp) <= 1 {
23                 return input
24         }
25         return string(temp[1])
26 }
27
28 func postProcess(input string) string {
29         input = stripParens(input)
30         return input
31 }
32
33 func GetAllMatches(input string, options MatchOptions) []string {
34         matches := [][][]byte{}
35         result := []string{}
36         s := string("")
37         // print(input)
38         matches = pathExtractor(input)
39         for _, match := range matches {
40                 s = string(match[1])
41                 if isEmail(s) || isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || endsWithInvalidString(s) || containsInvalidString(s) || len(s) <= 2 {
42                         continue
43                 }
44                 if isGitPath(s) {
45                         s = replaceGitPath(s)
46                 }
47                 s = postProcess(s)
48                 if options.format == "ackmate" {
49                         s = fmt.Sprint(s, ":45")
50                 }
51                 result = append(result, s)
52         }
53         return result
54 }