stringutils2: add RemoveUtf8Strings func

This commit is contained in:
Zexi Li
2020-10-15 19:33:18 +08:00
parent fc15772ab6
commit f0cc1d3d1a
6 changed files with 40 additions and 19 deletions

View File

@@ -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

View File

@@ -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