The R Project SVN R

Rev

Rev 79719 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 79719 Rev 80028
Line 1... Line 1...
1
% File src/library/base/man/validUTF8.Rd
1
% File src/library/base/man/validUTF8.Rd
2
% Part of the R package, https://www.R-project.org
2
% Part of the R package, https://www.R-project.org
3
% Copyright 2015-2020 R Core Team
3
% Copyright 2015-2021 R Core Team
4
% Distributed under GPL 2 or later
4
% Distributed under GPL 2 or later
5
 
5
 
6
\name{validUTF8}
6
\name{validUTF8}
7
\title{Check if a Character Vector is Validly Encoded}
7
\title{Check if a Character Vector is Validly Encoded}
8
\alias{validUTF8}
8
\alias{validUTF8}
Line 60... Line 60...
60
Encoding(x) <-"UTF-8"
60
Encoding(x) <-"UTF-8"
61
validEnc(x) # typically the last, x[10], is invalid
61
validEnc(x) # typically the last, x[10], is invalid
62
 
62
 
63
## Maybe advantageous to declare it "unknown":
63
## Maybe advantageous to declare it "unknown":
64
G <- x ; Encoding(G[!validEnc(G)]) <- "unknown"
64
G <- x ; Encoding(G[!validEnc(G)]) <- "unknown"
65
try( substr(x, 1,1) ) # gives 'invalid multibyte string' error
65
try( substr(x, 1,1) ) # gives 'invalid multibyte string' error in a UTF-8 locale
66
try( substr(G, 1,1) ) # works
66
try( substr(G, 1,1) ) # works in a UTF-8 locale
67
nchar(G) # fine, too
67
nchar(G) # fine, too
68
## but it is not "more valid" typically:
68
## but it is not "more valid" typically:
69
all.equal(validEnc(x),
69
all.equal(validEnc(x),
70
          validEnc(G)) # typically TRUE
70
          validEnc(G)) # typically TRUE
71
}
71
}