From 52ecab61c5f2f34ce0063e700096b7e1c943e3d6 Mon Sep 17 00:00:00 2001 From: "dyfan.jones" Date: Sat, 10 Feb 2024 10:46:48 +0000 Subject: [PATCH] faster paws_parse_match --- paws.common/R/url.R | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/paws.common/R/url.R b/paws.common/R/url.R index 5565804e6..e93c04841 100644 --- a/paws.common/R/url.R +++ b/paws.common/R/url.R @@ -76,13 +76,15 @@ paws_url_parse <- function(url) { ) } -paws_parse_match <- function(x, pattern) { - m <- regexec(pattern, x, perl = TRUE) - pieces <- regmatches(x, m)[[1]][-1] - empty <- pieces == "" - pieces <- as.list(pieces) - pieces[empty] <- list(NULL) - return(pieces) +paws_parse_match <- function(char, pattern) { + match_loc <- regexpr(pattern, char, perl = TRUE) + cap_start <- attr(match_loc, "capture.start") + cap_len <- attr(match_loc, "capture.length") + cap_end <- cap_start + cap_len - 1 + cap_end[cap_end == -1] <- 0 + pieces <- as.list(substring(char, cap_start, cap_end)) + pieces[pieces == ""] <- list(NULL) + pieces } # Build a URL from a Url object.