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

str_trunc() incorrectly snips rhs-of-ellipsis for truncated strings #512

Closed
eauleaf opened this issue Jun 4, 2023 · 1 comment · Fixed by #515
Closed

str_trunc() incorrectly snips rhs-of-ellipsis for truncated strings #512

eauleaf opened this issue Jun 4, 2023 · 1 comment · Fixed by #515
Labels
bug an unexpected problem or unintended behavior

Comments

@eauleaf
Copy link

eauleaf commented Jun 4, 2023

str_trunc with param side = 'left' or side = 'center' returns incorrect result when ellipsis is the the same size or one larger than width. The rhs of the ellipsis gets appended without trunctation.

datr <- c('', 'a', 'aa', 'aaa', 'aaaa', 'aaaaaaa')

stringr::str_trunc(datr, width=4, side = 'left',   ellipsis = "..") # correct
#> [1] ""     "a"    "aa"   "aaa"  "aaaa" "..aa"
stringr::str_trunc(datr, width=4, side = 'right',  ellipsis = "..") # correct
#> [1] ""     "a"    "aa"   "aaa"  "aaaa" "aa.."
stringr::str_trunc(datr, width=4, side = 'center', ellipsis = "..") # correct
#> [1] ""     "a"    "aa"   "aaa"  "aaaa" "a..a"
stringr::str_trunc(datr, width=3, side = 'left',   ellipsis = "..") # correct
#> [1] ""    "a"   "aa"  "aaa" "..a" "..a"
stringr::str_trunc(datr, width=3, side = 'right',  ellipsis = "..") # correct
#> [1] ""    "a"   "aa"  "aaa" "a.." "a.."
stringr::str_trunc(datr, width=3, side = 'center', ellipsis = "..") # should be c("","a","aa","aaa","a..","a..")
#> [1] ""           "a"          "aa"         "aaa"        "a..aaaa"      "a..aaaaaaa"
stringr::str_trunc(datr, width=2, side = 'left',   ellipsis = "..") # should be c("","a","aa","..","..","..")
#> [1] ""          "a"         "aa"        "..aaa"     "..aaaa"    "..aaaaaaa"
stringr::str_trunc(datr, width=2, side = 'right',  ellipsis = "..") # correct
#> [1] ""   "a"  "aa" ".." ".." ".."
stringr::str_trunc(datr, width=2, side = 'center', ellipsis = "..") # should be c("","a","aa","..","..","..")
#> [1] ""          "a"         "aa"        "..aaa"     "..aaaa"    "..aaaaaaa"
stringr::str_trunc(datr, width=1, side = 'left',   ellipsis = "..") # correct
#> Error in `stringr::str_trunc()`:
stringr::str_trunc(datr, width=1, side = 'right',  ellipsis = "..") # correct
#> Error in `stringr::str_trunc()`:
stringr::str_trunc(datr, width=1, side = 'center', ellipsis = "..") # correct
#> Error in `stringr::str_trunc()`:

