The R Project SVN R

Rev

Rev 59039 | Rev 68833 | Go to most recent revision | 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
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2011 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{
46864 ripley 29
  Character strings in \R can be declared to be in \code{"latin1"} or
54744 ripley 30
  \code{"UTF-8"} or \code{"bytes"}.  These declarations can be read by
31
  \code{Encoding}, which will return a character vector of values
32
  \code{"latin1"}, \code{"UTF-8"} \code{"bytes"} or \code{"unknown"}, or
33
  set, when \code{value} is recycled as needed and other values are
34
  silently treated as \code{"unknown"}.  ASCII strings will never be
35
  marked with a declared encoding, since their representation is the
36
  same in all supported encodings.  Strings marked as \code{"bytes"} are
37
  intended to be non-ASCII strings which should be manipulated as bytes,
38
  and never converted to a character encoding.
50762 ripley 39
 
40
  \code{enc2native} and \code{enc2utf8} convert elements of character
50903 hornik 41
  vectors to the native encoding or UTF-8 respectively, taking any
51317 ripley 42
  marked encoding into account.  They are \link{primitive} functions,
43
  designed to do minimal copying.
61433 ripley 44
 
40696 ripley 45
  There are other ways for character strings to acquire a declared
46864 ripley 46
  encoding apart from explicitly setting it (and these have changed as
47
  \R has evolved).  Functions \code{\link{scan}},
48
  \code{\link{read.table}}, \code{\link{readLines}}, and
49
  \code{\link{parse}} have an \code{encoding} argument that is used to
50
  declare encodings, \code{\link{iconv}} declares encodings from its
51
  \code{from} argument, and console input in suitable locales is also
52
  declared.  \code{\link{intToUtf8}} declares its output as
54236 ripley 53
  \code{"UTF-8"}, and output text connections (see
54
  \code{\link{textConnection}}) are marked if running in a
46864 ripley 55
  suitable locale.  Under some circumstances (see its help page)
56
  \code{\link{source}(encoding=)} will mark encodings of character
57
  strings it outputs.
43715 ripley 58
 
43724 ripley 59
  Most character manipulation functions will set the encoding on output
60
  strings if it was declared on the corresponding input.  These include
50269 ripley 61
  \code{\link{chartr}}, \code{\link{strsplit}(useBytes = FALSE)},
50230 ripley 62
  \code{\link{tolower}} and \code{\link{toupper}} as well as
63
  \code{\link{sub}(useBytes = FALSE)} and \code{\link{gsub}(useBytes =
64
  FALSE)}.  Note that such functions do not \emph{preserve} the
65
  encoding, but if they know the input encoding and that the string has
66
  been successfully re-encoded (to the current encoding or UTF-8), they
67
  mark the output.
45847 ripley 68
 
46864 ripley 69
  \code{\link{substr}} does preserve the encoding, and
44054 ripley 70
  \code{\link{chartr}}, \code{\link{tolower}} and \code{\link{toupper}}
46848 ripley 71
  preserve UTF-8 encoding on systems with Unicode wide characters.  With
45847 ripley 72
  their \code{fixed} and \code{perl} options, \code{\link{strsplit}},
46864 ripley 73
  \code{\link{sub}} and \code{gsub} will give a marked UTF-8 result if
74
  any of the inputs are UTF-8.
45847 ripley 75
 
56996 hornik 76
  \code{\link{paste}} and \code{\link{sprintf}} return elements marked
61433 ripley 77
  as bytes if any of the corresponding inputs is marked as bytes, and
56996 hornik 78
  otherwise marked as UTF-8 of any of the inputs is marked as UTF-8.
49415 ripley 79
 
80
  \code{\link{match}}, \code{\link{pmatch}}, \code{\link{charmatch}},
81
  \code{\link{duplicated}} and \code{\link{unique}} all match in UTF-8
82
  if any of the elements are marked as UTF-8.
40678 ripley 83
}
84
\value{
85
  A character vector.
86
}
87
\examples{
88
## x is intended to be in latin1
89
x <- "fa\xE7ile"
90
Encoding(x)
91
Encoding(x) <- "latin1"
92
x
40765 ripley 93
xx <- iconv(x, "latin1", "UTF-8")
94
Encoding(c(x, xx))
95
c(x, xx)
54747 ripley 96
Encoding(xx) <- "bytes"
97
xx # will be encoded in hex
98
cat("xx = ", xx, "\n", sep = "")
40678 ripley 99
}
100
\keyword{utilities}
101
\keyword{character}