[med-svn] [r-cran-stringr] 01/03: Imported Upstream version 0.6.2
Andreas Tille
tille at debian.org
Sat May 24 06:47:29 UTC 2014
This is an automated email from the git hooks/post-receive script.
tille pushed a commit to branch master
in repository r-cran-stringr.
commit 402179279d6a58fd7b78a454cfce019f96a760f6
Author: Andreas Tille <tille at debian.org>
Date: Sat May 24 07:19:28 2014 +0200
Imported Upstream version 0.6.2
---
DESCRIPTION | 23 ++++++++-------
MD5 | 71 ++++++++++++++++++++++++-----------------------
NAMESPACE | 12 ++++----
NEWS | 14 ++++++++++
R/c.r | 19 ++++++-------
R/checks.r | 14 +++++-----
R/count.r | 4 +--
R/detect.r | 8 +++---
R/dup.r | 8 +++---
R/extract.r | 2 +-
R/length.r | 2 +-
R/locate.r | 24 ++++++++--------
R/match.r | 22 +++++++--------
R/modifiers.r | 14 +++++-----
R/pad-trim.r | 22 +++++++--------
R/replace.r | 22 +++++++--------
R/split.r | 44 ++++++++++++++++-------------
R/sub.r | 42 ++++++++++++++--------------
R/utils.r | 1 +
R/vectorise.r | 16 +++++------
R/word.r | 20 ++++++-------
R/wrap.r | 11 ++++----
inst/tests/test-check.r | 10 +++----
inst/tests/test-count.r | 4 +--
inst/tests/test-detect.r | 10 +++----
inst/tests/test-dup.r | 4 +--
inst/tests/test-extract.r | 6 ++--
inst/tests/test-join.r | 8 +++---
inst/tests/test-locate.r | 20 ++++++-------
inst/tests/test-match.r | 34 +++++++++++------------
inst/tests/test-pad.r | 13 +++++----
inst/tests/test-split.r | 36 ++++++++++++------------
inst/tests/test-sub.r | 34 +++++++++++------------
inst/tests/test-trim.r | 4 +--
man/str_replace_all.Rd | 2 +-
man/str_sub_replace.Rd | 2 +-
man/str_wrap.Rd | 3 +-
37 files changed, 314 insertions(+), 291 deletions(-)
diff --git a/DESCRIPTION b/DESCRIPTION
index b0d7adf..65535bd 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,24 +1,23 @@
Package: stringr
-Imports: plyr
Maintainer: Hadley Wickham <h.wickham at gmail.com>
License: GPL-2
Title: Make it easier to work with strings.
Type: Package
Author: Hadley Wickham <h.wickham at gmail.com>
-Description: stringr is a set of simple wrappers that make
- R's string functions more consistent, simpler and
- easier to use. It does this by ensuring that: function
- and argument names (and positions) are consistent, all
- functions deal with NA's and zero length character
- appropriately, and the output data structures from each
- function matches the input data structures of other
- functions.
-Version: 0.6
+Description: stringr is a set of simple wrappers that make R's string
+ functions more consistent, simpler and easier to use. It does
+ this by ensuring that: function and argument names (and
+ positions) are consistent, all functions deal with NA's and
+ zero length character appropriately, and the output data
+ structures from each function matches the input data structures
+ of other functions.
+Version: 0.6.2
Depends: R (>= 2.14)
Suggests: testthat (>= 0.3)
Collate: 'c.r' 'checks.r' 'count.r' 'detect.r' 'dup.r' 'extract.r'
'length.r' 'locate.r' 'match.r' 'modifiers.r' 'pad-trim.r'
'replace.r' 'split.r' 'sub.r' 'vectorise.r' 'word.r' 'wrap.r'
-Packaged: 2011-12-07 18:00:28 UTC; hadley
+ 'utils.r'
+Packaged: 2012-12-05 21:47:03 UTC; hadley
Repository: CRAN
-Date/Publication: 2011-12-08 19:01:59
+Date/Publication: 2012-12-06 08:39:59
diff --git a/MD5 b/MD5
index 33788a8..13cb908 100644
--- a/MD5
+++ b/MD5
@@ -1,37 +1,38 @@
-78377b278a2ab7dc4ec2e72389f1d3a4 *DESCRIPTION
-1cd28bab77fc770f65e3776ac9722c9e *NAMESPACE
-cf6b0eecdb356e9175ddd3373bf5c1aa *NEWS
-509e04a6851160587abee049e2c9993c *R/c.r
-1cacfadf8d0c8cbece564e6a23cb3855 *R/checks.r
-877d928271666e5f628e20261797834d *R/count.r
-ba2cf7a3444a095f797641df88faeaa0 *R/detect.r
-6626b13c99a62a3503dc89dae575adfe *R/dup.r
-a513b0c93966e488d2e0967b030e4473 *R/extract.r
-c0c4ed8abea5cf08679ce22590f25997 *R/length.r
-0063a7d283b850dfaa26d7c596613b89 *R/locate.r
-269d1bd08a3f3316dff679681310f496 *R/match.r
-833fd0322d094eb43186c034eb5f3aa6 *R/modifiers.r
-33688a2a4447daff4d64557142cb1b02 *R/pad-trim.r
-a9078c4b6433e412682448d84962448f *R/replace.r
-91a99c4df185d918458123ebb120e4ad *R/split.r
-e47df99eebd4a9a4f9f22414d259a032 *R/sub.r
-dd94bd05deb361ffc12912d74c0c35b0 *R/vectorise.r
-be21e230447f5b43910ca16de510f2ff *R/word.r
-4a967d7113a6179410f234b21c8a3589 *R/wrap.r
+c14761e3000b5eaae6ff0aa5262a6630 *DESCRIPTION
+0cca89f86586b39903872782ad0c2d92 *NAMESPACE
+84346581b10f2d04598438e428488268 *NEWS
+3d81c2717c2e42df6b55d2b264502c0f *R/c.r
+1711405698cf3015be163e208f1eff40 *R/checks.r
+e55ebb86acca2cb4ee3026c7f2694457 *R/count.r
+729c91c257c5b4642c03a4db03f5f96a *R/detect.r
+93397237016f3ceaedbe6d2a4b1548b4 *R/dup.r
+2293db9f73f80f83f15286b5531c9766 *R/extract.r
+2b8caff655dbdccedc752c496c1f1041 *R/length.r
+95d57be6ed4e6e8786d39a0797098d72 *R/locate.r
+37072fa39b5a3a41f0cf0b54b6341a2e *R/match.r
+b650885277dba55b82664f2edf42aeec *R/modifiers.r
+27735fdcdf9c7bb201bf148cef58e7a5 *R/pad-trim.r
+44165abbb4926ee6bee46f3b94f425ef *R/replace.r
+f84ed242cc4fbc0b817e6fb9178388dd *R/split.r
+6f82f94cbc0d13367b53828923db57a2 *R/sub.r
+524e1f157dac5ce09334df6c6ae0c774 *R/utils.r
+b5557d805bac52a3e4c8ce27e5a02c35 *R/vectorise.r
+59b72fb1d808b48dc72ee1d22bb07f03 *R/word.r
+83d187f96099d2fb3e0e74fde6c5f180 *R/wrap.r
1234794765eabc33a0e77211b8681688 *README.md
-e5b5c991143791d8ab3cecf4f29e89a4 *inst/tests/test-check.r
-a09a24b88d53949c1a97c863fb70c6e6 *inst/tests/test-count.r
-03792fd14d0f3cada31487c74e8db2e7 *inst/tests/test-detect.r
-683b95acc25312948c662d97b9e2662b *inst/tests/test-dup.r
-3513d286f5d678a33006e0c84a197383 *inst/tests/test-extract.r
-ef3b86f7f42bf4ebca8f557a656c34bb *inst/tests/test-join.r
+f24da3c0d81e9d8fb6dc9eab10f303dd *inst/tests/test-check.r
+61f9d77768cf9ff813d382f9337178fb *inst/tests/test-count.r
+d2a6a58e44de1968cf46bd3e8c2d0e26 *inst/tests/test-detect.r
+065f752787f210c753d5bb5feea7f7a5 *inst/tests/test-dup.r
+bcdf3dd9ddd2d00d189d43eac13347a8 *inst/tests/test-extract.r
+8f03149944d3937c9b5a30d686c6e492 *inst/tests/test-join.r
922366c3451f88871b9ce063529edb7a *inst/tests/test-length.r
-82d1861371945335cbb617ed859b9b68 *inst/tests/test-locate.r
-dfba8e6d40dbb7f594a7bfaf2e68b20a *inst/tests/test-match.r
-d556cb694be5dc4a7ad422c193f7926f *inst/tests/test-pad.r
-e41a8f4bf9a13f15a67f2e622f22cc1a *inst/tests/test-split.r
-2e85f99483960116e82b23546f6b827f *inst/tests/test-sub.r
-1409a5533571ac0e0102ebfc2def072c *inst/tests/test-trim.r
+76249df3c11c62fb11aef63899029790 *inst/tests/test-locate.r
+d723b2fc4e6682042b9ac6339f2b4bdf *inst/tests/test-match.r
+3cfc28d6785f4a8c0796a7980c9aac90 *inst/tests/test-pad.r
+f339473f66b14267ec4b86db14b97820 *inst/tests/test-split.r
+c95563eafc4fad4c60504ae59225b9d0 *inst/tests/test-sub.r
+7dc6b256c7c2d3af1483b84698494819 *inst/tests/test-trim.r
fad8767b5232cda34cea611d7e461796 *man/fixed.Rd
828127f0f7f43842bd27ebe9e366a737 *man/ignore.case.Rd
8aca10a194e602482768ad9540784f18 *man/invert_match.Rd
@@ -49,12 +50,12 @@ a5719d08876a471dbc136e9c69a80b17 *man/str_locate.Rd
28e18ed2325d9fbf4bf1cb9a343c269f *man/str_match_all.Rd
499efab9a76d60d78c58ba641c9dd761 *man/str_pad.Rd
ea6ff066d63136cacb172a2b0c4cf5c2 *man/str_replace.Rd
-737a4a142d4acba314c6e59ce7c5f95b *man/str_replace_all.Rd
+7fac10186ba22f54a80021913ffd878a *man/str_replace_all.Rd
93e6eea98bd572829b8e164384673a2a *man/str_split.Rd
5031e079d68b1e5ff4dd034e0dfe4cac *man/str_split_fixed.Rd
c397afe69fc8dc833a0ed0990a9abce5 *man/str_sub.Rd
-eeb1463abe0e05c61660f046910f8717 *man/str_sub_replace.Rd
+bf353bfff3f33db800910f7cf498c6c4 *man/str_sub_replace.Rd
2ac1d755e7a56c11ab8d08fb8b692e33 *man/str_trim.Rd
-617ccbf8cf62b9487ebd5dea7456ccbe *man/str_wrap.Rd
+ae0c6fcf7ea0086ab5e87b6d25d23c8b *man/str_wrap.Rd
62d4953c6ee32543df92481fe26e9377 *man/word.Rd
37129f1e586caa1da9010b485199f41b *tests/test-all.R
diff --git a/NAMESPACE b/NAMESPACE
index 6c34b5b..149ba82 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -7,20 +7,20 @@ export(str_c)
export(str_count)
export(str_detect)
export(str_dup)
-export(str_extract_all)
export(str_extract)
+export(str_extract_all)
+export(str_join)
export(str_length)
-export(str_locate_all)
export(str_locate)
-export(str_match_all)
+export(str_locate_all)
export(str_match)
+export(str_match_all)
export(str_pad)
-export(str_replace_all)
export(str_replace)
-export(str_split_fixed)
+export(str_replace_all)
export(str_split)
+export(str_split_fixed)
export(str_sub)
export(str_trim)
export(str_wrap)
export(word)
-import(plyr)
diff --git a/NEWS b/NEWS
index 5d409f5..1994db8 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,17 @@
+stringr 0.6.2
+================
+
+* fixed path in `str_wrap` example so works for more R installations.
+
+* remove dependency on plyr
+
+stringr 0.6.1
+=============
+
+* Zero input to `str_split_fixed` returns 0 row matrix with `n` columns
+
+* Export `str_join`
+
stringr 0.6
===========
diff --git a/R/c.r b/R/c.r
index b968439..44e7eac 100644
--- a/R/c.r
+++ b/R/c.r
@@ -1,26 +1,25 @@
#' Join multiple strings into a single string.
#'
#' To understand how \code{str_c} works, you need to imagine that you are
-#' building up a matrix of strings. Each input argument forms a column, and
-#' is expanded to the length of the longest argument, using the usual
+#' building up a matrix of strings. Each input argument forms a column, and
+#' is expanded to the length of the longest argument, using the usual
#' recyling rules. The \code{sep} string is inserted between each column. If
#' collapse is \code{NULL} each row is collapsed into a single string. If
-#' non-\code{NULL} that string is inserted at the end of each row, and
+#' non-\code{NULL} that string is inserted at the end of each row, and
#' the entire matrix collapsed to a single string.
#'
-#' @param ... one or more character vectors. Zero length arguments
+#' @param ... one or more character vectors. Zero length arguments
#' are removed
#' @param sep string to insert between input vectors
#' @param collapse optional string used to combine input vectors into single
#' string
-#' @return If \code{collapse = NULL} (the default) a character vector with
+#' @return If \code{collapse = NULL} (the default) a character vector with
#' length equal to the longest input string. If \code{collapse} is non-
#' NULL, a character vector of length 1.
#' @keywords character
#' @seealso \code{\link{paste}} which this function wraps
#' @aliases str_c str_join
-#' @import plyr
-#' @export
+#' @export str_c str_join
#' @examples
#' str_c("Letter: ", letters)
#' str_c("Letter", letters, sep = ": ")
@@ -31,10 +30,10 @@
#' str_c(letters, collapse = ", ")
str_c <- str_join <- function(..., sep = "", collapse = NULL) {
strings <- Filter(function(x) length(x) > 0, list(...))
- if (!all(unlist(llply(strings, is.atomic)))) {
+ atomic <- vapply(strings, is.atomic, logical(1))
+ if (!all(atomic)) {
stop("Input to str_c should be atomic vectors", call. = FALSE)
}
-
-
+
do.call("paste", c(strings, list(sep = sep, collapse = collapse)))
}
diff --git a/R/checks.r b/R/checks.r
index d1455c0..613d610 100644
--- a/R/checks.r
+++ b/R/checks.r
@@ -1,19 +1,19 @@
-# Check that stringr is of the correct type for stringr functions
+# Check that string is of the correct type for stringr functions
check_string <- function(string) {
- if (!is.atomic(string))
+ if (!is.atomic(string))
stop("String must be an atomic vector", call. = FALSE)
-
- if (!is.character(string))
+
+ if (!is.character(string))
string <- as.character(string)
-
+
string
}
# Check that pattern is of the correct type for stringr functions
check_pattern <- function(pattern, string, replacement = NULL) {
- if (!is.character(pattern))
+ if (!is.character(pattern))
stop("Pattern must be a character vector", call. = FALSE)
-
+
if (!recyclable(string, pattern, replacement)) {
stop("Lengths of string and pattern not compatible")
}
diff --git a/R/count.r b/R/count.r
index a49c3ce..c737a29 100644
--- a/R/count.r
+++ b/R/count.r
@@ -11,7 +11,7 @@
#'
#' \code{\link{str_locate}}/\code{\link{str_locate_all}} to locate position
#' of matches
-#'
+#'
#' @export
#' @examples
#' fruit <- c("apple", "banana", "pear", "pineapple")
@@ -27,7 +27,7 @@ str_count <- function(string, pattern) {
if (length(pattern) == 1) {
matches <- re_call("gregexpr", string, pattern)
} else {
- matches <- unlist(re_mapply("gregexpr", string, pattern),
+ matches <- unlist(re_mapply("gregexpr", string, pattern),
recursive = FALSE)
}
diff --git a/R/detect.r b/R/detect.r
index 31a2e5f..2a8de1c 100644
--- a/R/detect.r
+++ b/R/detect.r
@@ -5,8 +5,8 @@
#' @param string input vector. This must be an atomic vector, and will be
#' coerced to a character vector
#' @param pattern pattern to look for, as defined by a POSIX regular
-#' expression. See the ``Extended Regular Expressions'' section of
-#' \code{\link{regex}} for details. See \code{\link{fixed}},
+#' expression. See the ``Extended Regular Expressions'' section of
+#' \code{\link{regex}} for details. See \code{\link{fixed}},
#' \code{\link{ignore.case}} and \code{\link{perl}} for how to use other
#' types of matching: fixed, case insensitive and perl-compatible.
#' @return boolean vector
@@ -26,13 +26,13 @@
str_detect <- function(string, pattern) {
string <- check_string(string)
pattern <- check_pattern(pattern, string)
-
+
if (length(pattern) == 1) {
results <- re_call("grepl", string, pattern)
} else {
results <- unlist(re_mapply("grepl", string, pattern))
}
is.na(results) <- is.na(string)
-
+
results
}
diff --git a/R/dup.r b/R/dup.r
index 084ee73..caf6a33 100644
--- a/R/dup.r
+++ b/R/dup.r
@@ -14,16 +14,16 @@
#' str_c("ba", str_dup("na", 0:5))
str_dup <- function(string, times) {
string <- check_string(string)
-
+
# Use data frame to do recycling
data <- data.frame(string, times)
n <- nrow(data)
string <- data$string
times <- data$times
-
- output <- unlist(lapply(seq_len(n), function(i){
+
+ output <- vapply(seq_len(n), function(i) {
paste(rep.int(string[i], times[i]), collapse = "")
- }))
+ }, character(1))
names(output) <- names(string)
output
diff --git a/R/extract.r b/R/extract.r
index ccde603..7ccae44 100644
--- a/R/extract.r
+++ b/R/extract.r
@@ -42,7 +42,7 @@ str_extract_all <- function(string, pattern) {
pattern <- check_pattern(pattern, string)
positions <- str_locate_all(string, pattern)
- llply(seq_along(string), function(i) {
+ lapply(seq_along(string), function(i) {
position <- positions[[i]]
str_sub(string[i], position[, "start"], position[, "end"])
})
diff --git a/R/length.r b/R/length.r
index 0c3394e..7e198d6 100644
--- a/R/length.r
+++ b/R/length.r
@@ -1,7 +1,7 @@
#' The length of a string (in characters).
#'
#' @inheritParams str_detect
-#' @return numeric vector giving number of characters in each element of the
+#' @return numeric vector giving number of characters in each element of the
#' character vector. Missing string have missing length.
#' @keywords character
#' @seealso \code{\link{nchar}} which this function wraps
diff --git a/R/locate.r b/R/locate.r
index 1b3f87e..ed74bd7 100644
--- a/R/locate.r
+++ b/R/locate.r
@@ -7,11 +7,11 @@
#' @return integer matrix. First column gives start postion of match, and
#' second column gives end position.
#' @keywords character
-#' @seealso
+#' @seealso
#' \code{\link{regexpr}} which this function wraps
-#'
+#'
#' \code{\link{str_extract}} for a convenient way of extracting matches
-#
+#
#' \code{\link{str_locate_all}} to locate position of all matches
#'
#' @export
@@ -29,7 +29,9 @@ str_locate <- function(string, pattern) {
match_to_matrix(results)
} else {
results <- re_mapply("regexpr", string, pattern)
- laply(results, match_to_matrix)
+ out <- t(vapply(results, match_to_matrix, integer(2)))
+ colnames(out) <- c("start", "end")
+ out
}
}
@@ -51,7 +53,7 @@ str_locate <- function(string, pattern) {
#' \code{\link{str_extract}} for a convenient way of extracting matches
#'
#' \code{\link{str_locate}} to locate position of first match
-#'
+#'
#' @export
#' @examples
#' fruit <- c("apple", "banana", "pear", "pineapple")
@@ -65,10 +67,10 @@ str_locate_all <- function(string, pattern) {
if (length(pattern) == 1) {
matches <- re_call("gregexpr", string, pattern)
} else {
- matches <- unlist(re_mapply("gregexpr", string, pattern),
+ matches <- unlist(re_mapply("gregexpr", string, pattern),
recursive = FALSE)
}
- llply(matches, match_to_matrix, global = TRUE)
+ lapply(matches, match_to_matrix, global = TRUE)
}
# Convert annoying regexpr format to something more useful
@@ -76,14 +78,14 @@ match_to_matrix <- function(match, global = FALSE) {
if (global && length(match) == 1 && (is.na(match) || match == -1)) {
null <- matrix(0, nrow = 0, ncol = 2)
colnames(null) <- c("start", "end")
-
+
return(null)
}
-
+
start <- as.vector(match)
start[start == -1] <- NA
end <- start + attr(match, "match.length") - 1L
-
+
cbind(start = start, end = end)
}
@@ -100,7 +102,7 @@ match_to_matrix <- function(match, global = FALSE) {
#' numbers <- "1 and 2 and 4 and 456"
#' num_loc <- str_locate_all(numbers, "[0-9]+")[[1]]
#' str_sub(numbers, num_loc[, "start"], num_loc[, "end"])
-#'
+#'
#' text_loc <- invert_match(num_loc)
#' str_sub(numbers, text_loc[, "start"], text_loc[, "end"])
invert_match <- function(loc) {
diff --git a/R/match.r b/R/match.r
index 6a8a60f..f4d4ae2 100644
--- a/R/match.r
+++ b/R/match.r
@@ -13,15 +13,15 @@
#' @keywords character
#' @export
#' @examples
-#' strings <- c(" 219 733 8965", "329-293-8753 ", "banana", "595 794 7569",
-#' "387 287 6718", "apple", "233.398.9187 ", "482 952 3315",
-#' "239 923 8115", "842 566 4692", "Work: 579-499-7527", "$1000",
+#' strings <- c(" 219 733 8965", "329-293-8753 ", "banana", "595 794 7569",
+#' "387 287 6718", "apple", "233.398.9187 ", "482 952 3315",
+#' "239 923 8115", "842 566 4692", "Work: 579-499-7527", "$1000",
#' "Home: 543.355.3679")
#' phone <- "([2-9][0-9]{2})[- .]([0-9]{3})[- .]([0-9]{4})"
#'
#' str_extract(strings, phone)
#' str_match(strings, phone)
-str_match <- function(string, pattern) {
+str_match <- function(string, pattern) {
string <- check_string(string)
pattern <- check_pattern(pattern, string)
@@ -29,15 +29,15 @@ str_match <- function(string, pattern) {
matcher <- re_call("regexec", string, pattern)
matches <- regmatches(string, matcher)
-
- # Figure out how many groups there are and coerce into a matrix with
+
+ # Figure out how many groups there are and coerce into a matrix with
# nmatches + 1 columns
tmp <- str_replace_all(pattern, "\\\\\\(", "")
n <- str_length(str_replace_all(tmp, "[^(]", "")) + 1
-
+
len <- vapply(matches, length, integer(1))
matches[len == 0] <- rep(list(rep(NA_character_, n)), sum(len == 0))
-
+
do.call("rbind", matches)
}
@@ -55,7 +55,7 @@ str_match <- function(string, pattern) {
#' @keywords character
#' @export
#' @examples
-#' strings <- c("Home: 219 733 8965. Work: 229-293-8753 ",
+#' strings <- c("Home: 219 733 8965. Work: 229-293-8753 ",
#' "banana pear apple", "595 794 7569 / 387 287 6718")
#' phone <- "([2-9][0-9]{2})[- .]([0-9]{3})[- .]([0-9]{4})"
#'
@@ -63,8 +63,8 @@ str_match <- function(string, pattern) {
#' str_match_all(strings, phone)
str_match_all <- function(string, pattern) {
matches <- str_extract_all(string, pattern)
-
- llply(matches, function(match) {
+
+ lapply(matches, function(match) {
str_match(match, pattern)
})
}
diff --git a/R/modifiers.r b/R/modifiers.r
index e5b275e..5f1947d 100644
--- a/R/modifiers.r
+++ b/R/modifiers.r
@@ -1,9 +1,9 @@
#' Match fixed characters, not regular expression.
-#'
+#'
#' This function specifies that a pattern is a fixed string, rather
-#' than a regular expression. This can yield substantial speed ups, if
+#' than a regular expression. This can yield substantial speed ups, if
#' regular expression matching is not needed.
-#'
+#'
#' @param string string to match exactly as is
#' @family modifiers
#' @keywords character
@@ -24,9 +24,9 @@ is.fixed <- function(string) {
}
#' Ignore case of match.
-#'
+#'
#' This function specifies that a pattern should ignore the case of matches.
-#'
+#'
#' @param string pattern for which to ignore case
#' @keywords character
#' @family modifiers
@@ -47,11 +47,11 @@ case.ignored <- function(string) {
#' Use perl regular expressions.
-#'
+#'
#' This function specifies that a pattern should use the Perl regular
#' expression egine, rather than the default POSIX 1003.2 extended
#' regular expressions
-#'
+#'
#' @param string pattern to match with Perl regexps
#' @family modifiers
#' @keywords character
diff --git a/R/pad-trim.r b/R/pad-trim.r
index 09e1c29..f4c98f1 100644
--- a/R/pad-trim.r
+++ b/R/pad-trim.r
@@ -1,7 +1,7 @@
#' Pad a string.
#'
#' Vectorised over \code{string}. All other inputs should be of length 1.
-#'
+#'
#' @param string input character vector
#' @param width pad strings to this minimum width
#' @param side side on which padding character is added (left, right or both)
@@ -26,24 +26,24 @@ str_pad <- function(string, width, side = "left", pad = " ") {
if (str_length(pad) != 1) {
stop("pad must be single character single")
}
-
- side <- match.arg(side, c("left", "right", "both"))
+
+ side <- match.arg(side, c("left", "right", "both"))
needed <- pmax(0, width - str_length(string))
-
- left <- switch(side,
+
+ left <- switch(side,
left = needed, right = 0, both = floor(needed / 2))
- right <- switch(side,
+ right <- switch(side,
left = 0, right = needed, both = ceiling(needed / 2))
-
+
# String duplication is slow, so only do the absolute necessary
lengths <- unique(c(left, right))
padding <- str_dup(pad, lengths)
-
+
str_c(padding[match(left, lengths)], string, padding[match(right, lengths)])
}
#' Trim whitespace from start and end of string.
-#'
+#'
#' @param string input character vector
#' @param side side on which whitespace is removed (left, right or both)
#' @return character vector with leading and trailing whitespace removed
@@ -57,8 +57,8 @@ str_trim <- function(string, side = "both") {
string <- check_string(string)
stopifnot(length(side) == 1)
- side <- match.arg(side, c("left", "right", "both"))
- pattern <- switch(side, left = "^\\s+", right = "\\s+$",
+ side <- match.arg(side, c("left", "right", "both"))
+ pattern <- switch(side, left = "^\\s+", right = "\\s+$",
both = "^\\s+|\\s+$")
str_replace_all(string, pattern, "")
diff --git a/R/replace.r b/R/replace.r
index bb73002..f5c574a 100644
--- a/R/replace.r
+++ b/R/replace.r
@@ -1,22 +1,22 @@
#' Replace first occurrence of a matched pattern in a string.
#'
-#' Vectorised over \code{string}, \code{pattern} and \code{replacement}.
-#' Shorter arguments will be expanded to length of longest.
+#' Vectorised over \code{string}, \code{pattern} and \code{replacement}.
+#' Shorter arguments will be expanded to length of longest.
#'
#' @inheritParams str_detect
-#' @param replacement replacement string. References of the form \code{\1},
+#' @param replacement replacement string. References of the form \code{\1},
#' \code{\2} will be replaced with the contents of the respective matched
#' group (created by \code{()}) within the pattern.
#' @return character vector.
#' @keywords character
-#' @seealso \code{\link{sub}} which this function wraps,
+#' @seealso \code{\link{sub}} which this function wraps,
#' \code{\link{str_replace_all}} to replace all matches
#' @export
#' @examples
#' fruits <- c("one apple", "two pears", "three bananas")
#' str_replace(fruits, "[aeiou]", "-")
#' str_replace_all(fruits, "[aeiou]", "-")
-#'
+#'
#' str_replace(fruits, "([aeiou])", "")
#' str_replace(fruits, "([aeiou])", "\\1\\1")
#' str_replace(fruits, "[aeiou]", c("1", "2", "3"))
@@ -34,23 +34,23 @@ str_replace <- function(string, pattern, replacement) {
#' Replace all occurrences of a matched pattern in a string.
#'
-#' Vectorised over \code{string}, \code{pattern} and \code{replacement}.
-#' Shorter arguments will be expanded to length of longest.
+#' Vectorised over \code{string}, \code{pattern} and \code{replacement}.
+#' Shorter arguments will be expanded to length of longest.
#'
#' @inheritParams str_detect
-#' @param replacement replacement string. References of the form \code{\1},
+#' @param replacement replacement string. References of the form \code{\1},
#' \code{\2} will be replaced with the contents of the respective matched
#' group (created by \code{()}) within the pattern.
#' @return character vector.
#' @keywords character
-#' @seealso \code{\link{gsub}} which this function wraps,
-#' \code{\link{str_replace_all}} to replace a single match
+#' @seealso \code{\link{gsub}} which this function wraps,
+#' \code{\link{str_replace}} to replace a single match
#' @export
#' @examples
#' fruits <- c("one apple", "two pears", "three bananas")
#' str_replace(fruits, "[aeiou]", "-")
#' str_replace_all(fruits, "[aeiou]", "-")
-#'
+#'
#' str_replace_all(fruits, "([aeiou])", "")
#' str_replace_all(fruits, "([aeiou])", "\\1\\1")
#' str_replace_all(fruits, "[aeiou]", c("1", "2", "3"))
diff --git a/R/split.r b/R/split.r
index b92b9ce..5f6e52e 100644
--- a/R/split.r
+++ b/R/split.r
@@ -2,14 +2,14 @@
#'
#' Vectorised over \code{string}. \code{pattern} should be a single pattern,
#' i.e. a character vector of length one.
-#'
+#'
#' @param string input character vector
#' @param pattern pattern to split up by, as defined by a POSIX regular
-#' expression. See the ``Extended Regular Expressions'' section of
-#' \code{\link{regex}} for details. If \code{NA}, returns original string.
+#' expression. See the ``Extended Regular Expressions'' section of
+#' \code{\link{regex}} for details. If \code{NA}, returns original string.
#' If \code{""} splits into individual characters.
#' @param n number of pieces to return. Default (Inf) uses all
-#' possible split positions. If n is greater than the number of pieces,
+#' possible split positions. If n is greater than the number of pieces,
#' the result will be padded with empty strings.
#' @return character matrix with \code{n} columns.
#' @keywords character
@@ -23,46 +23,48 @@
#' str_split_fixed(fruits, " and ", 3)
#' str_split_fixed(fruits, " and ", 4)
str_split_fixed <- function(string, pattern, n) {
- if (length(string) == 0) return(matrix(character(), nrow = n, ncol = 1))
+ if (length(string) == 0) {
+ return(matrix(character(), nrow = 0, ncol = n))
+ }
string <- check_string(string)
pattern <- check_pattern(pattern, string)
-
+
if (!is.numeric(n) || length(n) != 1) {
stop("n should be a numeric vector of length 1")
}
-
+
if (n == Inf) {
stop("n must be finite", call. = FALSE)
} else if (n == 1) {
matrix(string, ncol = 1)
} else {
locations <- str_locate_all(string, pattern)
- do.call("rbind", llply(seq_along(locations), function(i) {
+ do.call("rbind", lapply(seq_along(locations), function(i) {
location <- locations[[i]]
string <- string[i]
-
+
pieces <- min(n - 1, nrow(location))
cut <- location[seq_len(pieces), , drop = FALSE]
keep <- invert_match(cut)
-
+
padding <- rep("", n - pieces - 1)
c(str_sub(string, keep[, 1], keep[, 2]), padding)
}))
- }
+ }
}
#' Split up a string into a variable number of pieces.
-#'
+#'
#' Vectorised over \code{string}. \code{pattern} should be a single pattern,
#' i.e. a character vector of length one.
-#'
+#'
#' @param string input character vector
#' @param pattern pattern to split up by, as defined by a POSIX regular
-#' expression. See the ``Extended Regular Expressions'' section of
-#' \code{\link{regex}} for details. If \code{NA}, returns original string.
+#' expression. See the ``Extended Regular Expressions'' section of
+#' \code{\link{regex}} for details. If \code{NA}, returns original string.
#' If \code{""} splits into individual characters.
#' @param n maximum number of pieces to return. Default (Inf) uses all
-#' possible split positions.
+#' possible split positions.
#' @return a list of character vectors.
#' @keywords character
#' @export
@@ -87,16 +89,18 @@ str_split <- function(string, pattern, n = Inf) {
if (!is.numeric(n) || length(n) != 1) {
stop("n should be a numeric vector of length 1")
}
-
+
if (n == 1) {
as.list(string)
} else {
locations <- str_locate_all(string, pattern)
- unname(c(mlply(cbind(mat = locations, string = string), function(mat, string) {
+ pieces <- function(mat, string) {
cut <- mat[seq_len(min(n - 1, nrow(mat))), , drop = FALSE]
keep <- invert_match(cut)
-
+
str_sub(string, keep[, 1], keep[, 2])
- })))
+ }
+ mapply(pieces, locations, string,
+ SIMPLIFY = FALSE, USE.NAMES = FALSE)
}
}
diff --git a/R/sub.r b/R/sub.r
index 9bce4b1..6bf73c6 100644
--- a/R/sub.r
+++ b/R/sub.r
@@ -1,24 +1,24 @@
#' Extract substrings from a character vector.
#'
-#' \code{str_sub} will recycle all arguments to be the same length as the
-#' longest argument. If any arguments are of length 0, the output will be
+#' \code{str_sub} will recycle all arguments to be the same length as the
+#' longest argument. If any arguments are of length 0, the output will be
#' a zero length character vector.
#'
-#' Substrings are inclusive - they include the characters at both start and
+#' Substrings are inclusive - they include the characters at both start and
#' end positions. \code{str_sub(string, 1, -1)} will return the complete
#' substring, from the first character to the last.
#'
#' @param string input character vector.
-#' @param start integer vector giving position of first charater in substring,
-#' defaults to first character. If negative, counts backwards from last
+#' @param start integer vector giving position of first charater in substring,
+#' defaults to first character. If negative, counts backwards from last
#' character.
-#' @param end integer vector giving position of last character in substring,
-#' defaults to last character. If negative, counts backwards from last
+#' @param end integer vector giving position of last character in substring,
+#' defaults to last character. If negative, counts backwards from last
#' character.
#' @return character vector of substring from \code{start} to \code{end}
#' (inclusive). Will be length of longest input argument.
#' @keywords character
-#' @seealso \code{\link{substring}} which this function wraps, and
+#' @seealso \code{\link{substring}} which this function wraps, and
#' \code{link{str_sub_replace}} for the replacement version
#' @export
#' @examples
@@ -42,12 +42,12 @@ str_sub <- function(string, start = 1L, end = -1L) {
}
string <- check_string(string)
-
+
n <- max(length(string), length(start), length(end))
string <- rep(string, length = n)
start <- rep(start, length = n)
end <- rep(end, length = n)
-
+
# Convert negative values into actual positions
len <- str_length(string)
@@ -56,21 +56,21 @@ str_sub <- function(string, start = 1L, end = -1L) {
neg_end <- !is.na(end) & end < 0L
end[neg_end] <- end[neg_end] + len[neg_end] + 1L
-
+
substring(string, start, end)
}
#' Replace substrings in a character vector.
-#
-#' \code{str_sub<-} will recycle all arguments to be the same length as the
-#' longest argument.
-#'
+#
+#' \code{str_sub<-} will recycle all arguments to be the same length as the
+#' longest argument.
+#'
#' @param string input character vector.
-#' @param start integer vector giving position of first charater in substring,
-#' defaults to first character. If negative, counts backwards from last
+#' @param start integer vector giving position of first charater in substring,
+#' defaults to first character. If negative, counts backwards from last
#' character.
-#' @param end integer vector giving position of last character in substring,
-#' defaults to last character. If negative, counts backwards from last
+#' @param end integer vector giving position of last character in substring,
+#' defaults to last character. If negative, counts backwards from last
#' character.
#' @param value replacement string
#' @return character vector of substring from \code{start} to \code{end}
@@ -86,9 +86,9 @@ str_sub <- function(string, start = 1L, end = -1L) {
#' str_sub(x, -2, -2) <- "GHIJ"; x
#' str_sub(x, 2, -2) <- ""; x
"str_sub<-" <- function(string, start = 1L, end = -1L, value) {
-
+
str_c(
- str_sub(string, end = start - 1L),
+ str_sub(string, end = start - 1L),
value,
ifelse(end == -1L, "", str_sub(string, start = end + 1L)))
}
diff --git a/R/utils.r b/R/utils.r
new file mode 100644
index 0000000..2c830e0
--- /dev/null
+++ b/R/utils.r
@@ -0,0 +1 @@
+compact <- function(l) Filter(Negate(is.null), l)
diff --git a/R/vectorise.r b/R/vectorise.r
index af753d9..450b9da 100644
--- a/R/vectorise.r
+++ b/R/vectorise.r
@@ -1,18 +1,18 @@
# General wrapper around sub, gsub, regexpr, gregexpr, grepl.
-# Vectorises with pattern and replacement, and uses fixed and ignored.case
+# Vectorises with pattern and replacement, and uses fixed and ignored.case
# attributes.
re_call <- function(f, string, pattern, replacement = NULL) {
args <- list(pattern, replacement, string,
- fixed = is.fixed(pattern), ignore.case = case.ignored(pattern),
+ fixed = is.fixed(pattern), ignore.case = case.ignored(pattern),
perl = is.perl(pattern))
-
+
if (!("perl" %in% names(formals(f)))) {
if (args$perl) message("Perl regexps not supported by ", f)
args$perl <- NULL
}
-
- do.call(f, compact(args))
+
+ do.call(f, compact(args))
}
re_mapply <- function(f, string, pattern, replacement = NULL) {
@@ -20,7 +20,7 @@ re_mapply <- function(f, string, pattern, replacement = NULL) {
FUN = f, SIMPLIFY = FALSE, USE.NAMES = FALSE,
pattern, replacement, string,
MoreArgs = list(
- fixed = is.fixed(pattern),
+ fixed = is.fixed(pattern),
ignore.case = case.ignored(pattern))
)
do.call("mapply", compact(args))
@@ -30,9 +30,9 @@ re_mapply <- function(f, string, pattern, replacement = NULL) {
# Ignores zero length vectors. Trivially TRUE if all inputs are zero length.
recyclable <- function(...) {
lengths <- vapply(list(...), length, integer(1))
-
+
lengths <- lengths[lengths != 0]
if (length(lengths) == 0) return(TRUE)
-
+
all(max(lengths) %% lengths == 0)
}
diff --git a/R/word.r b/R/word.r
index 70a936d..e883ef1 100644
--- a/R/word.r
+++ b/R/word.r
@@ -1,11 +1,11 @@
#' Extract words from a sentence.
-#'
+#'
#' @param string input character vector.
#' @param start integer vector giving position of first word to extract.
-#' Defaults to first word. If negative, counts backwards from last
+#' Defaults to first word. If negative, counts backwards from last
#' character.
-#' @param end integer vector giving position of last word to extract.
-#' Defaults to first word. If negative, counts backwards from last
+#' @param end integer vector giving position of last word to extract.
+#' Defaults to first word. If negative, counts backwards from last
#' character.
#' @param sep separator between words. Defaults to single space.
#' @return character vector of words from \code{start} to \code{end}
@@ -31,23 +31,23 @@ word <- function(string, start = 1L, end = start, sep = fixed(" ")) {
string <- rep(string, length = n)
start <- rep(start, length = n)
end <- rep(end, length = n)
-
+
breaks <- str_locate_all(string, sep)
words <- lapply(breaks, invert_match)
-
+
# Convert negative values into actual positions
len <- vapply(words, nrow, integer(1))
-
+
neg_start <- !is.na(start) & start < 0L
start[neg_start] <- start[neg_start] + len[neg_start] + 1L
-
+
neg_end <- !is.na(end) & end < 0L
end[neg_end] <- end[neg_end] + len[neg_end] + 1L
-
+
# Extract locations
starts <- mapply(function(word, loc) word[loc, "start"], words, start)
ends <- mapply(function(word, loc) word[loc, "end"], words, end)
-
+
str_sub(string, starts, ends)
}
diff --git a/R/wrap.r b/R/wrap.r
index 5db493e..2a2d98a 100644
--- a/R/wrap.r
+++ b/R/wrap.r
@@ -1,19 +1,20 @@
#' Wrap strings into nicely formatted paragraphs.
-#'
+#'
#' This is currently implemented as thin wrapper over \code{\link{strwrap}},
#' but is vectorised over \code{stringr}, and collapses output into single
#' strings. See \code{\link{strwrap}} for more details.
-#'
+#'
#' @param string character vector of strings to reformat.
#' @param width positive integer giving target line width in characters.
-#' @param indent non-negative integer giving indentation of first line in
+#' @param indent non-negative integer giving indentation of first line in
#' each paragraph
-#' @param exdent non-negative integer giving indentation of following lines in
+#' @param exdent non-negative integer giving indentation of following lines in
#' each paragraph
#' @return a character vector of reformatted strings.
#' @export
#' @examples
-#' thanks <- str_c(readLines(R.home("doc/THANKS")), collapse = "\n")
+#' thanks_path <- file.path(R.home("doc"), "THANKS")
+#' thanks <- str_c(readLines(thanks_path), collapse = "\n")
#' thanks <- word(thanks, 1, 3, fixed("\n\n"))
#' cat(str_wrap(thanks), "\n")
#' cat(str_wrap(thanks, width = 40), "\n")
diff --git a/inst/tests/test-check.r b/inst/tests/test-check.r
index 16f1940..da71498 100644
--- a/inst/tests/test-check.r
+++ b/inst/tests/test-check.r
@@ -1,22 +1,22 @@
context("String and pattern checks")
test_that("string is atomic", {
- expect_that(check_string(list()),
+ expect_that(check_string(list()),
throws_error("must be an atomic"))
})
test_that("pattern is a string", {
- expect_that(check_pattern(1),
+ expect_that(check_pattern(1),
throws_error("must be a character vector"))
})
test_that("error when string and pattern lengths incompatible", {
expect_that(check_pattern(letters, "a"), equals(letters))
expect_that(check_pattern("a", letters), equals("a"))
-
- expect_that(check_pattern(c("a", "b", "c"), c("a", "b")),
+
+ expect_that(check_pattern(c("a", "b", "c"), c("a", "b")),
throws_error("not compatible"))
- expect_that(check_pattern(c("a", "b"), c("a", "b", "c")),
+ expect_that(check_pattern(c("a", "b"), c("a", "b", "c")),
throws_error("not compatible"))
})
diff --git a/inst/tests/test-count.r b/inst/tests/test-count.r
index 90cc66f..77dad99 100644
--- a/inst/tests/test-count.r
+++ b/inst/tests/test-count.r
@@ -6,6 +6,6 @@ test_that("counts are as expected", {
expect_equal(str_count(fruit, "p"), c(2, 0, 1, 3))
expect_equal(str_count(fruit, "e"), c(1, 0, 1, 2))
expect_equal(str_count(fruit, c("a", "b", "p", "n")), c(1, 1, 1, 1))
-
-
+
+
})
diff --git a/inst/tests/test-detect.r b/inst/tests/test-detect.r
index ceb0297..d8edafb 100644
--- a/inst/tests/test-detect.r
+++ b/inst/tests/test-detect.r
@@ -2,7 +2,7 @@ context("Detecting patterns")
test_that("special cases are correct", {
expect_that(str_detect(NA, ""), equals(NA))
- expect_that(str_detect(character(), ""), equals(logical()))
+ expect_that(str_detect(character(), ""), equals(logical()))
})
test_that("vectorised patterns work", {
@@ -13,11 +13,11 @@ test_that("vectorised patterns work", {
test_that("modifiers work", {
expect_that(str_detect("ab", "AB"), equals(FALSE))
expect_that(str_detect("ab", ignore.case("AB")), equals(TRUE))
-
+
expect_that(str_detect("abc", "ab[c]"), equals(TRUE))
expect_that(str_detect("abc", fixed("ab[c]")), equals(FALSE))
expect_that(str_detect("ab[c]", fixed("ab[c]")), equals(TRUE))
-
+
expect_that(str_detect("abc", perl("(?x)a b c")), equals(TRUE))
-
-})
\ No newline at end of file
+
+})
diff --git a/inst/tests/test-dup.r b/inst/tests/test-dup.r
index f837272..4932648 100644
--- a/inst/tests/test-dup.r
+++ b/inst/tests/test-dup.r
@@ -4,10 +4,10 @@ test_that("basic duplication works", {
expect_that(str_dup("a", 3), equals("aaa"))
expect_that(str_dup("abc", 2), equals("abcabc"))
expect_that(str_dup(c("a", "b"), 2), equals(c("aa", "bb")))
- expect_that(str_dup(c("a", "b"), c(2, 3)), equals(c("aa", "bbb")))
+ expect_that(str_dup(c("a", "b"), c(2, 3)), equals(c("aa", "bbb")))
})
test_that("0 duplicates equals empty string", {
expect_that(str_dup("a", 0), equals(""))
expect_that(str_dup(c("a", "b"), 0), equals(rep("", 2)))
-})
\ No newline at end of file
+})
diff --git a/inst/tests/test-extract.r b/inst/tests/test-extract.r
index 085da0e..06fd89b 100644
--- a/inst/tests/test-extract.r
+++ b/inst/tests/test-extract.r
@@ -2,13 +2,13 @@ context("Extract patterns")
test_that("single pattern extracted correctly", {
test <- c("one two three", "a b c")
-
+
expect_that(
str_extract_all(test, "[a-z]+"),
equals(list(c("one", "two", "three"), c("a", "b", "c"))))
-
+
expect_that(
str_extract_all(test, "[a-z]{3,}"),
equals(list(c("one", "two", "three"), character())))
-
+
})
diff --git a/inst/tests/test-join.r b/inst/tests/test-join.r
index f6cb20b..bd71139 100644
--- a/inst/tests/test-join.r
+++ b/inst/tests/test-join.r
@@ -2,7 +2,7 @@ context("Joining strings")
test_that("basic case works", {
test <- c("a", "b", "c")
-
+
expect_that(str_c(test), equals(test))
expect_that(str_c(test, sep = " "), equals(test))
expect_that(str_c(test, collapse = ""), equals("abc"))
@@ -10,11 +10,11 @@ test_that("basic case works", {
test_that("zero length vectors dropped", {
test <- letters[1:3]
-
+
expect_that(str_c(test, c()), equals(test))
expect_that(str_c(test, NULL), equals(test))
expect_that(
- str_c(test, NULL, "a", sep = " "),
+ str_c(test, NULL, "a", sep = " "),
equals(c("a a", "b a", "c a")))
-})
\ No newline at end of file
+})
diff --git a/inst/tests/test-locate.r b/inst/tests/test-locate.r
index aa3b7af..e5da63f 100644
--- a/inst/tests/test-locate.r
+++ b/inst/tests/test-locate.r
@@ -1,32 +1,32 @@
context("Locations")
test_that("basic location matching works", {
- expect_that(str_locate("abc", "a")[1, ], equals(c(1, 1),
+ expect_that(str_locate("abc", "a")[1, ], equals(c(1, 1),
check.attributes = F))
- expect_that(str_locate("abc", "b")[1, ], equals(c(2, 2),
+ expect_that(str_locate("abc", "b")[1, ], equals(c(2, 2),
check.attributes = F))
- expect_that(str_locate("abc", "c")[1, ], equals(c(3, 3),
+ expect_that(str_locate("abc", "c")[1, ], equals(c(3, 3),
check.attributes = F))
-
- expect_that(str_locate("abc", ".+")[1, ], equals(c(1, 3),
+
+ expect_that(str_locate("abc", ".+")[1, ], equals(c(1, 3),
check.attributes = F))
})
test_that("locations are integers", {
strings <- c("a b c", "d e f")
expect_that(is.integer(str_locate(strings, "[a-z]")), is_true())
-
+
res <- str_locate_all(strings, "[a-z]")[[1]]
- expect_that(is.integer(res), is_true())
+ expect_that(is.integer(res), is_true())
expect_that(is.integer(invert_match(res)), is_true())
})
test_that("both string and patterns are vectorised", {
strings <- c("abc", "def")
-
+
locs <- str_locate(strings, "a")
expect_that(locs[, "start"], equals(c(1, NA)))
-
+
locs <- str_locate(strings, c("a", "d"))
expect_that(locs[, "start"], equals(c(1, 1)))
expect_that(locs[, "end"], equals(c(1, 1)))
@@ -34,4 +34,4 @@ test_that("both string and patterns are vectorised", {
locs <- str_locate_all(c("abab"), c("a", "b"))
expect_that(locs[[1]][, "start"], equals(c(1, 3)))
expect_that(locs[[2]][, "start"], equals(c(2, 4)))
-})
\ No newline at end of file
+})
diff --git a/inst/tests/test-match.r b/inst/tests/test-match.r
index 8ed8697..92130bb 100644
--- a/inst/tests/test-match.r
+++ b/inst/tests/test-match.r
@@ -2,60 +2,60 @@ context("Matching groups")
set.seed(1410)
num <- matrix(sample(9, 10 * 10, rep = T), ncol = 10)
-num_flat <- aaply(num, 1, str_c, collapse = "")
+num_flat <- apply(num, 1, str_c, collapse = "")
phones <- str_c(
"(", num[, 1], num[ ,2], num[, 3], ") ",
- num[, 4], num[, 5], num[, 6], " ",
+ num[, 4], num[, 5], num[, 6], " ",
num[, 7], num[, 8], num[, 9], num[, 10])
test_that("special case are correct", {
# These tests really should compare to character matrices, but str_match
# returns matrices with dimnames set it's real pain
- expect_that(c(str_match(NA, "(a)")),
+ expect_that(c(str_match(NA, "(a)")),
equals(c(NA_character_, NA_character_)))
- expect_that(c(str_match(character(), "(a)")),
+ expect_that(c(str_match(character(), "(a)")),
equals(character()))
})
test_that("no matching cases returns 1 column matrix", {
res <- str_match(c("a", "b"), ".")
-
+
expect_that(nrow(res), equals(2))
expect_that(ncol(res), equals(1))
-
+
expect_that(res[, 1], equals(c("a", "b")))
})
test_that("single match works when all match", {
matches <- str_match(phones, "\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})")
-
+
expect_that(nrow(matches), equals(length(phones)))
expect_that(ncol(matches), equals(4))
-
+
expect_that(matches[, 1], equals(phones))
-
- matches_flat <- aaply(matches[, -1], 1, str_c, collapse = "")
+
+ matches_flat <- apply(matches[, -1], 1, str_c, collapse = "")
expect_that(matches_flat, equals(num_flat))
})
test_that("single match works when some don't match", {
- matches <- str_match(c(phones, "blah", NA),
+ matches <- str_match(c(phones, "blah", NA),
"\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})")
-
+
expect_that(nrow(matches), equals(length(phones) + 2))
expect_that(ncol(matches), equals(4))
-
+
expect_that(matches[11, ], equals(rep(NA_character_, 4)))
expect_that(matches[12, ], equals(rep(NA_character_, 4)))
})
test_that("multiple match works", {
phones_one <- str_c(phones, collapse = " ")
- multi_match <- str_match_all(phones_one,
+ multi_match <- str_match_all(phones_one,
"\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})")
- single_matches <- str_match(phones,
+ single_matches <- str_match(phones,
"\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})")
-
+
expect_that(multi_match[[1]], equals(single_matches))
-})
\ No newline at end of file
+})
diff --git a/inst/tests/test-pad.r b/inst/tests/test-pad.r
index cf6ede4..06a38be 100644
--- a/inst/tests/test-pad.r
+++ b/inst/tests/test-pad.r
@@ -2,18 +2,19 @@ context("Test padding")
test_that("long strings are unchanged", {
lengths <- sample(40:100, 10)
- strings <- laply(lengths, function(x)
- str_c(letters[sample(26, x, rep = T)], collapse = ""))
-
+ strings <- vapply(lengths, function(x)
+ str_c(letters[sample(26, x, rep = T)], collapse = ""),
+ character(1))
+
padded <- str_pad(strings, width = 30)
expect_that(str_length(padded), equals(str_length(padded)))
})
test_that("directions work for simple case", {
-
+
pad <- function(direction) str_pad("had", direction, width = 10)
-
+
expect_that(pad("right"), equals("had "))
expect_that(pad("left"), equals(" had"))
expect_that(pad("both"), equals(" had "))
-})
\ No newline at end of file
+})
diff --git a/inst/tests/test-split.r b/inst/tests/test-split.r
index e3fb4df..942bf9e 100644
--- a/inst/tests/test-split.r
+++ b/inst/tests/test-split.r
@@ -2,21 +2,21 @@ context("Splitting strings")
test_that("special cases are correct", {
expect_that(str_split(NA, "")[[1]], equals(NA_character_))
- expect_that(str_split(character(), ""), equals(list()))
+ expect_that(str_split(character(), ""), equals(list()))
})
test_that("str_split functions as expected", {
test <- c("bab", "cac", "dadad")
result <- str_split(test, "a")
-
+
expect_that(result, is_a("list"))
expect_that(length(result), equals(3))
-
- lengths <- sapply(result, length)
+
+ lengths <- vapply(result, length, integer(1))
expect_that(lengths, equals(c(2, 2, 3)))
-
+
expect_that(result, equals(
- list(c("b", "b"), c("c", "c"), c("d", "d", "d"))))
+ list(c("b", "b"), c("c", "c"), c("d", "d", "d"))))
})
test_that("vectors give correct results dealt with correctly", {
@@ -26,44 +26,44 @@ test_that("vectors give correct results dealt with correctly", {
expect_that(result, is_a("matrix"))
expect_that(nrow(result), equals(4))
expect_that(ncol(result), equals(3))
-
+
expect_that(result[1, ], equals(c("b", "b", "")))
expect_that(result[3, ], equals(c("d", "d", "d")))
expect_that(result[, 1], equals(c("b", "c", "d", "e")))
-
+
})
test_that("n sets maximum number of splits in str_split", {
test <- "Subject: Roger: his drinking problems"
-
+
expect_that(length(str_split(test, ": ")[[1]]), equals(3))
expect_that(length(str_split(test, ": ", 4)[[1]]), equals(3))
expect_that(length(str_split(test, ": ", 3)[[1]]), equals(3))
expect_that(length(str_split(test, ": ", 2)[[1]]), equals(2))
expect_that(length(str_split(test, ": ", 1)[[1]]), equals(1))
-
+
expect_that(
- str_split(test, ": ", 3)[[1]],
+ str_split(test, ": ", 3)[[1]],
equals(c("Subject", "Roger", "his drinking problems")))
expect_that(
- str_split(test, ": ", 2)[[1]],
+ str_split(test, ": ", 2)[[1]],
equals(c("Subject", "Roger: his drinking problems")))
-
+
})
test_that("n sets exact number of splits in str_split_fixed", {
test <- "Subject: Roger: his drinking problems"
-
+
expect_that(ncol(str_split_fixed(test, ": ", 4)), equals(4))
expect_that(ncol(str_split_fixed(test, ": ", 3)), equals(3))
expect_that(ncol(str_split_fixed(test, ": ", 2)), equals(2))
expect_that(ncol(str_split_fixed(test, ": ", 1)), equals(1))
-
+
expect_that(
- str_split_fixed(test, ": ", 3)[1, ],
+ str_split_fixed(test, ": ", 3)[1, ],
equals(c("Subject", "Roger", "his drinking problems")))
expect_that(
- str_split_fixed(test, ": ", 2)[1, ],
+ str_split_fixed(test, ": ", 2)[1, ],
equals(c("Subject", "Roger: his drinking problems")))
-
+
})
diff --git a/inst/tests/test-sub.r b/inst/tests/test-sub.r
index 95f6461..df7db7f 100644
--- a/inst/tests/test-sub.r
+++ b/inst/tests/test-sub.r
@@ -8,31 +8,31 @@ test_that("correct substring extracted", {
test_that("arguments expanded to longest", {
alphabet <- str_c(letters, collapse = "")
-
+
expect_that(
- str_sub(alphabet, c(1, 24), c(3, 26)),
+ str_sub(alphabet, c(1, 24), c(3, 26)),
equals(c("abc", "xyz")))
expect_that(
- str_sub(c("abc", "xyz"), 2, 2),
- equals(c("b", "y")))
+ str_sub(c("abc", "xyz"), 2, 2),
+ equals(c("b", "y")))
})
test_that("specifying only end subsets from start", {
- expect_that(str_sub(alphabet, end = 3), equals(c("abc")))
+ expect_that(str_sub(alphabet, end = 3), equals(c("abc")))
})
test_that("specifying only start subsets to end", {
- expect_that(str_sub(alphabet, 24), equals(c("xyz")))
+ expect_that(str_sub(alphabet, 24), equals(c("xyz")))
})
test_that("specifying -1 as end selects entire string", {
expect_that(
str_sub("ABCDEF", c(4, 5), c(5, -1)),
equals(c("DE", "EF"))
- )
-
+ )
+
expect_that(
str_sub("ABCDEF", c(4, 5), c(-1, -1)),
equals(c("DEF", "EF"))
@@ -48,25 +48,25 @@ test_that("missing arguments give missing results", {
expect_that(str_sub(NA), equals(NA_character_))
expect_that(str_sub(NA, 1, 3), equals(NA_character_))
expect_that(str_sub(c(NA, "NA"), 1, 3), equals(c(NA, "NA")))
-
+
expect_that(str_sub("test", NA, NA), equals(NA_character_))
expect_that(str_sub(c(NA, "test"), NA, NA), equals(rep(NA_character_, 2)))
-
+
})
test_that("replacement works", {
x <- "BBCDEF"
str_sub(x, 1, 1) <- "A"
expect_that(x, equals("ABCDEF"))
-
+
str_sub(x, -1, -1) <- "K"
- expect_that(x, equals("ABCDEK"))
-
+ expect_that(x, equals("ABCDEK"))
+
str_sub(x, -2, -1) <- "EFGH"
- expect_that(x, equals("ABCDEFGH"))
+ expect_that(x, equals("ABCDEFGH"))
str_sub(x, 2, -2) <- ""
expect_that(x, equals("AH"))
-
-
-})
\ No newline at end of file
+
+
+})
diff --git a/inst/tests/test-trim.r b/inst/tests/test-trim.r
index 752bc1b..3cb7c13 100644
--- a/inst/tests/test-trim.r
+++ b/inst/tests/test-trim.r
@@ -16,5 +16,5 @@ test_that("trimming removes tabs", {
test_that("side argument restricts trimming", {
expect_that(str_trim(" abc ", "left"), equals("abc "))
- expect_that(str_trim(" abc ", "right"), equals(" abc"))
-})
\ No newline at end of file
+ expect_that(str_trim(" abc ", "right"), equals(" abc"))
+})
diff --git a/man/str_replace_all.Rd b/man/str_replace_all.Rd
index f5d9307..d5363f5 100644
--- a/man/str_replace_all.Rd
+++ b/man/str_replace_all.Rd
@@ -40,7 +40,7 @@ str_replace_all(fruits, c("a", "e", "i"), "-")
}
\seealso{
\code{\link{gsub}} which this function wraps,
- \code{\link{str_replace_all}} to replace a single match
+ \code{\link{str_replace}} to replace a single match
}
\keyword{character}
diff --git a/man/str_sub_replace.Rd b/man/str_sub_replace.Rd
index 8d30fea..43002b2 100644
--- a/man/str_sub_replace.Rd
+++ b/man/str_sub_replace.Rd
@@ -1,6 +1,6 @@
\name{str_sub_replace}
-\alias{str_sub_replace}
\alias{str_sub<-}
+\alias{str_sub_replace}
\title{Replace substrings in a character vector.
\code{str_sub<-} will recycle all arguments to be the same length as the
longest argument.}
diff --git a/man/str_wrap.Rd b/man/str_wrap.Rd
index 7409a96..6869676 100644
--- a/man/str_wrap.Rd
+++ b/man/str_wrap.Rd
@@ -26,7 +26,8 @@
See \code{\link{strwrap}} for more details.
}
\examples{
-thanks <- str_c(readLines(R.home("doc/THANKS")), collapse = "\\n")
+thanks_path <- file.path(R.home("doc"), "THANKS")
+thanks <- str_c(readLines(thanks_path), collapse = "\\n")
thanks <- word(thanks, 1, 3, fixed("\\n\\n"))
cat(str_wrap(thanks), "\\n")
cat(str_wrap(thanks, width = 40), "\\n")
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/r-cran-stringr.git
More information about the debian-med-commit
mailing list