Skip to content

Commit e352c08

Browse files
committed
Preserve names where sensible.
1 parent abb5cf0 commit e352c08

File tree

19 files changed

+132
-38
lines changed

19 files changed

+132
-38
lines changed

R/case.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,37 @@ NULL
2828
str_to_upper <- function(string, locale = "en") {
2929
check_string(locale)
3030

31-
stri_trans_toupper(string, locale = locale)
31+
out <- stri_trans_toupper(string, locale = locale)
32+
if (length(out) == length(string)) names(out) <- names(string)
33+
out
3234
}
3335
#' @export
3436
#' @rdname case
3537
str_to_lower <- function(string, locale = "en") {
3638
check_string(locale)
3739

38-
stri_trans_tolower(string, locale = locale)
40+
out <- stri_trans_tolower(string, locale = locale)
41+
if (length(out) == length(string)) names(out) <- names(string)
42+
out
3943
}
4044
#' @export
4145
#' @rdname case
4246
str_to_title <- function(string, locale = "en") {
4347
check_string(locale)
4448

45-
stri_trans_totitle(string, opts_brkiter = stri_opts_brkiter(locale = locale))
49+
out <- stri_trans_totitle(string, opts_brkiter = stri_opts_brkiter(locale = locale))
50+
if (length(out) == length(string)) names(out) <- names(string)
51+
out
4652
}
4753
#' @export
4854
#' @rdname case
4955
str_to_sentence <- function(string, locale = "en") {
5056
check_string(locale)
5157

52-
stri_trans_totitle(
58+
out <- stri_trans_totitle(
5359
string,
5460
opts_brkiter = stri_opts_brkiter(type = "sentence", locale = locale)
5561
)
62+
if (length(out) == length(string)) names(out) <- names(string)
63+
out
5664
}

R/conv.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
1515
str_conv <- function(string, encoding) {
1616
check_string(encoding)
1717

18-
stri_conv(string, encoding, "UTF-8")
18+
out <- stri_conv(string, encoding, "UTF-8")
19+
if (length(out) == length(string)) names(out) <- names(string)
20+
out
1921
}

R/count.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@
3737
str_count <- function(string, pattern = "") {
3838
check_lengths(string, pattern)
3939

40-
switch(type(pattern),
40+
out <- switch(type(pattern),
4141
empty = ,
4242
bound = stri_count_boundaries(string, opts_brkiter = opts(pattern)),
4343
fixed = stri_count_fixed(string, pattern, opts_fixed = opts(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
4749
}

R/detect.R

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,19 @@ str_detect <- function(string, pattern, negate = FALSE) {
4242
check_lengths(string, pattern)
4343
check_bool(negate)
4444

45-
switch(type(pattern),
45+
out <- switch(type(pattern),
4646
empty = no_empty(),
4747
bound = no_boundary(),
4848
fixed = stri_detect_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)),
4949
coll = stri_detect_coll(string, pattern, negate = negate, opts_collator = opts(pattern)),
5050
regex = stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern))
5151
)
52+
53+
# Preserve names when there's a 1:1 correspondence with `string`
54+
if (length(out) == length(string)) {
55+
names(out) <- names(string)
56+
}
57+
out
5258
}
5359

5460
#' Detect the presence/absence of a match at the start/end
@@ -78,7 +84,7 @@ str_starts <- function(string, pattern, negate = FALSE) {
7884
check_lengths(string, pattern)
7985
check_bool(negate)
8086

81-
switch(type(pattern),
87+
out <- switch(type(pattern),
8288
empty = no_empty(),
8389
bound = no_boundary(),
8490
fixed = stri_startswith_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)),
@@ -88,6 +94,8 @@ str_starts <- function(string, pattern, negate = FALSE) {
8894
stri_detect_regex(string, pattern2, negate = negate, opts_regex = opts(pattern))
8995
}
9096
)
97+
if (length(out) == length(string)) names(out) <- names(string)
98+
out
9199
}
92100

93101
#' @rdname str_starts
@@ -96,7 +104,7 @@ str_ends <- function(string, pattern, negate = FALSE) {
96104
check_lengths(string, pattern)
97105
check_bool(negate)
98106

99-
switch(type(pattern),
107+
out <- switch(type(pattern),
100108
empty = no_empty(),
101109
bound = no_boundary(),
102110
fixed = stri_endswith_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)),
@@ -106,6 +114,8 @@ str_ends <- function(string, pattern, negate = FALSE) {
106114
stri_detect_regex(string, pattern2, negate = negate, opts_regex = opts(pattern))
107115
}
108116
)
117+
if (length(out) == length(string)) names(out) <- names(string)
118+
out
109119
}
110120

