writer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package candiedyaml
  13. /*
  14. * Set the writer error and return 0.
  15. */
  16. func yaml_emitter_set_writer_error(emitter *yaml_emitter_t, problem string) bool {
  17. emitter.error = yaml_WRITER_ERROR
  18. emitter.problem = problem
  19. return false
  20. }
  21. /*
  22. * Flush the output buffer.
  23. */
  24. func yaml_emitter_flush(emitter *yaml_emitter_t) bool {
  25. if emitter.write_handler == nil {
  26. panic("Write handler must be set") /* Write handler must be set. */
  27. }
  28. if emitter.encoding == yaml_ANY_ENCODING {
  29. panic("Encoding must be set") /* Output encoding must be set. */
  30. }
  31. /* Check if the buffer is empty. */
  32. if emitter.buffer_pos == 0 {
  33. return true
  34. }
  35. /* If the output encoding is UTF-8, we don't need to recode the buffer. */
  36. if emitter.encoding == yaml_UTF8_ENCODING {
  37. if err := emitter.write_handler(emitter,
  38. emitter.buffer[:emitter.buffer_pos]); err != nil {
  39. return yaml_emitter_set_writer_error(emitter, "write error: "+err.Error())
  40. }
  41. emitter.buffer_pos = 0
  42. return true
  43. }
  44. /* Recode the buffer into the raw buffer. */
  45. var low, high int
  46. if emitter.encoding == yaml_UTF16LE_ENCODING {
  47. low, high = 0, 1
  48. } else {
  49. high, low = 1, 0
  50. }
  51. pos := 0
  52. for pos < emitter.buffer_pos {
  53. /*
  54. * See the "reader.c" code for more details on UTF-8 encoding. Note
  55. * that we assume that the buffer contains a valid UTF-8 sequence.
  56. */
  57. /* Read the next UTF-8 character. */
  58. octet := emitter.buffer[pos]
  59. var w int
  60. var value rune
  61. switch {
  62. case octet&0x80 == 0x00:
  63. w, value = 1, rune(octet&0x7F)
  64. case octet&0xE0 == 0xC0:
  65. w, value = 2, rune(octet&0x1F)
  66. case octet&0xF0 == 0xE0:
  67. w, value = 3, rune(octet&0x0F)
  68. case octet&0xF8 == 0xF0:
  69. w, value = 4, rune(octet&0x07)
  70. }
  71. for k := 1; k < w; k++ {
  72. octet = emitter.buffer[pos+k]
  73. value = (value << 6) + (rune(octet) & 0x3F)
  74. }
  75. pos += w
  76. /* Write the character. */
  77. if value < 0x10000 {
  78. var b [2]byte
  79. b[high] = byte(value >> 8)
  80. b[low] = byte(value & 0xFF)
  81. emitter.raw_buffer = append(emitter.raw_buffer, b[0], b[1])
  82. } else {
  83. /* Write the character using a surrogate pair (check "reader.c"). */
  84. var b [4]byte
  85. value -= 0x10000
  86. b[high] = byte(0xD8 + (value >> 18))
  87. b[low] = byte((value >> 10) & 0xFF)
  88. b[high+2] = byte(0xDC + ((value >> 8) & 0xFF))
  89. b[low+2] = byte(value & 0xFF)
  90. emitter.raw_buffer = append(emitter.raw_buffer, b[0], b[1], b[2], b[3])
  91. }
  92. }
  93. /* Write the raw buffer. */
  94. // Write the raw buffer.
  95. if err := emitter.write_handler(emitter, emitter.raw_buffer); err != nil {
  96. return yaml_emitter_set_writer_error(emitter, "write error: "+err.Error())
  97. }
  98. emitter.buffer_pos = 0
  99. emitter.raw_buffer = emitter.raw_buffer[:0]
  100. return true
  101. }