-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vectorise replacement function #462
base: main
Are you sure you want to change the base?
Changes from 7 commits
ea44fd4
5ff5a74
406a136
d5c8d88
7e27abe
9dab725
4767c88
820b266
d126f49
8145645
7495d99
95a3d8b
8d94ae6
432d7db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,8 @@ | |
#' To perform multiple replacements in each element of `string`, | ||
#' pass a named vector (`c(pattern1 = replacement1)`) to | ||
#' `str_replace_all`. Alternatively, pass a function (or formula) to | ||
#' `replacement`: it will be called once for each match (from right to left) | ||
#' and its return value will be used to replace the match. | ||
#' `replacement`: it will be passed a single character vector and should | ||
#' return a character vector of the same length. | ||
#' | ||
#' To replace the complete string with `NA`, use | ||
#' `replacement = NA_character_`. | ||
|
@@ -55,7 +55,7 @@ | |
#' colours <- str_c("\\b", colors(), "\\b", collapse="|") | ||
#' col2hex <- function(col) { | ||
#' rgb <- col2rgb(col) | ||
#' rgb(rgb["red", ], rgb["green", ], rgb["blue", ], max = 255) | ||
#' rgb(rgb["red", ], rgb["green", ], rgb["blue", ], maxColorValue = 255) | ||
#' } | ||
#' | ||
#' x <- c( | ||
|
@@ -179,18 +179,50 @@ str_replace_na <- function(string, replacement = "NA") { | |
|
||
str_transform <- function(string, pattern, replacement) { | ||
loc <- str_locate(string, pattern) | ||
str_sub(string, loc, omit_na = TRUE) <- replacement(str_sub(string, loc)) | ||
new <- replacement(str_sub(string, loc)) | ||
str_sub(string, loc, omit_na = TRUE) <- new | ||
string | ||
} | ||
str_transform_all <- function(string, pattern, replacement) { | ||
|
||
str_transform_all <- function(string, pattern, replacement, error_call = caller_env()) { | ||
locs <- str_locate_all(string, pattern) | ||
|
||
for (i in seq_along(string)) { | ||
for (j in rev(seq_len(nrow(locs[[i]])))) { | ||
loc <- locs[[i]] | ||
str_sub(string[[i]], loc[j, 1], loc[j, 2]) <- replacement(str_sub(string[[i]], loc[j, 1], loc[j, 2])) | ||
} | ||
old <- str_sub_all(string, locs) | ||
idx <- chop_index(old) | ||
|
||
# unchop list into a vector, apply replacement(), and then rechop back into | ||
# a list | ||
old_flat <- vctrs::vec_unchop(old, idx) | ||
if (length(old_flat) == 0) { | ||
# minor optimisation to avoid problems with the many replacement | ||
# functions that use paste | ||
new_flat <- character() | ||
Comment on lines
+199
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternate idea is to actually apply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we could do that, I just don't think it's worth the effort since we do know what the correct answer is in the 0 element case. |
||
} else { | ||
new_flat <- replacement(old_flat) | ||
} | ||
|
||
if (!is.character(new_flat)) { | ||
cli::cli_abort( | ||
"{.fn replacement} must return a character vector, not {.obj_type_friendly {new_flat}}.", | ||
call = error_call | ||
) | ||
} | ||
if (length(new_flat) != length(old_flat)) { | ||
cli::cli_abort( | ||
"{.fn replacement} must return a vector the same length as the input ({length(old_flat)}), not length {length(new_flat)}.", | ||
call = error_call | ||
) | ||
} | ||
|
||
new <- vctrs::vec_chop(new_flat, idx) | ||
|
||
stringi::stri_sub_all(string, locs) <- new | ||
string | ||
} | ||
|
||
chop_index <- function(x) { | ||
ls <- lengths(x) | ||
start <- cumsum(c(1, ls[-length(ls)])) | ||
end <- start + ls - 1 | ||
hadley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lapply(seq_along(ls), function(i) seq2(start[[i]], end[[i]])) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depending on how much you value readability over performance, you don't actually need to pass
idx
here since you are unchopping in the order the input was providedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In dev vctrs: