Skip to content

Commit c91419f

Browse files
authored
Preserve names where sensible (#585)
Systematic review of all stringr functions. Fixes #575
1 parent b4507a9 commit c91419f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+406
-70
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# stringr (development version)
22

3+
* All relevant stringr functions now preserve names (@jonovik, #575).
34
* New `vignette("locale-sensitive")` about locale sensitive functions (@kylieainslie, #404)
45
* New `str_ilike()` that follows the conventions of the SQL ILIKE operator (@edward-burn, #543).
56
* `str_like(ignore_case)` is deprecated, with `str_like()` now always case sensitive to better follow the conventions of the SQL LIKE operator (@edward-burn, #543).

R/case.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,31 @@ NULL
2727
#' @rdname case
2828
str_to_upper <- function(string, locale = "en") {
2929
check_string(locale)
30-
31-
stri_trans_toupper(string, locale = locale)
30+
copy_names(string, stri_trans_toupper(string, locale = locale))
3231
}
3332
#' @export
3433
#' @rdname case
3534
str_to_lower <- function(string, locale = "en") {
3635
check_string(locale)
37-
38-
stri_trans_tolower(string, locale = locale)
36+
copy_names(string, stri_trans_tolower(string, locale = locale))
3937
}
4038
#' @export
4139
#' @rdname case
4240
str_to_title <- function(string, locale = "en") {
4341
check_string(locale)
44-
45-
stri_trans_totitle(string, opts_brkiter = stri_opts_brkiter(locale = locale))
42+
out <- stri_trans_totitle(
43+
string,
44+
opts_brkiter = stri_opts_brkiter(locale = locale)
45+
)
46+
copy_names(string, out)
4647
}
4748
#' @export
4849
#' @rdname case
4950
str_to_sentence <- function(string, locale = "en") {
5051
check_string(locale)
51-
52-
stri_trans_totitle(
52+
out <- stri_trans_totitle(
5353
string,
5454
opts_brkiter = stri_opts_brkiter(type = "sentence", locale = locale)
5555
)
56+
copy_names(string, out)
5657
}

R/conv.R

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

18-
stri_conv(string, encoding, "UTF-8")
18+
copy_names(string, stri_conv(string, encoding, "UTF-8"))
1919
}

R/count.R

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

40-
switch(
40+
out <- switch(
4141
type(pattern),
4242
empty = ,
4343
bound = stri_count_boundaries(string, opts_brkiter = opts(pattern)),
4444
fixed = stri_count_fixed(string, pattern, opts_fixed = opts(pattern)),
4545
coll = stri_count_coll(string, pattern, opts_collator = opts(pattern)),
4646
regex = stri_count_regex(string, pattern, opts_regex = opts(pattern))
4747
)
48+
preserve_names_if_possible(string, pattern, out)
4849
}

R/detect.R

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

45-
switch(
45+
out <- switch(
4646
type(pattern),
4747
empty = no_empty(),
4848
bound = no_boundary(),
@@ -65,6 +65,8 @@ str_detect <- function(string, pattern, negate = FALSE) {
6565
opts_regex = opts(pattern)
6666
)
6767
)
68+
69+
preserve_names_if_possible(string, pattern, out)
6870
}
6971

7072
#' Detect the presence/absence of a match at the start/end
@@ -94,7 +96,7 @@ str_starts <- function(string, pattern, negate = FALSE) {
9496
check_lengths(string, pattern)
9597
check_bool(negate)
9698

97-
switch(
99+
out <- switch(
98100
type(pattern),
99101
empty = no_empty(),
100102
bound = no_boundary(),
@@ -120,6 +122,7 @@ str_starts <- function(string, pattern, negate = FALSE) {
120122
)
121123
}
122124
)
125+
preserve_names_if_possible(string, pattern, out)
123126
}
124127

125128
#' @rdname str_starts
@@ -128,7 +131,7 @@ str_ends <- function(string, pattern, negate = FALSE) {
128131
check_lengths(string, pattern)
129132
check_bool(negate)
130133

131-
switch(
134+
out <- switch(
132135
type(pattern),
133136
empty = no_empty(),
134137
bound = no_boundary(),
@@ -154,6 +157,7 @@ str_ends <- function(string, pattern, negate = FALSE) {
154157
)
155158
}
156159
)
160+
preserve_names_if_possible(string, pattern, out)
157161
}
158162

159163
#' Detect a pattern in the same way as `SQL`'s `LIKE` and `ILIKE` operators
@@ -216,7 +220,8 @@ str_like <- function(string, pattern, ignore_case = deprecated()) {
216220
}
217221

218222
pattern <- regex(like_to_regex(pattern), ignore_case = FALSE)
219-
stri_detect_regex(string, pattern, opts_regex = opts(pattern))
223+
out <- stri_detect_regex(string, pattern, opts_regex = opts(pattern))
224+
preserve_names_if_possible(string, pattern, out)
220225
}
221226

222227
#' @export
@@ -231,7 +236,8 @@ str_ilike <- function(string, pattern) {
231236
}
232237

233238
pattern <- regex(like_to_regex(pattern), ignore_case = TRUE)
234-
stri_detect_regex(string, pattern, opts_regex = opts(pattern))
239+
out <- stri_detect_regex(string, pattern, opts_regex = opts(pattern))
240+
preserve_names_if_possible(string, pattern, out)
235241
}
236242

237243
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+
names(out) <- names(input$string)
29+
out
2830
}

R/escape.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
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+
copy_names(string, out)
1617
}

R/extract.R

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +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+
return(preserve_names_if_possible(string, pattern, out))
4445
}
4546

