Skip to content

Commit

Permalink
👔 up(str): optimize the ToByteSize() and GlobMatch() logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 16, 2023
1 parent f420df7 commit 777ad5f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
31 changes: 26 additions & 5 deletions strutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,37 @@ 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) }
// PathMatch check for a string match the pattern. alias of the path.Match()
//
// TIP: `*` can match any char, not contain `/`.
func PathMatch(pattern, s string) bool {
ok, err := path.Match(pattern, s)
if err != nil {
ok = false
}
return ok
}

// GlobMatch check for a string.
// GlobMatch check for a string match the pattern.
//
// Difference with PathMatch() is: `*` can match any char, contain `/`.
func GlobMatch(pattern, s string) bool {
// replace `/` to `S` for path.Match
if strings.Contains(pattern, "/") {
pattern = strings.Replace(pattern, "/", "S", -1)
}
if strings.Contains(s, "/") {
s = strings.Replace(s, "/", "S", -1)
}

ok, err := path.Match(pattern, s)
if err != nil {
ok = false
}
return ok
}

// MatchNodePath check for a string.
// MatchNodePath check for a string match the pattern.
//
// Use on pattern:
// - `*` match any to sep
Expand All @@ -281,7 +299,10 @@ func MatchNodePath(pattern, s string, sep string) bool {
}

pattern = strings.Replace(pattern, sep, "/", -1)
s = strings.Replace(s, sep, "/", -1)
if strings.Contains(s, sep) {
s = strings.Replace(s, sep, "/", -1)
}

ok, err := path.Match(pattern, s)
if err != nil {
ok = false
Expand Down
20 changes: 16 additions & 4 deletions strutil/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ func TestGlobMatch(t *testing.T) {
{"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},
// {"a*/b", "a/c/b", false},
// {"a*", "a/c/b", false},
// {"a**", "a/c/b", false},
}

for _, tt := range tests {
Expand All @@ -212,8 +212,20 @@ 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.PathMatch("ab*", "abc"))
assert.True(t, strutil.QuickMatch("ab*", "abc"))
assert.True(t, strutil.PathMatch("ab*", "abc"))
}

func TestPathMatch_GlobMatch_diff(t *testing.T) {
// different of the PathMatch and GlobMatch
assert.True(t, strutil.GlobMatch("a*/b", "a/c/b"))
assert.False(t, strutil.PathMatch("a*/b", "a/c/b"))

assert.True(t, strutil.GlobMatch("a*", "a/c/b"))
assert.False(t, strutil.PathMatch("a*", "a/c/b"))

assert.True(t, strutil.GlobMatch("a**", "a/c/b"))
assert.False(t, strutil.PathMatch("a**", "a/c/b"))
}

func TestMatchNodePath(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions strutil/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ func ToByteSize(sizeStr string) (uint64, error) {
if lastSec > 'A' {
lastPos--
}
} else if IsNumChar(sizeStr[lastPos]) { // not unit suffix. eg: 346
return strconv.ParseUint(sizeStr, 10, 32)
}

multiplier := float64(1)
Expand Down
2 changes: 1 addition & 1 deletion strutil/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func TestToByteSize(t *testing.T) {
{346777, "338.65 Kb"},
{12341739, "11.77 M"},
{1202590842, "1.12GB"},
{1234567890123, "1.12 TB"},
{1231453023109, "1.12 TB"},
}

for _, tt := range tests {
Expand Down

0 comments on commit 777ad5f

Please sign in to comment.