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 string truncation to properly handle multi-byte characters #1080

Open
kipkaev55 opened this issue May 22, 2024 · 0 comments · May be fixed by #1105
Open

Fix string truncation to properly handle multi-byte characters #1080

kipkaev55 opened this issue May 22, 2024 · 0 comments · May be fixed by #1105
Milestone

Comments

@kipkaev55
Copy link

Description:

This feature change aims to fix the issue with truncating strings encoded in a multi-byte character set. In the current implementation, truncation was occurring at the byte level, which could lead to incorrect handling of multi-byte characters, such as UTF-8 characters.

import (
    // ...
    "unicode/utf8"
    // ...
)
// ...
func truncate(s string, max int) string {
	if utf8.RuneCountInString(s) <= max || max < 0 {
		return s
	}
	runes := []rune(s)
	if max > 3 {
		return string(runes[:max-3]) + "..."
	}
	return string(runes[:max])
}

Changes:

  • Changes have been made to the logic of string truncation to account for multi-byte characters.
  • The usage of the unicode/utf8 package has been added to accurately count characters in the string.
  • String truncation now occurs at the character level, ensuring proper behavior when dealing with multi-byte characters.

Why It Matters:

The problem with truncating strings encoded in a multi-byte character set can lead to unpredictable behavior and errors in the application, especially when it deals with multilingual data or text containing special characters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants