Skip to content

Commit

Permalink
define [[.stringr_pattern
Browse files Browse the repository at this point in the history
closes #529

copy of [.stringr_pattern with simple test added for both
  • Loading branch information
edward-burn committed Aug 15, 2024
1 parent 83a72a1 commit 48c9c0b
Show file tree
Hide file tree
Showing 3 changed files with 18 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
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
8 changes: 8 additions & 0 deletions tests/testthat/test-modifiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ 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 48c9c0b

Please sign in to comment.