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 behaves unexpectedly when width is shorter than elipsis #163

Closed
ClaytonJY opened this issue May 9, 2017 · 2 comments
Closed
Labels
bug an unexpected problem or unintended behavior wip work in progress

Comments

@ClaytonJY
Copy link
Contributor

For example

> sapply(3:1, function(x) str_trunc("blahblahblah", x))
[1] "..."             "blahblahblah..." "blahblahbla..." 
> sapply(5:1, function(x) str_trunc("blahblahblah", x))
[1] "bl..."           "b..."            "..."             "blahblahblah..." "blahblahbla..." 
> sapply(5:1, function(x) str_trunc("blahblahblah", x, side = "left"))
[1] "...ah"           "...h"            "...blahblahblah" "...blahblahblah" "...lahblahblah" 
> sapply(5:1, function(x) str_trunc("blahblahblah", x, side = "center"))
[1] "b...h"                       "b...blahblahblah"            "...blahblahblah"             "...blahblahblah"            
[5] "blahblahblah...blahblahblah"
> sapply(5:1, function(x) str_trunc("blahblahblah", x, ellipsis = ".."))
[1] "bla.."          "bl.."           "b.."            ".."             "blahblahblah.."
> sapply(5:1, function(x) str_trunc("blahblahblah", x, ellipsis = "...."))
[1] "b...."            "...."             "blahblahblah...." "blahblahbla...."  "blahblahbl...."  

I'm not sure what should happen when width < str_length(elipsis) (warning? error? NA?), but making the string longer can't be right.

Session Info

> devtools::session_info()
Session info ----------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.0 (2017-04-21)
 system   x86_64, linux-gnu           
 ui       RStudio (1.0.143)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/New_York            
 date     2017-05-09                  

Packages --------------------------------------------------------------------------------------------------------------------------------
 package  * version date       source        
 devtools   1.12.0  2016-12-05 CRAN (R 3.4.0)
 digest     0.6.12  2017-01-27 CRAN (R 3.4.0)
 magrittr * 1.5     2014-11-22 CRAN (R 3.4.0)
 memoise    1.1.0   2017-04-21 CRAN (R 3.4.0)
 stringi    1.1.5   2017-04-07 CRAN (R 3.4.0)
 stringr  * 1.2.0   2017-02-18 CRAN (R 3.4.0)
 withr      1.0.2   2016-06-20 CRAN (R 3.4.0)

(not dev, but this function hasn't changed in awhile)

@ClaytonJY
Copy link
Contributor Author

I'd be happy to fix this in a PR (maybe add on to #165), but I'm unsure what the right behavior is; truncate the elipsis? e.g.

> str_trunc("foobar", 2)
[1] ".."

@hadley
Copy link
Member

hadley commented Nov 14, 2017

I think an error would be most appropriate. A PR would be great!

@hadley hadley added feature a feature request or enhancement wip work in progress bug an unexpected problem or unintended behavior and removed feature a feature request or enhancement labels Nov 14, 2017
hadley pushed a commit that referenced this issue Nov 20, 2017
…194)

* Throw error when asked to truncate to length shorter than ellipsis.

* Update NEWS with fix for #163.
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 wip work in progress
Projects
None yet
Development

No branches or pull requests

2 participants