Skip to content

Commit 982ff70

Browse files
committed
Refactor keep_names() and copy_names().
This enables many functions to finish with if (keep_names(string, pattern)) copy_names(string, out) else out
1 parent ec51727 commit 982ff70

File tree

11 files changed

+73
-121
lines changed

11 files changed

+73
-121
lines changed

R/count.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,5 @@ str_count <- function(string, pattern = "") {
4444
coll = stri_count_coll(string, pattern, opts_collator = opts(pattern)),
4545
regex = stri_count_regex(string, pattern, opts_regex = opts(pattern))
4646
)
47-
if (length(out) == length(string)) names(out) <- names(string)
48-
out
47+
if (keep_names(string, pattern)) copy_names(string, out) else out
4948
}

R/detect.R

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ str_detect <- function(string, pattern, negate = FALSE) {
5050
regex = stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern))
5151
)
5252

53-
if (length(out) == length(string)) {
54-
names(out) <- names(string)
55-
}
56-
out
53+
if (keep_names(string, pattern)) copy_names(string, out) else out
5754
}
5855

5956
#' Detect the presence/absence of a match at the start/end
@@ -93,8 +90,7 @@ str_starts <- function(string, pattern, negate = FALSE) {
9390
stri_detect_regex(string, pattern2, negate = negate, opts_regex = opts(pattern))
9491
}
9592
)
96-
if (length(out) == length(string)) names(out) <- names(string)
97-
out
93+
if (keep_names(string, pattern)) copy_names(string, out) else out
9894
}
9995

10096
#' @rdname str_starts
@@ -113,8 +109,7 @@ str_ends <- function(string, pattern, negate = FALSE) {
113109
stri_detect_regex(string, pattern2, negate = negate, opts_regex = opts(pattern))
114110
}
115111
)
116-
if (length(out) == length(string)) names(out) <- names(string)
117-
out
112+
if (keep_names(string, pattern)) copy_names(string, out) else out
118113
}
119114

120115
#' Detect a pattern in the same way as `SQL`'s `LIKE` and `ILIKE` operators
@@ -176,8 +171,7 @@ str_like <- function(string, pattern, ignore_case = deprecated()) {
176171

177172
pattern <- regex(like_to_regex(pattern), ignore_case = FALSE)
178173
out <- stri_detect_regex(string, pattern, opts_regex = opts(pattern))
179-
if (length(out) == length(string)) names(out) <- names(string)
180-
out
174+
if (keep_names(string, pattern)) copy_names(string, out) else out
181175
}
182176

183177
#' @export
@@ -191,8 +185,7 @@ str_ilike <- function(string, pattern) {
191185

192186
pattern <- regex(like_to_regex(pattern), ignore_case = TRUE)
193187
out <- stri_detect_regex(string, pattern, opts_regex = opts(pattern))
194-
if (length(out) == length(string)) names(out) <- names(string)
195-
out
188+
if (keep_names(string, pattern)) copy_names(string, out) else out
196189
}
197190

198191
like_to_regex <- function(pattern) {

R/extract.R

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
str_extract <- function(string, pattern, group = NULL) {
4242
if (!is.null(group)) {
4343
out <- str_match(string, pattern)[, group + 1]
44-
if (length(out) == length(string)) names(out) <- names(string)
45-
return(out)
44+
return(if (keep_names(string, pattern)) copy_names(string, out) else out)
4645
}
4746

4847
check_lengths(string, pattern)
@@ -54,8 +53,7 @@ str_extract <- function(string, pattern, group = NULL) {
5453
coll = stri_extract_first_coll(string, pattern, opts_collator = opt),
5554
regex = stri_extract_first_regex(string, pattern, opts_regex = opt)
5655
)
57-
if (length(out) == length(string)) names(out) <- names(string)
58-
out
56+
if (keep_names(string, pattern)) copy_names(string, out) else out
5957
}
6058

6159
#' @rdname str_extract
@@ -77,10 +75,5 @@ str_extract_all <- function(string, pattern, simplify = FALSE) {
7775
regex = stri_extract_all_regex(string, pattern,
7876
simplify = simplify, omit_no_match = TRUE, opts_regex = opt)
7977
)
80-
if (simplify) {
81-
if (nrow(out) == length(string)) rownames(out) <- names(string)
82-
} else {
83-
if (length(out) == length(string)) names(out) <- names(string)
84-
}
85-
out
78+
if (keep_names(string, pattern)) copy_names(string, out) else out
8679
}

