Skip to content

Commit

Permalink
Change: arguments order #6
Browse files Browse the repository at this point in the history
Input is the first one, and pattern is the second one.
  • Loading branch information
qinwf committed Apr 23, 2016
1 parent fee1259 commit 1a9f78b
Show file tree
Hide file tree
Showing 24 changed files with 240 additions and 220 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ LinkingTo: Rcpp
Suggests:
knitr (>= 1.12.3),testthat,
rmarkdown (>= 0.9.5),microbenchmark,rex
Enhances:
directlabels, ggplot2 ,stringi
URL: https://github.com/qinwf/re2r/
BugReports: https://github.com/qinwf/re2r/issues
VignetteBuilder: knitr
Expand Down
6 changes: 0 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,re2exp)
S3method(re2_extract,character)
S3method(re2_extract,re2exp)
S3method(re2_match,character)
S3method(re2_match,re2exp)
S3method(re2_replace,character)
S3method(re2_replace,re2exp)
export("%!~%")
export("%<~%")
export("%=~%")
Expand Down
12 changes: 6 additions & 6 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This file was generated by Rcpp::compileAttributes
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

cpp_match <- function(pattern, input, value, anchor, all) {
.Call('re2r_cpp_match', PACKAGE = 're2r', pattern, input, value, anchor, all)
cpp_match <- function(input, pattern, value, anchor, all) {
.Call('re2r_cpp_match', PACKAGE = 're2r', input, pattern, value, anchor, all)
}

cpp_re2_compile <- function(pattern, log_errors_value, utf_8_value, posix_syntax_value, case_sensitive_value, dot_nl_value, literal_value, longest_match_value, never_nl_value, never_capture_value, one_line_value, perl_classes_value, word_boundary_value, max_mem_value) {
Expand Down Expand Up @@ -52,12 +52,12 @@ cpp_quote_meta <- function(input) {
.Call('re2r_cpp_quote_meta', PACKAGE = 're2r', input)
}

cpp_replace <- function(regexp, rewrite, input, global_) {
.Call('re2r_cpp_replace', PACKAGE = 're2r', regexp, rewrite, input, global_)
cpp_replace <- function(input, regexp, rewrite, global_) {
.Call('re2r_cpp_replace', PACKAGE = 're2r', input, regexp, rewrite, global_)
}

cpp_extract <- function(regexp, rewrite, input) {
.Call('re2r_cpp_extract', PACKAGE = 're2r', regexp, rewrite, input)
cpp_extract <- function(input, regexp, rewrite) {
.Call('re2r_cpp_extract', PACKAGE = 're2r', input, regexp, rewrite)
}

cpp_get_program_fanout <- function(regexp) {
Expand Down
12 changes: 6 additions & 6 deletions R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@



`%==%` = function(x1,x2){
identical(x1,x2)
`%==%` = function(x1, x2) {
identical(x1, x2)
}

`%!==%` = function(x1,x2){
!identical(x1,x2)
`%!==%` = function(x1, x2) {
!identical(x1, x2)
}

check_windows_strings = function(strings){
check_windows_strings = function(strings) {
.Platform$OS.type %==% "windows" &&
all(Encoding(strings) == "UTF-8")
}

update_windows_strings = function(){
update_windows_strings = function() {
.Platform$OS.type %==% "windows"
}
60 changes: 31 additions & 29 deletions R/compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,38 @@
#' regexp
#' @export
re2 = function(pattern,
utf_8 = TRUE,
case_sensitive = TRUE,
posix_syntax = FALSE,
dot_nl = FALSE,
literal = FALSE,
longest_match = FALSE,
never_nl = FALSE,
never_capture = FALSE,
one_line= FALSE,
perl_classes = FALSE,
word_boundary = FALSE,
max_mem = 8388608){
utf_8 = TRUE,
case_sensitive = TRUE,
posix_syntax = FALSE,
dot_nl = FALSE,
literal = FALSE,
longest_match = FALSE,
never_nl = FALSE,
never_capture = FALSE,
one_line = FALSE,
perl_classes = FALSE,
word_boundary = FALSE,
max_mem = 8388608) {
# if ( .Platform$OS.type %==% "windows" &&
# Encoding(pattern[1]) %!==% "UTF-8" ) {
# pattern = enc2utf8(pattern)
# }
regexp = cpp_re2_compile(pattern,
log_errors_value = FALSE,
utf_8_value = utf_8,
case_sensitive_value = case_sensitive,
posix_syntax_value = posix_syntax,
dot_nl_value = dot_nl,
literal_value = literal,
longest_match_value = longest_match,
never_nl_value = never_nl,
never_capture_value = never_capture,
one_line_value = one_line,
perl_classes_value = perl_classes,
word_boundary_value = word_boundary,
max_mem_value = max_mem)
regexp = cpp_re2_compile(
pattern,
log_errors_value = FALSE,
utf_8_value = utf_8,
case_sensitive_value = case_sensitive,
posix_syntax_value = posix_syntax,
dot_nl_value = dot_nl,
literal_value = literal,
longest_match_value = longest_match,
never_nl_value = never_nl,
never_capture_value = never_capture,
one_line_value = one_line,
perl_classes_value = perl_classes,
word_boundary_value = word_boundary,
max_mem_value = max_mem
)

class(regexp) = "re2exp"
regexp
Expand All @@ -134,7 +136,7 @@ re2 = function(pattern,
#' get_pattern(regexp)
#' @return a string
#' @export
get_pattern = function(regexp){
get_pattern = function(regexp) {
res = cpp_get_pattern(regexp)
# if (.Platform$OS.type %==% "windows") {
# Encoding(res) = "UTF-8"
Expand All @@ -152,7 +154,7 @@ get_pattern = function(regexp){
#' (res = get_named_groups(regexp))
#' names(res)
#' @export
get_named_groups = function(regexp){
get_named_groups = function(regexp) {
res = cpp_get_named_groups(regexp)
# if (.Platform$OS.type %==% "windows") {
# Encoding(names(res)) = "UTF-8"
Expand All @@ -175,7 +177,7 @@ get_named_groups = function(regexp){
#' quote_meta(c("1.2","abc"))
#' @return quoted string
#' @export
quote_meta = function(unquoted){
quote_meta = function(unquoted) {
# if (check_windows_strings(unquoted)) {
# unquoted = enc2utf8(unquoted)
# }
Expand Down
30 changes: 7 additions & 23 deletions R/extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,12 @@
#' @param input a character vector
#' @param ... further arguments passed to or from other methods.
#' @examples
#' re2_extract("(.)","yabba dabba doo")
#' re2_extract("(.*)@([^.]*)","test@me.com","\\2!\\1")
#' re2_extract("yabba dabba doo", "(.)")
#' re2_extract("test@me.com", "(.*)@([^.]*)", "\\2!\\1")
#' @export
re2_extract = function(pattern, input, rewrite = "\\1", ...) UseMethod("re2_extract")

#' @rdname re2_extract
#' @export
re2_extract.re2exp = function(pattern, input, rewrite = "\\1", ...){
# if (check_windows_strings(input)) input = enc2utf8(input)
# if (check_windows_strings(rewrite)) rewrite = enc2utf8(rewrite)

res = cpp_extract(pattern, rewrite, input)

# if (update_windows_strings()) {
# Encoding(res) = "UTF-8"
# }
return(res)
}

#' @rdname re2_extract
#' @export
re2_extract.character = function(pattern, input, rewrite = "\\1", ...){
pattern = re2(pattern, ...)
re2_extract.re2exp(pattern, input, rewrite)
re2_extract = function(input, pattern, rewrite = "\\1", ...) {
if (!inherits(pattern, "re2exp")) {
pattern = re2(pattern, ...)
}
cpp_extract(input, pattern, rewrite)
}
39 changes: 34 additions & 5 deletions R/fanout.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
## This file is part of the 're2r' package for R.
## Copyright (C) 2016, Qin Wenfeng
## All rights reserved.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
##
## 1. Redistributions of source code must retain the above copyright notice,
## this list of conditions and the following disclaimer.
##
## 2. Redistributions in binary form must reproduce the above copyright notice,
## this list of conditions and the following disclaimer in the documentation
## and/or other materials provided with the distribution.
##
## 3. Neither the name of the copyright holder nor the names of its
## contributors may be used to endorse or promote products derived from
## this software without specific prior written permission.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
## BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
## FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
## HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
## PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
## OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
## WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
## OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#' Get program fanout
#' Outputs the program fanout as a histogram bucketed by powers of 2.
#' Returns the number of the largest non-empty bucket.
#' @param regexp a pre-compiled regular expression
#' @export
get_program_fanout = function(regexp){
get_program_fanout = function(regexp) {
res = cpp_get_program_fanout(regexp)
return(data.frame(
index = as.numeric(names(res)),
value = as.numeric(res))
)
return(data.frame(index = as.numeric(names(res)),
value = as.numeric(res)))
}
46 changes: 32 additions & 14 deletions R/match.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,41 @@
#' Match patterns in a string.
#'
#' @param pattern a pre-compiled regular expression or a string
#' @param string a character vector
#' @param input a character vector
#' @param value return value instead of bool result
#' @param anchor "start": anchor match at the beginning of the string, "both": anchor match at the beginning and the end of the string, "none": no anchor.
#' @param all find all matches instead of the first match. When result = "value", a matched character matrix will be returned.
#' @param ... further arguments passed to or from other methods.
#' @examples
#'
#' test_string = "this is just one test";
#' re2_match(test_string, "(o.e)")
#'
#' (res = re2_match(test_string, "(o.e)", value = TRUE))
#' str(res)
#'
#' (res = re2_match(test_string, "(?P<testname>this)( is)", value = TRUE))
#' str(res)
#'
#' (res = re2_match(test_string, "(is)", value = TRUE, all = TRUE))
#'
#' test_string = c("this is just one test", "the second test");
#' (res = re2_match(test_string, "(is)", value = TRUE, all = TRUE))
#'
#' test_string = c("this is just one test", "the second test")
#' (res = re2_match(test_string, "is", value = TRUE))
#'
#' regexp = re2("test",case_sensitive = FALSE)
#' re2_match("TEST", regexp)
#' @export
re2_match = function(pattern, string, value = FALSE, anchor = "none", all = FALSE, ...) UseMethod("re2_match")

#' @rdname re2_match
#' @export
re2_match.re2exp = function(pattern, string, value = FALSE, anchor = "none", all = FALSE, ...){
cpp_match(pattern, string, value, anchor, all)
}

#' @rdname re2_match
#' @export
re2_match.character = function(pattern, string, value = FALSE, anchor = "none", all = FALSE, ...){
pattern = re2(pattern, ...)
re2_match.re2exp(pattern, string, value, anchor, all)
re2_match = function(input,
pattern,
value = FALSE,
anchor = "none",
all = FALSE,
...) {
if (!inherits(pattern, "re2exp")) {
pattern = re2(pattern, ...)
}
cpp_match(input, pattern, value, anchor, all)
}
10 changes: 5 additions & 5 deletions R/ops.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#' regexp
#' @seealso more options with \code{\link{re2}}
#' @export
`%<~%` = function(varible, pattern){
`%<~%` = function(varible, pattern) {
parent <- parent.frame()
rhss <- substitute(pattern) # the right-hand sides
lhs <- substitute(varible) # the left-hand side.
Expand All @@ -56,12 +56,12 @@
#' c("pt","sd") %=~% "sd"
#' c("pt","sd") %!~% "sd"
#' @export
`%=~%` = function(string, pattern){
re2_match(pattern, string, value = FALSE, anchor = "none")
`%=~%` = function(string, pattern) {
re2_match(string, pattern, value = FALSE, anchor = "none")
}

#' @rdname grapes-equals-twiddle-grapes
#' @export
`%!~%` = function(string, pattern){
!re2_match(pattern, string, value = FALSE, anchor = "none")
`%!~%` = function(string, pattern) {
!re2_match(string, pattern, value = FALSE, anchor = "none")
}
17 changes: 12 additions & 5 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@
#' re2("(.*)@([^.]*)")
#' re2("(?P<name>sd)")
#' @export
print.re2exp = function(x, ...){
print.re2exp = function(x, ...) {
cat("re2 pre-compiled regular expression\n\n")
cat("pattern: "); cat(get_pattern(x)); cat("\n")
cat("number of capturing subpatterns: "); cat(get_number_of_groups(x)) ; cat("\n")
cat("capturing names with indices: \n"); print(get_named_groups(x)) ;
cat("expression size: "); cat(get_expression_size(x))
cat("pattern: ")
cat(get_pattern(x))
cat("\n")
cat("number of capturing subpatterns: ")
cat(get_number_of_groups(x))
cat("\n")
cat("capturing names with indices: \n")
print(get_named_groups(x))

cat("expression size: ")
cat(get_expression_size(x))
invisible()
}
30 changes: 7 additions & 23 deletions R/replace.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,12 @@
#' @return a character vector
#' @examples
#' regexp = re2("b+")
#' re2_replace(regexp,"d", "yabba dabba doo") == "yada dada doo"
#' re2_replace("b+","d", "yabba dabba doo", all = FALSE) == "yada dabba doo"
#' re2_replace("yabba dabba doo", regexp,"d") == "yada dada doo"
#' re2_replace("yabba dabba doo", "b+","d", all = FALSE) == "yada dabba doo"
#' @export
re2_replace = function(pattern, rewrite, input, all = FALSE, ...) UseMethod("re2_replace")

#' @rdname re2_replace
#' @export
re2_replace.re2exp = function(pattern, rewrite, input, all = FALSE, ...){
# if (check_windows_strings(input)) input = enc2utf8(input)
# if (check_windows_strings(rewrite)) rewrite = enc2utf8(rewrite)

res = cpp_replace(pattern, rewrite, input, all)

# if (update_windows_strings()) {
# Encoding(res) = "UTF-8"
# }
return(res)
}

#' @rdname re2_replace
#' @export
re2_replace.character = function(pattern, rewrite, input, all = FALSE, ...){
pattern = re2(pattern, ...)
re2_replace.re2exp(pattern, rewrite, input, all)
re2_replace = function(input, pattern, rewrite, all = FALSE, ...) {
if (!inherits(pattern, "re2exp")) {
pattern = re2(pattern, ...)
}
cpp_replace(input, pattern, rewrite, all)
}
Loading

0 comments on commit 1a9f78b

Please sign in to comment.