mattermost / mattermost-plugin-jira

JIRA plugin for Mattermost 🔌
https://mattermost.gitbook.io/plugin-jira/
Apache License 2.0
98 stars 127 forks source link

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

Open kipkaev55 opened 4 months ago

kipkaev55 commented 4 months ago

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:

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.