Path: blob/main/vendor/go.yaml.in/yaml/v3/yamlprivateh.go
2872 views
//1// Copyright (c) 2011-2019 Canonical Ltd2// Copyright (c) 2006-2010 Kirill Simonov3//4// Permission is hereby granted, free of charge, to any person obtaining a copy of5// this software and associated documentation files (the "Software"), to deal in6// the Software without restriction, including without limitation the rights to7// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies8// of the Software, and to permit persons to whom the Software is furnished to do9// so, subject to the following conditions:10//11// The above copyright notice and this permission notice shall be included in all12// copies or substantial portions of the Software.13//14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20// SOFTWARE.2122package yaml2324const (25// The size of the input raw buffer.26input_raw_buffer_size = 5122728// The size of the input buffer.29// It should be possible to decode the whole raw buffer.30input_buffer_size = input_raw_buffer_size * 33132// The size of the output buffer.33output_buffer_size = 1283435// The size of the output raw buffer.36// It should be possible to encode the whole output buffer.37output_raw_buffer_size = (output_buffer_size*2 + 2)3839// The size of other stacks and queues.40initial_stack_size = 1641initial_queue_size = 1642initial_string_size = 1643)4445// Check if the character at the specified position is an alphabetical46// character, a digit, '_', or '-'.47func is_alpha(b []byte, i int) bool {48return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'Z' || b[i] >= 'a' && b[i] <= 'z' || b[i] == '_' || b[i] == '-'49}5051// Check if the character at the specified position is a digit.52func is_digit(b []byte, i int) bool {53return b[i] >= '0' && b[i] <= '9'54}5556// Get the value of a digit.57func as_digit(b []byte, i int) int {58return int(b[i]) - '0'59}6061// Check if the character at the specified position is a hex-digit.62func is_hex(b []byte, i int) bool {63return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'F' || b[i] >= 'a' && b[i] <= 'f'64}6566// Get the value of a hex-digit.67func as_hex(b []byte, i int) int {68bi := b[i]69if bi >= 'A' && bi <= 'F' {70return int(bi) - 'A' + 1071}72if bi >= 'a' && bi <= 'f' {73return int(bi) - 'a' + 1074}75return int(bi) - '0'76}7778// Check if the character is ASCII.79func is_ascii(b []byte, i int) bool {80return b[i] <= 0x7F81}8283// Check if the character at the start of the buffer can be printed unescaped.84func is_printable(b []byte, i int) bool {85return ((b[i] == 0x0A) || // . == #x0A86(b[i] >= 0x20 && b[i] <= 0x7E) || // #x20 <= . <= #x7E87(b[i] == 0xC2 && b[i+1] >= 0xA0) || // #0xA0 <= . <= #xD7FF88(b[i] > 0xC2 && b[i] < 0xED) ||89(b[i] == 0xED && b[i+1] < 0xA0) ||90(b[i] == 0xEE) ||91(b[i] == 0xEF && // #xE000 <= . <= #xFFFD92!(b[i+1] == 0xBB && b[i+2] == 0xBF) && // && . != #xFEFF93!(b[i+1] == 0xBF && (b[i+2] == 0xBE || b[i+2] == 0xBF))))94}9596// Check if the character at the specified position is NUL.97func is_z(b []byte, i int) bool {98return b[i] == 0x0099}100101// Check if the beginning of the buffer is a BOM.102func is_bom(b []byte, i int) bool {103return b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF104}105106// Check if the character at the specified position is space.107func is_space(b []byte, i int) bool {108return b[i] == ' '109}110111// Check if the character at the specified position is tab.112func is_tab(b []byte, i int) bool {113return b[i] == '\t'114}115116// Check if the character at the specified position is blank (space or tab).117func is_blank(b []byte, i int) bool {118//return is_space(b, i) || is_tab(b, i)119return b[i] == ' ' || b[i] == '\t'120}121122// Check if the character at the specified position is a line break.123func is_break(b []byte, i int) bool {124return (b[i] == '\r' || // CR (#xD)125b[i] == '\n' || // LF (#xA)126b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85)127b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028)128b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9) // PS (#x2029)129}130131func is_crlf(b []byte, i int) bool {132return b[i] == '\r' && b[i+1] == '\n'133}134135// Check if the character is a line break or NUL.136func is_breakz(b []byte, i int) bool {137//return is_break(b, i) || is_z(b, i)138return (139// is_break:140b[i] == '\r' || // CR (#xD)141b[i] == '\n' || // LF (#xA)142b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85)143b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028)144b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029)145// is_z:146b[i] == 0)147}148149// Check if the character is a line break, space, or NUL.150func is_spacez(b []byte, i int) bool {151//return is_space(b, i) || is_breakz(b, i)152return (153// is_space:154b[i] == ' ' ||155// is_breakz:156b[i] == '\r' || // CR (#xD)157b[i] == '\n' || // LF (#xA)158b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85)159b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028)160b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029)161b[i] == 0)162}163164// Check if the character is a line break, space, tab, or NUL.165func is_blankz(b []byte, i int) bool {166//return is_blank(b, i) || is_breakz(b, i)167return (168// is_blank:169b[i] == ' ' || b[i] == '\t' ||170// is_breakz:171b[i] == '\r' || // CR (#xD)172b[i] == '\n' || // LF (#xA)173b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85)174b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028)175b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029)176b[i] == 0)177}178179// Determine the width of the character.180func width(b byte) int {181// Don't replace these by a switch without first182// confirming that it is being inlined.183if b&0x80 == 0x00 {184return 1185}186if b&0xE0 == 0xC0 {187return 2188}189if b&0xF0 == 0xE0 {190return 3191}192if b&0xF8 == 0xF0 {193return 4194}195return 0196197}198199200