From b03574af5499d952a542e4f85de5f1b2da5000d4 Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 11:25:35 +0200 Subject: [PATCH 1/4] Fix #30 --- R/RcppExports.R | 40 +++++----- R/get.R | 89 +++++---------------- src/RcppExports.cpp | 110 ++++++++++++-------------- src/adaR.cpp | 187 ++++++++------------------------------------ 4 files changed, 123 insertions(+), 303 deletions(-) diff --git a/R/RcppExports.R b/R/RcppExports.R index 8c5613d..978c6ee 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -37,44 +37,44 @@ Rcpp_ada_has_search <- function(url_vec) { .Call(`_adaR_Rcpp_ada_has_search`, url_vec) } -Rcpp_ada_get_href <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_href`, input_vec, length_vec) +Rcpp_ada_get_href <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_href`, url_vec) } -Rcpp_ada_get_username <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_username`, input_vec, length_vec) +Rcpp_ada_get_username <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_username`, url_vec) } -Rcpp_ada_get_password <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_password`, input_vec, length_vec) +Rcpp_ada_get_password <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_password`, url_vec) } -Rcpp_ada_get_port <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_port`, input_vec, length_vec) +Rcpp_ada_get_port <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_port`, url_vec) } -Rcpp_ada_get_hash <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_hash`, input_vec, length_vec) +Rcpp_ada_get_hash <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_hash`, url_vec) } -Rcpp_ada_get_host <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_host`, input_vec, length_vec) +Rcpp_ada_get_host <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_host`, url_vec) } -Rcpp_ada_get_hostname <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_hostname`, input_vec, length_vec) +Rcpp_ada_get_hostname <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_hostname`, url_vec) } -Rcpp_ada_get_pathname <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_pathname`, input_vec, length_vec) +Rcpp_ada_get_pathname <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_pathname`, url_vec) } -Rcpp_ada_get_search <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_search`, input_vec, length_vec) +Rcpp_ada_get_search <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_search`, url_vec) } -Rcpp_ada_get_protocol <- function(input_vec, length_vec) { - .Call(`_adaR_Rcpp_ada_get_protocol`, input_vec, length_vec) +Rcpp_ada_get_protocol <- function(url_vec) { + .Call(`_adaR_Rcpp_ada_get_protocol`, url_vec) } #' Function to percent-decode characters in URLs diff --git a/R/get.R b/R/get.R index ddeb72a..35f9947 100644 --- a/R/get.R +++ b/R/get.R @@ -1,3 +1,14 @@ +.get <- function(url, decode, func) { + if (is.null(url)) { + return(character(0)) + } + out <- func(url) + if (isTRUE(decode)) { + return(url_decode2(out)) + } + return(out) +} + #' Get href component of URL #' @inheritParams ada_url_parse #' @return logical @@ -5,12 +16,7 @@ #' ada_get_href("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_href <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_href(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_href) } #' Get username component of URL @@ -20,15 +26,9 @@ ada_get_href <- function(url, decode = TRUE) { #' ada_get_username("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_username <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_username(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_username) } - #' Get password component of URL #' @inheritParams ada_url_parse #' @return logical @@ -36,15 +36,9 @@ ada_get_username <- function(url, decode = TRUE) { #' ada_get_password("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_password <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_password(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_password) } - #' Get port component of URL #' @inheritParams ada_url_parse #' @return logical @@ -52,15 +46,9 @@ ada_get_password <- function(url, decode = TRUE) { #' ada_get_port("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_port <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_port(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_port) } - #' Get hash component of URL #' @inheritParams ada_url_parse #' @return logical @@ -68,15 +56,9 @@ ada_get_port <- function(url, decode = TRUE) { #' ada_get_hash("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_hash <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_hash(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_hash) } - #' Get host component of URL #' @inheritParams ada_url_parse #' @return logical @@ -84,15 +66,9 @@ ada_get_hash <- function(url, decode = TRUE) { #' ada_get_host("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_host <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_host(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_host) } - #' Get hostname component of URL #' @inheritParams ada_url_parse #' @return logical @@ -100,15 +76,9 @@ ada_get_host <- function(url, decode = TRUE) { #' ada_get_hostname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_hostname <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_hostname(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_hostname) } - #' Get pathname component of URL #' @inheritParams ada_url_parse #' @return logical @@ -116,15 +86,9 @@ ada_get_hostname <- function(url, decode = TRUE) { #' ada_get_pathname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_pathname <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_pathname(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_pathname) } - #' Get search component of URL #' @inheritParams ada_url_parse #' @return logical @@ -132,15 +96,9 @@ ada_get_pathname <- function(url, decode = TRUE) { #' ada_get_search("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_search <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_search(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_search) } - #' Get protocol component of URL #' @inheritParams ada_url_parse #' @return logical @@ -148,10 +106,5 @@ ada_get_search <- function(url, decode = TRUE) { #' ada_get_protocol("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") #' @export ada_get_protocol <- function(url, decode = TRUE) { - len <- vapply(url, function(x) nchar(x, type = "bytes"), integer(1), USE.NAMES = FALSE) - out <- Rcpp_ada_get_protocol(url, len) - if (isTRUE(decode)) { - return(url_decode2(out)) - } - return(out) + .get(url, decode, Rcpp_ada_get_protocol) } diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index e70e9da..27b0269 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -110,122 +110,112 @@ BEGIN_RCPP END_RCPP } // Rcpp_ada_get_href -CharacterVector Rcpp_ada_get_href(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_href(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_href(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_href(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_href(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_href(url_vec)); return rcpp_result_gen; END_RCPP } // Rcpp_ada_get_username -CharacterVector Rcpp_ada_get_username(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_username(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_username(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_username(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_username(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_username(url_vec)); return rcpp_result_gen; END_RCPP } // Rcpp_ada_get_password -CharacterVector Rcpp_ada_get_password(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_password(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_password(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_password(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_password(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_password(url_vec)); return rcpp_result_gen; END_RCPP } // Rcpp_ada_get_port -CharacterVector Rcpp_ada_get_port(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_port(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_port(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_port(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_port(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_port(url_vec)); return rcpp_result_gen; END_RCPP } // Rcpp_ada_get_hash -CharacterVector Rcpp_ada_get_hash(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_hash(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_hash(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_hash(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_hash(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_hash(url_vec)); return rcpp_result_gen; END_RCPP } // Rcpp_ada_get_host -CharacterVector Rcpp_ada_get_host(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_host(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_host(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_host(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_host(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_host(url_vec)); return rcpp_result_gen; END_RCPP } // Rcpp_ada_get_hostname -CharacterVector Rcpp_ada_get_hostname(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_hostname(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_hostname(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_hostname(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_hostname(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_hostname(url_vec)); return rcpp_result_gen; END_RCPP } // Rcpp_ada_get_pathname -CharacterVector Rcpp_ada_get_pathname(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_pathname(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_pathname(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_pathname(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_pathname(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_pathname(url_vec)); return rcpp_result_gen; END_RCPP } // Rcpp_ada_get_search -CharacterVector Rcpp_ada_get_search(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_search(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_search(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_search(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_search(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_search(url_vec)); return rcpp_result_gen; END_RCPP } // Rcpp_ada_get_protocol -CharacterVector Rcpp_ada_get_protocol(CharacterVector input_vec, IntegerVector length_vec); -RcppExport SEXP _adaR_Rcpp_ada_get_protocol(SEXP input_vecSEXP, SEXP length_vecSEXP) { +CharacterVector Rcpp_ada_get_protocol(const CharacterVector& url_vec); +RcppExport SEXP _adaR_Rcpp_ada_get_protocol(SEXP url_vecSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< CharacterVector >::type input_vec(input_vecSEXP); - Rcpp::traits::input_parameter< IntegerVector >::type length_vec(length_vecSEXP); - rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_protocol(input_vec, length_vec)); + Rcpp::traits::input_parameter< const CharacterVector& >::type url_vec(url_vecSEXP); + rcpp_result_gen = Rcpp::wrap(Rcpp_ada_get_protocol(url_vec)); return rcpp_result_gen; END_RCPP } @@ -251,16 +241,16 @@ static const R_CallMethodDef CallEntries[] = { {"_adaR_Rcpp_ada_has_port", (DL_FUNC) &_adaR_Rcpp_ada_has_port, 1}, {"_adaR_Rcpp_ada_has_hash", (DL_FUNC) &_adaR_Rcpp_ada_has_hash, 1}, {"_adaR_Rcpp_ada_has_search", (DL_FUNC) &_adaR_Rcpp_ada_has_search, 1}, - {"_adaR_Rcpp_ada_get_href", (DL_FUNC) &_adaR_Rcpp_ada_get_href, 2}, - {"_adaR_Rcpp_ada_get_username", (DL_FUNC) &_adaR_Rcpp_ada_get_username, 2}, - {"_adaR_Rcpp_ada_get_password", (DL_FUNC) &_adaR_Rcpp_ada_get_password, 2}, - {"_adaR_Rcpp_ada_get_port", (DL_FUNC) &_adaR_Rcpp_ada_get_port, 2}, - {"_adaR_Rcpp_ada_get_hash", (DL_FUNC) &_adaR_Rcpp_ada_get_hash, 2}, - {"_adaR_Rcpp_ada_get_host", (DL_FUNC) &_adaR_Rcpp_ada_get_host, 2}, - {"_adaR_Rcpp_ada_get_hostname", (DL_FUNC) &_adaR_Rcpp_ada_get_hostname, 2}, - {"_adaR_Rcpp_ada_get_pathname", (DL_FUNC) &_adaR_Rcpp_ada_get_pathname, 2}, - {"_adaR_Rcpp_ada_get_search", (DL_FUNC) &_adaR_Rcpp_ada_get_search, 2}, - {"_adaR_Rcpp_ada_get_protocol", (DL_FUNC) &_adaR_Rcpp_ada_get_protocol, 2}, + {"_adaR_Rcpp_ada_get_href", (DL_FUNC) &_adaR_Rcpp_ada_get_href, 1}, + {"_adaR_Rcpp_ada_get_username", (DL_FUNC) &_adaR_Rcpp_ada_get_username, 1}, + {"_adaR_Rcpp_ada_get_password", (DL_FUNC) &_adaR_Rcpp_ada_get_password, 1}, + {"_adaR_Rcpp_ada_get_port", (DL_FUNC) &_adaR_Rcpp_ada_get_port, 1}, + {"_adaR_Rcpp_ada_get_hash", (DL_FUNC) &_adaR_Rcpp_ada_get_hash, 1}, + {"_adaR_Rcpp_ada_get_host", (DL_FUNC) &_adaR_Rcpp_ada_get_host, 1}, + {"_adaR_Rcpp_ada_get_hostname", (DL_FUNC) &_adaR_Rcpp_ada_get_hostname, 1}, + {"_adaR_Rcpp_ada_get_pathname", (DL_FUNC) &_adaR_Rcpp_ada_get_pathname, 1}, + {"_adaR_Rcpp_ada_get_search", (DL_FUNC) &_adaR_Rcpp_ada_get_search, 1}, + {"_adaR_Rcpp_ada_get_protocol", (DL_FUNC) &_adaR_Rcpp_ada_get_protocol, 1}, {"_adaR_url_decode2", (DL_FUNC) &_adaR_url_decode2, 1}, {NULL, NULL, 0} }; diff --git a/src/adaR.cpp b/src/adaR.cpp index 46bb308..4172c74 100644 --- a/src/adaR.cpp +++ b/src/adaR.cpp @@ -114,192 +114,69 @@ LogicalVector Rcpp_ada_has_search(const CharacterVector& url_vec) { return Rcpp_ada_has(url_vec, &ada_has_search); } -// [[Rcpp::export]] -CharacterVector Rcpp_ada_get_href(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); +//higher-order function for all Rcpp_ada_get_* +CharacterVector Rcpp_ada_get(const CharacterVector& url_vec, std::function func) { + unsigned int n = url_vec.length(); CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; + for (int i = 0; i < url_vec.length(); i++) { + String s = url_vec[i]; const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_href(url)); - } else { + ada_url url = ada_parse(input, std::strlen(input)); + if (!ada_is_valid(url)) { out[i] = NA_STRING; + } else { + out[i] = charsub(func(url)); } } return (out); } // [[Rcpp::export]] -CharacterVector Rcpp_ada_get_username(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); - CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; - const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_username(url)); - } else { - out[i] = NA_STRING; - } - } - return (out); +CharacterVector Rcpp_ada_get_href(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_href); } // [[Rcpp::export]] -CharacterVector Rcpp_ada_get_password(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); - CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; - const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_password(url)); - } else { - out[i] = NA_STRING; - } - } - return (out); +CharacterVector Rcpp_ada_get_username(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_username); } // [[Rcpp::export]] -CharacterVector Rcpp_ada_get_port(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); - CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; - const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_port(url)); - } else { - out[i] = NA_STRING; - } - } - return (out); +CharacterVector Rcpp_ada_get_password(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_password); } // [[Rcpp::export]] -CharacterVector Rcpp_ada_get_hash(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); - CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; - const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_hash(url)); - } else { - out[i] = NA_STRING; - } - } - return (out); +CharacterVector Rcpp_ada_get_port(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_port); } // [[Rcpp::export]] -CharacterVector Rcpp_ada_get_host(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); - CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; - const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_host(url)); - } else { - out[i] = NA_STRING; - } - } - return (out); +CharacterVector Rcpp_ada_get_hash(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_hash); } // [[Rcpp::export]] -CharacterVector Rcpp_ada_get_hostname(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); - CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; - const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_hostname(url)); - } else { - out[i] = NA_STRING; - } - } - return (out); +CharacterVector Rcpp_ada_get_host(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_host); } // [[Rcpp::export]] -CharacterVector Rcpp_ada_get_pathname(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); - CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; - const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_pathname(url)); - } else { - out[i] = NA_STRING; - } - } - return (out); +CharacterVector Rcpp_ada_get_hostname(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_hostname); } // [[Rcpp::export]] -CharacterVector Rcpp_ada_get_search(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); - CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; - const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_search(url)); - } else { - out[i] = NA_STRING; - } - } - return (out); +CharacterVector Rcpp_ada_get_pathname(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_pathname); } // [[Rcpp::export]] -CharacterVector Rcpp_ada_get_protocol(CharacterVector input_vec, - IntegerVector length_vec) { - int n = length_vec.length(); - CharacterVector out(n); - for (int i = 0; i < input_vec.length(); i++) { - String s = input_vec[i]; - const char* input = s.get_cstring(); - size_t length = length_vec[i]; - ada_url url = ada_parse(input, length); - if (ada_is_valid(url)) { - out[i] = charsub(ada_get_protocol(url)); - } else { - out[i] = NA_STRING; - } - } - return (out); +CharacterVector Rcpp_ada_get_search(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_search); +} + +// [[Rcpp::export]] +CharacterVector Rcpp_ada_get_protocol(const CharacterVector& url_vec) { + return Rcpp_ada_get(url_vec, &ada_get_protocol); } From 4c310cc0cb6e7b0ed161d7d4f3f8295319706ecc Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 11:34:52 +0200 Subject: [PATCH 2/4] Consolidate doc --- R/get.R | 73 ++++++++++++++--------------------------- man/ada_get_hash.Rd | 22 ------------- man/ada_get_host.Rd | 22 ------------- man/ada_get_hostname.Rd | 22 ------------- man/ada_get_href.Rd | 48 ++++++++++++++++++++++++--- man/ada_get_password.Rd | 22 ------------- man/ada_get_pathname.Rd | 22 ------------- man/ada_get_port.Rd | 22 ------------- man/ada_get_protocol.Rd | 22 ------------- man/ada_get_search.Rd | 22 ------------- man/ada_get_username.Rd | 22 ------------- 11 files changed, 69 insertions(+), 250 deletions(-) delete mode 100644 man/ada_get_hash.Rd delete mode 100644 man/ada_get_host.Rd delete mode 100644 man/ada_get_hostname.Rd delete mode 100644 man/ada_get_password.Rd delete mode 100644 man/ada_get_pathname.Rd delete mode 100644 man/ada_get_port.Rd delete mode 100644 man/ada_get_protocol.Rd delete mode 100644 man/ada_get_search.Rd delete mode 100644 man/ada_get_username.Rd diff --git a/R/get.R b/R/get.R index 35f9947..087d20a 100644 --- a/R/get.R +++ b/R/get.R @@ -9,101 +9,78 @@ return(out) } -#' Get href component of URL +#' Get a specific component of URL #' @inheritParams ada_url_parse -#' @return logical +#' @return character, `NA` if not a valid URL #' @examples -#' ada_get_href("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' url <- "https://user_1:password_1@example.org:8080/dir/../api?q=1#frag" +#' ada_get_href(url) +#' ada_get_username(url) +#' ada_get_password(url) +#' ada_get_port(url) +#' ada_get_hash(url) +#' ada_get_host(url) +#' ada_get_hostname(url) +#' ada_get_pathname(url) +#' ada_get_search(url) +#' ada_get_protocol(url) +#' ## these functions are vectorized +#' urls <- c("http://www.google.com", "http://www.google.com:80", "noturl") +#' ada_get_port(urls) #' @export ada_get_href <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_href) } -#' Get username component of URL -#' @inheritParams ada_url_parse -#' @return logical -#' @examples -#' ada_get_username("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_get_href #' @export ada_get_username <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_username) } -#' Get password component of URL -#' @inheritParams ada_url_parse -#' @return logical -#' @examples -#' ada_get_password("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_get_href #' @export ada_get_password <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_password) } -#' Get port component of URL -#' @inheritParams ada_url_parse -#' @return logical -#' @examples -#' ada_get_port("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_get_href #' @export ada_get_port <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_port) } -#' Get hash component of URL -#' @inheritParams ada_url_parse -#' @return logical -#' @examples -#' ada_get_hash("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_get_href #' @export ada_get_hash <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_hash) } -#' Get host component of URL -#' @inheritParams ada_url_parse -#' @return logical -#' @examples -#' ada_get_host("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_get_href #' @export ada_get_host <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_host) } -#' Get hostname component of URL -#' @inheritParams ada_url_parse -#' @return logical -#' @examples -#' ada_get_hostname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_get_href #' @export ada_get_hostname <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_hostname) } -#' Get pathname component of URL -#' @inheritParams ada_url_parse -#' @return logical -#' @examples -#' ada_get_pathname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_get_href #' @export ada_get_pathname <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_pathname) } -#' Get search component of URL -#' @inheritParams ada_url_parse -#' @return logical -#' @examples -#' ada_get_search("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_get_href #' @export ada_get_search <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_search) } -#' Get protocol component of URL -#' @inheritParams ada_url_parse -#' @return logical -#' @examples -#' ada_get_protocol("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +#' @rdname ada_get_href #' @export ada_get_protocol <- function(url, decode = TRUE) { .get(url, decode, Rcpp_ada_get_protocol) diff --git a/man/ada_get_hash.Rd b/man/ada_get_hash.Rd deleted file mode 100644 index 414fb66..0000000 --- a/man/ada_get_hash.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get.R -\name{ada_get_hash} -\alias{ada_get_hash} -\title{Get hash component of URL} -\usage{ -ada_get_hash(url, decode = TRUE) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} - -\item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} -} -\value{ -logical -} -\description{ -Get hash component of URL -} -\examples{ -ada_get_hash("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_get_host.Rd b/man/ada_get_host.Rd deleted file mode 100644 index b0eb5af..0000000 --- a/man/ada_get_host.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get.R -\name{ada_get_host} -\alias{ada_get_host} -\title{Get host component of URL} -\usage{ -ada_get_host(url, decode = TRUE) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} - -\item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} -} -\value{ -logical -} -\description{ -Get host component of URL -} -\examples{ -ada_get_host("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_get_hostname.Rd b/man/ada_get_hostname.Rd deleted file mode 100644 index 2c7bdca..0000000 --- a/man/ada_get_hostname.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get.R -\name{ada_get_hostname} -\alias{ada_get_hostname} -\title{Get hostname component of URL} -\usage{ -ada_get_hostname(url, decode = TRUE) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} - -\item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} -} -\value{ -logical -} -\description{ -Get hostname component of URL -} -\examples{ -ada_get_hostname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_get_href.Rd b/man/ada_get_href.Rd index a261416..de5409b 100644 --- a/man/ada_get_href.Rd +++ b/man/ada_get_href.Rd @@ -2,9 +2,36 @@ % Please edit documentation in R/get.R \name{ada_get_href} \alias{ada_get_href} -\title{Get href component of URL} +\alias{ada_get_username} +\alias{ada_get_password} +\alias{ada_get_port} +\alias{ada_get_hash} +\alias{ada_get_host} +\alias{ada_get_hostname} +\alias{ada_get_pathname} +\alias{ada_get_search} +\alias{ada_get_protocol} +\title{Get a specific component of URL} \usage{ ada_get_href(url, decode = TRUE) + +ada_get_username(url, decode = TRUE) + +ada_get_password(url, decode = TRUE) + +ada_get_port(url, decode = TRUE) + +ada_get_hash(url, decode = TRUE) + +ada_get_host(url, decode = TRUE) + +ada_get_hostname(url, decode = TRUE) + +ada_get_pathname(url, decode = TRUE) + +ada_get_search(url, decode = TRUE) + +ada_get_protocol(url, decode = TRUE) } \arguments{ \item{url}{character. one or more URL to be parsed} @@ -12,11 +39,24 @@ ada_get_href(url, decode = TRUE) \item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} } \value{ -logical +character, \code{NA} if not a valid URL } \description{ -Get href component of URL +Get a specific component of URL } \examples{ -ada_get_href("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") +url <- "https://user_1:password_1@example.org:8080/dir/../api?q=1#frag" +ada_get_href(url) +ada_get_username(url) +ada_get_password(url) +ada_get_port(url) +ada_get_hash(url) +ada_get_host(url) +ada_get_hostname(url) +ada_get_pathname(url) +ada_get_search(url) +ada_get_protocol(url) +## these functions are vectorized +urls <- c("http://www.google.com", "http://www.google.com:80", "noturl") +ada_get_port(urls) } diff --git a/man/ada_get_password.Rd b/man/ada_get_password.Rd deleted file mode 100644 index eccd2cf..0000000 --- a/man/ada_get_password.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get.R -\name{ada_get_password} -\alias{ada_get_password} -\title{Get password component of URL} -\usage{ -ada_get_password(url, decode = TRUE) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} - -\item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} -} -\value{ -logical -} -\description{ -Get password component of URL -} -\examples{ -ada_get_password("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_get_pathname.Rd b/man/ada_get_pathname.Rd deleted file mode 100644 index 81e2b6e..0000000 --- a/man/ada_get_pathname.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get.R -\name{ada_get_pathname} -\alias{ada_get_pathname} -\title{Get pathname component of URL} -\usage{ -ada_get_pathname(url, decode = TRUE) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} - -\item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} -} -\value{ -logical -} -\description{ -Get pathname component of URL -} -\examples{ -ada_get_pathname("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_get_port.Rd b/man/ada_get_port.Rd deleted file mode 100644 index 60a78b8..0000000 --- a/man/ada_get_port.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get.R -\name{ada_get_port} -\alias{ada_get_port} -\title{Get port component of URL} -\usage{ -ada_get_port(url, decode = TRUE) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} - -\item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} -} -\value{ -logical -} -\description{ -Get port component of URL -} -\examples{ -ada_get_port("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_get_protocol.Rd b/man/ada_get_protocol.Rd deleted file mode 100644 index df747ef..0000000 --- a/man/ada_get_protocol.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get.R -\name{ada_get_protocol} -\alias{ada_get_protocol} -\title{Get protocol component of URL} -\usage{ -ada_get_protocol(url, decode = TRUE) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} - -\item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} -} -\value{ -logical -} -\description{ -Get protocol component of URL -} -\examples{ -ada_get_protocol("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_get_search.Rd b/man/ada_get_search.Rd deleted file mode 100644 index 20ca77a..0000000 --- a/man/ada_get_search.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get.R -\name{ada_get_search} -\alias{ada_get_search} -\title{Get search component of URL} -\usage{ -ada_get_search(url, decode = TRUE) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} - -\item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} -} -\value{ -logical -} -\description{ -Get search component of URL -} -\examples{ -ada_get_search("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} diff --git a/man/ada_get_username.Rd b/man/ada_get_username.Rd deleted file mode 100644 index 9537afa..0000000 --- a/man/ada_get_username.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get.R -\name{ada_get_username} -\alias{ada_get_username} -\title{Get username component of URL} -\usage{ -ada_get_username(url, decode = TRUE) -} -\arguments{ -\item{url}{character. one or more URL to be parsed} - -\item{decode}{logical. Whether to decode the output (see \code{\link[utils:URLencode]{utils::URLdecode()}}), default to \code{TRUE}} -} -\value{ -logical -} -\description{ -Get username component of URL -} -\examples{ -ada_get_username("https://user_1:password_1@example.org:8080/dir/../api?q=1#frag") -} From 6891377976cf37bd8865834af939237189b8bc93 Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 11:41:28 +0200 Subject: [PATCH 3/4] Add tests --- tests/testthat/test-get.R | 42 ++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test-get.R b/tests/testthat/test-get.R index 8b8cb0f..dca1a87 100644 --- a/tests/testthat/test-get.R +++ b/tests/testthat/test-get.R @@ -1,11 +1,35 @@ test_that("all get functions work", { - url <- "https://user_1:password_1@example.org:8080/dir/../api?q=1#frag" - expect_equal(ada_get_username(url), "user_1") - expect_equal(ada_get_password(url), "password_1") - expect_equal(ada_get_host(url), "example.org:8080") - expect_equal(ada_get_hostname(url), "example.org") - expect_equal(ada_get_port(url), "8080") - expect_equal(ada_get_pathname(url), "/api") - expect_equal(ada_get_search(url), "?q=1") - expect_equal(ada_get_hash(url), "#frag") + url <- "https://user_1:password_1@example.org:8080/dir/../api?q=1#frag" + expect_equal(ada_get_username(url), "user_1") + expect_equal(ada_get_password(url), "password_1") + expect_equal(ada_get_host(url), "example.org:8080") + expect_equal(ada_get_hostname(url), "example.org") + expect_equal(ada_get_port(url), "8080") + expect_equal(ada_get_pathname(url), "/api") + expect_equal(ada_get_search(url), "?q=1") + expect_equal(ada_get_hash(url), "#frag") +}) + +get_functions <- c(ada_get_username, ada_get_password, ada_get_host, ada_get_hostname, ada_get_port, ada_get_pathname, + ada_get_search, ada_get_hash) + +test_that("invalid urls should return NA, #26", { + url <- "thisisnoturl" + for (func in get_functions) { + expect_error(func(url), NA) + } +}) + +test_that("corners #31", { + for (func in get_functions) { + expect_error(func(c(NA, NA_character_, "")), NA) + } + for (func in get_functions) { + expect_error(func(NULL), NA) + } +}) + +test_that("decode can pass", { + expect_equal(ada_get_search("https://www.google.co.jp/search?q=\u30c9\u30a4\u30c4"), "?q=\u30c9\u30a4\u30c4") + expect_equal(ada_get_search("https://www.google.co.jp/search?q=\u30c9\u30a4\u30c4", decode = FALSE), "?q=%E3%83%89%E3%82%A4%E3%83%84") }) From afe421cd53a08e246f76e22db4a90cde3d8f6911 Mon Sep 17 00:00:00 2001 From: chainsawriot Date: Mon, 25 Sep 2023 11:55:14 +0200 Subject: [PATCH 4/4] Update doc [no ci] --- R/get.R | 2 ++ man/ada_get_href.Rd | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/R/get.R b/R/get.R index 087d20a..a3f6f7b 100644 --- a/R/get.R +++ b/R/get.R @@ -10,6 +10,8 @@ } #' Get a specific component of URL +#' +#' These functions get a specific component of URL. #' @inheritParams ada_url_parse #' @return character, `NA` if not a valid URL #' @examples diff --git a/man/ada_get_href.Rd b/man/ada_get_href.Rd index de5409b..1bd38cb 100644 --- a/man/ada_get_href.Rd +++ b/man/ada_get_href.Rd @@ -42,7 +42,7 @@ ada_get_protocol(url, decode = TRUE) character, \code{NA} if not a valid URL } \description{ -Get a specific component of URL +These functions get a specific component of URL. } \examples{ url <- "https://user_1:password_1@example.org:8080/dir/../api?q=1#frag"