Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_dup()` now recycles `string` to the length of `times` when `sep` is
supplied, instead of failing with a subscript out of bounds error.

# stringr 1.6.0

## Breaking changes
Expand Down
2 changes: 1 addition & 1 deletion R/dup.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ str_dup <- function(string, times, sep = NULL) {
out <- stri_dup(input$string, input$times)
} else {
out <- map_chr(seq_along(input$string), function(i) {
paste(rep(string[[i]], input$times[[i]]), collapse = sep)
paste(rep(input$string[[i]], input$times[[i]]), collapse = sep)
})
}
names(out) <- names(input$string)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/sub.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
str_sub(x, 1:2, 1:3)
Condition
Error in `str_sub()`:
! Can't recycle `string` (size 2) to match `end` (size 3).
! Can't recycle `start` (size 2) to match `end` (size 3).
Code
str_sub(x, 1:2, 1:2) <- 1:3
Condition
Error in `str_sub<-`:
! Can't recycle `string` (size 2) to match `value` (size 3).
! Can't recycle `start` (size 2) to match `value` (size 3).

5 changes: 5 additions & 0 deletions tests/testthat/test-dup.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ test_that("uses sep argument", {

expect_equal(str_dup(character(), 1, sep = "-"), character())
expect_equal(str_dup(character(), 2, sep = "-"), character())

expect_equal(
str_dup("na", 0:5, sep = "-"),
c("", "na", "na-na", "na-na-na", "na-na-na-na", "na-na-na-na-na")
)
})

test_that("separator must be a single string", {
Expand Down
Loading