Skip to content

Commit

Permalink
👔 up(str): update the func SimpleMatch() support match end withs
Browse files Browse the repository at this point in the history
- update some func comments
  • Loading branch information
inhere committed May 7, 2023
1 parent f8adc83 commit 6cb3562
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
15 changes: 12 additions & 3 deletions strutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,30 @@ func VersionCompare(v1, v2, op string) bool {

// SimpleMatch all sub-string in the give text string.
//
// Difference the ContainsAll, start with ^ for exclude contains check.
// Difference the ContainsAll:
//
// - start with ^ for exclude contains check.
// - end with $ for check end with keyword.
func SimpleMatch(s string, keywords []string) bool {
for _, keyword := range keywords {
if keyword == "" {
kln := len(keyword)
if kln == 0 {
continue
}

// exclude
if keyword[0] == '^' && len(keyword) > 1 {
if kln > 1 && keyword[0] == '^' {
if strings.Contains(s, keyword[1:]) {
return false
}
continue
}

// end with
if kln > 1 && keyword[kln-1] == '$' {
return strings.HasSuffix(s, keyword[:kln-1])
}

// include
if !strings.Contains(s, keyword) {
return false
Expand Down
2 changes: 2 additions & 0 deletions strutil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,7 @@ func TestHasEmpty(t *testing.T) {
func TestSimpleMatch(t *testing.T) {
str := "hi inhere, age is 120"
assert.True(t, strutil.SimpleMatch(str, []string{"hi", "inhere"}))
assert.True(t, strutil.SimpleMatch(str, []string{"hi", "inhere", "120$"}))
assert.False(t, strutil.SimpleMatch(str, []string{"hi", "^inhere"}))
assert.False(t, strutil.SimpleMatch(str, []string{"hi", "inhere$"}))
}
7 changes: 5 additions & 2 deletions strutil/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import (

// some consts string chars
const (
Numbers = "0123456789"
HexChars = "0123456789abcdef"
Numbers = "0123456789"
HexChars = "0123456789abcdef"

AlphaBet = "abcdefghijklmnopqrstuvwxyz"
AlphaBet1 = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"

AlphaNum = "abcdefghijklmnopqrstuvwxyz0123456789"
AlphaNum2 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
AlphaNum3 = "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"

Base62Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Base64Chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"
)

Expand Down
3 changes: 2 additions & 1 deletion strutil/strutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
)

// OrCond return s1 on cond is True, OR return s2.
// Like: cond ? s1 : s2
func OrCond(cond bool, s1, s2 string) string {
if cond {
return s1
}
return s2
}

// OrElse return s OR nv(new-value) on s is empty
// OrElse return s OR orVal(new-value) on s is empty
func OrElse(s, orVal string) string {
if s != "" {
return s
Expand Down

0 comments on commit 6cb3562

Please sign in to comment.