Skip to content

Commit

Permalink
do not add a '~' when filename is not truncated
Browse files Browse the repository at this point in the history
This fixes an off-by-one when the filename length exactly matches our
size limit. As an example, today if the filename size limit is one
character and the filename is named "a", it will be incorrectly
formatted as "~a" despite no truncation occuring.
  • Loading branch information
elindsey committed Nov 3, 2023
1 parent 7a6bd11 commit a5e8b04
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion processor/formatters.go
Expand Up @@ -931,7 +931,7 @@ func unicodeAwareTrim(tmp string, size int) string {
// iterate all the runes so we can cut off correctly and get the correct length
r := []rune(tmp)

if len(r) >= size {
if len(r) > size {
for runewidth.StringWidth(tmp) > size {
// remove character one at a time till we get the length we want
tmp = string([]rune(tmp)[1:])
Expand Down
8 changes: 8 additions & 0 deletions processor/formatters_test.go
Expand Up @@ -1392,6 +1392,14 @@ func TestUnicodeAwareTrimAscii(t *testing.T) {
}
}

func TestUnicodeAwareTrimExactSizeAscii(t *testing.T) {
tmp := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.md"
res := unicodeAwareTrim(tmp, len(tmp))
if res != tmp {
t.Errorf("expected %s got %s", tmp, res)
}
}

func TestUnicodeAwareTrimUnicode(t *testing.T) {
tmp := "中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文中文.md"
res := unicodeAwareTrim(tmp, shortFormatFileTruncate)
Expand Down

0 comments on commit a5e8b04

Please sign in to comment.