libgo: update to Go 1.11
[official-gcc.git] / libgo / go / runtime / utf8.go
blob0ba0dad835cd0d86ef3c5d0aaa242991bfe1f017
1 // Copyright 2016 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 runtime
7 import _ "unsafe" // For go:linkname.
9 // For gccgo, use go:linkname to rename compiler-called functions to
10 // themselves, so that the compiler will export them.
12 //go:linkname decoderune runtime.decoderune
14 // Numbers fundamental to the encoding.
15 const (
16 runeError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
17 runeSelf = 0x80 // characters below Runeself are represented as themselves in a single byte.
18 maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
21 // Code points in the surrogate range are not valid for UTF-8.
22 const (
23 surrogateMin = 0xD800
24 surrogateMax = 0xDFFF
27 const (
28 t1 = 0x00 // 0000 0000
29 tx = 0x80 // 1000 0000
30 t2 = 0xC0 // 1100 0000
31 t3 = 0xE0 // 1110 0000
32 t4 = 0xF0 // 1111 0000
33 t5 = 0xF8 // 1111 1000
35 maskx = 0x3F // 0011 1111
36 mask2 = 0x1F // 0001 1111
37 mask3 = 0x0F // 0000 1111
38 mask4 = 0x07 // 0000 0111
40 rune1Max = 1<<7 - 1
41 rune2Max = 1<<11 - 1
42 rune3Max = 1<<16 - 1
44 // The default lowest and highest continuation byte.
45 locb = 0x80 // 1000 0000
46 hicb = 0xBF // 1011 1111
49 // countrunes returns the number of runes in s.
50 func countrunes(s string) int {
51 n := 0
52 for range s {
53 n++
55 return n
58 // decoderune returns the non-ASCII rune at the start of
59 // s[k:] and the index after the rune in s.
61 // decoderune assumes that caller has checked that
62 // the to be decoded rune is a non-ASCII rune.
64 // If the string appears to be incomplete or decoding problems
65 // are encountered (runeerror, k + 1) is returned to ensure
66 // progress when decoderune is used to iterate over a string.
67 func decoderune(s string, k int) (r rune, pos int) {
68 pos = k
70 if k >= len(s) {
71 return runeError, k + 1
74 s = s[k:]
76 switch {
77 case t2 <= s[0] && s[0] < t3:
78 // 0080-07FF two byte sequence
79 if len(s) > 1 && (locb <= s[1] && s[1] <= hicb) {
80 r = rune(s[0]&mask2)<<6 | rune(s[1]&maskx)
81 pos += 2
82 if rune1Max < r {
83 return
86 case t3 <= s[0] && s[0] < t4:
87 // 0800-FFFF three byte sequence
88 if len(s) > 2 && (locb <= s[1] && s[1] <= hicb) && (locb <= s[2] && s[2] <= hicb) {
89 r = rune(s[0]&mask3)<<12 | rune(s[1]&maskx)<<6 | rune(s[2]&maskx)
90 pos += 3
91 if rune2Max < r && !(surrogateMin <= r && r <= surrogateMax) {
92 return
95 case t4 <= s[0] && s[0] < t5:
96 // 10000-1FFFFF four byte sequence
97 if len(s) > 3 && (locb <= s[1] && s[1] <= hicb) && (locb <= s[2] && s[2] <= hicb) && (locb <= s[3] && s[3] <= hicb) {
98 r = rune(s[0]&mask4)<<18 | rune(s[1]&maskx)<<12 | rune(s[2]&maskx)<<6 | rune(s[3]&maskx)
99 pos += 4
100 if rune3Max < r && r <= maxRune {
101 return
106 return runeError, k + 1
109 // encoderune writes into p (which must be large enough) the UTF-8 encoding of the rune.
110 // It returns the number of bytes written.
111 func encoderune(p []byte, r rune) int {
112 // Negative values are erroneous. Making it unsigned addresses the problem.
113 switch i := uint32(r); {
114 case i <= rune1Max:
115 p[0] = byte(r)
116 return 1
117 case i <= rune2Max:
118 _ = p[1] // eliminate bounds checks
119 p[0] = t2 | byte(r>>6)
120 p[1] = tx | byte(r)&maskx
121 return 2
122 case i > maxRune, surrogateMin <= i && i <= surrogateMax:
123 r = runeError
124 fallthrough
125 case i <= rune3Max:
126 _ = p[2] // eliminate bounds checks
127 p[0] = t3 | byte(r>>12)
128 p[1] = tx | byte(r>>6)&maskx
129 p[2] = tx | byte(r)&maskx
130 return 3
131 default:
132 _ = p[3] // eliminate bounds checks
133 p[0] = t4 | byte(r>>18)
134 p[1] = tx | byte(r>>12)&maskx
135 p[2] = tx | byte(r>>6)&maskx
136 p[3] = tx | byte(r)&maskx
137 return 4