Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/golang.org/x/text/internal/utf8internal/utf8internal.go
2893 views
1
// Copyright 2015 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
5
// Package utf8internal contains low-level utf8-related constants, tables, etc.
6
// that are used internally by the text package.
7
package utf8internal
8
9
// The default lowest and highest continuation byte.
10
const (
11
LoCB = 0x80 // 1000 0000
12
HiCB = 0xBF // 1011 1111
13
)
14
15
// Constants related to getting information of first bytes of UTF-8 sequences.
16
const (
17
// ASCII identifies a UTF-8 byte as ASCII.
18
ASCII = as
19
20
// FirstInvalid indicates a byte is invalid as a first byte of a UTF-8
21
// sequence.
22
FirstInvalid = xx
23
24
// SizeMask is a mask for the size bits. Use use x&SizeMask to get the size.
25
SizeMask = 7
26
27
// AcceptShift is the right-shift count for the first byte info byte to get
28
// the index into the AcceptRanges table. See AcceptRanges.
29
AcceptShift = 4
30
31
// The names of these constants are chosen to give nice alignment in the
32
// table below. The first nibble is an index into acceptRanges or F for
33
// special one-byte cases. The second nibble is the Rune length or the
34
// Status for the special one-byte case.
35
xx = 0xF1 // invalid: size 1
36
as = 0xF0 // ASCII: size 1
37
s1 = 0x02 // accept 0, size 2
38
s2 = 0x13 // accept 1, size 3
39
s3 = 0x03 // accept 0, size 3
40
s4 = 0x23 // accept 2, size 3
41
s5 = 0x34 // accept 3, size 4
42
s6 = 0x04 // accept 0, size 4
43
s7 = 0x44 // accept 4, size 4
44
)
45
46
// First is information about the first byte in a UTF-8 sequence.
47
var First = [256]uint8{
48
// 1 2 3 4 5 6 7 8 9 A B C D E F
49
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F
50
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F
51
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x20-0x2F
52
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x30-0x3F
53
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x40-0x4F
54
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x50-0x5F
55
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x60-0x6F
56
as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x70-0x7F
57
// 1 2 3 4 5 6 7 8 9 A B C D E F
58
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x80-0x8F
59
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x90-0x9F
60
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xA0-0xAF
61
xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xB0-0xBF
62
xx, xx, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xC0-0xCF
63
s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xD0-0xDF
64
s2, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s4, s3, s3, // 0xE0-0xEF
65
s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF
66
}
67
68
// AcceptRange gives the range of valid values for the second byte in a UTF-8
69
// sequence for any value for First that is not ASCII or FirstInvalid.
70
type AcceptRange struct {
71
Lo uint8 // lowest value for second byte.
72
Hi uint8 // highest value for second byte.
73
}
74
75
// AcceptRanges is a slice of AcceptRange values. For a given byte sequence b
76
//
77
// AcceptRanges[First[b[0]]>>AcceptShift]
78
//
79
// will give the value of AcceptRange for the multi-byte UTF-8 sequence starting
80
// at b[0].
81
var AcceptRanges = [...]AcceptRange{
82
0: {LoCB, HiCB},
83
1: {0xA0, HiCB},
84
2: {LoCB, 0x9F},
85
3: {0x90, HiCB},
86
4: {LoCB, 0x8F},
87
}
88
89