forked from client9/misspell
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,17 @@ import ( | |
"bytes" | ||
"regexp" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
var ( | ||
reEmail = regexp.MustCompile(`[a-zA-Z0-9_.%+-]+@[a-zA-Z0-9-.]+\.[a-zA-Z]{2,6}[^a-zA-Z]`) | ||
reHost = regexp.MustCompile(`[a-zA-Z0-9-.]+\.[a-zA-Z]+`) | ||
reBackslash = regexp.MustCompile(`\\[a-z]`) | ||
reEmail = regexp.MustCompile(`[[:alnum:]_.%+-]+@[[:alnum:]-.]+\.[[:alpha:]]{2,6}[^[:alpha:]]`) | ||
reBackslash = regexp.MustCompile(`\\[[:lower:]]`) | ||
|
||
// reHost Host name regular expression. | ||
// The length of any one label is limited between 1 and 63 octets. (https://www.ietf.org/rfc/rfc2181.txt) | ||
// A TLD has at least 2 letters. | ||
reHost = regexp.MustCompile(`([[:alnum:]-]+\.)+[[:alpha:]]{2,63}`) | ||
) | ||
|
||
// RemovePath attempts to strip away embedded file system paths, e.g. | ||
|
@@ -62,14 +67,26 @@ func replaceWithBlanks(s string) string { | |
return strings.Repeat(" ", len(s)) | ||
} | ||
|
||
// replaceHost same as replaceWithBlanks but if the string contains at least one uppercase letter returns the string. | ||
// Domain names are case-insensitive but browsers and DNS convert uppercase to lower case. (https://www.ietf.org/rfc/rfc4343.txt) | ||
func replaceHost(s string) string { | ||
for _, r := range s { | ||
if unicode.IsUpper(r) { | ||
return s | ||
} | ||
} | ||
|
||
return replaceWithBlanks(s) | ||
} | ||
|
||
// RemoveEmail remove email-like strings, e.g. "[email protected]", "[email protected]". | ||
func RemoveEmail(s string) string { | ||
return reEmail.ReplaceAllStringFunc(s, replaceWithBlanks) | ||
} | ||
|
||
// RemoveHost removes host-like strings "foobar.com" "abc123.fo1231.biz". | ||
func RemoveHost(s string) string { | ||
return reHost.ReplaceAllStringFunc(s, replaceWithBlanks) | ||
return reHost.ReplaceAllStringFunc(s, replaceHost) | ||
} | ||
|
||
// RemoveBackslashEscapes removes characters that are preceded by a backslash. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,20 @@ func TestNotWords(t *testing.T) { | |
{word: "[/foo/bar] abc", want: "[ ] abc"}, | ||
{word: "/", want: "/"}, | ||
{word: "x [email protected] y", want: "x y"}, | ||
{word: "x fqdn.example.org. y", want: "x . y"}, | ||
{word: "x infinitie.net y", want: "x y"}, | ||
{word: "(s.svc.GetObject(", want: "( ("}, | ||
{word: "x infinitie.net ", want: "x "}, | ||
{word: "x infinitie.net", want: "x "}, | ||
{word: "x foo.example.com y", want: "x y"}, | ||
{word: "x foo.example.com ", want: "x "}, | ||
{word: "x foo.example.com", want: "x "}, | ||
{word: "foo.example.com y", want: " y"}, | ||
{word: "foo.example.com", want: " "}, | ||
{word: "(s.svc.GetObject(", want: "(s.svc.GetObject("}, | ||
{word: "defer file.Close()", want: "defer file.Close()"}, | ||
{word: "defer file.c()", want: "defer file.c()"}, | ||
{word: "defer file.cl()", want: "defer ()"}, // false negative | ||
{word: "defer file.close()", want: "defer ()"}, // false negative | ||
{word: "\\nto", want: " to"}, | ||
} | ||
|
||
|