Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 1 | package yaml |
| 2 | |
| 3 | const ( |
| 4 | // The size of the input raw buffer. |
| 5 | input_raw_buffer_size = 512 |
| 6 | |
| 7 | // The size of the input buffer. |
| 8 | // It should be possible to decode the whole raw buffer. |
| 9 | input_buffer_size = input_raw_buffer_size * 3 |
| 10 | |
| 11 | // The size of the output buffer. |
| 12 | output_buffer_size = 128 |
| 13 | |
| 14 | // The size of the output raw buffer. |
| 15 | // It should be possible to encode the whole output buffer. |
| 16 | output_raw_buffer_size = (output_buffer_size*2 + 2) |
| 17 | |
| 18 | // The size of other stacks and queues. |
| 19 | initial_stack_size = 16 |
| 20 | initial_queue_size = 16 |
| 21 | initial_string_size = 16 |
| 22 | ) |
| 23 | |
| 24 | // Check if the character at the specified position is an alphabetical |
| 25 | // character, a digit, '_', or '-'. |
| 26 | func is_alpha(b []byte, i int) bool { |
| 27 | return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'Z' || b[i] >= 'a' && b[i] <= 'z' || b[i] == '_' || b[i] == '-' |
| 28 | } |
| 29 | |
| 30 | // Check if the character at the specified position is a digit. |
| 31 | func is_digit(b []byte, i int) bool { |
| 32 | return b[i] >= '0' && b[i] <= '9' |
| 33 | } |
| 34 | |
| 35 | // Get the value of a digit. |
| 36 | func as_digit(b []byte, i int) int { |
| 37 | return int(b[i]) - '0' |
| 38 | } |
| 39 | |
| 40 | // Check if the character at the specified position is a hex-digit. |
| 41 | func is_hex(b []byte, i int) bool { |
| 42 | return b[i] >= '0' && b[i] <= '9' || b[i] >= 'A' && b[i] <= 'F' || b[i] >= 'a' && b[i] <= 'f' |
| 43 | } |
| 44 | |
| 45 | // Get the value of a hex-digit. |
| 46 | func as_hex(b []byte, i int) int { |
| 47 | bi := b[i] |
| 48 | if bi >= 'A' && bi <= 'F' { |
| 49 | return int(bi) - 'A' + 10 |
| 50 | } |
| 51 | if bi >= 'a' && bi <= 'f' { |
| 52 | return int(bi) - 'a' + 10 |
| 53 | } |
| 54 | return int(bi) - '0' |
| 55 | } |
| 56 | |
| 57 | // Check if the character is ASCII. |
| 58 | func is_ascii(b []byte, i int) bool { |
| 59 | return b[i] <= 0x7F |
| 60 | } |
| 61 | |
| 62 | // Check if the character at the start of the buffer can be printed unescaped. |
| 63 | func is_printable(b []byte, i int) bool { |
| 64 | return ((b[i] == 0x0A) || // . == #x0A |
| 65 | (b[i] >= 0x20 && b[i] <= 0x7E) || // #x20 <= . <= #x7E |
| 66 | (b[i] == 0xC2 && b[i+1] >= 0xA0) || // #0xA0 <= . <= #xD7FF |
| 67 | (b[i] > 0xC2 && b[i] < 0xED) || |
| 68 | (b[i] == 0xED && b[i+1] < 0xA0) || |
| 69 | (b[i] == 0xEE) || |
| 70 | (b[i] == 0xEF && // #xE000 <= . <= #xFFFD |
| 71 | !(b[i+1] == 0xBB && b[i+2] == 0xBF) && // && . != #xFEFF |
| 72 | !(b[i+1] == 0xBF && (b[i+2] == 0xBE || b[i+2] == 0xBF)))) |
| 73 | } |
| 74 | |
| 75 | // Check if the character at the specified position is NUL. |
| 76 | func is_z(b []byte, i int) bool { |
| 77 | return b[i] == 0x00 |
| 78 | } |
| 79 | |
| 80 | // Check if the beginning of the buffer is a BOM. |
| 81 | func is_bom(b []byte, i int) bool { |
| 82 | return b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF |
| 83 | } |
| 84 | |
| 85 | // Check if the character at the specified position is space. |
| 86 | func is_space(b []byte, i int) bool { |
| 87 | return b[i] == ' ' |
| 88 | } |
| 89 | |
| 90 | // Check if the character at the specified position is tab. |
| 91 | func is_tab(b []byte, i int) bool { |
| 92 | return b[i] == '\t' |
| 93 | } |
| 94 | |
| 95 | // Check if the character at the specified position is blank (space or tab). |
| 96 | func is_blank(b []byte, i int) bool { |
| 97 | //return is_space(b, i) || is_tab(b, i) |
| 98 | return b[i] == ' ' || b[i] == '\t' |
| 99 | } |
| 100 | |
| 101 | // Check if the character at the specified position is a line break. |
| 102 | func is_break(b []byte, i int) bool { |
| 103 | return (b[i] == '\r' || // CR (#xD) |
| 104 | b[i] == '\n' || // LF (#xA) |
| 105 | b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) |
| 106 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) |
| 107 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9) // PS (#x2029) |
| 108 | } |
| 109 | |
| 110 | func is_crlf(b []byte, i int) bool { |
| 111 | return b[i] == '\r' && b[i+1] == '\n' |
| 112 | } |
| 113 | |
| 114 | // Check if the character is a line break or NUL. |
| 115 | func is_breakz(b []byte, i int) bool { |
| 116 | //return is_break(b, i) || is_z(b, i) |
| 117 | return ( // is_break: |
| 118 | b[i] == '\r' || // CR (#xD) |
| 119 | b[i] == '\n' || // LF (#xA) |
| 120 | b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) |
| 121 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) |
| 122 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) |
| 123 | // is_z: |
| 124 | b[i] == 0) |
| 125 | } |
| 126 | |
| 127 | // Check if the character is a line break, space, or NUL. |
| 128 | func is_spacez(b []byte, i int) bool { |
| 129 | //return is_space(b, i) || is_breakz(b, i) |
| 130 | return ( // is_space: |
| 131 | b[i] == ' ' || |
| 132 | // is_breakz: |
| 133 | b[i] == '\r' || // CR (#xD) |
| 134 | b[i] == '\n' || // LF (#xA) |
| 135 | b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) |
| 136 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) |
| 137 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) |
| 138 | b[i] == 0) |
| 139 | } |
| 140 | |
| 141 | // Check if the character is a line break, space, tab, or NUL. |
| 142 | func is_blankz(b []byte, i int) bool { |
| 143 | //return is_blank(b, i) || is_breakz(b, i) |
| 144 | return ( // is_blank: |
| 145 | b[i] == ' ' || b[i] == '\t' || |
| 146 | // is_breakz: |
| 147 | b[i] == '\r' || // CR (#xD) |
| 148 | b[i] == '\n' || // LF (#xA) |
| 149 | b[i] == 0xC2 && b[i+1] == 0x85 || // NEL (#x85) |
| 150 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA8 || // LS (#x2028) |
| 151 | b[i] == 0xE2 && b[i+1] == 0x80 && b[i+2] == 0xA9 || // PS (#x2029) |
| 152 | b[i] == 0) |
| 153 | } |
| 154 | |
| 155 | // Determine the width of the character. |
| 156 | func width(b byte) int { |
| 157 | // Don't replace these by a switch without first |
| 158 | // confirming that it is being inlined. |
| 159 | if b&0x80 == 0x00 { |
| 160 | return 1 |
| 161 | } |
| 162 | if b&0xE0 == 0xC0 { |
| 163 | return 2 |
| 164 | } |
| 165 | if b&0xF0 == 0xE0 { |
| 166 | return 3 |
| 167 | } |
| 168 | if b&0xF8 == 0xF0 { |
| 169 | return 4 |
| 170 | } |
| 171 | return 0 |
| 172 | |
| 173 | } |