R/guess_expected_encoding.R

Defines functions guess_expected_encoding

# guess_expected_encoding ------------------------------------------------------
guess_expected_encoding <- function(file, expected, dbg = TRUE)
{
  encodings <- readr::guess_encoding(file)$encoding

  kwb.utils::printIf(dbg, encodings, "Encodings guessed by readr")

  common <- intersect(tolower(encodings), tolower(expected))

  encoding <- if (length(common)) {
    common[1]
  } else {
    expected[1]
  }

  kwb.utils::catIf(dbg, sprintf("Selected encoding: %s\n", encoding))
  encoding
}
KWB-R/fakin.path.app documentation built on Nov. 25, 2020, 10:20 p.m.