goutils icon indicating copy to clipboard operation
goutils copied to clipboard

fix: handle multi-byte UTF-8 characters in DeleteWhiteSpace

Open Yanhu007 opened this issue 7 months ago • 0 comments

Fixes #29

Problem

DeleteWhiteSpace iterates by byte index (str[i]) and casts each byte to rune. For multi-byte UTF-8 characters (Chinese, Japanese, Korean, emoji, etc.), this produces mojibake:

DeleteWhiteSpace(" 测试 测试 ")
// Got:    "æµ\x8dè¯\x95æµ\x8dè¯\x95"
// Expect: "测试测试"

Fix

Use range-based iteration which correctly yields Unicode runes instead of raw bytes.

// Before
for i := 0; i < sz; i++ {
    ch := rune(str[i])  // BUG: treats each byte as a rune

// After
for _, ch := range str {  // correctly iterates runes

All existing tests pass.

Yanhu007 avatar Apr 15 '26 03:04 Yanhu007