Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace stringr functions with base R equivalents #1043

Merged
merged 14 commits into from
Sep 7, 2022
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ Imports:
rlang (>= 1.0.2),
sass (>= 0.4.1),
scales (>= 1.2.0),
stringr (>= 1.4.0),
tibble (>= 3.1.6),
tidyselect (>= 1.1.1)
Suggests:
Expand Down
16 changes: 9 additions & 7 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -3686,13 +3686,15 @@ fmt_markdown <- function(
markdown_to_xml(x)
},
default = function(x) {
vapply(
x,
FUN.VALUE = character(1),
USE.NAMES = FALSE,
commonmark::markdown_text
) %>%
stringr::str_replace("\n$", "")
sub(
"\n$", "",
vapply(
x,
FUN.VALUE = character(1),
USE.NAMES = FALSE,
commonmark::markdown_text
)
)
}
)
)
Expand Down
18 changes: 17 additions & 1 deletion R/info_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,22 @@ info_google_fonts <- function() {
"Work Sans"
)

to_title_case <- function(x) {

title_case_i <- function(y) {

s <- strsplit(y, " ")[[1]]

paste(
toupper(substring(s, 1,1)),
substring(s, 2),
sep = "", collapse = " "
)
}

vapply(x, FUN.VALUE = character(1), USE.NAMES = FALSE, FUN = title_case_i)
}

