Sfoglia il codice sorgente

Rename internal function run_regex() -> regex()

pull/21/head
Garrick Aden-Buie 7 anni fa
parent
commit
5b04489eb8
5 ha cambiato i file con 18 aggiunte e 18 eliminazioni
  1. +4
    -4
      R/regex.R
  2. +3
    -3
      man/regex.Rd
  3. +1
    -1
      man/wrap_result.Rd
  4. +3
    -3
      tests/testthat/test-regex.R
  5. +7
    -7
      tests/testthat/test-wrap_result.R

+ 4
- 4
R/regex.R Vedi File

@@ -4,7 +4,7 @@
#' @param pattern regexp
#' @param global If `TRUE`, enables global pattern matching
#' @inheritParams base::regexec
run_regex <- function(
regex <- function(
text,
pattern,
ignore.case = FALSE,
@@ -24,7 +24,7 @@ run_regex <- function(
if (any(!is.na(mmi))) {
subtext <- purrr::map_chr(m, "text") %>% purrr::map2_chr(mmi, substring)
subtext[is.na(subtext)] <- ""
m2 <- run_regex(subtext, pattern, ignore.case, perl, fixed, useBytes)
m2 <- regex(subtext, pattern, ignore.case, perl, fixed, useBytes)
for (i in seq_along(m2)) {
if (is.null(m2[[i]]$idx[[1]])) next
m2[[i]]$idx[, c(1, 2)] <- m2[[i]]$idx[, c(1, 2)] + mmi[i] - 1L
@@ -60,7 +60,7 @@ max_match_index <- function(m) {

#' Wrap matches in HTML span tags to colorize via CSS
#'
#' @param x Individual list item in list returned by [run_regex()]
#' @param x Individual list item in list returned by [regex()]
#' @inheritParams view_regex
#' @keywords internal
wrap_result <- function(x, escape = FALSE, exact = FALSE) {
@@ -241,7 +241,7 @@ view_regex <- function(
regex_opts <- deprecate_knitr_option(...)
regex_opts$text <- text
regex_opts$pattern <- pattern
res <- do.call(run_regex, regex_opts)
res <- do.call(regex, regex_opts)
res <- purrr::map_chr(res, wrap_result, escape = escape, exact = exact)
res <- purrr::map_chr(res, function(resi) {
result_pad <- ""

man/run_regex.Rd → man/regex.Rd Vedi File

@@ -1,10 +1,10 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/regex.R
\name{run_regex}
\alias{run_regex}
\name{regex}
\alias{regex}
\title{Extract matched groups from regexp}
\usage{
run_regex(text, pattern, ignore.case = FALSE, perl = FALSE,
regex(text, pattern, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE, global = TRUE)
}
\arguments{

+ 1
- 1
man/wrap_result.Rd Vedi File

@@ -7,7 +7,7 @@
wrap_result(x, escape = FALSE, exact = FALSE)
}
\arguments{
\item{x}{Individual list item in list returned by \code{\link[=run_regex]{run_regex()}}}
\item{x}{Individual list item in list returned by \code{\link[=regex]{regex()}}}

\item{escape}{Escape HTML-related characters in \code{text}?}


+ 3
- 3
tests/testthat/test-regex.R Vedi File

@@ -14,20 +14,20 @@ test_that("expand_matches gives data frame of indices with groups", {
test_that("start/end indices are integers", {
text <- "ab ab"
pattern <- "(a)(b)"
m <- run_regex(text, pattern, global = TRUE)
m <- regex(text, pattern, global = TRUE)
expect_is(m[[1]]$idx$start, "integer")
expect_is(m[[1]]$idx$end, "integer")
expect_is(m[[1]]$idx$group, "integer")
})

test_that("max_match_index works", {
m <- run_regex(c("abcaba", "aba", "z"), c("(a)(b)(d)?c?"), global = FALSE)
m <- regex(c("abcaba", "aba", "z"), c("(a)(b)(d)?c?"), global = FALSE)
expect_equal(max_match_index(m), c(4, 3, NA_integer_))
})

test_that("results group (pass) is calculated correctly", {
text <- "ab ab"
pattern <- "(a)(b)"
m <- run_regex(text, pattern, global = TRUE)
m <- regex(text, pattern, global = TRUE)
expect_equal(unique(m[[1]]$idx$pass), c(1L, 2L))
})

+ 7
- 7
tests/testthat/test-wrap_result.R Vedi File

@@ -6,31 +6,31 @@ test_that("wrap_result handles zero length groups", {
# (?<=\()([^)]*)(?=\))
text <- "Type 'q()' to quit R."
pattern <- "(?<=\\()([^)]*)(?=\\))"
res <- wrap_result(run_regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, "Type 'q(<span class=\"group g00\"><span class=\"group g01\"></span></span>)' to quit R.")
})

test_that("wrap_results generally works", {
text <- "apples"
pattern <- "apples"
res <- wrap_result(run_regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, "<span class=\"group g00\">apples</span>")

text <- "He wheeled the bike past the winding road."
pattern <- "(a|the) ([^ ]+)"
res <- wrap_result(run_regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, "He wheeled <span class=\"group g00\"><span class=\"group g01\">the</span> <span class=\"group g02\">bike</span></span> past the winding road.")

text <- ".15in"
pattern <- "^(auto|inherit|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|em|ex|pt|pc|px|vh|vw|vmin|vmax))$"
res <- wrap_result(run_regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, "<span class=\"group g00\"><span class=\"group g01\"><span class=\"group g02 pad01\"><span class=\"group g03 pad02\">.15</span></span><span class=\"group g06 pad01\">in</span></span></span>")
})

test_that("wrap_results works when groups start and end at same index", {
text <- "7282298386"
pattern <- "\\(?(\\d{3})[-). ]?(\\d{3})[- .]?(\\d{4})"
res <- wrap_result(run_regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
res <- wrap_result(regex(text, pattern, perl = TRUE, global = FALSE)[[1]])
expect_equal(res, "<span class=\"group g00\"><span class=\"group g01\">728</span><span class=\"group g02\">229</span><span class=\"group g03\">8386</span></span>")
})

@@ -38,7 +38,7 @@ test_that("wrap_result searches globally", {
text <- "ab ab"
pattern <- "(a)(b)"
result <- paste(rep("<span class=\"group g00\"><span class=\"group g01\">a</span><span class=\"group g02\">b</span></span>", 2), collapse = " ")
expect_equal(wrap_result(run_regex(text, pattern, global = TRUE)[[1]]), result)
expect_equal(wrap_result(regex(text, pattern, global = TRUE)[[1]]), result)
})

test_that("wrap_result starts/ends correctly with touching groups", {
@@ -50,5 +50,5 @@ test_that("wrap_result starts/ends correctly with touching groups", {
'<span class=\"group g00\"><span class=\"group g01\">fell</span> <span class=\"group g02\">to</span> </span>',
'the ground.'
)
expect_equal(wrap_result(run_regex(text, pattern, global = TRUE)[[1]]), result)
expect_equal(wrap_result(regex(text, pattern, global = TRUE)[[1]]), result)
})

Loading…
Annulla
Salva