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.

889 lines
24 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>utf8 - 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 utf8</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 "unicode/utf8"</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. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package utf8 implements functions and constants to support text encoded in
  69. UTF-8. It includes functions to translate between runes and UTF-8 byte sequences.
  70. </p>
  71. </div>
  72. </div>
  73. <div id="pkg-index" class="toggleVisible">
  74. <div class="collapsed">
  75. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  76. </div>
  77. <div class="expanded">
  78. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  79. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  80. <div id="manual-nav">
  81. <dl>
  82. <dd><a href="index.html#pkg-constants">Constants</a></dd>
  83. <dd><a href="index.html#DecodeLastRune">func DecodeLastRune(p []byte) (r rune, size int)</a></dd>
  84. <dd><a href="index.html#DecodeLastRuneInString">func DecodeLastRuneInString(s string) (r rune, size int)</a></dd>
  85. <dd><a href="index.html#DecodeRune">func DecodeRune(p []byte) (r rune, size int)</a></dd>
  86. <dd><a href="index.html#DecodeRuneInString">func DecodeRuneInString(s string) (r rune, size int)</a></dd>
  87. <dd><a href="index.html#EncodeRune">func EncodeRune(p []byte, r rune) int</a></dd>
  88. <dd><a href="index.html#FullRune">func FullRune(p []byte) bool</a></dd>
  89. <dd><a href="index.html#FullRuneInString">func FullRuneInString(s string) bool</a></dd>
  90. <dd><a href="index.html#RuneCount">func RuneCount(p []byte) int</a></dd>
  91. <dd><a href="index.html#RuneCountInString">func RuneCountInString(s string) (n int)</a></dd>
  92. <dd><a href="index.html#RuneLen">func RuneLen(r rune) int</a></dd>
  93. <dd><a href="index.html#RuneStart">func RuneStart(b byte) bool</a></dd>
  94. <dd><a href="index.html#Valid">func Valid(p []byte) bool</a></dd>
  95. <dd><a href="index.html#ValidRune">func ValidRune(r rune) bool</a></dd>
  96. <dd><a href="index.html#ValidString">func ValidString(s string) bool</a></dd>
  97. </dl>
  98. </div><!-- #manual-nav -->
  99. <div id="pkg-examples">
  100. <h4>Examples</h4>
  101. <dl>
  102. <dd><a class="exampleLink" href="index.html#example_DecodeLastRune">DecodeLastRune</a></dd>
  103. <dd><a class="exampleLink" href="index.html#example_DecodeLastRuneInString">DecodeLastRuneInString</a></dd>
  104. <dd><a class="exampleLink" href="index.html#example_DecodeRune">DecodeRune</a></dd>
  105. <dd><a class="exampleLink" href="index.html#example_DecodeRuneInString">DecodeRuneInString</a></dd>
  106. <dd><a class="exampleLink" href="index.html#example_EncodeRune">EncodeRune</a></dd>
  107. <dd><a class="exampleLink" href="index.html#example_FullRune">FullRune</a></dd>
  108. <dd><a class="exampleLink" href="index.html#example_FullRuneInString">FullRuneInString</a></dd>
  109. <dd><a class="exampleLink" href="index.html#example_RuneCount">RuneCount</a></dd>
  110. <dd><a class="exampleLink" href="index.html#example_RuneCountInString">RuneCountInString</a></dd>
  111. <dd><a class="exampleLink" href="index.html#example_RuneLen">RuneLen</a></dd>
  112. <dd><a class="exampleLink" href="index.html#example_RuneStart">RuneStart</a></dd>
  113. <dd><a class="exampleLink" href="index.html#example_Valid">Valid</a></dd>
  114. <dd><a class="exampleLink" href="index.html#example_ValidRune">ValidRune</a></dd>
  115. <dd><a class="exampleLink" href="index.html#example_ValidString">ValidString</a></dd>
  116. </dl>
  117. </div>
  118. <h4>Package files</h4>
  119. <p>
  120. <span style="font-size:90%">
  121. <a href="http://localhost:6060/src/unicode/utf8/utf8.go">utf8.go</a>
  122. </span>
  123. </p>
  124. </div><!-- .expanded -->
  125. </div><!-- #pkg-index -->
  126. <div id="pkg-callgraph" class="toggle" style="display: none">
  127. <div class="collapsed">
  128. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  129. </div> <!-- .expanded -->
  130. <div class="expanded">
  131. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  132. <p>
  133. In the call graph viewer below, each node
  134. is a function belonging to this package
  135. and its children are the functions it
  136. calls&mdash;perhaps dynamically.
  137. </p>
  138. <p>
  139. The root nodes are the entry points of the
  140. package: functions that may be called from
  141. outside the package.
  142. There may be non-exported or anonymous
  143. functions among them if they are called
  144. dynamically from another package.
  145. </p>
  146. <p>
  147. Click a node to visit that function's source code.
  148. From there you can visit its callers by
  149. clicking its declaring <code>func</code>
  150. token.
  151. </p>
  152. <p>
  153. Functions may be omitted if they were
  154. determined to be unreachable in the
  155. particular programs or tests that were
  156. analyzed.
  157. </p>
  158. <!-- Zero means show all package entry points. -->
  159. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  160. </div>
  161. </div> <!-- #pkg-callgraph -->
  162. <h2 id="pkg-constants">Constants</h2>
  163. <pre>const (
  164. <span id="RuneError">RuneError</span> = &#39;\uFFFD&#39; <span class="comment">// the &#34;error&#34; Rune or &#34;Unicode replacement character&#34;</span>
  165. <span id="RuneSelf">RuneSelf</span> = 0x80 <span class="comment">// characters below Runeself are represented as themselves in a single byte.</span>
  166. <span id="MaxRune">MaxRune</span> = &#39;\U0010FFFF&#39; <span class="comment">// Maximum valid Unicode code point.</span>
  167. <span id="UTFMax">UTFMax</span> = 4 <span class="comment">// maximum number of bytes of a UTF-8 encoded Unicode character.</span>
  168. )</pre>
  169. <p>
  170. Numbers fundamental to the encoding.
  171. </p>
  172. <h2 id="DecodeLastRune">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=8153:8201#L239">DecodeLastRune</a></h2>
  173. <pre>func DecodeLastRune(p []<a href="../../builtin/index.html#byte">byte</a>) (r <a href="../../builtin/index.html#rune">rune</a>, size <a href="../../builtin/index.html#int">int</a>)</pre>
  174. <p>
  175. DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and
  176. its width in bytes. If p is empty it returns (RuneError, 0). Otherwise, if
  177. the encoding is invalid, it returns (RuneError, 1). Both are impossible
  178. results for correct, non-empty UTF-8.
  179. </p>
  180. <p>
  181. An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
  182. out of range, or is not the shortest possible UTF-8 encoding for the
  183. value. No other validation is performed.
  184. </p>
  185. <div id="example_DecodeLastRune" class="toggle">
  186. <div class="collapsed">
  187. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  188. </div>
  189. <div class="expanded">
  190. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  191. <p>Code:</p>
  192. <pre class="code">b := []byte(&#34;Hello, 世界&#34;)
  193. for len(b) &gt; 0 {
  194. r, size := utf8.DecodeLastRune(b)
  195. fmt.Printf(&#34;%c %v\n&#34;, r, size)
  196. b = b[:len(b)-size]
  197. }
  198. <span class="comment"></pre>
  199. <p>Output:</p>
  200. <pre class="output">界 3
  201. 世 3
  202. 1
  203. , 1
  204. o 1
  205. l 1
  206. l 1
  207. e 1
  208. H 1
  209. </pre>
  210. </div>
  211. </div>
  212. <h2 id="DecodeLastRuneInString">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=9163:9219#L279">DecodeLastRuneInString</a></h2>
  213. <pre>func DecodeLastRuneInString(s <a href="../../builtin/index.html#string">string</a>) (r <a href="../../builtin/index.html#rune">rune</a>, size <a href="../../builtin/index.html#int">int</a>)</pre>
  214. <p>
  215. DecodeLastRuneInString is like DecodeLastRune but its input is a string. If
  216. s is empty it returns (RuneError, 0). Otherwise, if the encoding is invalid,
  217. it returns (RuneError, 1). Both are impossible results for correct,
  218. non-empty UTF-8.
  219. </p>
  220. <p>
  221. An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
  222. out of range, or is not the shortest possible UTF-8 encoding for the
  223. value. No other validation is performed.
  224. </p>
  225. <div id="example_DecodeLastRuneInString" class="toggle">
  226. <div class="collapsed">
  227. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  228. </div>
  229. <div class="expanded">
  230. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  231. <p>Code:</p>
  232. <pre class="code">str := &#34;Hello, 世界&#34;
  233. for len(str) &gt; 0 {
  234. r, size := utf8.DecodeLastRuneInString(str)
  235. fmt.Printf(&#34;%c %v\n&#34;, r, size)
  236. str = str[:len(str)-size]
  237. }
  238. <span class="comment"></pre>
  239. <p>Output:</p>
  240. <pre class="output">界 3
  241. 世 3
  242. 1
  243. , 1
  244. o 1
  245. l 1
  246. l 1
  247. e 1
  248. H 1
  249. </pre>
  250. </div>
  251. </div>
  252. <h2 id="DecodeRune">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=5307:5351#L143">DecodeRune</a></h2>
  253. <pre>func DecodeRune(p []<a href="../../builtin/index.html#byte">byte</a>) (r <a href="../../builtin/index.html#rune">rune</a>, size <a href="../../builtin/index.html#int">int</a>)</pre>
  254. <p>
  255. DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and
  256. its width in bytes. If p is empty it returns (RuneError, 0). Otherwise, if
  257. the encoding is invalid, it returns (RuneError, 1). Both are impossible
  258. results for correct, non-empty UTF-8.
  259. </p>
  260. <p>
  261. An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
  262. out of range, or is not the shortest possible UTF-8 encoding for the
  263. value. No other validation is performed.
  264. </p>
  265. <div id="example_DecodeRune" class="toggle">
  266. <div class="collapsed">
  267. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  268. </div>
  269. <div class="expanded">
  270. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  271. <p>Code:</p>
  272. <pre class="code">b := []byte(&#34;Hello, 世界&#34;)
  273. for len(b) &gt; 0 {
  274. r, size := utf8.DecodeRune(b)
  275. fmt.Printf(&#34;%c %v\n&#34;, r, size)
  276. b = b[size:]
  277. }
  278. <span class="comment"></pre>
  279. <p>Output:</p>
  280. <pre class="output">H 1
  281. e 1
  282. l 1
  283. l 1
  284. o 1
  285. , 1
  286. 1
  287. 世 3
  288. 界 3
  289. </pre>
  290. </div>
  291. </div>
  292. <h2 id="DecodeRuneInString">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=6710:6762#L191">DecodeRuneInString</a></h2>
  293. <pre>func DecodeRuneInString(s <a href="../../builtin/index.html#string">string</a>) (r <a href="../../builtin/index.html#rune">rune</a>, size <a href="../../builtin/index.html#int">int</a>)</pre>
  294. <p>
  295. DecodeRuneInString is like DecodeRune but its input is a string. If s is
  296. empty it returns (RuneError, 0). Otherwise, if the encoding is invalid, it
  297. returns (RuneError, 1). Both are impossible results for correct, non-empty
  298. UTF-8.
  299. </p>
  300. <p>
  301. An encoding is invalid if it is incorrect UTF-8, encodes a rune that is
  302. out of range, or is not the shortest possible UTF-8 encoding for the
  303. value. No other validation is performed.
  304. </p>
  305. <div id="example_DecodeRuneInString" class="toggle">
  306. <div class="collapsed">
  307. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  308. </div>
  309. <div class="expanded">
  310. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  311. <p>Code:</p>
  312. <pre class="code">str := &#34;Hello, 世界&#34;
  313. for len(str) &gt; 0 {
  314. r, size := utf8.DecodeRuneInString(str)
  315. fmt.Printf(&#34;%c %v\n&#34;, r, size)
  316. str = str[size:]
  317. }
  318. <span class="comment"></pre>
  319. <p>Output:</p>
  320. <pre class="output">H 1
  321. e 1
  322. l 1
  323. l 1
  324. o 1
  325. , 1
  326. 1
  327. 世 3
  328. 界 3
  329. </pre>
  330. </div>
  331. </div>
  332. <h2 id="EncodeRune">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=10279:10316#L333">EncodeRune</a></h2>
  333. <pre>func EncodeRune(p []<a href="../../builtin/index.html#byte">byte</a>, r <a href="../../builtin/index.html#rune">rune</a>) <a href="../../builtin/index.html#int">int</a></pre>
  334. <p>
  335. EncodeRune writes into p (which must be large enough) the UTF-8 encoding of the rune.
  336. It returns the number of bytes written.
  337. </p>
  338. <div id="example_EncodeRune" class="toggle">
  339. <div class="collapsed">
  340. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  341. </div>
  342. <div class="expanded">
  343. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  344. <p>Code:</p>
  345. <pre class="code">r := &#39;&#39;
  346. buf := make([]byte, 3)
  347. n := utf8.EncodeRune(buf, r)
  348. fmt.Println(buf)
  349. fmt.Println(n)
  350. <span class="comment"></pre>
  351. <p>Output:</p>
  352. <pre class="output">[228 184 150]
  353. 3
  354. </pre>
  355. </div>
  356. </div>
  357. <h2 id="FullRune">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=3999:4027#L92">FullRune</a></h2>
  358. <pre>func FullRune(p []<a href="../../builtin/index.html#byte">byte</a>) <a href="../../builtin/index.html#bool">bool</a></pre>
  359. <p>
  360. FullRune reports whether the bytes in p begin with a full UTF-8 encoding of a rune.
  361. An invalid encoding is considered a full Rune since it will convert as a width-1 error rune.
  362. </p>
  363. <div id="example_FullRune" class="toggle">
  364. <div class="collapsed">
  365. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  366. </div>
  367. <div class="expanded">
  368. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  369. <p>Code:</p>
  370. <pre class="code">buf := []byte{228, 184, 150} <span class="comment">// 世</span>
  371. fmt.Println(utf8.FullRune(buf))
  372. fmt.Println(utf8.FullRune(buf[:2]))
  373. <span class="comment"></pre>
  374. <p>Output:</p>
  375. <pre class="output">true
  376. false
  377. </pre>
  378. </div>
  379. </div>
  380. <h2 id="FullRuneInString">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=4448:4484#L114">FullRuneInString</a></h2>
  381. <pre>func FullRuneInString(s <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#bool">bool</a></pre>
  382. <p>
  383. FullRuneInString is like FullRune but its input is a string.
  384. </p>
  385. <div id="example_FullRuneInString" class="toggle">
  386. <div class="collapsed">
  387. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  388. </div>
  389. <div class="expanded">
  390. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  391. <p>Code:</p>
  392. <pre class="code">str := &#34;&#34;
  393. fmt.Println(utf8.FullRuneInString(str))
  394. fmt.Println(utf8.FullRuneInString(str[:2]))
  395. <span class="comment"></pre>
  396. <p>Output:</p>
  397. <pre class="output">true
  398. false
  399. </pre>
  400. </div>
  401. </div>
  402. <h2 id="RuneCount">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=11033:11061#L362">RuneCount</a></h2>
  403. <pre>func RuneCount(p []<a href="../../builtin/index.html#byte">byte</a>) <a href="../../builtin/index.html#int">int</a></pre>
  404. <p>
  405. RuneCount returns the number of runes in p. Erroneous and short
  406. encodings are treated as single runes of width 1 byte.
  407. </p>
  408. <div id="example_RuneCount" class="toggle">
  409. <div class="collapsed">
  410. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  411. </div>
  412. <div class="expanded">
  413. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  414. <p>Code:</p>
  415. <pre class="code">buf := []byte(&#34;Hello, 世界&#34;)
  416. fmt.Println(&#34;bytes =&#34;, len(buf))
  417. fmt.Println(&#34;runes =&#34;, utf8.RuneCount(buf))
  418. <span class="comment"></pre>
  419. <p>Output:</p>
  420. <pre class="output">bytes = 13
  421. runes = 9
  422. </pre>
  423. </div>
  424. </div>
  425. <h2 id="RuneCountInString">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=11705:11745#L399">RuneCountInString</a></h2>
  426. <pre>func RuneCountInString(s <a href="../../builtin/index.html#string">string</a>) (n <a href="../../builtin/index.html#int">int</a>)</pre>
  427. <p>
  428. RuneCountInString is like RuneCount but its input is a string.
  429. </p>
  430. <div id="example_RuneCountInString" class="toggle">
  431. <div class="collapsed">
  432. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  433. </div>
  434. <div class="expanded">
  435. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  436. <p>Code:</p>
  437. <pre class="code">str := &#34;Hello, 世界&#34;
  438. fmt.Println(&#34;bytes =&#34;, len(str))
  439. fmt.Println(&#34;runes =&#34;, utf8.RuneCountInString(str))
  440. <span class="comment"></pre>
  441. <p>Output:</p>
  442. <pre class="output">bytes = 13
  443. runes = 9
  444. </pre>
  445. </div>
  446. </div>
  447. <h2 id="RuneLen">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=9883:9907#L313">RuneLen</a></h2>
  448. <pre>func RuneLen(r <a href="../../builtin/index.html#rune">rune</a>) <a href="../../builtin/index.html#int">int</a></pre>
  449. <p>
  450. RuneLen returns the number of bytes required to encode the rune.
  451. It returns -1 if the rune is not a valid value to encode in UTF-8.
  452. </p>
  453. <div id="example_RuneLen" class="toggle">
  454. <div class="collapsed">
  455. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  456. </div>
  457. <div class="expanded">
  458. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  459. <p>Code:</p>
  460. <pre class="code">fmt.Println(utf8.RuneLen(&#39;a&#39;))
  461. fmt.Println(utf8.RuneLen(&#39;&#39;))
  462. <span class="comment"></pre>
  463. <p>Output:</p>
  464. <pre class="output">1
  465. 3
  466. </pre>
  467. </div>
  468. </div>
  469. <h2 id="RuneStart">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=12485:12512#L436">RuneStart</a></h2>
  470. <pre>func RuneStart(b <a href="../../builtin/index.html#byte">byte</a>) <a href="../../builtin/index.html#bool">bool</a></pre>
  471. <p>
  472. RuneStart reports whether the byte could be the first byte of an encoded,
  473. possibly invalid rune. Second and subsequent bytes always have the top two
  474. bits set to 10.
  475. </p>
  476. <div id="example_RuneStart" class="toggle">
  477. <div class="collapsed">
  478. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  479. </div>
  480. <div class="expanded">
  481. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  482. <p>Code:</p>
  483. <pre class="code">buf := []byte(&#34;a界&#34;)
  484. fmt.Println(utf8.RuneStart(buf[0]))
  485. fmt.Println(utf8.RuneStart(buf[1]))
  486. fmt.Println(utf8.RuneStart(buf[2]))
  487. <span class="comment"></pre>
  488. <p>Output:</p>
  489. <pre class="output">true
  490. true
  491. false
  492. </pre>
  493. </div>
  494. </div>
  495. <h2 id="Valid">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=12615:12640#L439">Valid</a></h2>
  496. <pre>func Valid(p []<a href="../../builtin/index.html#byte">byte</a>) <a href="../../builtin/index.html#bool">bool</a></pre>
  497. <p>
  498. Valid reports whether p consists entirely of valid UTF-8-encoded runes.
  499. </p>
  500. <div id="example_Valid" class="toggle">
  501. <div class="collapsed">
  502. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  503. </div>
  504. <div class="expanded">
  505. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  506. <p>Code:</p>
  507. <pre class="code">valid := []byte(&#34;Hello, 世界&#34;)
  508. invalid := []byte{0xff, 0xfe, 0xfd}
  509. fmt.Println(utf8.Valid(valid))
  510. fmt.Println(utf8.Valid(invalid))
  511. <span class="comment"></pre>
  512. <p>Output:</p>
  513. <pre class="output">true
  514. false
  515. </pre>
  516. </div>
  517. </div>
  518. <h2 id="ValidRune">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=14008:14035#L504">ValidRune</a></h2>
  519. <pre>func ValidRune(r <a href="../../builtin/index.html#rune">rune</a>) <a href="../../builtin/index.html#bool">bool</a></pre>
  520. <p>
  521. ValidRune reports whether r can be legally encoded as UTF-8.
  522. Code points that are out of range or a surrogate half are illegal.
  523. </p>
  524. <div id="example_ValidRune" class="toggle">
  525. <div class="collapsed">
  526. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  527. </div>
  528. <div class="expanded">
  529. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  530. <p>Code:</p>
  531. <pre class="code">valid := &#39;a&#39;
  532. invalid := rune(0xfffffff)
  533. fmt.Println(utf8.ValidRune(valid))
  534. fmt.Println(utf8.ValidRune(invalid))
  535. <span class="comment"></pre>
  536. <p>Output:</p>
  537. <pre class="output">true
  538. false
  539. </pre>
  540. </div>
  541. </div>
  542. <h2 id="ValidString">func <a href="http://localhost:6060/src/unicode/utf8/utf8.go?s=13282:13313#L471">ValidString</a></h2>
  543. <pre>func ValidString(s <a href="../../builtin/index.html#string">string</a>) <a href="../../builtin/index.html#bool">bool</a></pre>
  544. <p>
  545. ValidString reports whether s consists entirely of valid UTF-8-encoded runes.
  546. </p>
  547. <div id="example_ValidString" class="toggle">
  548. <div class="collapsed">
  549. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  550. </div>
  551. <div class="expanded">
  552. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  553. <p>Code:</p>
  554. <pre class="code">valid := &#34;Hello, 世界&#34;
  555. invalid := string([]byte{0xff, 0xfe, 0xfd})
  556. fmt.Println(utf8.ValidString(valid))
  557. fmt.Println(utf8.ValidString(invalid))
  558. <span class="comment"></pre>
  559. <p>Output:</p>
  560. <pre class="output">true
  561. false
  562. </pre>
  563. </div>
  564. </div>
  565. <div id="footer">
  566. Build version go1.6.<br>
  567. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  568. the content of this page is licensed under the
  569. Creative Commons Attribution 3.0 License,
  570. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  571. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  572. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  573. </div>
  574. </div><!-- .container -->
  575. </div><!-- #page -->
  576. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  577. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  578. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  579. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  580. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  581. </body>
  582. </html>