From 5830d7156f41b312eeb55afd3d0e9a89b978a0de Mon Sep 17 00:00:00 2001 From: Maximilian Scholz Date: Tue, 8 Sep 2026 22:01:25 +0200 Subject: [PATCH 1/2] Index recycled string in str_dup() sep branch --- NEWS.md | 3 +++ R/dup.R | 2 +- tests/testthat/test-dup.R | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) 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/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", { From cc74fb30f45d8c8c2c393f70aa4a43071c388c3c Mon Sep 17 00:00:00 2001 From: Maximilian Scholz Date: Wed, 9 Sep 2026 11:10:35 +0200 Subject: [PATCH 2/2] Update sub snapshots for vctrs recycle message --- tests/testthat/_snaps/sub.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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).