Skip to content

Commit

Permalink
Merge branch 'main' into str_dup_sep
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored Aug 20, 2024
2 parents 7d802a1 + b511603 commit fd9c8b7
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 7 deletions.
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

* 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).

* `str_*` now errors if `pattern` includes any `NA`s (@nash-delcamp-slp, #546).
* `str_view()` now displays a message when called with a zero-length character
vector (@LouisMPenrod, #497).
* Adds `[[.stringr_pattern` method to go along with existing `[.stringr_pattern`
method (@edward-burn, #569).

* In `str_replace_all()`, a `replacement` function now receives all values in
a single vector. This radically improves performance at the cost of breaking
some existing uses (#462).
Expand Down
9 changes: 8 additions & 1 deletion R/modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ type.stringr_fixed <- function(x, error_call = caller_env()) {
}
#' @export
type.character <- function(x, error_call = caller_env()) {
if (any(is.na(x))) {
cli::cli_abort(
tr_("{.arg pattern} must be a character vector that does not contain NAs."),
call = error_call
)
}

if (identical(x, "")) "empty" else "regex"
}

Expand All @@ -213,7 +220,7 @@ type.default <- function(x, error_call = caller_env()) {
}

cli::cli_abort(
tr_("{.arg pattern} must be a string, not {.obj_type_friendly {x}}."),
tr_("{.arg pattern} must be a character vector, not {.obj_type_friendly {x}}."),
call = error_call
)
}
Expand Down
1 change: 1 addition & 0 deletions R/view.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ print.stringr_view <- function(x, ..., n = getOption("stringr.view_n", 20)) {
}

if (length(x) == 0) {
cli::cli_inform(c(x = "Empty `string` provided.\n"))
return(invisible(x))
}

Expand Down
15 changes: 14 additions & 1 deletion tests/testthat/_snaps/modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,18 @@
type(1:3)
Condition
Error:
! `pattern` must be a string, not an integer vector.
! `pattern` must be a character vector, not an integer vector.

# useful errors for NAs

Code
type(NA)
Condition
Error:
! `pattern` must be a character vector, not `NA`.
Code
type(c("a", "b", NA_character_, "c"))
Condition
Error:
! `pattern` must be a character vector that does not contain NAs.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/split.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
str_split("x", 1)
Condition
Error in `str_split()`:
! `pattern` must be a string, not a number.
! `pattern` must be a character vector, not a number.
Code
str_split("x", "x", n = 0)
Condition
Expand Down
4 changes: 3 additions & 1 deletion tests/testthat/_snaps/view.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@
[3] | \n
[4] | \t

# view displays nothing for empty vectors
# view displays message for empty vectors

Code
str_view(character())
Message
x Empty `string` provided.

# can match across lines

Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ test_that("subsetting preserves class and options", {
expect_equal(x[], x)
})

test_that("useful errors for NAs", {
expect_snapshot(error = TRUE, {
type(NA)
type(c("a", "b", NA_character_, "c"))
})
})

test_that("stringr_pattern methods", {
ex <- coll(c("foo", "bar"))
expect_true(inherits(ex[1], "stringr_pattern"))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-view.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test_that("view highlights whitespace (except a space/nl)", {
})
})

test_that("view displays nothing for empty vectors",{
test_that("view displays message for empty vectors",{
expect_snapshot(str_view(character()))
})

Expand Down

0 comments on commit fd9c8b7

Please sign in to comment.