CoRC / data /R /utils_stringr.R
introvoyz045's picture
Migrated from GitHub
efd3676 verified
Raw History Blame Contribute Delete
1.05 kB
#' String matching engines from stringr
#'
#' @details CoRC makes use of the string matching engines from the \pkg{stringr} package.
#'
#' The functions are described at \link[stringr]{modifiers}.
#' By default, matching is done with \code{coll}.
#'
#' @name regex
NULL
#' @export
#' @rdname regex
stringr::fixed
#' @export
#' @rdname regex
stringr::coll
#' @export
#' @rdname regex
stringr::regex
# checks if a stringr engine is applied
# this function depends on the version of the stringr package. If an old version is detected, this is overwritten in .onLoad
has_engine <- function(x) inherits(x, c("stringr_fixed", "stringr_coll", "stringr_regex"))
# apply an engine for a character vector
# do nothing if engine is already applied
apply_engine <- function(x, engine = coll) {
if (!has_engine(x))
x <- engine(x)
x
}
# Subsetting vectors with engine attribute (x[1]) usually clears all attributes
# This method prevents that
subset_engine <- function(x, i) {
attrs <- attributes(x)
x <- x[i]
attributes(x) <- attrs
x
}