Skip to content

Commit

Permalink
Add as.regex and c methods
Browse files Browse the repository at this point in the history
Partially addresses #10 when the `regex` object is the first object to
be Concatenated.
  • Loading branch information
jimhester committed Oct 3, 2014
1 parent 1e3002a commit fea2f5b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 44 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2 (4.0.2.9000): do not edit by hand

S3method(as.character,regex)
S3method(as.regex,default)
S3method(character_class_escape,character)
S3method(character_class_escape,character_class)
S3method(character_class_escape,default)
Expand All @@ -18,6 +19,7 @@ export("%if_next_isnt%")
export("%if_prev_is%")
export("%if_prev_isnt%")
export("%or%")
export(as.regex)
export(at_least)
export(at_most)
export(between)
Expand Down
32 changes: 23 additions & 9 deletions R/rex.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,28 @@ rex_ <- function(args, env = parent.frame()) {
return(output)
}

#' Convert regex to a character
#' @param x \code{regex} object.
#' @param ... further args ignored by method.
#' @describeIn regex coerce regex object to a character
#' @export
as.character.regex <- function(x, ...) escape(x)
as.character.regex <- function(x, ...) x

#' Print regex object
#' @param x Object to be printed.
#' @param ... further args are ignored.
#' Coerce objects to a \code{\link{regex}}.
#' @name as.regex
#' @param x Object to coerce to \code{\link{regex}}.
#' @param ... further arguments passed to methods.
#' @export
as.regex <- function(x, ...) UseMethod("as.regex")

#' @export
#' @describeIn as.regex Simply escape the Object.
as.regex.default <- function(x, ...) escape(x)

#' @describeIn regex concatenate regex
c.regex <- function(...) {
p(escape_dots(...))
}

#' @export
#' @describeIn regex Print regex object
print.regex <- function(x, ...){
cat(paste(strwrap(x), collapse = "\n"), "\n", sep = "")
}
Expand All @@ -43,7 +55,9 @@ print.regex <- function(x, ...){
#'
#' Specify an explicit regular expression. This expression must already be
#' escaped.
#' @param x Object to be coerced to a regex.
#' @param x Object
#' @param ... further arguments
#' @seealso \code{\link{as.regex}} to coerce to a regex object.
#' @export
regex <- function(x) structure(x, class = "regex")
regex <- function(x, ...) structure(x, class = "regex")

16 changes: 0 additions & 16 deletions man/as.character.regex.Rd

This file was deleted.

23 changes: 23 additions & 0 deletions man/as.regex.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
% Generated by roxygen2 (4.0.2.9000): do not edit by hand
\name{as.regex}
\alias{as.regex}
\alias{as.regex.default}
\title{Coerce objects to a \code{\link{regex}}.}
\usage{
as.regex(x, ...)

\method{as.regex}{default}(x, ...)
}
\arguments{
\item{x}{Object to coerce to \code{\link{regex}}.}

\item{...}{further arguments passed to methods.}
}
\description{
Coerce objects to a \code{\link{regex}}.
}
\section{Methods (by class)}{
\itemize{
\item \code{default}: Simply escape the Object.
}}

16 changes: 0 additions & 16 deletions man/print.regex.Rd

This file was deleted.

28 changes: 25 additions & 3 deletions man/regex.Rd
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
% Generated by roxygen2 (4.0.2.9000): do not edit by hand
\name{regex}
\name{as.character.regex}
\alias{as.character.regex}
\alias{c.regex}
\alias{print.regex}
\alias{regex}
\title{Regular Expression}
\usage{
regex(x)
\method{as.character}{regex}(x, ...)

\method{c}{regex}(...)

\method{print}{regex}(x, ...)

regex(x, ...)
}
\arguments{
\item{x}{Object to be coerced to a regex.}
\item{x}{Object}

\item{...}{further arguments}
}
\description{
Specify an explicit regular expression. This expression must already be
escaped.
}
\section{Methods (by generic)}{
\itemize{
\item \code{as.character}: coerce regex object to a character

\item \code{c}: concatenate regex

\item \code{print}: Print regex object
}}
\seealso{
\code{\link{as.regex}} to coerce to a regex object.
}

0 comments on commit fea2f5b

Please sign in to comment.