Skip to content

Commit

Permalink
define [[.stringr_pattern (#569)
Browse files Browse the repository at this point in the history
Fixes #529
  • Loading branch information
edward-burn authored Aug 15, 2024
1 parent 83a72a1 commit 6f85a06
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method("[",stringr_pattern)
S3method("[",stringr_view)
S3method("[[",stringr_pattern)
S3method(print,stringr_view)
S3method(type,character)
S3method(type,default)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# stringr (development version)

* 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: 9 additions & 0 deletions R/modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ type.default <- function(x, error_call = caller_env()) {
)
}

#' @export
`[[.stringr_pattern` <- function(x, i) {
structure(
NextMethod(),
options = attr(x, "options"),
class = class(x)
)
}

as_bare_character <- function(x, call = caller_env()) {
if (is.character(x) && !is.object(x)) {
# All OK!
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 @@ -36,3 +36,10 @@ test_that("subsetting preserves class and options", {
x <- regex("a", multiline = TRUE)
expect_equal(x[], x)
})

test_that("stringr_pattern methods", {
ex <- coll(c("foo", "bar"))
expect_true(inherits(ex[1], "stringr_pattern"))
expect_true(inherits(ex[[1]], "stringr_pattern"))

})

0 comments on commit 6f85a06

Please sign in to comment.