Skip to content

Commit

Permalink
Vectorise str_replace
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Aug 24, 2010
1 parent bd1dd6c commit 2d8c55c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
22 changes: 15 additions & 7 deletions R/replace.r
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Replace first occurrence of a matched pattern in a string.
#'
#' Vectorised over \code{string}. \code{pattern} and \code{replacement}
#' should both be single strings, i.e. a character vectors of length one.
#' Vectorised over \code{string}, \code{pattern} and \code{replacement}.
#' Shorter arguments will be expanded to length of longest.
#'
#' @param string input character vector
#' @param pattern pattern to look for, as defined by a POSIX regular
Expand All @@ -19,13 +19,17 @@ str_replace <- function(string, pattern, replacement) {
string <- check_string(string)
pattern <- check_pattern(pattern, string, replacement)

sub(pattern, replacement, string)
if (length(pattern) == 1) {
re_call("sub", string, pattern, replacement)
} else {
unlist(re_mapply("sub", string, pattern, replacement))
}
}

#' Replace all occurrences of a matched pattern in a string.
#'
#' Vectorised over \code{string}. \code{pattern} and \code{replacement}
#' should both be single strings, i.e. a character vectors of length one.
#' Vectorised over \code{string}, \code{pattern} and \code{replacement}.
#' Shorter arguments will be expanded to length of longest.
#'
#' @param string input character vector
#' @param pattern pattern to look for, as defined by a POSIX regular
Expand All @@ -37,11 +41,15 @@ str_replace <- function(string, pattern, replacement) {
#' @return character vector.
#' @keywords character
#' @seealso \code{\link{gsub}} which this function wraps,
#' \code{\link{str_replace_all}} to replace a single matche
#' \code{\link{str_replace_all}} to replace a single match
#' @export
str_replace_all <- function(string, pattern, replacement) {
string <- check_string(string)
pattern <- check_pattern(pattern, string, replacement)

gsub(pattern, replacement, string)
if (length(pattern) == 1) {
re_call("gsub", string, pattern, replacement)
} else {
unlist(re_mapply("gsub", string, pattern, replacement))
}
}
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
* add examples to str_replace and str_match
* check that str_extract, str_match, str_replace and str_split all
use new vectorisation and modifier strategy.
5 changes: 2 additions & 3 deletions man/str_replace.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
}

\details{
Vectorised over \code{string}. \code{pattern} and \code{replacement}
should both be single strings, i.e. a character vectors of length
one.
Vectorised over \code{string}, \code{pattern} and \code{replacement}.
Shorter arguments will be expanded to length of longest.
}
\value{character vector.}
\keyword{character}
Expand Down
7 changes: 3 additions & 4 deletions man/str_replace_all.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
}

\details{
Vectorised over \code{string}. \code{pattern} and \code{replacement}
should both be single strings, i.e. a character vectors of length
one.
Vectorised over \code{string}, \code{pattern} and \code{replacement}.
Shorter arguments will be expanded to length of longest.
}
\value{character vector.}
\keyword{character}
\seealso{\code{\link{gsub}} which this function wraps,
\code{\link{str_replace_all}} to replace a single matche}
\code{\link{str_replace_all}} to replace a single match}
\arguments{
\item{string}{input character vector}
\item{pattern}{pattern to look for, as defined by a POSIX regular
Expand Down

0 comments on commit 2d8c55c

Please sign in to comment.