You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

637 lines
19 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>encoding - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package encoding</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "golang.org/x/text/encoding"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. <dd><a href="index.html#pkg-subdirectories">Subdirectories</a></dd>
  59. </dl>
  60. </div>
  61. <!-- The package's Name is printed as title by the top-level template -->
  62. <div id="pkg-overview" class="toggleVisible">
  63. <div class="collapsed">
  64. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  65. </div>
  66. <div class="expanded">
  67. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  68. <p>
  69. Package encoding defines an interface for character encodings, such as Shift
  70. JIS and Windows 1252, that can convert to and from UTF-8.
  71. </p>
  72. <p>
  73. Encoding implementations are provided in other packages, such as
  74. golang.org/x/text/encoding/charmap and
  75. golang.org/x/text/encoding/japanese.
  76. </p>
  77. </div>
  78. </div>
  79. <div id="pkg-index" class="toggleVisible">
  80. <div class="collapsed">
  81. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  82. </div>
  83. <div class="expanded">
  84. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  85. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  86. <div id="manual-nav">
  87. <dl>
  88. <dd><a href="index.html#pkg-constants">Constants</a></dd>
  89. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  90. <dd><a href="index.html#Decoder">type Decoder</a></dd>
  91. <dd>&nbsp; &nbsp; <a href="index.html#Decoder.Bytes">func (d *Decoder) Bytes(b []byte) ([]byte, error)</a></dd>
  92. <dd>&nbsp; &nbsp; <a href="index.html#Decoder.Reader">func (d *Decoder) Reader(r io.Reader) io.Reader</a></dd>
  93. <dd>&nbsp; &nbsp; <a href="index.html#Decoder.String">func (d *Decoder) String(s string) (string, error)</a></dd>
  94. <dd><a href="index.html#Encoder">type Encoder</a></dd>
  95. <dd>&nbsp; &nbsp; <a href="index.html#HTMLEscapeUnsupported">func HTMLEscapeUnsupported(e *Encoder) *Encoder</a></dd>
  96. <dd>&nbsp; &nbsp; <a href="index.html#ReplaceUnsupported">func ReplaceUnsupported(e *Encoder) *Encoder</a></dd>
  97. <dd>&nbsp; &nbsp; <a href="index.html#Encoder.Bytes">func (e *Encoder) Bytes(b []byte) ([]byte, error)</a></dd>
  98. <dd>&nbsp; &nbsp; <a href="index.html#Encoder.String">func (e *Encoder) String(s string) (string, error)</a></dd>
  99. <dd>&nbsp; &nbsp; <a href="index.html#Encoder.Writer">func (e *Encoder) Writer(w io.Writer) io.Writer</a></dd>
  100. <dd><a href="index.html#Encoding">type Encoding</a></dd>
  101. </dl>
  102. </div><!-- #manual-nav -->
  103. <div id="pkg-examples">
  104. <h4>Examples</h4>
  105. <dl>
  106. <dd><a class="exampleLink" href="index.html#example_UTF8Validator">UTF8Validator</a></dd>
  107. </dl>
  108. </div>
  109. <h4>Package files</h4>
  110. <p>
  111. <span style="font-size:90%">
  112. <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go">encoding.go</a>
  113. </span>
  114. </p>
  115. </div><!-- .expanded -->
  116. </div><!-- #pkg-index -->
  117. <div id="pkg-callgraph" class="toggle" style="display: none">
  118. <div class="collapsed">
  119. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  120. </div> <!-- .expanded -->
  121. <div class="expanded">
  122. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  123. <p>
  124. In the call graph viewer below, each node
  125. is a function belonging to this package
  126. and its children are the functions it
  127. calls&mdash;perhaps dynamically.
  128. </p>
  129. <p>
  130. The root nodes are the entry points of the
  131. package: functions that may be called from
  132. outside the package.
  133. There may be non-exported or anonymous
  134. functions among them if they are called
  135. dynamically from another package.
  136. </p>
  137. <p>
  138. Click a node to visit that function's source code.
  139. From there you can visit its callers by
  140. clicking its declaring <code>func</code>
  141. token.
  142. </p>
  143. <p>
  144. Functions may be omitted if they were
  145. determined to be unreachable in the
  146. particular programs or tests that were
  147. analyzed.
  148. </p>
  149. <!-- Zero means show all package entry points. -->
  150. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  151. </div>
  152. </div> <!-- #pkg-callgraph -->
  153. <h2 id="pkg-constants">Constants</h2>
  154. <pre>const <span id="ASCIISub">ASCIISub</span> = &#39;\x1a&#39;</pre>
  155. <p>
  156. ASCIISub is the ASCII substitute character, as recommended by
  157. <a href="http://unicode.org/reports/tr36/#Text_Comparison">http://unicode.org/reports/tr36/#Text_Comparison</a>
  158. </p>
  159. <h2 id="pkg-variables">Variables</h2>
  160. <pre>var <span id="ErrInvalidUTF8">ErrInvalidUTF8</span> = <a href="../../../../errors/index.html">errors</a>.<a href="../../../../errors/index.html#New">New</a>(&#34;encoding: invalid UTF-8&#34;)</pre>
  161. <p>
  162. ErrInvalidUTF8 means that a transformer encountered invalid UTF-8.
  163. </p>
  164. <pre>var <span id="UTF8Validator">UTF8Validator</span> <a href="../transform/index.html">transform</a>.<a href="../transform/index.html#Transformer">Transformer</a> = utf8Validator{}</pre>
  165. <p>
  166. UTF8Validator is a transformer that returns ErrInvalidUTF8 on the first
  167. input byte that is not valid UTF-8.
  168. </p>
  169. <h2 id="Decoder">type <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=1542:1756#L35">Decoder</a></h2>
  170. <pre>type Decoder struct {
  171. <a href="../transform/index.html">transform</a>.<a href="../transform/index.html#Transformer">Transformer</a>
  172. <span class="comment">// contains filtered or unexported fields</span>
  173. }</pre>
  174. <p>
  175. A Decoder converts bytes to UTF-8. It implements transform.Transformer.
  176. </p>
  177. <p>
  178. Transforming source bytes that are not of that encoding will not result in an
  179. error per se. Each byte that cannot be transcoded will be represented in the
  180. output by the UTF-8 encoding of &#39;\uFFFD&#39;, the replacement rune.
  181. </p>
  182. <h3 id="Decoder.Bytes">func (*Decoder) <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=1879:1928#L46">Bytes</a></h3>
  183. <pre>func (d *<a href="index.html#Decoder">Decoder</a>) Bytes(b []<a href="../../../../builtin/index.html#byte">byte</a>) ([]<a href="../../../../builtin/index.html#byte">byte</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  184. <p>
  185. Bytes converts the given encoded bytes to UTF-8. It returns the converted
  186. bytes or nil, err if any error occurred.
  187. </p>
  188. <h3 id="Decoder.Reader">func (*Decoder) <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=2446:2493#L68">Reader</a></h3>
  189. <pre>func (d *<a href="index.html#Decoder">Decoder</a>) Reader(r <a href="../../../../io/index.html">io</a>.<a href="../../../../io/index.html#Reader">Reader</a>) <a href="../../../../io/index.html">io</a>.<a href="../../../../io/index.html#Reader">Reader</a></pre>
  190. <p>
  191. Reader wraps another Reader to decode its bytes.
  192. </p>
  193. <p>
  194. The Decoder may not be used for any other operation as long as the returned
  195. Reader is in use.
  196. </p>
  197. <h3 id="Decoder.String">func (*Decoder) <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=2146:2196#L56">String</a></h3>
  198. <pre>func (d *<a href="index.html#Decoder">Decoder</a>) String(s <a href="../../../../builtin/index.html#string">string</a>) (<a href="../../../../builtin/index.html#string">string</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  199. <p>
  200. String converts the given encoded string to UTF-8. It returns the converted
  201. string or &#34;&#34;, err if any error occurred.
  202. </p>
  203. <h2 id="Encoder">type <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=2973:3187#L79">Encoder</a></h2>
  204. <pre>type Encoder struct {
  205. <a href="../transform/index.html">transform</a>.<a href="../transform/index.html#Transformer">Transformer</a>
  206. <span class="comment">// contains filtered or unexported fields</span>
  207. }</pre>
  208. <p>
  209. An Encoder converts bytes from UTF-8. It implements transform.Transformer.
  210. </p>
  211. <p>
  212. Each rune that cannot be transcoded will result in an error. In this case,
  213. the transform will consume all source byte up to, not including the offending
  214. rune. Transforming source bytes that are not valid UTF-8 will be replaced by
  215. `\uFFFD`. To return early with an error instead, use transform.Chain to
  216. preprocess the data with a UTF8Validator.
  217. </p>
  218. <h3 id="HTMLEscapeUnsupported">func <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=6734:6781#L214">HTMLEscapeUnsupported</a></h3>
  219. <pre>func HTMLEscapeUnsupported(e *<a href="index.html#Encoder">Encoder</a>) *<a href="index.html#Encoder">Encoder</a></pre>
  220. <p>
  221. HTMLEscapeUnsupported wraps encoders to replace source runes outside the
  222. repertoire of the destination encoding with HTML escape sequences.
  223. </p>
  224. <p>
  225. This wrapper exists to comply to URL and HTML forms requiring a
  226. non-terminating legacy encoder. The produced sequences may lead to data
  227. loss as they are indistinguishable from legitimate input. To avoid this
  228. issue, use UTF-8 encodings whenever possible.
  229. </p>
  230. <h3 id="ReplaceUnsupported">func <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=7155:7199#L224">ReplaceUnsupported</a></h3>
  231. <pre>func ReplaceUnsupported(e *<a href="index.html#Encoder">Encoder</a>) *<a href="index.html#Encoder">Encoder</a></pre>
  232. <p>
  233. ReplaceUnsupported wraps encoders to replace source runes outside the
  234. repertoire of the destination encoding with an encoding-specific
  235. replacement.
  236. </p>
  237. <p>
  238. This wrapper is only provided for backwards compatibility and legacy
  239. handling. Its use is strongly discouraged. Use UTF-8 whenever possible.
  240. </p>
  241. <h3 id="Encoder.Bytes">func (*Encoder) <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=3294:3343#L90">Bytes</a></h3>
  242. <pre>func (e *<a href="index.html#Encoder">Encoder</a>) Bytes(b []<a href="../../../../builtin/index.html#byte">byte</a>) ([]<a href="../../../../builtin/index.html#byte">byte</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  243. <p>
  244. Bytes converts bytes from UTF-8. It returns the converted bytes or nil, err if
  245. any error occurred.
  246. </p>
  247. <h3 id="Encoder.String">func (*Encoder) <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=3547:3597#L100">String</a></h3>
  248. <pre>func (e *<a href="index.html#Encoder">Encoder</a>) String(s <a href="../../../../builtin/index.html#string">string</a>) (<a href="../../../../builtin/index.html#string">string</a>, <a href="../../../../builtin/index.html#error">error</a>)</pre>
  249. <p>
  250. String converts a string from UTF-8. It returns the converted string or
  251. &#34;&#34;, err if any error occurred.
  252. </p>
  253. <h3 id="Encoder.Writer">func (*Encoder) <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=3854:3901#L112">Writer</a></h3>
  254. <pre>func (e *<a href="index.html#Encoder">Encoder</a>) Writer(w <a href="../../../../io/index.html">io</a>.<a href="../../../../io/index.html#Writer">Writer</a>) <a href="../../../../io/index.html">io</a>.<a href="../../../../io/index.html#Writer">Writer</a></pre>
  255. <p>
  256. Writer wraps another Writer to encode its UTF-8 output.
  257. </p>
  258. <p>
  259. The Encoder may not be used for any other operation as long as the returned
  260. Writer is in use.
  261. </p>
  262. <h2 id="Encoding">type <a href="http://localhost:6060/src/golang.org/x/text/encoding/encoding.go?s=1091:1234#L22">Encoding</a></h2>
  263. <pre>type Encoding interface {
  264. <span class="comment">// NewDecoder returns a Decoder.</span>
  265. NewDecoder() *<a href="index.html#Decoder">Decoder</a>
  266. <span class="comment">// NewEncoder returns an Encoder.</span>
  267. NewEncoder() *<a href="index.html#Encoder">Encoder</a>
  268. }</pre>
  269. <p>
  270. Encoding is a character set encoding that can be transformed to and from
  271. UTF-8.
  272. </p>
  273. <pre>var <span id="Nop">Nop</span> <a href="index.html#Encoding">Encoding</a> = nop{}</pre>
  274. <p>
  275. Nop is the nop encoding. Its transformed bytes are the same as the source
  276. bytes; it does not replace invalid UTF-8 sequences.
  277. </p>
  278. <pre>var <span id="Replacement">Replacement</span> <a href="index.html#Encoding">Encoding</a> = replacement{}</pre>
  279. <p>
  280. Replacement is the replacement encoding. Decoding from the replacement
  281. encoding yields a single &#39;\uFFFD&#39; replacement rune. Encoding from UTF-8 to
  282. the replacement encoding yields the same as the source bytes except that
  283. invalid UTF-8 is converted to &#39;\uFFFD&#39;.
  284. </p>
  285. <p>
  286. It is defined at <a href="http://encoding.spec.whatwg.org/#replacement">http://encoding.spec.whatwg.org/#replacement</a>
  287. </p>
  288. <h2 id="pkg-subdirectories">Subdirectories</h2>
  289. <div class="pkg-dir">
  290. <table>
  291. <tr>
  292. <th class="pkg-name">Name</th>
  293. <th class="pkg-synopsis">Synopsis</th>
  294. </tr>
  295. <tr>
  296. <td colspan="2"><a href="../index.html">..</a></td>
  297. </tr>
  298. <tr>
  299. <td class="pkg-name" style="padding-left: 0px;">
  300. <a href="charmap/index.html">charmap</a>
  301. </td>
  302. <td class="pkg-synopsis">
  303. Package charmap provides simple character encodings such as IBM Code Page 437 and Windows 1252.
  304. </td>
  305. </tr>
  306. <tr>
  307. <td class="pkg-name" style="padding-left: 0px;">
  308. <a href="htmlindex/index.html">htmlindex</a>
  309. </td>
  310. <td class="pkg-synopsis">
  311. Package htmlindex maps character set encoding names to Encodings as recommended by the W3C for use in HTML 5.
  312. </td>
  313. </tr>
  314. <tr>
  315. <td class="pkg-name" style="padding-left: 0px;">
  316. <a href="ianaindex/index.html">ianaindex</a>
  317. </td>
  318. <td class="pkg-synopsis">
  319. Package ianaindex maps names to Encodings as specified by the IANA registry.
  320. </td>
  321. </tr>
  322. <tr>
  323. <td class="pkg-name" style="padding-left: 0px;">
  324. <a href="japanese/index.html">japanese</a>
  325. </td>
  326. <td class="pkg-synopsis">
  327. Package japanese provides Japanese encodings such as EUC-JP and Shift JIS.
  328. </td>
  329. </tr>
  330. <tr>
  331. <td class="pkg-name" style="padding-left: 0px;">
  332. <a href="korean/index.html">korean</a>
  333. </td>
  334. <td class="pkg-synopsis">
  335. Package korean provides Korean encodings such as EUC-KR.
  336. </td>
  337. </tr>
  338. <tr>
  339. <td class="pkg-name" style="padding-left: 0px;">
  340. <a href="simplifiedchinese/index.html">simplifiedchinese</a>
  341. </td>
  342. <td class="pkg-synopsis">
  343. Package simplifiedchinese provides Simplified Chinese encodings such as GBK.
  344. </td>
  345. </tr>
  346. <tr>
  347. <td class="pkg-name" style="padding-left: 0px;">
  348. <a href="traditionalchinese/index.html">traditionalchinese</a>
  349. </td>
  350. <td class="pkg-synopsis">
  351. Package traditionalchinese provides Traditional Chinese encodings such as Big5.
  352. </td>
  353. </tr>
  354. <tr>
  355. <td class="pkg-name" style="padding-left: 0px;">
  356. <a href="unicode/index.html">unicode</a>
  357. </td>
  358. <td class="pkg-synopsis">
  359. Package unicode provides Unicode encodings such as UTF-16.
  360. </td>
  361. </tr>
  362. <tr>
  363. <td class="pkg-name" style="padding-left: 20px;">
  364. <a href="unicode/utf32/index.html">utf32</a>
  365. </td>
  366. <td class="pkg-synopsis">
  367. Package utf32 provides the UTF-32 Unicode encoding.
  368. </td>
  369. </tr>
  370. </table>
  371. </div>
  372. <div id="footer">
  373. Build version go1.6.<br>
  374. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  375. the content of this page is licensed under the
  376. Creative Commons Attribution 3.0 License,
  377. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  378. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  379. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  380. </div>
  381. </div><!-- .container -->
  382. </div><!-- #page -->
  383. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  384. <script type="text/javascript" src="../../../../../lib/godoc/jquery.js"></script>
  385. <script type="text/javascript" src="../../../../../lib/godoc/jquery.treeview.js"></script>
  386. <script type="text/javascript" src="../../../../../lib/godoc/jquery.treeview.edit.js"></script>
  387. <script type="text/javascript" src="../../../../../lib/godoc/godocs.js"></script>
  388. </body>
  389. </html>