~hrbrmstr/playdoh

ref: be6148eec445b65dc8281ba8c32e0f576ffe64d0 playdoh/R/to-inaddr-arpa.R -rw-r--r-- 506 bytes
be6148eeboB Rudis tibble return and more examples in README 1 year, 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ipv4_regex <-
  "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

#' Convert a vector of IPv4 addresses to in-addr.arpa format
#'
#' @md
#' @param ipv4 vector of ipv4 addresses
#' @export
to_inaddr_arpa <- function(ipv4) {

  ifelse(
    test = grepl(ipv4_regex, ipv4),
    yes =  paste0(
      c(
        rev(unlist(strsplit(ipv4, ".", 4), use.names = FALSE)),
        "in-addr.arpa."
      ),
      sep = "",
      collapse = "."
    ),
    no = ipv4
  )

}