adaR is a wrapper for ada-url, a WHATWG-compliant and fast URL parser written in modern C++ .
You can install the development version of adaR from GitHub with:
# install.packages("devtools")
devtools::install_github("schochastics/adaR")
This is a basic example which shows all the returned components
library(adaR)
ada_url_parse("https://user_1:[email protected]:8080/dir/../api?q=1#frag")
#> $href
#> [1] "https://user_1:[email protected]:8080/api?q=1#frag"
#>
#> $protocol
#> [1] "https:"
#>
#> $username
#> [1] "user_1"
#>
#> $password
#> [1] "password_1"
#>
#> $host
#> [1] "example.org:8080"
#>
#> $hostname
#> [1] "example.org"
#>
#> $port
#> [1] "8080"
#>
#> $pathname
#> [1] "/api"
#>
#> $search
#> [1] "?q=1"
#>
#> $hash
#> [1] "#frag"
It solves some problems of urltools with more complex urls.
urltools::url_parse("https://www.google.com/maps/place/Pennsylvania+Station/@40.7519848,-74.0015045,14.
7z/data=!4m5!3m4!1s0x89c259ae15b2adcb:0x7955420634fd7eba!8m2!3d40.750568!4d-73.993519")
#> scheme domain port
#> 1 https 40.7519848,-74.0015045,14.\n 7z <NA>
#> path
#> 1 data=!4m5!3m4!1s0x89c259ae15b2adcb:0x7955420634fd7eba!8m2!3d40.750568!4d-73.993519
#> parameter fragment
#> 1 <NA> <NA>
ada_url_parse("https://www.google.com/maps/place/Pennsylvania+Station/@40.7519848,-74.0015045,14.7z/data=!4m
5!3m4!1s0x89c259ae15b2adcb:0x7955420634fd7eba!8m2!3d40.750568!4d-73.993519")
#> $href
#> [1] "https://www.google.com/maps/place/Pennsylvania+Station/@40.7519848,-74.0015045,14.7z/data=!4m%20%20%205!3m4!1s0x89c259ae15b2adcb:0x7955420634fd7eba!8m2!3d40.750568!4d-73.993519"
#>
#> $protocol
#> [1] "https:"
#>
#> $username
#> [1] ""
#>
#> $password
#> [1] ""
#>
#> $host
#> [1] "www.google.com"
#>
#> $hostname
#> [1] "www.google.com"
#>
#> $port
#> [1] ""
#>
#> $pathname
#> [1] "/maps/place/Pennsylvania+Station/@40.7519848,-74.0015045,14.7z/data=!4m%20%20%205!3m4!1s0x89c259ae15b2adcb:0x7955420634fd7eba!8m2!3d40.750568!4d-73.993519"
#>
#> $search
#> [1] ""
#>
#> $hash
#> [1] ""
and it is fast!
bench::mark(
ada = replicate(1000, ada_url_parse("https://user_1:[email protected]:8080/dir/../api?q=1#frag")),
urltools = replicate(1000, urltools::url_parse("https://user_1:[email protected]:8080/dir/../api?q=1#frag")),
iterations = 1, check = FALSE
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 2 ร 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 ada 10.2ms 10.2ms 98.4 2.67MB 0
#> 2 urltools 304.2ms 304.2ms 3.29 2.59MB 19.7