diff --git a/stringutils.go b/stringutils.go index 741bb53..356b707 100644 --- a/stringutils.go +++ b/stringutils.go @@ -17,7 +17,6 @@ limitations under the License. package goutils import ( - "bytes" "fmt" "strings" "unicode" @@ -107,23 +106,14 @@ Returns: the string without whitespaces */ func DeleteWhiteSpace(str string) string { - if str == "" { - return str - } - sz := len(str) - var chs bytes.Buffer - count := 0 - for i := 0; i < sz; i++ { - ch := rune(str[i]) + var b strings.Builder + b.Grow(len(str)) + for _, ch := range str { if !unicode.IsSpace(ch) { - chs.WriteRune(ch) - count++ + b.WriteRune(ch) } } - if count == sz { - return str - } - return chs.String() + return b.String() } /* diff --git a/stringutils_test.go b/stringutils_test.go index 76ef753..6ae991c 100644 --- a/stringutils_test.go +++ b/stringutils_test.go @@ -148,6 +148,14 @@ func TestDeleteWhiteSpace(t *testing.T) { if x := DeleteWhiteSpace(str); x != out { t.Errorf("IndexOf(%v) = %v, want %v", str, x, out) } + + // Test 3 + str = " 测试 测试 测试 " + out = "测试测试测试" + + if x := DeleteWhiteSpace(str); x != out { + t.Errorf("IndexOf(%v) = %v, want %v", str, x, out) + } } func TestIndexOfDifference(t *testing.T) {