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