1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package utf8 implements functions and constants to support text encoded in
6 // UTF-8. It includes functions to translate between runes and UTF-8 byte sequences.
9 import "unicode" // only needed for a couple of constants
11 // Numbers fundamental to the encoding.
13 RuneError = unicode.ReplacementChar // the "error" Rune or "replacement character".
14 RuneSelf = 0x80 // characters below Runeself are represented as themselves in a single byte.
15 UTFMax = 4 // maximum number of bytes of a UTF-8 encoded Unicode character.
19 t1 = 0x00 // 0000 0000
20 tx = 0x80 // 1000 0000
21 t2 = 0xC0 // 1100 0000
22 t3 = 0xE0 // 1110 0000
23 t4 = 0xF0 // 1111 0000
24 t5 = 0xF8 // 1111 1000
26 maskx = 0x3F // 0011 1111
27 mask2 = 0x1F // 0001 1111
28 mask3 = 0x0F // 0000 1111
29 mask4 = 0x07 // 0000 0111
37 func decodeRuneInternal(p []byte) (r rune, size int, short bool) {
40 return RuneError, 0, true
44 // 1-byte, 7-bit sequence?
46 return rune(c0), 1, false
49 // unexpected continuation byte?
51 return RuneError, 1, false
54 // need first continuation byte
56 return RuneError, 1, true
59 if c1 < tx || t2 <= c1 {
60 return RuneError, 1, false
63 // 2-byte, 11-bit sequence?
65 r = rune(c0&mask2)<<6 | rune(c1&maskx)
67 return RuneError, 1, false
72 // need second continuation byte
74 return RuneError, 1, true
77 if c2 < tx || t2 <= c2 {
78 return RuneError, 1, false
81 // 3-byte, 16-bit sequence?
83 r = rune(c0&mask3)<<12 | rune(c1&maskx)<<6 | rune(c2&maskx)
85 return RuneError, 1, false
90 // need third continuation byte
92 return RuneError, 1, true
95 if c3 < tx || t2 <= c3 {
96 return RuneError, 1, false
99 // 4-byte, 21-bit sequence?
101 r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx)
103 return RuneError, 1, false
109 return RuneError, 1, false
112 func decodeRuneInStringInternal(s string) (r rune, size int, short bool) {
115 return RuneError, 0, true
119 // 1-byte, 7-bit sequence?
121 return rune(c0), 1, false
124 // unexpected continuation byte?
126 return RuneError, 1, false
129 // need first continuation byte
131 return RuneError, 1, true
134 if c1 < tx || t2 <= c1 {
135 return RuneError, 1, false
138 // 2-byte, 11-bit sequence?
140 r = rune(c0&mask2)<<6 | rune(c1&maskx)
142 return RuneError, 1, false
147 // need second continuation byte
149 return RuneError, 1, true
152 if c2 < tx || t2 <= c2 {
153 return RuneError, 1, false
156 // 3-byte, 16-bit sequence?
158 r = rune(c0&mask3)<<12 | rune(c1&maskx)<<6 | rune(c2&maskx)
160 return RuneError, 1, false
165 // need third continuation byte
167 return RuneError, 1, true
170 if c3 < tx || t2 <= c3 {
171 return RuneError, 1, false
174 // 4-byte, 21-bit sequence?
176 r = rune(c0&mask4)<<18 | rune(c1&maskx)<<12 | rune(c2&maskx)<<6 | rune(c3&maskx)
178 return RuneError, 1, false
184 return RuneError, 1, false
187 // FullRune reports whether the bytes in p begin with a full UTF-8 encoding of a rune.
188 // An invalid encoding is considered a full Rune since it will convert as a width-1 error rune.
189 func FullRune(p []byte) bool {
190 _, _, short := decodeRuneInternal(p)
194 // FullRuneInString is like FullRune but its input is a string.
195 func FullRuneInString(s string) bool {
196 _, _, short := decodeRuneInStringInternal(s)
200 // DecodeRune unpacks the first UTF-8 encoding in p and returns the rune and its width in bytes.
201 // If the encoding is invalid, it returns (RuneError, 1), an impossible result for correct UTF-8.
202 func DecodeRune(p []byte) (r rune, size int) {
203 r, size, _ = decodeRuneInternal(p)
207 // DecodeRuneInString is like DecodeRune but its input is a string.
208 // If the encoding is invalid, it returns (RuneError, 1), an impossible result for correct UTF-8.
209 func DecodeRuneInString(s string) (r rune, size int) {
210 r, size, _ = decodeRuneInStringInternal(s)
214 // DecodeLastRune unpacks the last UTF-8 encoding in p and returns the rune and its width in bytes.
215 // If the encoding is invalid, it returns (RuneError, 1), an impossible result for correct UTF-8.
216 func DecodeLastRune(p []byte) (r rune, size int) {
226 // guard against O(n^2) behavior when traversing
227 // backwards through strings with long sequences of
233 for start--; start >= lim; start-- {
234 if RuneStart(p[start]) {
241 r, size = DecodeRune(p[start:end])
242 if start+size != end {
248 // DecodeLastRuneInString is like DecodeLastRune but its input is a string.
249 // If the encoding is invalid, it returns (RuneError, 1), an impossible result for correct UTF-8.
250 func DecodeLastRuneInString(s string) (r rune, size int) {
260 // guard against O(n^2) behavior when traversing
261 // backwards through strings with long sequences of
267 for start--; start >= lim; start-- {
268 if RuneStart(s[start]) {
275 r, size = DecodeRuneInString(s[start:end])
276 if start+size != end {
282 // RuneLen returns the number of bytes required to encode the rune.
283 func RuneLen(r rune) int {
297 // EncodeRune writes into p (which must be large enough) the UTF-8 encoding of the rune.
298 // It returns the number of bytes written.
299 func EncodeRune(p []byte, r rune) int {
300 // Negative values are erroneous. Making it unsigned addresses the problem.
301 if uint32(r) <= rune1Max {
306 if uint32(r) <= rune2Max {
307 p[0] = t2 | byte(r>>6)
308 p[1] = tx | byte(r)&maskx
312 if uint32(r) > unicode.MaxRune {
316 if uint32(r) <= rune3Max {
317 p[0] = t3 | byte(r>>12)
318 p[1] = tx | byte(r>>6)&maskx
319 p[2] = tx | byte(r)&maskx
323 p[0] = t4 | byte(r>>18)
324 p[1] = tx | byte(r>>12)&maskx
325 p[2] = tx | byte(r>>6)&maskx
326 p[3] = tx | byte(r)&maskx
330 // RuneCount returns the number of runes in p. Erroneous and short
331 // encodings are treated as single runes of width 1 byte.
332 func RuneCount(p []byte) int {
335 for n = 0; i < len(p); n++ {
339 _, size := DecodeRune(p[i:])
346 // RuneCountInString is like RuneCount but its input is a string.
347 func RuneCountInString(s string) (n int) {
354 // RuneStart reports whether the byte could be the first byte of
355 // an encoded rune. Second and subsequent bytes always have the top
356 // two bits set to 10.
357 func RuneStart(b byte) bool { return b&0xC0 != 0x80 }
359 // Valid reports whether p consists entirely of valid UTF-8-encoded runes.
360 func Valid(p []byte) bool {
366 _, size := DecodeRune(p[i:])
368 // All valid runes of size of 1 (those
369 // below RuneSelf) were handled above.
370 // This must be a RuneError.
379 // ValidString reports whether s consists entirely of valid UTF-8-encoded runes.
380 func ValidString(s string) bool {
381 for i, r := range s {
383 // The RuneError value can be an error
384 // sentinel value (if it's size 1) or the same
385 // value encoded properly. Decode it to see if
386 // it's the 1 byte sentinel value.
387 _, size := DecodeRuneInString(s[i:])