Skip to content

Commit

Permalink
updates after review
Browse files Browse the repository at this point in the history
  • Loading branch information
edward-burn committed Aug 15, 2024
1 parent a4b45b8 commit efefb58
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# stringr (development version)

* Add sep argument to `str_dup()` so that it is possible to repeat a string and
add a separator between every repeated value. (@edward-burn, #564).
* Add `sep` argument to `str_dup()` so that it is possible to repeat a string and
add a separator between every repeated value (@edward-burn, #564).

* In `str_replace_all()`, a `replacement` function now receives all values in
a single vector. This radically improves performance at the cost of breaking
Expand Down
26 changes: 14 additions & 12 deletions R/dup.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@
#' str_dup(fruit, 1:3)
#' str_c("ba", str_dup("na", 0:5))
str_dup <- function(string, times, sep = NULL) {
vctrs::vec_size_common(string = string, times = times)
if(is.null(sep)){
size <- vctrs::vec_size_common(string = string, times = times)
if (is.null(sep)) {
stri_dup(string, times)
} else {
# stri_dup does not currently support sep
check_string(sep)
lapply(seq_along(string), function(i) {
if (length(times) == 1) {
paste(rep(string[i], times), collapse = sep)
} else {
paste(rep(string[i], times[i]), collapse = sep)
}
}) %>%
rlang::flatten_chr()
# stri_dup does not currently support sep
check_string(sep)
lapply(seq_along(string), function(i) {
if (size == 1) {
paste(rep(string[i], times), collapse = sep)
} else {
paste(rep(string[i], times[i]), collapse = sep)
}
}) %>%
rlang::flatten_chr()
}

}


13 changes: 6 additions & 7 deletions tests/testthat/test-dup.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ test_that("uses tidyverse recycling rules", {
})

test_that("uses sep argument", {
expect_equal(str_dup("a", 3, sep = ";"), "a;a;a")
expect_equal(str_dup("abc", 2, sep = "-"), "abc-abc")
expect_equal(str_dup("a", 3, sep = ";"), "a;a;a")
expect_equal(str_dup("abc", 2, sep = "-"), "abc-abc")
expect_equal(str_dup(c("a", "b"), 2, sep = "x"), c("axa", "bxb"))
expect_equal(str_dup(c("aa", "bb", "ccc"), c(2, 3, 4), sep = ";"),
c("aa;aa", "bb;bb;bb", "ccc;ccc;ccc;ccc"))

expect_error(str_dup("a", 3, sep = 1))
expect_error(str_dup("a", 3, sep = c("-", ";")))
expect_error(str_dup(c("aa", "bb"), c(2, 3, 4), sep = ";"))
expect_error(str_dup(c("aa", "bb", "cc"), c(2, 3), sep = ";"))
expect_identical(character(), str_dup(character(), 1, sep = ":"))

expect_snapshot(str_dup("a", 3, sep = 1), error = TRUE)
expect_snapshot(str_dup("a", 3, sep = c("-", ";")), error = TRUE)
expect_snapshot(str_dup(c("aa", "bb"), c(2, 3, 4), sep = ";"), error = TRUE)
expect_snapshot(str_dup(c("aa", "bb", "cc"), c(2, 3), sep = ";"), error = TRUE)

})

0 comments on commit efefb58

Please sign in to comment.