4647
check_lengths(string, pattern)
47-
switch(
48+
opt <- opts(pattern)
49+
out <- switch(
4850
type(pattern),
49-
empty = stri_extract_first_boundaries(string, opts_brkiter = opts(pattern)),
50-
bound = stri_extract_first_boundaries(string, opts_brkiter = opts(pattern)),
51-
fixed = stri_extract_first_fixed(
52-
string,
53-
pattern,
54-
opts_fixed = opts(pattern)
55-
),
56-
coll = stri_extract_first_coll(
57-
string,
58-
pattern,
59-
opts_collator = opts(pattern)
60-
),
61-
regex = stri_extract_first_regex(
62-
string,
63-
pattern,
64-
opts_regex = opts(pattern)
65-
)
51+
empty = stri_extract_first_boundaries(string, opts_brkiter = opt),
52+
bound = stri_extract_first_boundaries(string, opts_brkiter = opt),
53+
fixed = stri_extract_first_fixed(string, pattern, opts_fixed = opt),
54+
coll = stri_extract_first_coll(string, pattern, opts_collator = opt),
55+
regex = stri_extract_first_regex(string, pattern, opts_regex = opt)
6656
)
57+
preserve_names_if_possible(string, pattern, out)
6758
}
6859

6960
#' @rdname str_extract
@@ -72,40 +63,42 @@ str_extract_all <- function(string, pattern, simplify = FALSE) {
7263
check_lengths(string, pattern)
7364
check_bool(simplify)
7465

75-
switch(
66+
opt <- opts(pattern)
67+
out <- switch(
7668
type(pattern),
7769
empty = stri_extract_all_boundaries(
7870
string,
7971
simplify = simplify,
8072
omit_no_match = TRUE,
81-
opts_brkiter = opts(pattern)
73+
opts_brkiter = opt
8274
),
8375
bound = stri_extract_all_boundaries(
8476
string,
8577
simplify = simplify,
8678
omit_no_match = TRUE,
87-
opts_brkiter = opts(pattern)
79+
opts_brkiter = opt
8880
),
8981
fixed = stri_extract_all_fixed(
9082
string,
9183
pattern,
9284
simplify = simplify,
9385
omit_no_match = TRUE,
94-
opts_fixed = opts(pattern)
86+
opts_fixed = opt
9587
),
9688
coll = stri_extract_all_coll(
9789
string,
9890
pattern,
9991
simplify = simplify,
10092
omit_no_match = TRUE,
101-
opts_collator = opts(pattern)
93+
opts_collator = opt
10294
),
10395
regex = stri_extract_all_regex(
10496
string,
10597
pattern,
10698
simplify = simplify,
10799
omit_no_match = TRUE,
108-
opts_regex = opts(pattern)
100+
opts_regex = opt
109101
)
110102
)
103+
preserve_names_if_possible(string, pattern, out)
111104
}

R/length.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
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+
copy_names(string, stri_length(string))
3838
}
3939

4040
#' @export
4141
#' @rdname str_length
4242
str_width <- function(string) {
43-
stri_width(string)
43+
copy_names(string, stri_width(string))
4444
}

R/locate.R

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

40-
switch(
40+
out <- switch(
4141
type(pattern),
4242
empty = ,
4343
bound = stri_locate_first_boundaries(string, opts_brkiter = opts(pattern)),
@@ -53,6 +53,7 @@ str_locate <- function(string, pattern) {
5353
),
5454
regex = stri_locate_first_regex(string, pattern, opts_regex = opts(pattern))
5555
)
56+
preserve_names_if_possible(string, pattern, out)
5657
}
5758

5859
#' @rdname str_locate
@@ -61,7 +62,7 @@ str_locate_all <- function(string, pattern) {
6162
check_lengths(string, pattern)
6263
opts <- opts(pattern)
6364

64-
switch(
65+
out <- switch(
6566
type(pattern),
6667
empty = ,
6768
bound = stri_locate_all_boundaries(
@@ -88,6 +89,7 @@ str_locate_all <- function(string, pattern) {
8889
opts_collator = opts
8990
)
9091
)
92+
preserve_names_if_possible(string, pattern, out)
9193
}
9294

9395

0 commit comments

Comments
 (0)