1 |
yakumo_izuru |
1.1 |
// |
2 |
|
|
// Copyright (c) 2011-2019 Canonical Ltd |
3 |
|
|
// Copyright (c) 2006-2010 Kirill Simonov |
4 |
|
|
// |
5 |
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of |
6 |
|
|
// this software and associated documentation files (the "Software"), to deal in |
7 |
|
|
// the Software without restriction, including without limitation the rights to |
8 |
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
9 |
|
|
// of the Software, and to permit persons to whom the Software is furnished to do |
10 |
|
|
// so, subject to the following conditions: |
11 |
|
|
// |
12 |
|
|
// The above copyright notice and this permission notice shall be included in all |
13 |
|
|
// copies or substantial portions of the Software. |
14 |
|
|
// |
15 |
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 |
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18 |
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 |
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 |
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21 |
|
|
// SOFTWARE. |
22 |
|
|
|
23 |
|
|
package yaml |
24 |
|
|
|
25 |
|
|
const ( |
26 |
|
|
// The size of the input raw buffer. |
27 |
|
|
input_raw_buffer_size = 512 |
28 |
|
|
|
29 |
|
|
// The size of the input buffer. |
30 |
|
|
// It should be possible to decode the whole raw buffer. |
31 |
|
|
input_buffer_size = input_raw_buffer_size * 3 |
32 |
|
|
|
33 |
|
|
// The size of the output buffer. |
34 |
|
|
output_buffer_size = 128 |
35 |
|
|
|
36 |
|
|
// The size of the output raw buffer. |
37 |
|
|
// It should be possible to encode the whole output buffer. |
38 |
|
|
output_raw_buffer_size = (output_buffer_size*2 + 2) |
39 |
|
|
|
40 |
|
|
// The size of other stacks and queues. |
41 |
|
|
initial_stack_size = 16 |
42 |
|
|
initial_queue_size = 16 |
43 |
|
|
initial_string_size = 16 |
44 |
|
|
) |
45 |
|
|
|
46 |
|
|
// Check if the character at the specified position is an alphabetical |
47 |
|
|
// character, a digit, '_', or '-'. |
48 |
|
|
func is_alpha(b []byte, i int) bool { |
49 |
|
|
return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'Z' || b[i] >= 'a' && b[i] <= 'z' || b[i] == '_' || b[i] == '-' |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
// Check if the character at the specified position is a digit. |
53 |
|
|
func is_digit(b []byte, i int) bool { |
54 |
|
|
return b[i] >= '0' && b[i] <= '9' |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
// Get the value of a digit. |
58 |
|
|
func as_digit(b []byte, i int) int { |
59 |
|
|
return int(b[i]) - '0' |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
// Check if the character at the specified position is a hex-digit. |
63 |
|
|
func is_hex(b []byte, i int) bool { |
64 |
|
|
return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'F' || b[i] >= 'a' && b[i] <= 'f' |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
// Get the value of a hex-digit. |
68 |
|
|
func as_hex(b []byte, i int) int { |
69 |
|
|
bi := b[i] |
70 |
|
|
if bi >= 'A' && bi <= 'F' { |
71 |
|
|
return int(bi) - 'A' + 10 |
72 |
|
|
} |
73 |
|
|
if bi >= 'a' && bi <= 'f' { |
74 |
|
|
return int(bi) - 'a' + 10 |
75 |
|
|
} |
76 |
|
|
return int(bi) - '0' |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
// Check if the character is ASCII. |
80 |
|
|
func is_ascii(b []byte, i int) bool { |
81 |
|
|
return b[i] <= 0x7F |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
// Check if the character at the start of the buffer can be printed unescaped. |
85 |
|
|
func is_printable(b []byte, i int) bool { |
86 |
|
|
return ((b[i] == 0x0A) || // . == #x0A |
87 |
|
|
(b[i] >= 0x20 && b[i] <= 0x7E) || // #x20 <= . <= #x7E |
88 |
|
|
(b[i] == 0xC2 && b[i+1] >= 0xA0) || // #0xA0 <= . <= #xD7FF |
89 |
|
|
(b[i] > 0xC2 && b[i] < 0xED) || |
90 |
|
|
(b[i] == 0xED && b[i+1] < 0xA0) || |
91 |
|
|
(b[i] == 0xEE) || |
92 |
|
|
(b[i] == 0xEF && // #xE000 <= . <= #xFFFD |
93 |
|
|
!(b[i+1] == 0xBB && b[i+2] == 0xBF) && // && . != #xFEFF |
94 |
|
|
!(b[i+1] == 0xBF && (b[i+2] == 0xBE || b[i+2] == 0xBF)))) |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
// Check if the character at the specified position is NUL. |
98 |
|
|
func is_z(b []byte, i int) bool { |
99 |
|
|
return b[i] == 0x00 |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
// Check if the beginning of the buffer is a BOM. |
103 |
|
|
func is_bom(b []byte, i int) bool { |
104 |
|
|
return b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
// Check if the character at the specified position is space. |
108 |
|
|
func is_space(b []byte, i int) bool { |
109 |
|
|
return b[i] == ' ' |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
// Check if the character at the specified position is tab. |
113 |
|
|
func is_tab(b []byte, i int) bool { |
114 |
|
|
return b[i] == '\t' |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
// Check if the character at the specified position is blank (space or tab). |
118 |
|
|
func is_blank(b []byte, i int) bool { |
119 |
|
|
//return is_space(b, i) || is_tab(b, i) |
120 |
|
|
return b[i] == ' ' || b[i] == '\t' |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
// Check if the character at the specified position is a line break. |
124 |
|
|
func is_break(b []byte, i int) bool { |
125 |
|
|
return (b[i] == '\r' || // CR (#xD) |
126 |
|
|
b[i] == '\n' || // LF (#xA) |
127 |
|
|
b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) |
128 |
|
|
b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) |
129 |
|
|
b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9) // PS (#x2029) |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
func is_crlf(b []byte, i int) bool { |
133 |
|
|
return b[i] == '\r' && b[i+1] == '\n' |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
// Check if the character is a line break or NUL. |
137 |
|
|
func is_breakz(b []byte, i int) bool { |
138 |
|
|
//return is_break(b, i) || is_z(b, i) |
139 |
|
|
return ( |
140 |
|
|
// is_break: |
141 |
|
|
b[i] == '\r' || // CR (#xD) |
142 |
|
|
b[i] == '\n' || // LF (#xA) |
143 |
|
|
b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) |
144 |
|
|
b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) |
145 |
|
|
b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) |
146 |
|
|
// is_z: |
147 |
|
|
b[i] == 0) |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
// Check if the character is a line break, space, or NUL. |
151 |
|
|
func is_spacez(b []byte, i int) bool { |
152 |
|
|
//return is_space(b, i) || is_breakz(b, i) |
153 |
|
|
return ( |
154 |
|
|
// is_space: |
155 |
|
|
b[i] == ' ' || |
156 |
|
|
// is_breakz: |
157 |
|
|
b[i] == '\r' || // CR (#xD) |
158 |
|
|
b[i] == '\n' || // LF (#xA) |
159 |
|
|
b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) |
160 |
|
|
b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) |
161 |
|
|
b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) |
162 |
|
|
b[i] == 0) |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
// Check if the character is a line break, space, tab, or NUL. |
166 |
|
|
func is_blankz(b []byte, i int) bool { |
167 |
|
|
//return is_blank(b, i) || is_breakz(b, i) |
168 |
|
|
return ( |
169 |
|
|
// is_blank: |
170 |
|
|
b[i] == ' ' || b[i] == '\t' || |
171 |
|
|
// is_breakz: |
172 |
|
|
b[i] == '\r' || // CR (#xD) |
173 |
|
|
b[i] == '\n' || // LF (#xA) |
174 |
|
|
b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) |
175 |
|
|
b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) |
176 |
|
|
b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) |
177 |
|
|
b[i] == 0) |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
// Determine the width of the character. |
181 |
|
|
func width(b byte) int { |
182 |
|
|
// Don't replace these by a switch without first |
183 |
|
|
// confirming that it is being inlined. |
184 |
|
|
if b&0x80 == 0x00 { |
185 |
|
|
return 1 |
186 |
|
|
} |
187 |
|
|
if b&0xE0 == 0xC0 { |
188 |
|
|
return 2 |
189 |
|
|
} |
190 |
|
|
if b&0xF0 == 0xE0 { |
191 |
|
|
return 3 |
192 |
|
|
} |
193 |
|
|
if b&0xF8 == 0xF0 { |
194 |
|
|
return 4 |
195 |
|
|
} |
196 |
|
|
return 0 |
197 |
|
|
|
198 |
|
|
} |