From 48c9c0b119d4524ffc14ff4e315d3777bb32778f Mon Sep 17 00:00:00 2001 From: edward-burn <9583964+edward-burn@users.noreply.github.com> Date: Thu, 15 Aug 2024 20:13:55 +0100 Subject: [PATCH] define [[.stringr_pattern closes #529 copy of [.stringr_pattern with simple test added for both --- NAMESPACE | 1 + R/modifiers.R | 9 +++++++++ tests/testthat/test-modifiers.R | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/NAMESPACE b/NAMESPACE index 07f842ab..3f1bffc3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ S3method("[",stringr_pattern) S3method("[",stringr_view) +S3method("[[",stringr_pattern) S3method(print,stringr_view) S3method(type,character) S3method(type,default) diff --git a/R/modifiers.R b/R/modifiers.R index d183ac0b..7783b807 100644 --- a/R/modifiers.R +++ b/R/modifiers.R @@ -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! diff --git a/tests/testthat/test-modifiers.R b/tests/testthat/test-modifiers.R index 34495494..6fe132e7 100644 --- a/tests/testthat/test-modifiers.R +++ b/tests/testthat/test-modifiers.R @@ -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")) + +}