Skip to content

Commit

Permalink
Fix bug in str_trunc() (#515)
Browse files Browse the repository at this point in the history
Fixes #512
  • Loading branch information
UchidaMizuki authored Aug 5, 2023
1 parent 52923bf commit 08ff36f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# stringr (development version)

* `str_trunc()` now correctly truncates strings when `side` is `"left"` or
`"center"` (@UchidaMizuki, #512).

# stringr 1.5.0

## Breaking changes
Expand Down
7 changes: 4 additions & 3 deletions R/trunc.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ str_trunc <- function(string, width, side = c("right", "left", "center"),
side <- arg_match(side)
check_string(ellipsis)

too_long <- !is.na(string) & str_length(string) > width
len <- str_length(string)
too_long <- !is.na(string) & len > width
width... <- width - str_length(ellipsis)

if (width... < 0) {
Expand All @@ -34,11 +35,11 @@ str_trunc <- function(string, width, side = c("right", "left", "center"),

string[too_long] <- switch(side,
right = str_c(str_sub(string[too_long], 1, width...), ellipsis),
left = str_c(ellipsis, str_sub(string[too_long], -width..., -1)),
left = str_c(ellipsis, str_sub(string[too_long], len[too_long] - width... + 1, -1)),
center = str_c(
str_sub(string[too_long], 1, ceiling(width... / 2)),
ellipsis,
str_sub(string[too_long], -floor(width... / 2), -1)
str_sub(string[too_long], len[too_long] - floor(width... / 2) + 1, -1)
)
)
string
Expand Down
41 changes: 36 additions & 5 deletions tests/testthat/test-trunc.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,27 @@ test_that("truncations work for all elements of a vector", {

test_that("truncations work for all sides", {

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

expect_equal(trunc("right"), "This string is mo...")
expect_equal(trunc("left"), "...s moderately long")
expect_equal(trunc("center"), "This stri...ely long")
expect_equal(trunc("right", 20), "This string is mo...")
expect_equal(trunc("left", 20), "...s moderately long")
expect_equal(trunc("center", 20), "This stri...ely long")

expect_equal(trunc("right", 3), "...")
expect_equal(trunc("left", 3), "...")
expect_equal(trunc("center", 3), "...")

expect_equal(trunc("right", 4), "T...")
expect_equal(trunc("left", 4), "...g")
expect_equal(trunc("center", 4), "T...")

expect_equal(trunc("right", 5), "Th...")
expect_equal(trunc("left", 5), "...ng")
expect_equal(trunc("center", 5), "T...g")
})

test_that("does not truncate to a length shorter than elipsis", {
Expand All @@ -35,3 +47,22 @@ test_that("does not truncate to a length shorter than elipsis", {
str_trunc("foobar", 3, ellipsis = "....")
})
})

test_that("str_trunc correctly snips rhs-of-ellipsis for truncated strings", {
trunc <- function(width, side) {
str_trunc(c("", "a", "aa", "aaa", "aaaa", "aaaaaaa"), width, side,
ellipsis = "..")
}

expect_equal(trunc(4, "right"), c("", "a", "aa", "aaa", "aaaa", "aa.."))
expect_equal(trunc(4, "left"), c("", "a", "aa", "aaa", "aaaa", "..aa"))
expect_equal(trunc(4, "center"), c("", "a", "aa", "aaa", "aaaa", "a..a"))

expect_equal(trunc(3, "right"), c("", "a", "aa", "aaa", "a..", "a.."))
expect_equal(trunc(3, "left"), c("", "a", "aa", "aaa", "..a", "..a"))
expect_equal(trunc(3, "center"), c("", "a", "aa", "aaa", "a..", "a.."))

expect_equal(trunc(2, "right"), c("", "a", "aa", "..", "..", ".."))
expect_equal(trunc(2, "left"), c("", "a", "aa", "..", "..", ".."))
expect_equal(trunc(2, "center"), c("", "a", "aa", "..", "..", ".."))
})

0 comments on commit 08ff36f

Please sign in to comment.