Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define [[.stringr_pattern #569

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))

})
Loading