gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/client9/misspell/notwords.go (about) 1 package misspell 2 3 import ( 4 "bytes" 5 "regexp" 6 "strings" 7 ) 8 9 var ( 10 reEmail = regexp.MustCompile(`[a-zA-Z0-9_.%+-]+@[a-zA-Z0-9-.]+\.[a-zA-Z]{2,6}[^a-zA-Z]`) 11 reHost = regexp.MustCompile(`[a-zA-Z0-9-.]+\.[a-zA-Z]+`) 12 reBackslash = regexp.MustCompile(`\\[a-z]`) 13 ) 14 15 // RemovePath attempts to strip away embedded file system paths, e.g. 16 // /foo/bar or /static/myimg.png 17 // 18 // TODO: windows style 19 // 20 func RemovePath(s string) string { 21 out := bytes.Buffer{} 22 var idx int 23 for len(s) > 0 { 24 if idx = strings.IndexByte(s, '/'); idx == -1 { 25 out.WriteString(s) 26 break 27 } 28 29 if idx > 0 { 30 idx-- 31 } 32 33 var chclass string 34 switch s[idx] { 35 case '/', ' ', '\n', '\t', '\r': 36 chclass = " \n\r\t" 37 case '[': 38 chclass = "]\n" 39 case '(': 40 chclass = ")\n" 41 default: 42 out.WriteString(s[:idx+2]) 43 s = s[idx+2:] 44 continue 45 } 46 47 endx := strings.IndexAny(s[idx+1:], chclass) 48 if endx != -1 { 49 out.WriteString(s[:idx+1]) 50 out.Write(bytes.Repeat([]byte{' '}, endx)) 51 s = s[idx+endx+1:] 52 } else { 53 out.WriteString(s) 54 break 55 } 56 } 57 return out.String() 58 } 59 60 // replaceWithBlanks returns a string with the same number of spaces as the input 61 func replaceWithBlanks(s string) string { 62 return strings.Repeat(" ", len(s)) 63 } 64 65 // RemoveEmail remove email-like strings, e.g. "nickg+junk@xfoobar.com", "nickg@xyz.abc123.biz" 66 func RemoveEmail(s string) string { 67 return reEmail.ReplaceAllStringFunc(s, replaceWithBlanks) 68 } 69 70 // RemoveHost removes host-like strings "foobar.com" "abc123.fo1231.biz" 71 func RemoveHost(s string) string { 72 return reHost.ReplaceAllStringFunc(s, replaceWithBlanks) 73 } 74 75 // RemoveBackslashEscapes removes characters that are preceeded by a backslash 76 // commonly found in printf format stringd "\nto" 77 func removeBackslashEscapes(s string) string { 78 return reBackslash.ReplaceAllStringFunc(s, replaceWithBlanks) 79 } 80 81 // RemoveNotWords blanks out all the not words 82 func RemoveNotWords(s string) string { 83 // do most selective/specific first 84 return removeBackslashEscapes(RemoveHost(RemoveEmail(RemovePath(StripURL(s))))) 85 }