styles_summary <-
google_styles_tbl %>%
dplyr::mutate(weight = as.integer(weight)) %>%
Expand Down Expand Up @@ -662,7 +678,7 @@ info_google_fonts <- function() {
dplyr::filter(name %in% recommended) %>%
dplyr::left_join(styles_summary, by = "name") %>%
dplyr::mutate(
category = stringr::str_to_title(category) %>%
category = to_title_case(tolower(category)) %>%
tidy_gsub("_", " ") %>%
tidy_gsub("serif", "Serif")
) %>%
Expand Down
40 changes: 20 additions & 20 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ markdown_to_xml <- function(text) {
text
}

# TODO: Make XML versions of these

cmark_rules_xml <- list(

heading = function(x, process) {
Expand Down Expand Up @@ -672,11 +672,9 @@ cmark_rules_xml <- list(
},
html_inline = function(x, process) {

# TODO: make this work for XML

tag <- xml2::xml_text(x)

match <- stringr::str_match(tag, pattern = "^<(/?)([a-zA-Z0-9\\-]+)")
match <- str_get_match(tag, pattern = "^<(/?)([a-zA-Z0-9\\-]+)")

if (!is.na(match[1, 1])) {

Expand Down Expand Up @@ -767,7 +765,9 @@ cmark_rules_xml <- list(
)

cmark_rules_rtf <- list(

heading = function(x, process) {

heading_sizes <- c(36, 32, 28, 24, 20, 16)
fs <- heading_sizes[as.numeric(xml2::xml_attr(x, attr = "level"))]

Expand Down Expand Up @@ -833,7 +833,7 @@ cmark_rules_rtf <- list(

tag <- xml2::xml_text(x)

match <- stringr::str_match(tag, pattern = "^<(/?)([a-zA-Z0-9\\-]+)")
match <- str_get_match(tag, pattern = "^<(/?)([a-zA-Z0-9\\-]+)")

if (!is.na(match[1, 1])) {

Expand Down Expand Up @@ -1419,16 +1419,16 @@ get_css_tbl <- function(data) {
css_tbl,
dplyr::tibble(
selector = rep(
stringr::str_remove(raw_css_vec[ruleset_start[i]], "\\s*\\{\\s*$"),
str_single_replace(raw_css_vec[ruleset_start[i]], "\\s*\\{\\s*$", ""),
(ruleset_end[i] - ruleset_start[i] - 1)),
property = raw_css_vec[(ruleset_start[i] + 1):(ruleset_end[i] - 1)] %>%
stringr::str_extract("[a-zA-z-]*?(?=:)") %>%
stringr::str_trim(),
str_single_extract("[a-zA-z-]*?(?=:)") %>%
str_trim_sides(),
value = raw_css_vec[(ruleset_start[i] + 1):(ruleset_end[i] - 1)] %>%
stringr::str_extract("(?<=:).*") %>%
stringr::str_remove(pattern = ";\\s*") %>%
stringr::str_remove(pattern = "\\/\\*.*") %>%
stringr::str_trim()
str_single_extract("(?<=:).*") %>%
str_single_replace(pattern = ";\\s*", "") %>%
str_single_replace(pattern = "\\/\\*.*", "") %>%
str_trim_sides()
) %>%
dplyr::filter(!is.na(property))
)
Expand All @@ -1441,8 +1441,8 @@ get_css_tbl <- function(data) {
dplyr::mutate(
css_tbl,
type = dplyr::case_when(
stringr::str_detect(selector, "^\\.") ~ "class",
!stringr::str_detect(selector, "^\\.") ~ NA_character_
str_has_match(selector, "^\\.") ~ "class",
!str_has_match(selector, "^\\.") ~ NA_character_
)
)
css_tbl <- dplyr::select(css_tbl, selector, type, property, value)
Expand All @@ -1465,7 +1465,7 @@ create_inline_styles <- function(
extra_style = ""
) {

class_names <- unlist(stringr::str_split(class_names, "\\s+"))
class_names <- unlist(strsplit(class_names, "\\s+"))

paste0(
"style=\"",
Expand Down Expand Up @@ -1503,7 +1503,7 @@ inline_html_styles <- function(html, css_tbl) {

class_names <- extract_strings(matching_css_style, cls_names_pattern)

existing_style <- stringr::str_match(matching_css_style, sty_exist_pattern)[, 2]
existing_style <- str_get_match(matching_css_style, sty_exist_pattern)[, 2]

inline_styles <-
create_inline_styles(
Expand All @@ -1513,7 +1513,7 @@ inline_html_styles <- function(html, css_tbl) {
)

html <-
stringr::str_replace(
str_single_replace(
html,
pattern = cls_sty_pattern,
replacement = inline_styles
Expand All @@ -1522,8 +1522,8 @@ inline_html_styles <- function(html, css_tbl) {

repeat {

class_names <- stringr::str_extract(html, pattern = cls_pattern)
class_names <- stringr::str_extract(class_names, pattern = cls_names_pattern)
class_names <- str_single_extract(html, pattern = cls_pattern)
class_names <- str_single_extract(class_names, pattern = cls_names_pattern)

if (is.na(class_names)) break

Expand All @@ -1534,7 +1534,7 @@ inline_html_styles <- function(html, css_tbl) {
)

html <-
stringr::str_replace(
str_single_replace(
html,
pattern = cls_pattern,
replacement = inline_styles
Expand Down
104 changes: 104 additions & 0 deletions R/utils_general_str_formatting.R
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,110 @@ str_catalog <- function(
}
}

str_substitute <- function(string, start = 1L, end = -1L) {

if (is.matrix(start)) {
end <- start[, 2]
start <- start[, 1]
}

start <- recycler(start, string)
end <- recycler(end, string)

n <- nchar(string)
start <- ifelse(start < 0, start + n + 1, start)
end <- ifelse(end < 0, end + n + 1, end)

substr(string, start, end)
}

recycler <- function(x, to, arg = deparse(substitute(x))) {

if (length(x) == length(to)) {
return(x)
}

if (length(x) != 1) {
stop("Can't recycle `", arg, "` to length ", length(to), call. = FALSE)
}

rep(x, length(to))
}

str_complete_locate <- function(string, pattern) {
out <- gregexpr(pattern, string, perl = TRUE)
lapply(out, location, all = TRUE)
}

str_single_locate <- function(string, pattern) {
out <- regexpr(pattern, string, perl = TRUE)
location(out)
}

str_complete_replace <- function(string, pattern, replacement) {
gsub(pattern, replacement, string, perl = TRUE)
}

str_single_replace <- function(string, pattern, replacement) {
sub(pattern, replacement, string, perl = TRUE)
}

location <- function(x, all = FALSE) {

start <- as.vector(x)
if (all && identical(start, -1L)) {
return(cbind(start = integer(), end = integer()))
}
end <- as.vector(x) + attr(x, "match.length") - 1

no_match <- start == -1L
start[no_match] <- NA
end[no_match] <- NA

cbind(start = start, end = end)
}

str_complete_extract <- function(string, pattern) {

loc <- str_complete_locate(string, pattern)
lapply(seq_along(string), function(i) {
loc <- loc[[i]]
str_substitute(rep(string[[i]], nrow(loc)), loc)
})
}

str_single_extract <- function(string, pattern) {
str_substitute(string, str_single_locate(string, pattern))
}

str_get_match <- function(string, pattern) {

loc <- regexec(pattern, string, perl = TRUE)
loc <- lapply(loc, location)

out <- lapply(seq_along(string), function(i) {
loc <- loc[[i]]
str_substitute(rep(string[[i]], nrow(loc)), loc)
})
do.call("rbind", out)
}

str_has_match <- function(string, pattern, negate = FALSE) {

out <- grepl(pattern, string, perl = TRUE)
out[is.na(string)] <- NA

if (negate) {
!out
} else {
out
}
}

str_trim_sides <- function(string) {
sub("\\s+$", "", sub("^\\s+", "", string))
}

glue_gt <- function(.x, ...) {
glue::glue_data(.x, ..., .transformer = get, .envir = emptyenv())
}
Loading