Skip to content

Commit

Permalink
use validUTF8() if available in base R to test if a character vector …
Browse files Browse the repository at this point in the history
…is valid UTF-8
  • Loading branch information
yihui committed Nov 17, 2021
1 parent dbc0e4a commit 765b2c8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ append_unique = function(text, con, sort = function(x) base::sort(unique(x))) {

# which lines are invalid UTF-8
invalid_utf8 = function(x) {
which(!is.na(x) & is.na(iconv(x, 'UTF-8', 'UTF-8')))
which(!is_utf8(x))
}

test_utf8 = function(x) {
is.na(x) | !is.na(iconv(x, 'UTF-8', 'UTF-8'))
}

# validUTF8() was added to base R 3.3.0
is_utf8 = function(x) {
if ('validUTF8' %in% ls(baseenv())) validUTF8(x) else test_utf8(x)
}

#' Read a text file and concatenate the lines by \code{'\n'}
Expand Down

0 comments on commit 765b2c8

Please sign in to comment.