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