Преглед на файлове

Rename internal function run_regex() -> regex()

pull/21/head
Garrick Aden-Buie преди 7 години
родител
ревизия
5b04489eb8
променени са 5 файла, в които са добавени 18 реда и са изтрити 18 реда
  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 Целия файл

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


#' Wrap matches in HTML span tags to colorize via CSS #' 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 #' @inheritParams view_regex
#' @keywords internal #' @keywords internal
wrap_result <- function(x, escape = FALSE, exact = FALSE) { wrap_result <- function(x, escape = FALSE, exact = FALSE) {
regex_opts <- deprecate_knitr_option(...) regex_opts <- deprecate_knitr_option(...)
regex_opts$text <- text regex_opts$text <- text
regex_opts$pattern <- pattern 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, wrap_result, escape = escape, exact = exact)
res <- purrr::map_chr(res, function(resi) { res <- purrr::map_chr(res, function(resi) {
result_pad <- "" result_pad <- ""

man/run_regex.Rd → man/regex.Rd Целия файл

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

+ 1
- 1
man/wrap_result.Rd Целия файл

wrap_result(x, escape = FALSE, exact = FALSE) wrap_result(x, escape = FALSE, exact = FALSE)
} }
\arguments{ \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}?} \item{escape}{Escape HTML-related characters in \code{text}?}



+ 3
- 3
tests/testthat/test-regex.R Целия файл

test_that("start/end indices are integers", { test_that("start/end indices are integers", {
text <- "ab ab" text <- "ab ab"
pattern <- "(a)(b)" 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$start, "integer")
expect_is(m[[1]]$idx$end, "integer") expect_is(m[[1]]$idx$end, "integer")
expect_is(m[[1]]$idx$group, "integer") expect_is(m[[1]]$idx$group, "integer")
}) })


test_that("max_match_index works", { 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_)) expect_equal(max_match_index(m), c(4, 3, NA_integer_))
}) })


test_that("results group (pass) is calculated correctly", { test_that("results group (pass) is calculated correctly", {
text <- "ab ab" text <- "ab ab"
pattern <- "(a)(b)" 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)) expect_equal(unique(m[[1]]$idx$pass), c(1L, 2L))
}) })

+ 7
- 7
tests/testthat/test-wrap_result.R Целия файл

# (?<=\()([^)]*)(?=\)) # (?<=\()([^)]*)(?=\))
text <- "Type 'q()' to quit R." text <- "Type 'q()' to quit R."
pattern <- "(?<=\\()([^)]*)(?=\\))" 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.") expect_equal(res, "Type 'q(<span class=\"group g00\"><span class=\"group g01\"></span></span>)' to quit R.")
}) })


test_that("wrap_results generally works", { test_that("wrap_results generally works", {
text <- "apples" text <- "apples"
pattern <- "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>") expect_equal(res, "<span class=\"group g00\">apples</span>")


text <- "He wheeled the bike past the winding road." text <- "He wheeled the bike past the winding road."
pattern <- "(a|the) ([^ ]+)" 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.") 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" text <- ".15in"
pattern <- "^(auto|inherit|((\\.\\d+)|(\\d+(\\.\\d+)?))(%|in|cm|mm|em|ex|pt|pc|px|vh|vw|vmin|vmax))$" 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>") 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", { test_that("wrap_results works when groups start and end at same index", {
text <- "7282298386" text <- "7282298386"
pattern <- "\\(?(\\d{3})[-). ]?(\\d{3})[- .]?(\\d{4})" 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>") 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>")
}) })


text <- "ab ab" text <- "ab ab"
pattern <- "(a)(b)" 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 = " ") 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", { 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>', '<span class=\"group g00\"><span class=\"group g01\">fell</span> <span class=\"group g02\">to</span> </span>',
'the ground.' '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…
Отказ
Запис