import "regexp"
-
func pathExtractor(input string) [][][]byte {
- surroundRegex := "[^][ \\t:'\"]"
- r := regexp.MustCompile("("+surroundRegex+"*[\\./]"+surroundRegex+"*)")
- temp := [][][]byte{}
- temp = r.FindAllSubmatch([]byte(input),-1)
- return temp
+ surroundRegex := "[^][ \\t:'\"]"
+ r := regexp.MustCompile("(" + surroundRegex + "*[\\./]" + surroundRegex + "*)")
+ temp := [][][]byte{}
+ temp = r.FindAllSubmatch([]byte(input), -1)
+ return temp
}
func getAllMatches(input string) []string {
- matches := [][][]byte{}
- result := []string{}
- s := string("")
- matches = pathExtractor(input)
- for _,match := range matches {
- s = string(match[1])
- if isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || containsInvalidString(s) || len(s)<=2 {
- continue
- }
- if isGitPath(s) {
- s = replaceGitPath(s)
- }
- result = append(result,s)
- }
- return result
+ matches := [][][]byte{}
+ result := []string{}
+ s := string("")
+ matches = pathExtractor(input)
+ for _, match := range matches {
+ s = string(match[1])
+ if isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || containsInvalidString(s) || len(s) <= 2 {
+ continue
+ }
+ if isGitPath(s) {
+ s = replaceGitPath(s)
+ }
+ result = append(result, s)
+ }
+ return result
}
package main
-import "os"
-import "bufio"
-import "fmt"
+import (
+ "bufio"
+ "fmt"
+ "os"
+)
func main() {
- stdin := os.Stdin
- if scanner := bufio.NewScanner(stdin); scanner != nil {
- for scanner.Scan() {
- matches := getAllMatches(scanner.Text())
- for _,match := range matches {
- fmt.Println(match)
- }
- }
- }
+ stdin := os.Stdin
+ if scanner := bufio.NewScanner(stdin); scanner != nil {
+ for scanner.Scan() {
+ matches := getAllMatches(scanner.Text())
+ for _, match := range matches {
+ fmt.Println(match)
+ }
+ }
+ }
}
-
import "testing"
func TestGitIgnore(t *testing.T) {
- output:=getAllMatches("?? alt/generateStore.php")
- if output[0] != "alt/generateStore.php" {
- t.Errorf("Doesnt match files", output[0])
- }
+ output := getAllMatches("?? alt/generateStore.php")
+ if output[0] != "alt/generateStore.php" {
+ t.Errorf("Doesnt match files", output[0])
+ }
- output=getAllMatches("hello .gitignore")
- if output[0] != ".gitignore" {
- t.Errorf("Doesnt match hidden files", output[0])
- }
+ output = getAllMatches("hello .gitignore")
+ if output[0] != ".gitignore" {
+ t.Errorf("Doesnt match hidden files", output[0])
+ }
- output=getAllMatches(" mail@mail.com ")
- if len(output) != 0 {
- t.Errorf("Matches email adresses", output[0])
- }
+ output = getAllMatches(" mail@mail.com ")
+ if len(output) != 0 {
+ t.Errorf("Matches email adresses", output[0])
+ }
- output=getAllMatches("v1.2")
- if len(output) != 0 {
- t.Errorf("Matches version number", output[0])
- }
+ output = getAllMatches("v1.2")
+ if len(output) != 0 {
+ t.Errorf("Matches version number", output[0])
+ }
- output=getAllMatches("obj.slice()")
- if len(output) != 0 {
- t.Errorf("Matches function call", output[0])
- }
+ output = getAllMatches("obj.slice()")
+ if len(output) != 0 {
+ t.Errorf("Matches function call", output[0])
+ }
- output=getAllMatches("~/www")
- if len(output) == 0 || output[0] != "~/www" {
- t.Errorf("Doesnt match home", output[0])
- }
+ output = getAllMatches("~/www")
+ if len(output) == 0 || output[0] != "~/www" {
+ t.Errorf("Doesnt match home", output[0])
+ }
- output=getAllMatches("origin/master")
- if len(output) != 0 {
- t.Errorf("Matches remote name", output[0])
- }
+ output = getAllMatches("origin/master")
+ if len(output) != 0 {
+ t.Errorf("Matches remote name", output[0])
+ }
- output=getAllMatches("john doe (dead on 28/04/2014)")
- if len(output) != 0 {
- t.Errorf("Matches date", output[0])
- }
+ output = getAllMatches("john doe (dead on 28/04/2014)")
+ if len(output) != 0 {
+ t.Errorf("Matches date", output[0])
+ }
- output=getAllMatches("john doe ,dead on 28/04/2014")
- if len(output) != 0 {
- t.Errorf("Matches date", output[0])
- }
+ output = getAllMatches("john doe ,dead on 28/04/2014")
+ if len(output) != 0 {
+ t.Errorf("Matches date", output[0])
+ }
- output=getAllMatches(".gitignore , ~/www")
- if len(output) != 2 {
- t.Errorf("Doesnt match multi", output[0])
- }
+ output = getAllMatches(".gitignore , ~/www")
+ if len(output) != 2 {
+ t.Errorf("Doesnt match multi", output[0])
+ }
- output=getAllMatches("var/")
- if len(output) != 1 {
- t.Errorf("Doesnt match dir", output[0])
- }
+ output = getAllMatches("var/")
+ if len(output) != 1 {
+ t.Errorf("Doesnt match dir", output[0])
+ }
- output=getAllMatches("//")
- if len(output) != 0 {
- t.Errorf("Comment matches", output[0])
- }
+ output = getAllMatches("//")
+ if len(output) != 0 {
+ t.Errorf("Comment matches", output[0])
+ }
}
package main
-import "strings"
-import "regexp"
-
-func isGitRange (input string) bool {
- r := regexp.MustCompile("[0-9a-f]{3,}\\.\\.[0-9a-f]{3,}")
- return r.Match([]byte(input))
+import (
+ "regexp"
+ "strings"
+)
+
+func isGitRange(input string) bool {
+ r := regexp.MustCompile("[0-9a-f]{3,}\\.\\.[0-9a-f]{3,}")
+ return r.Match([]byte(input))
}
-func isGitPath (input string) bool {
- r := regexp.MustCompile("^[ab]/")
- return r.Match([]byte(input))
+func isGitPath(input string) bool {
+ r := regexp.MustCompile("^[ab]/")
+ return r.Match([]byte(input))
}
-func isDate (input string) bool {
- r := regexp.MustCompile("^[0-9]+/[0-9]+/[0-9]+")
- return r.Match([]byte(input))
+func isDate(input string) bool {
+ r := regexp.MustCompile("^[0-9]+/[0-9]+/[0-9]+")
+ return r.Match([]byte(input))
}
-func isGitInstruction (input string) bool {
- r := regexp.MustCompile("\\.{3,}")
- return r.Match([]byte(input))
+func isGitInstruction(input string) bool {
+ r := regexp.MustCompile("\\.{3,}")
+ return r.Match([]byte(input))
}
-func replaceGitPath (input string) string {
- r := regexp.MustCompile("^[ab]/(.*)")
- temp := [][]byte{}
- temp = r.FindSubmatch([]byte(input))
- return string(temp[1])
+func replaceGitPath(input string) string {
+ r := regexp.MustCompile("^[ab]/(.*)")
+ temp := [][]byte{}
+ temp = r.FindSubmatch([]byte(input))
+ return string(temp[1])
}
-func isVersion (input string) bool {
- r := regexp.MustCompile("[0-9x]\\.[0-9x]{1,2}(\\.[0-9x]{1,3})?")
- return r.Match([]byte(input))
+func isVersion(input string) bool {
+ r := regexp.MustCompile("[0-9x]\\.[0-9x]{1,2}(\\.[0-9x]{1,3})?")
+ return r.Match([]byte(input))
}
-func containsInvalidString (input string) bool {
- invalidStrings := []string{"(",")","@","origin/","{","}","<",">","$","*"}
- for _,s := range invalidStrings {
- if strings.Contains(input,s) {
- return true
- }
- }
- return false
+func containsInvalidString(input string) bool {
+ invalidStrings := []string{"(", ")", "@", "origin/", "{", "}", "<", ">", "$", "*"}
+ for _, s := range invalidStrings {
+ if strings.Contains(input, s) {
+ return true
+ }
+ }
+ return false
}