Skip to content

Commit

Permalink
misc: support fixed regexes in str_detect() and grepl()
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Nov 24, 2023
1 parent 6708003 commit eb69c26
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

* `rename()` now accepts unquoted names both old and new names.

* Support fixed regexes in `str_detect()` (using `fixed()`) and in `grepl()`
(using `fixed = TRUE`).

**Bug fixes**

* Improve robustness of sequential expressions in `mutate()` and `summarize()`
Expand Down
11 changes: 7 additions & 4 deletions R/funs-string.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@

pl_str_detect <- function(string, pattern, negate = FALSE, ...) {
check_empty_dots(...)
out <- string$str$contains(pattern)
is_fixed <- isTRUE(attr(pattern, "stringr_attr") == "fixed")
out <- string$str$contains(pattern, literal = is_fixed)
if (isTRUE(negate)) {
out$is_not()
} else {
out
}
}

pl_grepl <- function(pattern, x, ...) {
check_empty_dots(...)
pl_str_detect(string = x, pattern = pattern)
pl_grepl <- function(pattern, x, fixed = FALSE, ...) {
if (isTRUE(fixed)) {
attr(x, "stringr_attr") <- "fixed"
}
pl_str_detect(string = x, pattern = pattern, ...)
}

pl_str_count_matches <- function(string, pattern = "", ...) {
Expand Down
14 changes: 14 additions & 0 deletions tests/tinytest/test_funs_string.R
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,20 @@ expect_equal(
pull(foo)
)

expect_equal(
mutate(test, foo = str_detect(x5, fixed("."))) |>
pull(foo),
mutate(test_df, foo = str_detect(x5, fixed("."))) |>
pull(foo)
)

expect_equal(
mutate(test, foo = grepl(".", x5, fixed = TRUE)) |>
pull(foo),
mutate(test_df, foo = grepl(".", x5, fixed = TRUE)) |>
pull(foo)
)

expect_warning(
mutate(test, foo = grepl("e", x1, ignore.case = TRUE)),
"will not be used"
Expand Down
14 changes: 14 additions & 0 deletions tests/tinytest/test_funs_string_lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,20 @@ expect_equal_lazy(
pull(foo)
)

expect_equal_lazy(
mutate(test, foo = str_detect(x5, fixed("."))) |>
pull(foo),
mutate(test_df, foo = str_detect(x5, fixed("."))) |>
pull(foo)
)

expect_equal_lazy(
mutate(test, foo = grepl(".", x5, fixed = TRUE)) |>
pull(foo),
mutate(test_df, foo = grepl(".", x5, fixed = TRUE)) |>
pull(foo)
)

expect_warning(
mutate(test, foo = grepl("e", x1, ignore.case = TRUE)),
"will not be used"
Expand Down

0 comments on commit eb69c26

Please sign in to comment.