yamlprivateh.go 6.0 KB

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