forked from GeoNet/delta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add internal network regexp for stationxml (GeoNet#213)
* add internal network regexp for stationxml * simplified regexp matching and added a negative match option
- Loading branch information
Showing
7 changed files
with
139 additions
and
105 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
package main | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type Matcher interface { | ||
MatchString(string) bool | ||
} | ||
|
||
type positiveMatch struct { | ||
re *regexp.Regexp | ||
} | ||
|
||
func (p positiveMatch) MatchString(s string) bool { | ||
if p.re != nil { | ||
return p.re.MatchString(s) | ||
} | ||
return true | ||
} | ||
|
||
type negativeMatch struct { | ||
re *regexp.Regexp | ||
} | ||
|
||
func (n negativeMatch) MatchString(s string) bool { | ||
if n.re != nil { | ||
return !n.re.MatchString(s) | ||
} | ||
return false | ||
} | ||
|
||
func Match(str string) (Matcher, error) { | ||
// prepare the matching string | ||
s := strings.TrimPrefix(strings.TrimSpace(str), "!") | ||
|
||
// compile the expression | ||
re, err := regexp.Compile(s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// a negative match if it starts with an "!" symbol | ||
if strings.HasPrefix(strings.TrimSpace(str), "!") { | ||
return negativeMatch{re}, nil | ||
} | ||
|
||
return positiveMatch{re}, nil | ||
} | ||
|
||
func MustMatch(str string) Matcher { | ||
m, err := Match(str) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestMatcher(t *testing.T) { | ||
|
||
var tests = map[string]struct { | ||
m Matcher | ||
s string | ||
r bool | ||
}{ | ||
"empty positive": {positiveMatch{}, "", true}, | ||
"empty negative": {negativeMatch{}, "", false}, | ||
"valid positive match": {MustMatch("AB"), "AB", true}, | ||
"invalid positive match": {MustMatch("AB"), "BB", false}, | ||
"valid negative match": {MustMatch("!AB"), "BB", true}, | ||
"invalid negative match": {MustMatch("!AB"), "AB", false}, | ||
} | ||
|
||
for k, v := range tests { | ||
t.Log("test matcher for " + k) | ||
if v.m.MatchString(v.s) != v.r { | ||
t.Errorf("%s: unable to match string: %s", k, v.s) | ||
} | ||
} | ||
} |