R/locate.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ str_locate <- function(string, pattern) {
4444
coll = stri_locate_first_coll(string, pattern, opts_collator = opts(pattern)),
4545
regex = stri_locate_first_regex(string, pattern, opts_regex = opts(pattern))
4646
)
47-
if (is.matrix(out) && nrow(out) == length(string)) rownames(out) <- names(string)
48-
out
47+
if (keep_names(string, pattern)) copy_names(string, out) else out
4948
}
5049

5150
#' @rdname str_locate
@@ -61,8 +60,7 @@ str_locate_all <- function(string, pattern) {
6160
regex = stri_locate_all_regex(string, pattern, omit_no_match = TRUE, opts_regex = opts),
6261
coll = stri_locate_all_coll(string, pattern, omit_no_match = TRUE, opts_collator = opts)
6362
)
64-
if (is.list(out) && length(out) == length(string)) names(out) <- names(string)
65-
out
63+
if (keep_names(string, pattern)) copy_names(string, out) else out
6664
}
6765

6866

R/match.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ str_match <- function(string, pattern) {
5858
pattern,
5959
opts_regex = opts(pattern)
6060
)
61-
if (is.matrix(out) && nrow(out) == length(string)) rownames(out) <- names(string)
62-
out
61+
if (keep_names(string, pattern)) copy_names(string, out) else out
6362
}
6463

6564
#' @rdname str_match
@@ -75,6 +74,5 @@ str_match_all <- function(string, pattern) {
7574
omit_no_match = TRUE,
7675
opts_regex = opts(pattern)
7776
)
78-
if (is.list(out) && length(out) == length(string)) names(out) <- names(string)
79-
out
77+
if (keep_names(string, pattern)) copy_names(string, out) else out
8078
}

R/pad.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,5 @@ str_pad <- function(string, width, side = c("left", "right", "both"), pad = " ",
3737
right = stri_pad_right(string, width, pad = pad, use_length = !use_width),
3838
both = stri_pad_both(string, width, pad = pad, use_length = !use_width)
3939
)
40-
if (length(out) == length(string)) names(out) <- names(string)
41-
out
40+
if (length(out) == length(string)) copy_names(string, out) else out
4241
}

R/replace.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ str_replace <- function(string, pattern, replacement) {
8686
regex = stri_replace_first_regex(string, pattern, fix_replacement(replacement),
8787
opts_regex = opts(pattern))
8888
)
89-
if (length(out) == length(string)) names(out) <- names(string)
90-
out
89+
if (keep_names(string, pattern)) copy_names(string, out) else out
9190
}
9291

9392
#' @export
@@ -119,8 +118,7 @@ str_replace_all <- function(string, pattern, replacement) {
119118
regex = stri_replace_all_regex(string, pattern, fix_replacement(replacement),
120119
vectorize_all = vec, opts_regex = opts(pattern))
121120
)
122-
if (length(out) == length(string)) names(out) <- names(string)
123-
out
121+
if (keep_names(string, pattern)) copy_names(string, out) else out
124122
}
125123

