From d4ed8d0a9e633c3e6df27f5bc42f163040410e41 Mon Sep 17 00:00:00 2001 From: Yanhu007 Date: Wed, 15 Apr 2026 11:09:59 +0800 Subject: [PATCH] fix: handle multi-byte UTF-8 characters in DeleteWhiteSpace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DeleteWhiteSpace iterated over string bytes (str[i]) and cast each byte to rune, which corrupts multi-byte UTF-8 characters like Chinese, Japanese, Korean, emoji, etc. DeleteWhiteSpace(" 测试 测试 ") → "æµ\x8dè¯\x95æµ\x8dè¯\x95" Fix: use range-based iteration which correctly yields Unicode runes. DeleteWhiteSpace(" 测试 测试 ") → "测试测试" Fixes #29 --- stringutils.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/stringutils.go b/stringutils.go index 741bb53..8d36c11 100644 --- a/stringutils.go +++ b/stringutils.go @@ -110,17 +110,13 @@ 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]) + for _, ch := range str { if !unicode.IsSpace(ch) { chs.WriteRune(ch) - count++ } } - if count == sz { + if chs.Len() == len(str) { return str } return chs.String()