mirror of https://github.com/mickael-menu/zk
Refine path filtering with regexes
parent
ac545f550a
commit
c10449a425
@ -0,0 +1,19 @@
|
||||
package icu
|
||||
|
||||
// EscapePattern adds backslash escapes to protect any characters that would
|
||||
// match as ICU pattern metacharacters.
|
||||
//
|
||||
// http://userguide.icu-project.org/strings/regexp
|
||||
func EscapePattern(s string) string {
|
||||
out := ""
|
||||
|
||||
for _, c := range s {
|
||||
switch c {
|
||||
case '\\', '.', '^', '$', '(', ')', '[', ']', '{', '}', '|', '*', '+', '?':
|
||||
out += `\`
|
||||
}
|
||||
out += string(c)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package icu
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mickael-menu/zk/util/test/assert"
|
||||
)
|
||||
|
||||
func TestEscapePAttern(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
`foo bar`: `foo bar`,
|
||||
`\a`: `\\a`,
|
||||
`.`: `\.`,
|
||||
`^`: `\^`,
|
||||
`$`: `\$`,
|
||||
`(`: `\(`,
|
||||
`)`: `\)`,
|
||||
`[`: `\[`,
|
||||
`]`: `\]`,
|
||||
`{`: `\{`,
|
||||
`}`: `\}`,
|
||||
`|`: `\|`,
|
||||
`*`: `\*`,
|
||||
`+`: `\+`,
|
||||
`?`: `\?`,
|
||||
`(?:[A-Za-z0-9]+[._]?){1,}[A-Za-z0-9]+\@(?:(?:[A-Za-z0-9]+[-]?){1,}[A-Za-z0-9]+\.){1,}`: `\(\?:\[A-Za-z0-9\]\+\[\._\]\?\)\{1,\}\[A-Za-z0-9\]\+\\@\(\?:\(\?:\[A-Za-z0-9\]\+\[-\]\?\)\{1,\}\[A-Za-z0-9\]\+\\\.\)\{1,\}`,
|
||||
}
|
||||
|
||||
for input, expected := range tests {
|
||||
assert.Equal(t, EscapePattern(input), expected)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue