Skip to content

Commit

Permalink
Adds a message when an empty string is provided (#568)
Browse files Browse the repository at this point in the history
Fixes #497

Co-authored-by: Hadley Wickham <[email protected]>
  • Loading branch information
LouisMPenrod and hadley authored Aug 20, 2024
1 parent 12839fc commit 1562054
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# stringr (development version)

* `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
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
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
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 1562054

Please sign in to comment.