72 lines
21 KiB
HTML
72 lines
21 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
|
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>13.1.1 Encodings and Locales</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="extras.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">►</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Reference</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="model.html" class="tocviewlink" data-pltdoc="x">Language Model</a></td></tr><tr><td align="right">2 </td><td><a href="notation.html" class="tocviewlink" data-pltdoc="x">Notation for Documentation</a></td></tr><tr><td align="right">3 </td><td><a href="syntax.html" class="tocviewlink" data-pltdoc="x">Syntactic Forms</a></td></tr><tr><td align="right">4 </td><td><a href="data.html" class="tocviewlink" data-pltdoc="x">Datatypes</a></td></tr><tr><td align="right">5 </td><td><a href="structures.html" class="tocviewlink" data-pltdoc="x">Structures</a></td></tr><tr><td align="right">6 </td><td><a href="mzlib_class.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">7 </td><td><a href="mzlib_unit.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">8 </td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">9 </td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">10 </td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Control Flow</a></td></tr><tr><td align="right">11 </td><td><a href="concurrency.html" class="tocviewlink" data-pltdoc="x">Concurrency and Parallelism</a></td></tr><tr><td align="right">12 </td><td><a href="Macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">13 </td><td><a href="input-and-output.html" class="tocviewselflink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">14 </td><td><a href="security.html" class="tocviewlink" data-pltdoc="x">Reflection and Security</a></td></tr><tr><td align="right">15 </td><td><a href="os.html" class="tocviewlink" data-pltdoc="x">Operating System</a></td></tr><tr><td align="right">16 </td><td><a href="memory.html" class="tocviewlink" data-pltdoc="x">Memory Management</a></td></tr><tr><td align="right">17 </td><td><a href="unsafe.html" class="tocviewlink" data-pltdoc="x">Unsafe Operations</a></td></tr><tr><td align="right">18 </td><td><a href="running.html"
|
||
|
<span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._read-char%29%29" class="RktValLink" data-pltdoc="x">read-char</a></span> or <span class="RktSym"><a href="Reading.html#%28def._%28%28quote._~23~25kernel%29._read%29%29" class="RktValLink" data-pltdoc="x">read</a></span>, the port’s bytes are read and
|
||
|
interpreted as a UTF-8 encoding of characters. Thus, reading a single
|
||
|
character may require reading multiple bytes, and a procedure like
|
||
|
<span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._char-ready~3f%29%29" class="RktValLink" data-pltdoc="x">char-ready?</a></span> may need to peek several bytes into the stream to
|
||
|
determine whether a character is available. In the case of a byte
|
||
|
stream that does not correspond to a valid UTF-8 encoding, functions
|
||
|
such as <span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._read-char%29%29" class="RktValLink" data-pltdoc="x">read-char</a></span> may need to peek one byte ahead in the
|
||
|
stream to discover that the stream is not a valid encoding.</p><p>When an input port produces a sequence of bytes that is not a valid
|
||
|
UTF-8 encoding in a character-reading context, then bytes that
|
||
|
constitute an invalid sequence are converted to the character
|
||
|
<span class="RktVal">#\uFFFD</span>. Specifically, bytes 255 and 254 are always converted
|
||
|
to <span class="RktVal">#\uFFFD</span>, bytes in the range 192 to 253 produce
|
||
|
<span class="RktVal">#\uFFFD</span> when they are not followed by bytes that form a valid
|
||
|
UTF-8 encoding, and bytes in the range 128 to 191 are converted to
|
||
|
<span class="RktVal">#\uFFFD</span> when they are not part of a valid encoding that was
|
||
|
started by a preceding byte in the range 192 to 253. To put it another
|
||
|
way, when reading a sequence of bytes as characters, a minimal set of
|
||
|
bytes are changed to the encoding of <span class="RktVal">#\uFFFD</span> so that the
|
||
|
entire sequence of bytes is a valid UTF-8 encoding.</p><p>See <a href="bytestrings.html" data-pltdoc="x">Byte Strings</a> for procedures that facilitate
|
||
|
conversions using UTF-8 or other encodings. See also
|
||
|
<span class="RktSym"><a href="port-lib.html#%28def._%28%28lib._racket%2Fport..rkt%29._reencode-input-port%29%29" class="RktValLink" data-pltdoc="x">reencode-input-port</a></span> and <span class="RktSym"><a href="port-lib.html#%28def._%28%28lib._racket%2Fport..rkt%29._reencode-output-port%29%29" class="RktValLink" data-pltdoc="x">reencode-output-port</a></span> for
|
||
|
obtaining a UTF-8-based port from one that uses a different encoding
|
||
|
of characters.</p><p>A <a name="(tech._locale)"></a><span style="font-style: italic">locale</span> captures information about a user’s
|
||
|
language-specific interpretation of character sequences. In particular,
|
||
|
a locale determines how strings are “alphabetized,” how a lowercase
|
||
|
character is converted to an uppercase character, and how strings are
|
||
|
compared without regard to case. String operations such as
|
||
|
<span class="RktSym"><a href="strings.html#%28def._%28%28quote._~23~25kernel%29._string-ci~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string-ci=?</a></span> are <span style="font-style: italic">not</span> sensitive to the current locale,
|
||
|
but operations such as <span class="RktSym"><a href="strings.html#%28def._%28%28quote._~23~25kernel%29._string-locale-ci~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string-locale-ci=?</a></span> (see
|
||
|
<a href="strings.html" data-pltdoc="x">Strings</a>) produce results consistent with the current
|
||
|
locale.</p><p>A locale also designates a particular encoding of code-point sequences
|
||
|
into byte sequences. Racket generally ignores this aspect of the
|
||
|
locale, with a few notable exceptions: command-line arguments passed
|
||
|
to Racket as byte strings are converted to character strings using the
|
||
|
locale’s encoding; command-line strings passed as byte strings to
|
||
|
other processes (through <span class="RktSym"><a href="subprocess.html#%28def._%28%28quote._~23~25kernel%29._subprocess%29%29" class="RktValLink" data-pltdoc="x">subprocess</a></span>) are converted to byte
|
||
|
strings using the locale’s encoding; environment variables are
|
||
|
converted to and from strings using the locale’s encoding; filesystem
|
||
|
paths are converted to and from strings (for display purposes) using
|
||
|
the locale’s encoding; and, finally, Racket provides functions such as
|
||
|
<span class="RktSym"><a href="bytestrings.html#%28def._%28%28quote._~23~25kernel%29._string-~3ebytes%2Flocale%29%29" class="RktValLink" data-pltdoc="x">string->bytes/locale</a></span> to specifically invoke a locale-specific
|
||
|
encoding.</p><p>A Unix user selects a locale by setting environment variables, such as
|
||
|
<span class="stt">LC_ALL</span>. On Windows and Mac OS, the operating system
|
||
|
provides other mechanisms for setting the locale. Within Racket, the
|
||
|
current locale can be changed by setting the <span class="RktSym"><a href="encodings.html#%28def._%28%28quote._~23~25kernel%29._current-locale%29%29" class="RktValLink" data-pltdoc="x">current-locale</a></span>
|
||
|
parameter. The locale name within Racket is a string, and the
|
||
|
available locale names depend on the platform and its configuration,
|
||
|
but the <span class="RktVal">""</span> locale means the current user’s default locale;
|
||
|
on Windows and Mac OS, the encoding for <span class="RktVal">""</span> is always
|
||
|
UTF-8, and locale-sensitive operations use the operating system’s
|
||
|
native interface. (In particular, setting the <span class="stt">LC_ALL</span> and
|
||
|
<span class="stt">LC_CTYPE</span> environment variables does not affect the locale
|
||
|
<span class="RktVal">""</span> on Mac OS. Use <span class="RktSym"><a href="envvars.html#%28def._%28%28lib._racket%2Fprivate%2Fmisc..rkt%29._getenv%29%29" class="RktValLink" data-pltdoc="x">getenv</a></span> and
|
||
|
<span class="RktSym"><a href="encodings.html#%28def._%28%28quote._~23~25kernel%29._current-locale%29%29" class="RktValLink" data-pltdoc="x">current-locale</a></span> to explicitly install the
|
||
|
environment-specified locale, if desired.) Setting the current locale
|
||
|
to <span class="RktVal">#f</span> makes locale-sensitive operations locale-insensitive,
|
||
|
which means using the Unicode mapping for case operations and using
|
||
|
UTF-8 for encoding.</p><p><div class="SIntrapara"><blockquote class="SVInsetFlow"><table cellspacing="0" cellpadding="0" class="boxed RBoxed"><tr><td><blockquote class="SubFlow"><div class="RBackgroundLabel SIEHidden"><div class="RBackgroundLabelInner"><p>parameter</p></div></div><p class="RForeground"><span class="RktPn">(</span><a name="(def._((quote._~23~25kernel)._current-locale))"></a><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="encodings.html#%28def._%28%28quote._~23~25kernel%29._current-locale%29%29" class="RktValDef RktValLink" data-pltdoc="x">current-locale</a></span></span><span class="RktPn"></span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29" class="RktValLink" data-pltdoc="x">or/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="strings.html#%28def._%28%28quote._~23~25kernel%29._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="hspace"> </span><span class="RktVal">#f</span><span class="RktPn">)</span></p></blockquote></td></tr><tr><td><span class="RktPn">(</span><span title="Provided from: racket/base, racket | Package: base"><span class="RktSym"><a href="encodings.html#%28def._%28%28quote._~23~25kernel%29._current-locale%29%29" class="RktValDef RktValLink" data-pltdoc="x">current-locale</a></span></span><span class="hspace"> </span><span class="RktVar">locale</span><span class="RktPn">)</span><span class="hspace"> </span>→<span class="hspace"> </span><span class="RktSym"><a href="void.html#%28def._%28%28quote._~23~25kernel%29._void~3f%29%29" class="RktValLink" data-pltdoc="x">void?</a></span></td></tr><tr><td><span class="hspace"> </span><span class="RktVar">locale</span><span class="hspace"> </span>:<span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym"><a href="data-structure-contracts.html#%28def._%28%28lib._racket%2Fcontract%2Fbase..rkt%29._or%2Fc%29%29" class="RktValLink" data-pltdoc="x">or/c</a></span><span class="hspace"> </span><span class="RktSym"><a href="strings.html#%28def._%28%28quote._~23~25kernel%29._string~3f%29%29" class="RktValLink" data-pltdoc="x">string?</a></span><span class="hspace"> </span><span class="RktVal">#f</span><span class="RktPn">)</span></td></tr></table></blockquote></div><div class="SIntrapara">A <a href="eval-model.html#%28tech._parameter%29" class="techoutside" data-pltdoc="x"><span class="techinside">parameter</span></a> that determines the current <a href="encodings.html#%28tech._locale%29" class="techoutside" data-pltdoc="x"><span class="techinside">locale</span></a> for
|
||
|
procedures such as <span class="RktSym"><a href="strings.html#%28def._%28%28quote._~23~25kernel%29._string-locale-ci~3d~3f%29%29" class="RktValLink" data-pltdoc="x">string-locale-ci=?</a></span>.</div></p><p>When locale sensitivity is disabled by setting the parameter to
|
||
|
<span class="RktVal">#f</span>, strings are compared, etc., in a fully portable manner,
|
||
|
which is the same as the standard procedures. Otherwise, strings are
|
||
|
interpreted according to a locale setting (in the sense of the C
|
||
|
library’s <span class="stt">setlocale</span>). The <span class="RktVal">""</span> locale is always an alias
|
||
|
for the current machine’s default locale, and it is the default. The
|
||
|
<span class="RktVal">"C"</span> locale is also always available; setting the locale to
|
||
|
<span class="RktVal">"C"</span> is the same as disabling locale sensitivity with
|
||
|
<span class="RktVal">#f</span> only when string operations are restricted to the first
|
||
|
128 characters. Other locale names are platform-specific.</p><p>String or character printing with <span class="RktSym"><a href="Writing.html#%28def._%28%28quote._~23~25kernel%29._write%29%29" class="RktValLink" data-pltdoc="x">write</a></span> is not affected by
|
||
|
the parameter, and neither are symbol case or regular expressions (see
|
||
|
<a href="regexp.html" data-pltdoc="x">Regular Expressions</a>).</p><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, "8.6", "../");"/></form> <a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot("8.6");">top</a><span class="tocsettoggle"> <a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright"> <a href="ports.html" title="backward to "13.1 Ports"" data-pltdoc="x">← prev</a> <a href="ports.html" title="up to "13.1 Ports"" data-pltdoc="x">up</a> <a href="port-ops.html" title="forward to "13.1.2 Managing Ports"" data-pltdoc="x">next →</a></span> </div></div></div><div id="contextindicator"> </div></body></html>
|