111121
#' Detect a pattern in the same way as `SQL`'s `LIKE` and `ILIKE` operators
@@ -166,7 +176,9 @@ str_like <- function(string, pattern, ignore_case = deprecated()) {
166176
}
167177

168178
pattern <- regex(like_to_regex(pattern), ignore_case = FALSE)
169-
stri_detect_regex(string, pattern, opts_regex = opts(pattern))
179+
out <- stri_detect_regex(string, pattern, opts_regex = opts(pattern))
180+
if (length(out) == length(string)) names(out) <- names(string)
181+
out
170182
}
171183

172184
#' @export
@@ -179,7 +191,9 @@ str_ilike <- function(string, pattern) {
179191
}
180192

181193
pattern <- regex(like_to_regex(pattern), ignore_case = TRUE)
182-
stri_detect_regex(string, pattern, opts_regex = opts(pattern))
194+
out <- stri_detect_regex(string, pattern, opts_regex = opts(pattern))
195+
if (length(out) == length(string)) names(out) <- names(string)
196+
out
183197
}
184198

185199
like_to_regex <- function(pattern) {

R/dup.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ str_dup <- function(string, times, sep = NULL) {
1919
check_string(sep, allow_null = TRUE)
2020

2121
if (is.null(sep)) {
22-
stri_dup(input$string, input$times)
22+
out <- stri_dup(input$string, input$times)
2323
} else {
24-
map_chr(seq_along(input$string), function(i) {
24+
out <- map_chr(seq_along(input$string), function(i) {
2525
paste(rep(string[[i]], input$times[[i]]), collapse = sep)
2626
})
2727
}
28+
if (length(out) == length(input$string)) names(out) <- names(input$string)
29+
out
2830
}

R/escape.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
#' str_detect(c("a", "."), ".")
1313
#' str_detect(c("a", "."), str_escape("."))
1414
str_escape <- function(string) {
15-
str_replace_all(string, "([.^$\\\\|*+?{}\\[\\]()])", "\\\\\\1")
15+
out <- str_replace_all(string, "([.^$\\\\|*+?{}\\[\\]()])", "\\\\\\1")
16+
if (length(out) == length(string)) names(out) <- names(string)
17+
out
1618
}

R/extract.R

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,21 @@
4040
#' str_extract_all("This is, suprisingly, a sentence.", boundary("word"))
4141
str_extract <- function(string, pattern, group = NULL) {
4242
if (!is.null(group)) {
43-
return(str_match(string, pattern)[, group + 1])
43+
out <- str_match(string, pattern)[, group + 1]
44+
if (length(out) == length(string)) names(out) <- names(string)
45+
return(out)
4446
}
4547

4648
check_lengths(string, pattern)
47-
switch(type(pattern),
49+
out <- switch(type(pattern),
4850
empty = stri_extract_first_boundaries(string, opts_brkiter = opts(pattern)),
4951
bound = stri_extract_first_boundaries(string, opts_brkiter = opts(pattern)),
5052
fixed = stri_extract_first_fixed(string, pattern, opts_fixed = opts(pattern)),
5153
coll = stri_extract_first_coll(string, pattern, opts_collator = opts(pattern)),
5254
regex = stri_extract_first_regex(string, pattern, opts_regex = opts(pattern))
5355
)
56+
if (length(out) == length(string)) names(out) <- names(string)
57+
out
5458
}
5559

5660
#' @rdname str_extract
@@ -59,7 +63,7 @@ str_extract_all <- function(string, pattern, simplify = FALSE) {
5963
check_lengths(string, pattern)
6064
check_bool(simplify)
6165

62-
switch(type(pattern),
66+
out <- switch(type(pattern),
6367
empty = stri_extract_all_boundaries(string,
6468
simplify = simplify, omit_no_match = TRUE, opts_brkiter = opts(pattern)),
6569
bound = stri_extract_all_boundaries(string,
@@ -71,4 +75,10 @@ str_extract_all <- function(string, pattern, simplify = FALSE) {
7175
regex = stri_extract_all_regex(string, pattern,
7276
simplify = simplify, omit_no_match = TRUE, opts_regex = opts(pattern))
7377
)
78+
if (isTRUE(simplify)) {
79+
if (is.matrix(out) && nrow(out) == length(string)) rownames(out) <- names(string)
80+
} else {
81+
if (is.list(out) && length(out) == length(string)) names(out) <- names(string)
82+
}
83+
out
7484
}

R/length.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@
3434
#' # Because the second element is made up of a u + an accent
3535
#' str_sub(u, 1, 1)
3636
str_length <- function(string) {
37-
stri_length(string)
37+
out <- stri_length(string)
38+
if (length(out) == length(string)) names(out) <- names(string)
39+
out
3840
}
3941

4042
#' @export
4143
#' @rdname str_length
4244
str_width <- function(string) {
43-
stri_width(string)
45+
out <- stri_width(string)
46+
if (length(out) == length(string)) names(out) <- names(string)
47+
out
4448
}

R/locate.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@
3737
str_locate <- function(string, pattern) {
3838
check_lengths(string, pattern)
3939

40-
switch(type(pattern),
40+
out <- switch(type(pattern),
4141
empty = ,
4242
bound = stri_locate_first_boundaries(string, opts_brkiter = opts(pattern)),
4343
fixed = stri_locate_first_fixed(string, pattern, opts_fixed = opts(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
4749
}
4850

4951
#' @rdname str_locate
@@ -52,13 +54,15 @@ str_locate_all <- function(string, pattern) {
5254
check_lengths(string, pattern)
5355
opts <- opts(pattern)
5456

55-
switch(type(pattern),
57+
out <- switch(type(pattern),
5658
empty = ,
5759
bound = stri_locate_all_boundaries(string, omit_no_match = TRUE, opts_brkiter = opts),
5860
fixed = stri_locate_all_fixed(string, pattern, omit_no_match = TRUE, opts_fixed = opts),
5961
regex = stri_locate_all_regex(string, pattern, omit_no_match = TRUE, opts_regex = opts),
6062
coll = stri_locate_all_coll(string, pattern, omit_no_match = TRUE, opts_collator = opts)
6163
)
64+
if (is.list(out) && length(out) == length(string)) names(out) <- names(string)
65+
out
6266
}
6367

6468

R/match.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ str_match <- function(string, pattern) {
5454
cli::cli_abort(tr_("{.arg pattern} must be a regular expression."))
5555
}
5656

57-
stri_match_first_regex(string,
57+
out <- stri_match_first_regex(string,
5858
pattern,
5959
opts_regex = opts(pattern)
6060
)
61+
if (is.matrix(out) && nrow(out) == length(string)) rownames(out) <- names(string)
62+
out
6163
}
6264

6365
#' @rdname str_match
@@ -68,9 +70,11 @@ str_match_all <- function(string, pattern) {
6870
cli::cli_abort(tr_("{.arg pattern} must be a regular expression."))
6971
}
7072

71-
stri_match_all_regex(string,
73+
out <- stri_match_all_regex(string,
7274
pattern,
7375
omit_no_match = TRUE,
7476
opts_regex = opts(pattern)
7577
)
78+
if (is.list(out) && length(out) == length(string)) names(out) <- names(string)
79+
out
7680
}

0 commit comments

Comments
 (0)