diff --git a/NEWS.md b/NEWS.md index 697c7c71..b83777e5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/dup.R b/R/dup.R index b8a85edd..93a5a9ef 100644 --- a/R/dup.R +++ b/R/dup.R @@ -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) diff --git a/tests/testthat/_snaps/sub.md b/tests/testthat/_snaps/sub.md index 4b76a065..056a63b0 100644 --- a/tests/testthat/_snaps/sub.md +++ b/tests/testthat/_snaps/sub.md @@ -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). diff --git a/tests/testthat/test-dup.R b/tests/testthat/test-dup.R index a0d00b97..f8558a45 100644 --- a/tests/testthat/test-dup.R +++ b/tests/testthat/test-dup.R @@ -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", {