The R Project SVN R

Rev

Rev 82597 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/Encoding.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
84678 ripley 3
% Copyright 1995-2023 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
80054 ripley 50
  \R has evolved).  The parser marks strings containing \samp{\\u} or
51
  \samp{\\U} escapes.  Functions \code{\link{scan}},
46864 ripley 52
  \code{\link{read.table}}, \code{\link{readLines}}, and
53
  \code{\link{parse}} have an \code{encoding} argument that is used to
54
  declare encodings, \code{\link{iconv}} declares encodings from its
65505 ripley 55
  \code{to} argument, and console input in suitable locales is also
46864 ripley 56
  declared.  \code{\link{intToUtf8}} declares its output as
54236 ripley 57
  \code{"UTF-8"}, and output text connections (see
58
  \code{\link{textConnection}}) are marked if running in a
46864 ripley 59
  suitable locale.  Under some circumstances (see its help page)
60
  \code{\link{source}(encoding=)} will mark encodings of character
61
  strings it outputs.
43715 ripley 62
 
43724 ripley 63
  Most character manipulation functions will set the encoding on output
64
  strings if it was declared on the corresponding input.  These include
50269 ripley 65
  \code{\link{chartr}}, \code{\link{strsplit}(useBytes = FALSE)},
50230 ripley 66
  \code{\link{tolower}} and \code{\link{toupper}} as well as
67
  \code{\link{sub}(useBytes = FALSE)} and \code{\link{gsub}(useBytes =
68
  FALSE)}.  Note that such functions do not \emph{preserve} the
69
  encoding, but if they know the input encoding and that the string has
70
  been successfully re-encoded (to the current encoding or UTF-8), they
71
  mark the output.
45847 ripley 72
 
46864 ripley 73
  \code{\link{substr}} does preserve the encoding, and
44054 ripley 74
  \code{\link{chartr}}, \code{\link{tolower}} and \code{\link{toupper}}
46848 ripley 75
  preserve UTF-8 encoding on systems with Unicode wide characters.  With
45847 ripley 76
  their \code{fixed} and \code{perl} options, \code{\link{strsplit}},
46864 ripley 77
  \code{\link{sub}} and \code{gsub} will give a marked UTF-8 result if
82597 kalibera 78
  any of the inputs are UTF-8.
45847 ripley 79
 
82597 kalibera 80
 %% \code{\link{strsplit}}, \code{\link{sub}} 
81
 %%  and \code{gsub} will mark their result \code{"bytes"} if it is non-ASCII
82
 %%  and any of the inputs are marked \code{"bytes"} or \code{useBytes = TRUE}
83
 %% was used.
84
 
56996 hornik 85
  \code{\link{paste}} and \code{\link{sprintf}} return elements marked
61433 ripley 86
  as bytes if any of the corresponding inputs is marked as bytes, and
80116 hornik 87
  otherwise marked as UTF-8 if any of the inputs is marked as UTF-8.
49415 ripley 88
 
89
  \code{\link{match}}, \code{\link{pmatch}}, \code{\link{charmatch}},
90
  \code{\link{duplicated}} and \code{\link{unique}} all match in UTF-8
91
  if any of the elements are marked as UTF-8.
79790 maechler 92
 
81697 kalibera 93
  Changing the current encoding from a running R session may lead to
94
  confusion (see \code{\link{Sys.setlocale}}).
95
 
73791 ripley 96
  There is some ambiguity as to what is meant by a \sQuote{Latin-1}
97
  locale, since some OSes (notably Windows) make use of character
79695 ripley 98
  positions undefined (or used for control characters) in the ISO 8859-1
99
  character set.  How such characters are interpreted is
100
  system-dependent but as from \R 3.5.0 they are if possible interpreted
101
  as per Windows codepage 1252 (which Microsoft calls \sQuote{Windows
102
  Latin 1 (ANSI)}) when converting to e.g.\sspace{}UTF-8.
40678 ripley 103
}
104
\value{
105
  A character vector.
65505 ripley 106
 
107
  For \code{enc2utf8} encodings are always marked: they are for
108
  \code{enc2native} in UTF-8 and Latin-1 locales.
40678 ripley 109
}
110
\examples{
111
## x is intended to be in latin1
84678 ripley 112
x. <- x <- "fran\xE7ais"
80172 hornik 113
Encoding(x.) # "unknown" (UTF-8 loc.) | "latin1" (8859-1/CP-1252 loc.) | ....
40678 ripley 114
Encoding(x) <- "latin1"
115
x
40765 ripley 116
xx <- iconv(x, "latin1", "UTF-8")
79790 maechler 117
Encoding(c(x., x, xx))
40765 ripley 118
c(x, xx)
79790 maechler 119
xb <- xx; Encoding(xb) <- "bytes"
120
xb # will be encoded in hex
121
cat("x = ", x, ", xx = ", xx, ", xb = ", xb, "\n", sep = "")
122
(Ex <- Encoding(c(x.,x,xx,xb)))
123
stopifnot(identical(Ex, c(Encoding(x.), Encoding(x),
124
                          Encoding(xx), Encoding(xb))))
40678 ripley 125
}
126
\keyword{utilities}
127
\keyword{character}