Skip to content

Commit

Permalink
✨ feat(str): add new util func PathMatch, MatchNodePath for match str…
Browse files Browse the repository at this point in the history
…ing path
  • Loading branch information
inhere committed Mar 16, 2023
1 parent 86abf8e commit 53f7769
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
36 changes: 36 additions & 0 deletions strutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ func QuickMatch(pattern, s string) bool {
return strings.Contains(s, pattern)
}

// PathMatch check for a string.
func PathMatch(pattern, s string) bool { return GlobMatch(pattern, s) }

// GlobMatch check for a string.
func GlobMatch(pattern, s string) bool {
ok, err := path.Match(pattern, s)
Expand All @@ -208,3 +211,36 @@ func GlobMatch(pattern, s string) bool {
}
return ok
}

// MatchNodePath check for a string.
//
// Use on pattern:
// - `*` match any to sep
// - `**` match any to end. only allow at start or end on pattern.
//
// Example:
//
// strutil.MatchNodePath()
func MatchNodePath(pattern, s string, sep string) bool {
if pattern == "**" || pattern == s {
return true
}
if pattern == "" {
return len(s) == 0
}

if i := strings.Index(pattern, "**"); i >= 0 {
if i == 0 { // at start
return strings.HasSuffix(s, pattern[2:])
}
return strings.HasPrefix(s, pattern[:len(pattern)-2])
}

pattern = strings.Replace(pattern, sep, "/", -1)
s = strings.Replace(s, sep, "/", -1)
ok, err := path.Match(pattern, s)
if err != nil {
ok = false
}
return ok
}
37 changes: 36 additions & 1 deletion strutil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func TestVersionCompare(t *testing.T) {

assert.True(t, strutil.VersionCompare("1.0", "1.0", ""))
assert.True(t, strutil.VersionCompare("1.0", "1.0", "="))
assert.True(t, strutil.Compare("1.0", "2.0", "!="))

assert.False(t, strutil.Compare("2020-12-16", "2021-12-17", ">="))
}
Expand All @@ -192,10 +193,13 @@ func TestGlobMatch(t *testing.T) {
want bool
}{
{"a*", "abc", true},
{"a*", "ab.cd.ef", true},
{"ab.*.ef", "ab.cd.ef", true},
{"ab.*.ef", "ab.cd.efg", false},
{"ab.*.*", "ab.cd.ef", true},
{"ab.cd.*", "ab.cd.ef", true},
{"ab.*", "ab.cd.ef", true},
{"a*/b", "acd/b", true},
{"a*/b", "a/c/b", false},
{"a*", "a/c/b", false},
{"a**", "a/c/b", false},
Expand All @@ -208,6 +212,37 @@ func TestGlobMatch(t *testing.T) {
assert.True(t, strutil.QuickMatch("ab", "abc"))
assert.True(t, strutil.QuickMatch("abc", "abc"))
assert.False(t, strutil.GlobMatch("ab", "abc"))
assert.True(t, strutil.GlobMatch("ab*", "abc"))
assert.True(t, strutil.PathMatch("ab*", "abc"))
assert.True(t, strutil.QuickMatch("ab*", "abc"))
}

func TestMatchNodePath(t *testing.T) {
tests := []struct {
p, s string
want bool
}{
{"a*", "abc", true},
{"ab.*.ef", "ab.cd.ef", true},
{"ab.*.*", "ab.cd.ef", true},
{"ab.cd.*", "ab.cd.ef", true},
{"a*.b", "acd.b", true},
{"a**", "a.c.b", true},
{"ab", "abc", false},
{"a*", "ab.cd.ef", false},
{"ab.*.ef", "ab.cd.efg", false},
{"ab.*", "ab.cd.ef", false},
{"a*.b", "a.c.b", false},
{"a*", "a.c.b", false},
}

for i, tt := range tests {
assert.Eq(t, tt.want, strutil.MatchNodePath(tt.p, tt.s, "."), "case#%d %v", i, tt)
}
}

func TestHasEmpty(t *testing.T) {
assert.False(t, strutil.HasEmpty("ab", "cd", "ef"))
assert.True(t, strutil.HasEmpty("ab", "", "ef"))
assert.False(t, strutil.IsAllEmpty("ab", "", "ef"))
assert.True(t, strutil.IsAllEmpty("", ""))
}
10 changes: 9 additions & 1 deletion strutil/textutil/var_replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ func (r *VarReplacer) Replace(s string, tplVars map[string]any) string {
return r.Init().doReplace(s, varMap)
}

// RenderSimple string-map vars in the text contents
// ReplaceSMap string-map vars in the text contents
func (r *VarReplacer) ReplaceSMap(s string, varMap map[string]string) string {
if len(varMap) == 0 || !strings.Contains(s, r.Left) {
return s
}
return r.Init().doReplace(s, varMap)
}

// RenderSimple string-map vars in the text contents. alias of ReplaceSMap()
func (r *VarReplacer) RenderSimple(s string, varMap map[string]string) string {
if len(varMap) == 0 || !strings.Contains(s, r.Left) {
return s
Expand Down

0 comments on commit 53f7769

Please sign in to comment.