@@ -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
185199like_to_regex <- function (pattern ) {
0 commit comments