]> Sergey Matveev's repositories - path-extractor.git/commitdiff
Small refactor
authorEdgar HIPP <hipp.edg@gmail.com>
Wed, 11 Nov 2015 10:49:21 +0000 (11:49 +0100)
committerEdgar HIPP <hipp.edg@gmail.com>
Wed, 11 Nov 2015 10:49:21 +0000 (11:49 +0100)
extractor.go
pe_test.go
validators.go

index 7cf69e4b8dcbe76cd58105a0fe49ef573d13f917..326855cf59e52e409b2d65661aba738550730982 100644 (file)
@@ -8,12 +8,10 @@ type MatchOptions struct {
        format string
 }
 
-func pathExtractor(input string) [][][]byte {
+func pathExtractor(input string) [][]int {
        surroundRegex := "[@~\\-_a-zA-Z/.0-9]*"
        r := regexp.MustCompile("(" + surroundRegex + "[\\./]" + surroundRegex + ")")
-       temp := [][][]byte{}
-       temp = r.FindAllSubmatch([]byte(input), -1)
-       return temp
+       return r.FindAllSubmatchIndex([]byte(input), -1)
 }
 
 func stripParens(input string) string {
@@ -32,28 +30,28 @@ func postProcess(input string) string {
 }
 
 func GetAllMatches(input string, options MatchOptions) []string {
-       matches := [][][]byte{}
        result := []string{}
-       s := string("")
-       // print(input)
-       matches = pathExtractor(input)
-       for _, match := range matches {
-               s = string(match[1])
-               if len(input) >= len(s+"(") && strings.Index(input, s+"(") != -1 {
+       candidatePath := string("")
+       indexes := pathExtractor(input)
+       for _, index := range indexes {
+               candidatePath = input[index[0]:index[1]]
+               if len(input) >= len(candidatePath+"(") && strings.Index(input, candidatePath+"(") != -1 {
                        continue
                }
 
-               if isEmail(s) || isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || endsWithInvalidString(s) || containsInvalidString(s) || len(s) <= 2 {
+               if isEmail(candidatePath) || isDate(candidatePath) || isVersion(candidatePath) || isGitRange(candidatePath) || isGitInstruction(candidatePath) || endsWithInvalidString(candidatePath) || containsInvalidString(candidatePath) || len(candidatePath) <= 2 {
                        continue
                }
-               if isGitPath(s) {
-                       s = replaceGitPath(s)
+               if isGitPath(candidatePath) {
+                       candidatePath = replaceGitPath(candidatePath)
                }
-               s = postProcess(s)
+               candidatePath = postProcess(candidatePath)
+               lineNumber := 45
+               columnNumber := 1
                if options.format == "ackmate" {
-                       s = fmt.Sprint(s, ":45")
+                       candidatePath = fmt.Sprint(candidatePath, ":", lineNumber, ":", columnNumber)
                }
-               result = append(result, s)
+               result = append(result, candidatePath)
        }
        return result
 }
index 5d447f4131a81e92a4709db8c89b6ad01f9b4377..7e52783ef9be9d5d92ebc37c3578884d7746081e 100644 (file)
@@ -103,16 +103,6 @@ func TestEverything(t *testing.T) {
        }
 
        output = GetAllMatches("and/or", MatchOptions{})
-       if len(output) != 0 {
-               t.Errorf("Matches and/or", output[0])
-       }
-
-       output = GetAllMatches("hello user.test.js", MatchOptions{})
-       if output[0] != "user.test.js" {
-               t.Errorf("Matches date", output[0])
-       }
-
-       output = GetAllMatches(" mail@mail.com ", MatchOptions{})
        if len(output) != 0 {
                t.Errorf("Matches and/or adresses", output)
        }
@@ -122,6 +112,11 @@ func TestEverything(t *testing.T) {
                t.Errorf("Matches version number", output)
        }
 
+       output = GetAllMatches("~/v1.2/js", MatchOptions{})
+       if len(output) != 1 {
+               t.Errorf("Should match path with version inside", output)
+       }
+
        output = GetAllMatches("obj.slice()", MatchOptions{})
        if len(output) != 0 {
                t.Errorf("Matches function call", output)
@@ -184,10 +179,11 @@ func TestEverything(t *testing.T) {
        if len(output) != 1 {
                t.Errorf("Ackmate doesnt match", output)
        }
+
        if output[0] == "test.js" {
                t.Errorf("Ackmate should not forget number", output)
        }
-       if output[0] != "test.js:45" {
+       if output[0] != "test.js:45:1" {
                t.Errorf("Ackmate should output right line number", output)
        }
 }
index 042862c2c77c131db800d58a96e7152d50758931..fe2156d3330572b6c612a18add76cda06ffbe8b6 100644 (file)
@@ -52,7 +52,7 @@ func replaceGitPath(input string) string {
 }
 
 func isVersion(input string) bool {
-       r := regexp.MustCompile("[0-9x]\\.[0-9x]{1,2}(\\.[0-9x]{1,3})?")
+       r := regexp.MustCompile("^v?[0-9x]\\.[0-9x]{1,2}(\\.[0-9x]{1,3})?$")
        return r.Match([]byte(input))
 }