Skip to content

Commit

Permalink
regexpre-style
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangchuangYu committed Dec 13, 2023
1 parent a2a880c commit 3803387
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: yulab.utils
Title: Supporting Functions for Packages Maintained by 'YuLab-SMU'
Version: 0.1.1.001
Version: 0.1.1.002
Authors@R: c(person("Guangchuang", "Yu", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6485-8781")))
Description: Miscellaneous functions commonly used by 'YuLab-SMU'.
Imports:
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(print,exec)
export(Biocpkg)
export(CRANpkg)
export(Githubpkg)
export(auto_set_regexpr_style)
export(baidu_translate)
export(check_pkg)
export(cn2en)
Expand All @@ -26,10 +27,14 @@ export(rbindlist)
export(read.cb)
export(scale_range)
export(scihub_dl)
export(set_PCRE)
export(set_TRE)
export(set_regexpr_style)
export(show_in_excel)
export(str_ends)
export(str_starts)
export(str_wrap)
export(use_perl)
export(yread)
export(yread_tsv)
importFrom(cli,cli_h2)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

# yulab.utils 0.1.1.001
# yulab.utils 0.1.1.002

+ tools to switch from PCRE or TRE in regular expression (2023-12-13, Wed)
+ `cn2en()` (2023-12-12, Tue)

# yulab.utils 0.1.1
Expand Down
68 changes: 68 additions & 0 deletions R/regexpr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
##' @rdname regexpr-style
##' @export
set_PCRE <- function() {
options(regexpr_use_perl = TRUE)
}

##' @rdname regexpr-style
##' @export
set_TRE <- function() {
options(regexpr_use_perl = FALSE)
}

##' @rdname regexpr-style
##' @export
use_perl <- function() {
res <- getOption("regexpr_use_perl", default = auto_set_regexpr_style())
return(res)
}


##' switch regular expression style (PCRE vs TRE)
##'
##' The `set_regexpr_style()` allows user to specify which style to be used,
##' while the `auto_set_regexpr_style()` automatically set the style depdending on
##' the operating system (TRE for Windows and PCRE for other OSs (Linux and Mac)).
##'
##' `set_PCRE()` force to use PCRE style while `set_TRE()` force to use TRE.
##'
##' Note that all these functions are not change the behavior of `gsub()` and `regexpr()`.
##' The functions are just set a global option to store the user's choice of whether using `perl = TRUE`.
##'
##' Users can access the option via `use_perl()` and pass the return value to `gusb()` or `regexpr()` to specify the style in use.
##'
##' @rdname regexpr-style
##' @param style one of 'PCRE' or 'TRE'
##' @return logical value of whether use perl
##' @reference <https://stackoverflow.com/questions/47240375/regular-expressions-in-base-r-perl-true-vs-the-default-pcre-vs-tre>
##' @export
##' @author Guangchuang Yu
set_regexpr_style <- function(style) {
if (missing(style)) {
message("style is not specific, set automatically.")
auto_set_regexpr_style()
} else {
style <- match.arg(style, c("PCRE", "TRE"))
if (style == "PCRE") {
set_PCRE()
} else {
set_TRE()
}
}
res <- getOption("regexpr_use_perl")
invisible(res)
}

##' @rdname regexpr-style
##' @export
auto_set_regexpr_style <- function() {
os <- Sys.info()[1]
if (os == "Windows") {
set_TRE()
res <- FALSE
} else {
set_PCRE()
res <- TRUE
}
invisible(res)
}
42 changes: 42 additions & 0 deletions man/regexpr-style.Rd

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

0 comments on commit 3803387

Please sign in to comment.