From 698a2f13d12ce6470ec7f6cdcfa0b776f0e0b77e Mon Sep 17 00:00:00 2001 From: Maximilian Muecke Date: Thu, 3 Oct 2024 19:30:41 +0200 Subject: [PATCH] perf: use fixed strings --- R/curl.R | 2 +- R/req-cache.R | 4 ++-- R/req-template.R | 4 ++-- R/resp-headers.R | 2 +- R/url.R | 2 +- tests/testthat/test-req-perform-stream.R | 2 +- tests/testthat/test-req.R | 6 +++--- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/R/curl.R b/R/curl.R index cc5cef56..6016638d 100644 --- a/R/curl.R +++ b/R/curl.R @@ -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 = '"') diff --git a/R/req-cache.R b/R/req-cache.R index 7be568cd..9fd253fd 100644 --- a/R/req-cache.R +++ b/R/req-cache.R @@ -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*") diff --git a/R/req-template.R b/R/req-template.R index 278b292a..a16c2a94 100644 --- a/R/req-template.R +++ b/R/req-template.R @@ -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) { @@ -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" diff --git a/R/resp-headers.R b/R/resp-headers.R index f90fc3c7..0560fc33 100644 --- a/R/resp-headers.R +++ b/R/resp-headers.R @@ -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 { diff --git a/R/url.R b/R/url.R index 8ffab225..24680aba 100644 --- a/R/url.R +++ b/R/url.R @@ -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) } diff --git a/tests/testthat/test-req-perform-stream.R b/tests/testthat/test-req-perform-stream.R index 788b52d8..b2ef3220 100644 --- a/tests/testthat/test-req-perform-stream.R +++ b/tests/testthat/test-req-perform-stream.R @@ -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 } diff --git a/tests/testthat/test-req.R b/tests/testthat/test-req.R index f2b3dda8..a445930c 100644 --- a/tests/testthat/test-req.R +++ b/tests/testthat/test-req.R @@ -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", {