| 42333 |
ripley |
1 |
% File src/library/base/man/Encoding.Rd
|
| 68948 |
ripley |
2 |
% Part of the R package, https://www.R-project.org
|
| 76967 |
ripley |
3 |
% Copyright 1995-2019 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 40678 |
ripley |
6 |
\name{Encoding}
|
| 56186 |
murdoch |
7 |
\alias{Encoding}
|
| 40678 |
ripley |
8 |
\alias{Encoding<-}
|
| 50762 |
ripley |
9 |
\alias{enc2native}
|
|
|
10 |
\alias{enc2utf8}
|
| 40765 |
ripley |
11 |
\concept{encoding}
|
| 40678 |
ripley |
12 |
\title{Read or Set the Declared Encodings for a Character Vector}
|
|
|
13 |
\description{
|
|
|
14 |
Read or set the declared encodings for a character vector.
|
|
|
15 |
}
|
|
|
16 |
\usage{
|
|
|
17 |
Encoding(x)
|
|
|
18 |
|
|
|
19 |
Encoding(x) <- value
|
| 50762 |
ripley |
20 |
|
|
|
21 |
enc2native(x)
|
|
|
22 |
enc2utf8(x)
|
| 40678 |
ripley |
23 |
}
|
|
|
24 |
\arguments{
|
|
|
25 |
\item{x}{A character vector.}
|
|
|
26 |
\item{value}{A character vector of positive length.}
|
|
|
27 |
}
|
|
|
28 |
\details{
|
| 65505 |
ripley |
29 |
Character strings in \R can be declared to be encoded in
|
|
|
30 |
\code{"latin1"} or \code{"UTF-8"} or as \code{"bytes"}. These
|
|
|
31 |
declarations can be read by \code{Encoding}, which will return a
|
|
|
32 |
character vector of values \code{"latin1"}, \code{"UTF-8"}
|
|
|
33 |
\code{"bytes"} or \code{"unknown"}, or set, when \code{value} is
|
|
|
34 |
recycled as needed and other values are silently treated as
|
|
|
35 |
\code{"unknown"}. ASCII strings will never be marked with a declared
|
|
|
36 |
encoding, since their representation is the same in all supported
|
|
|
37 |
encodings. Strings marked as \code{"bytes"} are intended to be
|
|
|
38 |
non-ASCII strings which should be manipulated as bytes, and never
|
| 68833 |
ripley |
39 |
converted to a character encoding (so writing them to a text file is
|
| 76967 |
ripley |
40 |
supported only by \code{writeLines(useBytes = TRUE)}).
|
|
|
41 |
% non-bug report PR#16327
|
| 50762 |
ripley |
42 |
|
|
|
43 |
\code{enc2native} and \code{enc2utf8} convert elements of character
|
| 50903 |
hornik |
44 |
vectors to the native encoding or UTF-8 respectively, taking any
|
| 51317 |
ripley |
45 |
marked encoding into account. They are \link{primitive} functions,
|
|
|
46 |
designed to do minimal copying.
|
| 61433 |
ripley |
47 |
|
| 40696 |
ripley |
48 |
There are other ways for character strings to acquire a declared
|
| 46864 |
ripley |
49 |
encoding apart from explicitly setting it (and these have changed as
|
|
|
50 |
\R has evolved). Functions \code{\link{scan}},
|
|
|
51 |
\code{\link{read.table}}, \code{\link{readLines}}, and
|
|
|
52 |
\code{\link{parse}} have an \code{encoding} argument that is used to
|
|
|
53 |
declare encodings, \code{\link{iconv}} declares encodings from its
|
| 65505 |
ripley |
54 |
\code{to} argument, and console input in suitable locales is also
|
| 46864 |
ripley |
55 |
declared. \code{\link{intToUtf8}} declares its output as
|
| 54236 |
ripley |
56 |
\code{"UTF-8"}, and output text connections (see
|
|
|
57 |
\code{\link{textConnection}}) are marked if running in a
|
| 46864 |
ripley |
58 |
suitable locale. Under some circumstances (see its help page)
|
|
|
59 |
\code{\link{source}(encoding=)} will mark encodings of character
|
|
|
60 |
strings it outputs.
|
| 43715 |
ripley |
61 |
|
| 43724 |
ripley |
62 |
Most character manipulation functions will set the encoding on output
|
|
|
63 |
strings if it was declared on the corresponding input. These include
|
| 50269 |
ripley |
64 |
\code{\link{chartr}}, \code{\link{strsplit}(useBytes = FALSE)},
|
| 50230 |
ripley |
65 |
\code{\link{tolower}} and \code{\link{toupper}} as well as
|
|
|
66 |
\code{\link{sub}(useBytes = FALSE)} and \code{\link{gsub}(useBytes =
|
|
|
67 |
FALSE)}. Note that such functions do not \emph{preserve} the
|
|
|
68 |
encoding, but if they know the input encoding and that the string has
|
|
|
69 |
been successfully re-encoded (to the current encoding or UTF-8), they
|
|
|
70 |
mark the output.
|
| 45847 |
ripley |
71 |
|
| 46864 |
ripley |
72 |
\code{\link{substr}} does preserve the encoding, and
|
| 44054 |
ripley |
73 |
\code{\link{chartr}}, \code{\link{tolower}} and \code{\link{toupper}}
|
| 46848 |
ripley |
74 |
preserve UTF-8 encoding on systems with Unicode wide characters. With
|
| 45847 |
ripley |
75 |
their \code{fixed} and \code{perl} options, \code{\link{strsplit}},
|
| 46864 |
ripley |
76 |
\code{\link{sub}} and \code{gsub} will give a marked UTF-8 result if
|
|
|
77 |
any of the inputs are UTF-8.
|
| 45847 |
ripley |
78 |
|
| 56996 |
hornik |
79 |
\code{\link{paste}} and \code{\link{sprintf}} return elements marked
|
| 61433 |
ripley |
80 |
as bytes if any of the corresponding inputs is marked as bytes, and
|
| 56996 |
hornik |
81 |
otherwise marked as UTF-8 of any of the inputs is marked as UTF-8.
|
| 49415 |
ripley |
82 |
|
|
|
83 |
\code{\link{match}}, \code{\link{pmatch}}, \code{\link{charmatch}},
|
|
|
84 |
\code{\link{duplicated}} and \code{\link{unique}} all match in UTF-8
|
|
|
85 |
if any of the elements are marked as UTF-8.
|
| 73791 |
ripley |
86 |
|
|
|
87 |
There is some ambiguity as to what is meant by a \sQuote{Latin-1}
|
|
|
88 |
locale, since some OSes (notably Windows) make use of character
|
|
|
89 |
positions used for control characters in the ISO 8859-1 character set.
|
|
|
90 |
How such characters are interpreted is system-dependent but as from \R
|
|
|
91 |
3.5.0 they are if possible interpreted as per Windows codepage 1252
|
|
|
92 |
(which Microsoft calls \sQuote{Windows Latin 1 (ANSI)}) when
|
|
|
93 |
converting to e.g.\sspace{}UTF-8.
|
| 40678 |
ripley |
94 |
}
|
|
|
95 |
\value{
|
|
|
96 |
A character vector.
|
| 65505 |
ripley |
97 |
|
|
|
98 |
For \code{enc2utf8} encodings are always marked: they are for
|
|
|
99 |
\code{enc2native} in UTF-8 and Latin-1 locales.
|
| 40678 |
ripley |
100 |
}
|
|
|
101 |
\examples{
|
|
|
102 |
## x is intended to be in latin1
|
|
|
103 |
x <- "fa\xE7ile"
|
|
|
104 |
Encoding(x)
|
|
|
105 |
Encoding(x) <- "latin1"
|
|
|
106 |
x
|
| 40765 |
ripley |
107 |
xx <- iconv(x, "latin1", "UTF-8")
|
|
|
108 |
Encoding(c(x, xx))
|
|
|
109 |
c(x, xx)
|
| 54747 |
ripley |
110 |
Encoding(xx) <- "bytes"
|
|
|
111 |
xx # will be encoded in hex
|
|
|
112 |
cat("xx = ", xx, "\n", sep = "")
|
| 40678 |
ripley |
113 |
}
|
|
|
114 |
\keyword{utilities}
|
|
|
115 |
\keyword{character}
|