diff --git a/DESCRIPTION b/DESCRIPTION index 8e1a25bb5..ecc13a7cd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,7 +29,6 @@ Imports: magrittr (>= 2.0.0), purrr (>= 0.2.3), R.cache (>= 0.15.0), - rematch2 (>= 2.0.1), rlang (>= 0.1.1), rprojroot (>= 1.1), tibble (>= 1.4.2), diff --git a/R/rules-spaces.R b/R/rules-spaces.R index 76d5acfc5..21be47897 100644 --- a/R/rules-spaces.R +++ b/R/rules-spaces.R @@ -266,7 +266,7 @@ start_comments_with_space <- function(pd, force_one = FALSE) { return(pd) } - comments <- rematch2::re_match( + comments <- re_match( pd$text[is_comment], "^(?#+['\\*]*)(? *)(?.*)$" ) diff --git a/R/utils.R b/R/utils.R index f7ff3dc91..81bea559d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -32,6 +32,32 @@ ensure_last_n_empty <- function(x, n = 1) { c(x, rep("", n)) } +#' @note Slightly simplified version of `rematch2::re_match()` (License: MIT). +#' @keywords internal +#' @noRd +re_match <- function(text, pattern) { + stopifnot(is.character(pattern), length(pattern) == 1L, !is.na(pattern)) + text <- as.character(text) + match <- regexpr(pattern, text, perl = TRUE) + start <- as.vector(match) + length <- attr(match, "match.length") + end <- start + length - 1L + matchstr <- substring(text, start, end) + matchstr[start == -1] <- NA_character_ + res <- data.frame(stringsAsFactors = FALSE, .text = text, .match = matchstr) + + gstart <- attr(match, "capture.start") + glength <- attr(match, "capture.length") + gend <- gstart + glength - 1L + groupstr <- substring(text, gstart, gend) + groupstr[gstart == -1] <- NA_character_ + dim(groupstr) <- dim(gstart) + res <- cbind(groupstr, res, stringsAsFactors = FALSE) + + names(res) <- c(attr(match, "capture.names"), ".text", ".match") + res +} + #' Replace the newline character with a line break #' #' @param text A character vector diff --git a/tests/testthat/test-cache-interaction-roxygen-code-examples.R b/tests/testthat/test-cache-interaction-roxygen-code-examples.R index 9a1248b84..bfbce2cd3 100644 --- a/tests/testthat/test-cache-interaction-roxygen-code-examples.R +++ b/tests/testthat/test-cache-interaction-roxygen-code-examples.R @@ -51,7 +51,7 @@ test_that("roxzgen code examples are written to cache as whole expressions bring # don't use full cache, only roxygen cache styled[1] <- "#' This is a nother text" second <- system.time(style_text(styled)) - expect_gt(first["elapsed"], 5 * second["elapsed"]) + expect_gt(first["elapsed"], 4 * second["elapsed"]) }) test_that("cache is deactivated at end of caching related testthat file", {