126124
is_replacement_fun <- function(x) {

R/split.R

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,7 @@ str_split <- function(string, pattern, n = Inf, simplify = FALSE) {
7777
coll = stri_split_coll(string, pattern, n = n, simplify = simplify, opts_collator = opts(pattern))
7878
)
7979

80-
if (is.list(out) && length(out) == length(string)) {
81-
names(out) <- names(string)
82-
} else if (is.matrix(out) && nrow(out) == length(string)) {
83-
rownames(out) <- names(string)
84-
}
85-
86-
out
80+
if (keep_names(string, pattern)) copy_names(string, out) else out
8781
}
8882

8983
#' @export
@@ -113,8 +107,7 @@ str_split_i <- function(string, pattern, i) {
113107
if (i > 0) {
114108
out <- str_split(string, pattern, simplify = NA, n = i + 1)
115109
col <- out[, i]
116-
if (length(col) == length(string)) names(col) <- names(string)
117-
col
110+
if (keep_names(string, pattern)) copy_names(string, col) else col
118111
} else if (i < 0) {
119112
i <- abs(i)
120113
pieces <- str_split(string, pattern)
@@ -127,8 +120,7 @@ str_split_i <- function(string, pattern, i) {
127120
}
128121
}
129122
out <- map_chr(pieces, last)
130-
if (length(out) == length(string)) names(out) <- names(string)
131-
out
123+
if (keep_names(string, pattern)) copy_names(string, out) else out
132124
} else {
133125
cli::cli_abort(tr_("{.arg i} must not be 0."))
134126
}

R/sub.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ str_sub <- function(string, start = 1L, end = -1L) {
6969
} else {
7070
stri_sub(string, from = start, to = end)
7171
}
72-
if (length(out) == length(string)) names(out) <- names(string)
73-
out
72+
if (length(out) == length(string)) copy_names(string, out) else out
7473
}
7574

7675

@@ -95,6 +94,5 @@ str_sub_all <- function(string, start = 1L, end = -1L) {
9594
} else {
9695
stri_sub_all(string, from = start, to = end)
9796
}
98-
if (is.list(out) && length(out) == length(string)) names(out) <- names(string)
99-
out
97+
if (length(out) == length(string)) copy_names(string, out) else out
10098
}

R/utils.R

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,18 @@ tr_ <- function(...) {
3131
}
3232

3333
copy_names <- function(from, to) {
34-
set_names(to, names(from))
35-
}
34+
nm <- names(from)
35+
if (is.null(nm)) return(to)
3636

37-
#' Preserve names based on inputs
38-
#'
39-
#' Decides whether to propagate `names(string)` to `out` based on the
40-
#' relationship between `string` and `pattern`, then applies those names to
41-
#' the appropriate structure.
42-
#'
43-
#' Names are preserved when `pattern` is missing, has length 1, or has the
44-
#' same length as `string`. For matrix outputs, row names are set; for vector
45-
#' or list outputs, names are set.
46-
#'
47-
#' @param out The result to potentially name; a vector, list, or matrix.
48-
#' @param string The primary input character vector whose names may be copied.
49-
#' @param pattern Optional pattern input used to decide if names should be
50-
#' preserved.
51-
#' @return `out`, with `names(out)` or `rownames(out)` set from
52-
#' `names(string)` when appropriate.
53-
#' @keywords internal
54-
#' @noRd
55-
keep_names <- function(out, string, pattern = NULL) {
56-
nm <- names(string)
57-
if (is.null(nm)) return(out)
58-
59-
keep <- is.null(pattern) || length(pattern) == 1L || length(pattern) == length(string)
60-
if (!keep) return(out)
61-
62-
if (is.matrix(out)) {
63-
rownames(out) <- nm
37+
if (is.matrix(to)) {
38+
rownames(to) <- nm
39+
to
6440
} else {
65-
names(out) <- nm
41+
set_names(to, nm)
6642
}
67-
out
43+
}
44+
45+
keep_names <- function(string, pattern) {
46+
if (is.null(names(string))) return(FALSE)
47+
length(pattern) == 1L || length(pattern) == length(string)
6848
}

0 commit comments

Comments
 (0)