Skip to content

Commit 7a12f4d

Browse files
committed
Don't special-case if string is logical NA. Restore switch() in str_subset().
Remove tests for special-casing NA. Remove a few redundant if's. Comment on copy_names() and keep_names() in utils.R.
1 parent 9471b98 commit 7a12f4d

File tree

7 files changed

+25
-37
lines changed

7 files changed

+25
-37
lines changed

R/sub.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,5 @@ str_sub_all <- function(string, start = 1L, end = -1L) {
9494
} else {
9595
stri_sub_all(string, from = start, to = end)
9696
}
97-
if (length(out) == length(string)) copy_names(string, out) else out
97+
copy_names(string, out)
9898
}

R/subset.R

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,17 @@ str_subset <- function(string, pattern, negate = FALSE) {
3030
check_lengths(string, pattern)
3131
check_bool(negate)
3232

33-
if (type(pattern) == "empty") no_empty()
34-
if (type(pattern) == "bound") no_boundary()
33+
idx <- switch(
34+
type(pattern),
35+
empty = no_empty(),
36+
bound = no_boundary(),
37+
fixed = str_detect(string, pattern, negate = negate),
38+
coll = str_detect(string, pattern, negate = negate),
39+
regex = str_detect(string, pattern, negate = negate)
40+
)
3541

36-
idx <- str_detect(string, pattern, negate = negate)
37-
# str_detect() returns NA for NAs in string, but str_subset() should drop them
3842
idx[is.na(idx)] <- FALSE
39-
out <- string[idx]
40-
# Work around the fact that as.character() drops names
41-
nm <- names(out)
42-
out <- as.character(out)
43-
names(out) <- nm
44-
out
43+
string[idx]
4544
}
4645

4746
#' Find matching indices

R/unique.R

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ str_unique <- function(string, locale = "en", ignore_case = FALSE, ...) {
2727
...
2828
)
2929

30-
# Ensure character output while preserving names of first occurrences
31-
string_chr <- as.character(string)
32-
keep <- !stringi::stri_duplicated(string_chr, opts_collator = opts)
33-
out <- string_chr[keep]
34-
names(out) <- names(string)[keep]
35-
out
30+
keep <- !stringi::stri_duplicated(string, opts_collator = opts)
31+
string[keep]
3632
}

R/utils.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ tr_ <- function(...) {
3030
enc2utf8(gettext(paste0(...), domain = "R-stringr"))
3131
}
3232

33+
# Helper to copy names from `string` to output.
34+
# For vector output (including list), set `names`.
35+
# For matrix output, set `rownames`.
3336
copy_names <- function(from, to) {
3437
nm <- names(from)
3538
if (is.null(nm)) return(to)
@@ -42,7 +45,8 @@ copy_names <- function(from, to) {
4245
}
4346
}
4447

48+
# Whether to name the output using names from `string`:
49+
# We'll preserve names if pattern is scalar or aligns with string.
4550
keep_names <- function(string, pattern) {
46-
if (is.null(names(string))) return(FALSE)
4751
length(pattern) == 1L || length(pattern) == length(string)
4852
}

tests/testthat/test-subset.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ test_that("str_subset() preserves names of retained elements", {
5555

5656
test_that("str_subset() never matches missing values", {
5757
expect_equal(str_subset(c("a", NA, "b"), "."), c("a", "b"))
58-
expect_identical(str_subset(NA, "."), character(0))
58+
expect_identical(str_subset(NA_character_, "."), character(0))
5959
})

tests/testthat/test-unique.R

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
test_that("unique values returned for strings with duplicate values", {
22
expect_equal(str_unique(c("a", "a", "a")), "a")
3-
expect_equal(str_unique(c(NA, NA)), NA_character_)
3+
expect_equal(str_unique(c(NA_character_, NA_character_)), NA_character_)
44
})
55

66
test_that("can ignore case", {
77
expect_equal(str_unique(c("a", "A"), ignore_case = TRUE), "a")
88
})
99

10-
test_that("str_unique() returns NA_character_ for NA inputs", {
11-
expect_equal(str_unique(c(NA_character_, NA_character_)), NA_character_)
12-
expect_equal(str_unique(c(NA, NA)), NA_character_)
13-
})
14-
1510
test_that("str_unique() preserves names of first occurrences", {
1611
y <- c(A = "a", A2 = "a", B = "b")
1712
out <- str_unique(y)

tests/testthat/test-utils.R

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
test_that("keep_names() returns logical flag based on inputs", {
2-
string_unnamed_scalar <- "s"
3-
string_named_scalar <- c("A" = "s")
4-
string_unnamed_vector <- c("s1", "s2")
5-
string_named_vector <- c(A = "s1", B = "s2")
2+
string_scalar <- c("A" = "s")
3+
string_vector <- c(A = "s1", B = "s2")
64
pattern_scalar <- "p"
75
pattern_vector <- c("p1", "p2")
86

9-
expect_true(keep_names(string_named_scalar, pattern_scalar))
10-
expect_false(keep_names(string_named_scalar, pattern_vector))
11-
expect_true(keep_names(string_named_vector, pattern_scalar))
12-
expect_true(keep_names(string_named_vector, pattern_vector))
13-
expect_false(keep_names(string_unnamed_scalar, pattern_scalar))
14-
expect_false(keep_names(string_unnamed_scalar, pattern_vector))
15-
expect_false(keep_names(string_unnamed_vector, pattern_scalar))
16-
expect_false(keep_names(string_unnamed_vector, pattern_vector))
7+
expect_true(keep_names(string_scalar, pattern_scalar))
8+
expect_false(keep_names(string_scalar, pattern_vector))
9+
expect_true(keep_names(string_vector, pattern_scalar))
10+
expect_true(keep_names(string_vector, pattern_vector))
1711
})
1812

1913
test_that("copy_names() applies names to vectors", {

0 commit comments

Comments
 (0)