Skip to content

Commit

Permalink
Use stringi for regex when available. (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonthegeek authored Sep 7, 2023
1 parent 0449de3 commit e0239e2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Imports:
rlang (>= 1.1.0),
vctrs
Suggests:
stringi,
testthat (>= 3.0.0)
Config/testthat/edition: 3
Config/testthat/parallel: true
Expand Down
12 changes: 10 additions & 2 deletions R/stabilize_chr.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#' @inheritParams .coerce-params
#' @inheritParams to_chr
#' @param regex Character scalar. An optional regex pattern to compare the
#' value(s) of `x` against.
#' value(s) of `x` against. If a complex regex pattern throws an error, try
#' installing the stringi package with `install.packages("stringi")`.
#'
#' @return The argument as a character vector.
#' @export
Expand Down Expand Up @@ -56,7 +57,7 @@ stabilize_chr <- function(x,
return(invisible(NULL))
}
regex <- to_chr_scalar(regex, call = call)
success <- grepl(regex, x)
success <- .has_regex_pattern(x, regex)
if (all(success)) {
return(invisible(NULL))
}
Expand All @@ -71,3 +72,10 @@ stabilize_chr <- function(x,
call = call
)
}

.has_regex_pattern <- function(x, regex) {
if (requireNamespace("stringi", quietly = TRUE)) {
return(stringi::stri_detect_regex(x, regex))
}
return(grepl(regex, x))
}
3 changes: 2 additions & 1 deletion man/stabilize_chr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/stabilize_chr_scalar.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/_snaps/stabilize_chr.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@
x Some values do not match.
* Locations: 1

# stabilize_chr() works with complex url regex

Code
stabilize_chr("example.com", regex = url_regex)
Output
[1] "example.com"

10 changes: 10 additions & 0 deletions tests/testthat/test-stabilize_chr.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ test_that("stabilize_chr() checks values", {
error = TRUE
)
})

test_that("stabilize_chr() works with complex url regex", {
url_regex <- r"(^(?:(?:(?:https?|ftp):)?\/\/)?(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$)"
expect_snapshot(
stabilize_chr(
"example.com",
regex = url_regex
)
)
})

0 comments on commit e0239e2

Please sign in to comment.