R/01-bit.R

Defines functions is_bit_vec is_logical_vec is_bitstring bit_to_bitstring bit_to_logical bitstring_to_bit bitstring_to_logical logical_to_bit logical_to_bitstring

Documented in bitstring_to_bit bitstring_to_logical bit_to_bitstring bit_to_logical is_bitstring is_bit_vec is_logical_vec logical_to_bit logical_to_bitstring

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname is_raw_vec
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
is_bit_vec <- function(x) {
  bit::is.bit(x)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname is_raw_vec
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
is_logical_vec <- function(x) {
  is.logical(x) && !any(is.na(x))
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname is_raw_vec
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
is_bitstring <- function(x) {
  is.character(x) &&
    length(x) == 1L &&
    (nchar(x) == 0L || grepl("^[01 \n\t]+$", x))
}




#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |           | bit | bitstring | logical |
# |-----------|-----|-----------|---------|
# | bit       | .   | o         | o       |
# | bitstring | o   | .         | o       |
# | logical   | o   | o         | .       |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bit_to_bitstring <- function(bit_vec, ...) {
  stopifnot(is_bit_vec(bit_vec))

  paste(as.integer(bit_vec), collapse = "")
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bit_to_logical <- function(bit_vec, ...) {
  stopifnot(is_bit_vec(bit_vec))

  as.logical(bit_vec)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bitstring_to_bit <- function(bitstring, ...) {
  logical_vec <- bitstring_to_logical(bitstring)
  logical_to_bit(logical_vec)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bitstring_to_logical <- function(bitstring, ...) {
  stopifnot(is_bitstring(bitstring))
  bitstring <- tidy_bitstring(bitstring)

  strsplit(bitstring, '')[[1L]] == 1L
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
logical_to_bit <- function(logical_vec, ...) {
  stopifnot(is_logical_vec(logical_vec))

  bit::as.bit(logical_vec)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
logical_to_bitstring <- function(logical_vec, ...) {
  stopifnot(is_logical_vec(logical_vec))

  paste(as.integer(logical_vec), collapse = "")
}
coolbutuseless/minitypes documentation built on May 27, 2019, 9:55 a.m.