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 c853852
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion processor/formatters.go
Original file line number Diff line number Diff line change
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
9 changes: 9 additions & 0 deletions processor/formatters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,15 @@ func TestUnicodeAwareTrimAscii(t *testing.T) {
}
}

func TestUnicodeAwareTrimExactSizeAscii(t *testing.T) {
tmp := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.md"
size := len(tmp)
res := unicodeAwareTrim(tmp, size)
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 c853852

Please sign in to comment.