| 1 |
|
| 2 |
renv_parse_file <- function(file = "", ...) { |
| 3 |
if (nzchar(file)) { |
| 4 |
renv_scope_options(warn = -1L) |
| 5 |
text <- readLines(file, warn = FALSE, encoding = "UTF-8") |
| 6 |
renv_parse_impl(text, srcfile = file, ...) |
| 7 |
} |
| 8 |
} |
| 9 |
|
| 10 |
renv_parse_text <- function(text = NULL, ...) { |
| 11 |
if (is.character(text)) { |
| 12 |
renv_parse_impl(text, ...) |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
renv_parse_impl <- function(text, ...) { |
| 17 |
|
| 18 |
# save default encoding |
| 19 |
enc <- Encoding(text) |
| 20 |
|
| 21 |
# disable warnings + encoding conversions |
| 22 |
renv_scope_options( |
| 23 |
warn = 1L, |
| 24 |
encoding = "native.enc" |
| 25 |
) |
| 26 |
|
| 27 |
# attempt multiple parse methods |
| 28 |
methods <- list( |
| 29 |
renv_parse_impl_asis, |
| 30 |
renv_parse_impl_native, |
| 31 |
renv_parse_impl_utf8 |
| 32 |
) |
| 33 |
|
| 34 |
# attempt with different guessed encodings |
| 35 |
encodings <- c("UTF-8", "unknown") |
| 36 |
|
| 37 |
for (encoding in encodings) { |
| 38 |
Encoding(text) <- encoding |
| 39 |
for (method in methods) { |
| 40 |
parsed <- catch(method(text, ...)) |
| 41 |
if (!inherits(parsed, "error")) |
| 42 |
return(parsed) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
# if these all fail, then just try the default |
| 47 |
# parse and let the error propagate |
| 48 |
defer(Sys.setlocale()) |
| 49 |
Encoding(text) <- enc |
| 50 |
parse(text = text, ...) |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
renv_parse_impl_asis <- function(text, ...) { |
| 55 |
defer(Sys.setlocale()) |
| 56 |
parse(text = text, ...) |
| 57 |
} |
| 58 |
|
| 59 |
renv_parse_impl_native <- function(text, ...) { |
| 60 |
defer(Sys.setlocale()) |
| 61 |
parse(text = enc2native(text), encoding = "unknown", ...) |
| 62 |
} |
| 63 |
|
| 64 |
renv_parse_impl_utf8 <- function(text, ...) { |
| 65 |
defer(Sys.setlocale()) |
| 66 |
parse(text = enc2utf8(text), encoding = "UTF-8", ...) |
| 67 |
} |
| 68 |
|