R/staticimports.R

Defines functions str_squish str_replace_all str_replace str_detect regex `%||%`

# Generated by staticimports; do not edit by hand.
# ======================================================================
# Imported from pkg:staticimports
# ======================================================================

`%||%` <- function(a, b) {
  if (is.null(a)) b else a
}
# Generated by staticimports; do not edit by hand.
# ======================================================================
# Imported from pkg:stringstatic
# ======================================================================

regex <- function(
	pattern,
	ignore_case = FALSE,
	multiline = FALSE,
	comments = FALSE,
	dotall = FALSE
) {
	options <- paste(
		if (isTRUE(ignore_case)) "i",
		if (isTRUE(multiline)) "m",
		if (isTRUE(dotall)) "s",
		if (isTRUE(comments)) "x",
		sep = ""
	)

	if (nzchar(options)) {
		if (!is.null(names(pattern))) {
			names(pattern) <- paste0("(?", options, ")", names(pattern))
		} else {
			pattern <- paste0("(?", options, ")", pattern)
		}
	}

	structure(pattern, class = c("stringr_regex", "stringr_pattern", "character"))
}

str_detect <- function(string, pattern, negate = FALSE) {
	if (length(string) == 0 || length(pattern) == 0) return(logical(0))

	is_fixed <- inherits(pattern, "stringr_fixed")

	indices <- Vectorize(grep, c("pattern", "x"), USE.NAMES = FALSE)(
		pattern,
		x = string,
		perl = !is_fixed,
		fixed = is_fixed,
		invert = negate
	)

	result <- as.logical(lengths(indices))
	result[is.na(string)] <- NA
	result
}

str_replace <- function(string, pattern, replacement) {
	if (length(string) == 0 || length(pattern) == 0 || length(replacement) == 0) {
		return(character(0))
	}

	is_fixed <- inherits(pattern, "stringr_fixed")

	Vectorize(sub, c("pattern", "replacement", "x"), USE.NAMES = FALSE)(
		pattern, replacement, x = string, perl = !is_fixed, fixed = is_fixed
	)
}

str_replace_all <- function(string, pattern, replacement) {
	is_fixed <- inherits(pattern, "stringr_fixed")

	if (!is.null(names(pattern))) {
		for (i in seq_along(pattern)) {
			string <- gsub(
				pattern = names(pattern)[[i]],
				replacement = pattern[[i]],
				x = string,
				perl = !is_fixed,
				fixed = is_fixed
			)
		}
		return(string)
	}

	if (length(string) == 0 || length(pattern) == 0 || length(replacement) == 0) {
		return(character(0))
	}

	Vectorize(gsub, c("pattern", "replacement", "x"), USE.NAMES = FALSE)(
		pattern, replacement, x = string, perl = !is_fixed, fixed = is_fixed
	)
}

str_squish <- function(string) {
	string <- sub("^\\s+", "", string, perl = TRUE)
	string <- sub("\\s+$", "", string, perl = TRUE)
	gsub("\\s+", " ", string, perl = TRUE)
}

Try the plu package in your browser

Any scripts or data that you put into this service are public.

plu documentation built on Sept. 24, 2023, 1:08 a.m.