stringr::str_trunc(datr, width=4, side = 'left',   ellipsis = "~") # correct
#> [1] ""     "a"    "aa"   "aaa"  "aaaa" "~aaa"
stringr::str_trunc(datr, width=4, side = 'right',  ellipsis = "~") # correct
#> [1] ""     "a"    "aa"   "aaa"  "aaaa" "aaa~"
stringr::str_trunc(datr, width=4, side = 'center', ellipsis = "~") # correct
#> [1] ""     "a"    "aa"   "aaa"  "aaaa" "aa~a"
stringr::str_trunc(datr, width=3, side = 'left',   ellipsis = "~") # correct
#> [1] ""    "a"   "aa"  "aaa" "~aa" "~aa"
stringr::str_trunc(datr, width=3, side = 'right',  ellipsis = "~") # correct
#> [1] ""    "a"   "aa"  "aaa" "aa~" "aa~"
stringr::str_trunc(datr, width=3, side = 'center', ellipsis = "~") # correct
#> [1] ""    "a"   "aa"  "aaa" "a~a" "a~a"
stringr::str_trunc(datr, width=2, side = 'left',   ellipsis = "~") # correct
#> [1] ""   "a"  "aa" "~a" "~a" "~a"
stringr::str_trunc(datr, width=2, side = 'right',  ellipsis = "~") # correct
#> [1] ""   "a"  "aa" "a~" "a~" "a~"
stringr::str_trunc(datr, width=2, side = 'center', ellipsis = "~") # should be c("","a","aa","a~","a~","a~")
#> [1] ""          "a"         "aa"        "a~aaa"     "a~aaaa"    "a~aaaaaaa"
stringr::str_trunc(datr, width=1, side = 'left',   ellipsis = "~") # should be c("","a","~","~","~","~")
#> [1] ""         "a"        "~aa"      "~aaa"     "~aaaa"    "~aaaaaaa"
stringr::str_trunc(datr, width=1, side = 'right',  ellipsis = "~") # correct
#> [1] ""  "a" "~" "~" "~" "~"
stringr::str_trunc(datr, width=1, side = 'center', ellipsis = "~") # should be c("","a","~","~","~","~")
#> [1] ""         "a"        "~aa"      "~aaa"     "~aaaa"    "~aaaaaaa"
stringr::str_trunc(datr, width=0, side = 'left',   ellipsis = "~") # correct
#> Error in `stringr::str_trunc()`:
stringr::str_trunc(datr, width=0, side = 'right',  ellipsis = "~") # correct
#> Error in `stringr::str_trunc()`:
stringr::str_trunc(datr, width=0, side = 'center', ellipsis = "~") # correct
#> Error in `stringr::str_trunc()`:

stringr::str_trunc(datr, width=3, side = 'left',   ellipsis = "") # correct
#> [1] ""    "a"   "aa"  "aaa" "aaa" "aaa"
stringr::str_trunc(datr, width=3, side = 'right',  ellipsis = "") # correct 
#> [1] ""    "a"   "aa"  "aaa" "aaa" "aaa"
stringr::str_trunc(datr, width=3, side = 'center', ellipsis = "") # correct 
#> [1] ""    "a"   "aa"  "aaa" "aaa" "aaa"
stringr::str_trunc(datr, width=2, side = 'left',   ellipsis = "") # correct
#> [1] ""   "a"  "aa" "aa" "aa" "aa"
stringr::str_trunc(datr, width=2, side = 'right',  ellipsis = "") # correct 
#> [1] ""   "a"  "aa" "aa" "aa" "aa"
stringr::str_trunc(datr, width=2, side = 'center', ellipsis = "") # correct 
#> [1] ""   "a"  "aa" "aa" "aa" "aa"
stringr::str_trunc(datr, width=1, side = 'left',   ellipsis = "") # correct
#> [1] ""  "a" "a" "a" "a" "a"
stringr::str_trunc(datr, width=1, side = 'right',  ellipsis = "") # correct 
#> [1] ""  "a" "a" "a" "a" "a"
stringr::str_trunc(datr, width=1, side = 'center', ellipsis = "") # should be c("","a","a","a","a","a")
#> [1] ""         "a"        "aaa"      "aaaa"     "aaaaa"    "aaaaaaaa"
stringr::str_trunc(datr, width=0, side = 'left',   ellipsis = "") # should be c("","","","","","")
#> [1] ""        "a"       "aa"      "aaa"     "aaaa"    "aaaaaaa"
stringr::str_trunc(datr, width=0, side = 'right',  ellipsis = "") # correct
#> [1] "" "" "" "" "" ""
stringr::str_trunc(datr, width=0, side = 'center', ellipsis = "") # should be c("","","","","","")
#> [1] ""        "a"       "aa"      "aaa"     "aaaa"    "aaaaaaa"

Created on 2023-06-03 with reprex v2.0.2.9000

@eauleaf
Copy link
Author

eauleaf commented Jun 7, 2023

I submitted a pull request for str_trunc and an additional unit test.

Below is the description of the issue in str_trunc().

  • when nchar(width) is 1 more than nchar(ellipsis), side 'center' fails to return correct result
  • when nchar(width) is == to nchar(ellipsis), sides 'center' and 'left' fail to return correct result

Both cases happen because:
str_sub(string[too_long], -width..., -1))
becomes:
str_sub('characters', 0, -1))
which returns all 'characters' rather than the correct number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants