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

Fix bug in str_trunc() #494

Closed
wants to merge 2 commits into from

Conversation

UchidaMizuki
Copy link
Contributor

I found a bug in str_trunc() where strings are not properly truncated when width is small, as shown below.
The bug seems to be caused by str_sub("abc", 0, -1) outputting "abc" instead of "".
I have fixed the bug by changing the behaviour of getting the tail of the strings.

library(stringr)

trunc <- function(direction, width) str_trunc(
  "This string is moderately long",
  direction,
  width = width
)

trunc("left", 3)
#> [1] "...This string is moderately long"
trunc("center", 3)
#> [1] "...This string is moderately long"
trunc("center", 4)
#> [1] "T...This string is moderately long"

str_sub("abc", 0, -1)
#> [1] "abc"
# => Not equal to ""

Created on 2023-01-14 with reprex v2.0.2

@UchidaMizuki
Copy link
Contributor Author

Close this pull request temporarily as I'm starting to think that stringi::stri_sub() might be misbehaving.

@UchidaMizuki
Copy link
Contributor Author

Furthermore, the use of ifelse() is not considered appropriate because it is not vectorized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant