-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from YakDriver/f-standardize-regexps
Standardize reg exprs before caching
- Loading branch information
Showing
4 changed files
with
135 additions
and
7 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
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
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package regexache | ||
|
||
import "regexp" | ||
|
||
var ( | ||
numFront *regexp.Regexp | ||
capFront *regexp.Regexp | ||
lowFront *regexp.Regexp | ||
undFront *regexp.Regexp | ||
word *regexp.Regexp | ||
) | ||
|
||
func init() { | ||
undFront = regexp.MustCompile(`(\[)([^\]]*)(_)([^\]]*)(\])`) | ||
lowFront = regexp.MustCompile(`(\[)([^\]]*)(a-[b-z])([^\]]*)(\])`) | ||
capFront = regexp.MustCompile(`(\[)([^\]]*)(A-[B-Z])([^\]]*)(\])`) | ||
numFront = regexp.MustCompile(`(\[)([^\]]*)(0-9)([^\]]*)(\])`) | ||
word = regexp.MustCompile(`(\[)([^\]]*)(0-9A-Za-z_)([^\]]*)(\])`) | ||
} | ||
|
||
func standardize(expr string) string { | ||
expr = undFront.ReplaceAllString(expr, "$1$3$2$4$5") | ||
expr = lowFront.ReplaceAllString(expr, "$1$3$2$4$5") | ||
expr = capFront.ReplaceAllString(expr, "$1$3$2$4$5") | ||
expr = numFront.ReplaceAllString(expr, "$1$3$2$4$5") | ||
expr = word.ReplaceAllString(expr, `$1\w$2$4$5`) | ||
return expr | ||
} |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package regexache | ||
|
||
import "testing" | ||
|
||
func TestStandardize(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
TestName string | ||
Input string | ||
Expected string | ||
}{ | ||
{ | ||
TestName: "empty", | ||
Input: "", | ||
Expected: "", | ||
}, | ||
{ | ||
TestName: "basic", | ||
Input: `.*\S.*`, | ||
Expected: `.*\S.*`, | ||
}, | ||
{ | ||
TestName: "numOrder", | ||
Input: `^[a-z0-9-_]+$`, | ||
Expected: `^[0-9a-z_-]+$`, | ||
}, | ||
{ | ||
TestName: "multiClass", | ||
Input: `^[a-z0-9-_]+[a-z0-9-_]+$`, | ||
Expected: `^[0-9a-z_-]+[0-9a-z_-]+$`, | ||
}, | ||
{ | ||
TestName: "everywhere", | ||
Input: `^[A-Za-z0-9-*&_]+$`, | ||
Expected: `^[\w-*&]+$`, | ||
}, | ||
{ | ||
TestName: "hex", | ||
Input: `^[A-Fa-f0-9-*&_]+$`, | ||
Expected: `^[0-9A-Fa-f_-*&]+$`, | ||
}, | ||
{ | ||
TestName: "hex2", | ||
Input: `^#[A-F0-9]{6}$`, | ||
Expected: `^#[0-9A-F]{6}$`, | ||
}, | ||
{ | ||
TestName: "parenthesis", | ||
Input: `(/)|(/(([^~])|(~[01]))+)`, | ||
Expected: `(/)|(/(([^~])|(~[01]))+)`, | ||
}, | ||
{ | ||
TestName: "ordering", | ||
Input: `^[a-zA-Z0-9]+$`, | ||
Expected: `^[0-9A-Za-z]+$`, | ||
}, | ||
{ | ||
TestName: "ordering2", | ||
Input: `^[-a-zA-Z0-9._]*$`, | ||
Expected: `^[\w-.]*$`, | ||
}, | ||
{ | ||
TestName: "ordering3", | ||
Input: `^[a-z0-9-]+$`, | ||
Expected: `^[0-9a-z-]+$`, | ||
}, | ||
{ | ||
TestName: "ordering4", | ||
Input: `^[a-zA-Z0-9._-]*$`, | ||
Expected: `^[\w.-]*$`, | ||
}, | ||
{ | ||
TestName: "ordering5", | ||
Input: `^[0-9a-zA-Z._-]+`, | ||
Expected: `^[\w.-]+`, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.TestName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := standardize(testCase.Input) | ||
|
||
if got != testCase.Expected { | ||
t.Errorf("got %s, expected %s", got, testCase.Expected) | ||
} | ||
}) | ||
} | ||
} |