forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CWA customer changes #2: Add support to WMI like syntax to regexp pat…
…tern for Procstat on Windows
- Loading branch information
1 parent
908f02f
commit 30da07b
Showing
3 changed files
with
164 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package like2regexp | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
const special = `\.+*?()|{}^$` // All regexp special chars excetp `[]` | ||
|
||
func WMILikeToRegexp(like string) string { | ||
var buf strings.Builder | ||
// Quote special characters | ||
inclass := false | ||
for i := 0; i < len(like); i++ { | ||
c := like[i] | ||
|
||
if inclass && c == ']' { | ||
inclass = false | ||
} else if c == '[' { | ||
inclass = true | ||
} else if !inclass { | ||
switch c { | ||
case '_': | ||
c = '.' | ||
case '%': | ||
buf.WriteByte('.') | ||
c = '*' | ||
default: | ||
if strings.IndexByte(special, c) != -1 { // Escape special chars | ||
buf.WriteByte('\\') | ||
} | ||
} | ||
} | ||
buf.WriteByte(c) | ||
} | ||
|
||
return `(?i:^` + buf.String() + `$)` | ||
} |
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,76 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package like2regexp | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
var tests = []struct { | ||
input, expected string | ||
matches, excludes []string | ||
}{ | ||
{`abc`, `(?i:^abc$)`, | ||
[]string{`abc`, `ABC`, `AbC`}, | ||
[]string{`def`, `DEF`}, | ||
}, | ||
{`a.c`, `(?i:^a\.c$)`, | ||
[]string{`a.c`}, | ||
[]string{`abc`, `abbc`, `xcc`, `abb`, `a`, `ac`}, | ||
}, | ||
{`%watch%`, `(?i:^.*watch.*$)`, | ||
[]string{`amazon-cloudwatch-agent.exe`, `WatchSomething`, `watch`, `amazoncloudwatch`}, | ||
[]string{`amazon-ssm-agent.exe`, `wach`, `amazoncloudwatc`}, | ||
}, | ||
{`[a-z]`, `(?i:^[a-z]$)`, | ||
[]string{`a`, `c`, `x`}, | ||
[]string{`1`, `2`, `3`, `ab`}, | ||
}, | ||
{`[^a-z]`, `(?i:^[^a-z]$)`, | ||
[]string{`1`, `2`}, | ||
[]string{`a`, `c`, `x`, `abc`}, | ||
}, | ||
{`abc^def[^a-z]`, `(?i:^abc\^def[^a-z]$)`, | ||
[]string{`abc^def1`, `abc^def_`}, | ||
[]string{`abc^defa`, `abc^defz`, `abcdef`}, | ||
}, | ||
{`a_c`, `(?i:^a.c$)`, | ||
[]string{`a.c`, `abc`, `acc`}, | ||
[]string{`bbc`, `abb`, `ac`}, | ||
}, | ||
{`%a_c%`, `(?i:^.*a.c.*$)`, | ||
[]string{`aa.cc`, `abc`, `acc`, `xxxabcxxx`}, | ||
[]string{`abbc`, `abbb`, `bbac`}, | ||
}, | ||
{`%[_][_]%`, `(?i:^.*[_][_].*$)`, | ||
[]string{`__`, `a__cc`}, | ||
[]string{`_x_`, `acc`}, | ||
}, | ||
{`he[[]ll]o.exe`, `(?i:^he[[]ll]o\.exe$)`, | ||
[]string{`he[ll]o.exe`}, | ||
[]string{`hello.exe`}, | ||
}, | ||
} | ||
|
||
func TestWMILikeToRegexp(t *testing.T) { | ||
for _, test := range tests { | ||
output := WMILikeToRegexp(test.input) | ||
if test.expected != output { | ||
t.Errorf("translated regexp does not match expected result:\n\tExpected: %v\n\tOutput : %v", test.expected, output) | ||
} | ||
|
||
re := regexp.MustCompile(output) | ||
for _, m := range test.matches { | ||
if !re.MatchString(m) { | ||
t.Errorf("case '%v': generated regexp %v does not match value '%v'", test.input, output, m) | ||
} | ||
} | ||
for _, m := range test.excludes { | ||
if re.MatchString(m) { | ||
t.Errorf("case '%v': generated regexp %v should not match value '%v'", test.input, output, m) | ||
} | ||
} | ||
} | ||
} |
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