mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-06-01 05:22:24 +08:00
stringutils2: add RemoveUtf8Strings func
This commit is contained in:
@@ -23,6 +23,16 @@ func IsUtf8(str string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func RemoveUtf8Strings(idOrNames []string) []string {
|
||||
ids := make([]string, 0)
|
||||
for _, idOrName := range idOrNames {
|
||||
if !IsUtf8(idOrName) {
|
||||
ids = append(ids, idOrName)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func IsPrintableAscii(b byte) bool {
|
||||
if b >= 32 && b <= 126 {
|
||||
return true
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package stringutils2
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -34,6 +35,31 @@ func TestIsUtf8(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveUtf8Strings(t *testing.T) {
|
||||
cases := []struct {
|
||||
in []string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
in: []string{},
|
||||
want: []string{},
|
||||
},
|
||||
{
|
||||
in: []string{"en", "中文"},
|
||||
want: []string{"en"},
|
||||
},
|
||||
{
|
||||
in: []string{"中文"},
|
||||
want: []string{},
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := RemoveUtf8Strings(c.in); !reflect.DeepEqual(got, c.want) {
|
||||
t.Errorf("RemoveUtf8Strings %s got %v want %v", c.in, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPrintableAscii(t *testing.T) {
|
||||
cases := []struct {
|
||||
in string
|
||||
|
||||
Reference in New Issue
Block a user