Skip to content
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

perf: use fixed strings #558

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/curl.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ curl_args <- function(cmd, error_call = caller_env()) {
call = error_call
)
}
if (grepl("'", cmd)) {
if (grepl("'", cmd, fixed = TRUE)) {
args <- parse_delim(pieces$right, " ", quote = "'")
} else {
args <- parse_delim(pieces$right, " ", quote = '"')
Expand Down
4 changes: 2 additions & 2 deletions R/req-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,11 @@ resp_cache_control <- function(resp) {
return(NULL)
}

pieces <- strsplit(x, ",")[[1]]
pieces <- strsplit(x, ",", fixed = TRUE)[[1]]
pieces <- gsub("^\\s+|\\s+$", "", pieces)
pieces <- tolower(pieces)

is_value <- grepl("=", pieces)
is_value <- grepl("=", pieces, fixed = TRUE)
flags <- pieces[!is_value]

keyvalues <- strsplit(pieces[is_value], "\\s*=\\s*")
Expand Down
4 changes: 2 additions & 2 deletions R/req-template.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ req_template <- function(req, template, ..., .env = parent.frame()) {
check_request(req)
check_string(template)

pieces <- strsplit(template, " ")[[1]]
pieces <- strsplit(template, " ", fixed = TRUE)[[1]]
if (length(pieces) == 1) {
template <- pieces[[1]]
} else if (length(pieces) == 2) {
Expand Down Expand Up @@ -114,7 +114,7 @@ template_vars <- function(x, type) {
template_type <- function(x) {
if (grepl("\\{\\w+?\\}", x)) {
"uri"
} else if (grepl(":", x)) {
} else if (grepl(":", x, fixed = TRUE)) {
"colon"
} else {
"none"
Expand Down
2 changes: 1 addition & 1 deletion R/resp-headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ resp_retry_after <- function(resp) {
val <- resp_header(resp, "Retry-After")
if (is.null(val)) {
NA
} else if (grepl(" ", val)) {
} else if (grepl(" ", val, fixed = TRUE)) {
diff <- difftime(parse_http_date(val), resp_date(resp), units = "secs")
as.numeric(diff)
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/url.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ print.httr2_url <- function(x, ...) {
cli::cli_li("{.field query}: ")
id <- cli::cli_ul()
# escape curly brackets for cli by replacing single with double brackets
query_vals <- gsub("\\{", "{{", gsub("\\}", "}}", x$query))
query_vals <- gsub("{", "{{", gsub("}", "}}", x$query, fixed = TRUE), fixed = TRUE)
cli::cli_li(paste0(" {.field ", names(x$query), "}: ", query_vals))
cli::cli_end(id)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-req-perform-stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test_that("can override error handling", {
test_that("can buffer to lines", {
lines <- character()
accumulate_lines <- function(x) {
lines <<- c(lines, strsplit(rawToChar(x), "\n")[[1]])
lines <<- c(lines, strsplit(rawToChar(x), "\n", fixed = TRUE)[[1]])
TRUE
}

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-req.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ test_that("print method obfuscates Authorization header unless requested", {
req <- request("https://example.com") %>%
req_auth_basic("user", "SECRET")
output <- testthat::capture_messages(print(req))
expect_false(any(grepl("SECRET", output)))
expect_false(any(grepl("SECRET", output, fixed = TRUE)))

output <- testthat::capture_messages(print(req, redact_headers = FALSE))
expect_true(any(grepl("Authorization: 'Basic", output)))
expect_false(any(grepl("REDACTED", output)))
expect_true(any(grepl("Authorization: 'Basic", output, fixed = TRUE)))
expect_false(any(grepl("REDACTED", output, fixed = TRUE)))
})

test_that("check_request() gives useful error", {
Expand Down
Loading