Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Bug for DeleteWhiteSpace when str is other utf8 string, eg. chinese #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions stringutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package goutils

import (
"bytes"
"fmt"
"strings"
"unicode"
Expand Down Expand Up @@ -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()
}

/*
Expand Down
8 changes: 8 additions & 0 deletions stringutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down