emitterc.go 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  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. import (
  24. "bytes"
  25. "fmt"
  26. )
  27. // Flush the buffer if needed.
  28. func flush(emitter *yaml_emitter_t) bool {
  29. if emitter.buffer_pos+5 >= len(emitter.buffer) {
  30. return yaml_emitter_flush(emitter)
  31. }
  32. return true
  33. }
  34. // Put a character to the output buffer.
  35. func put(emitter *yaml_emitter_t, value byte) bool {
  36. if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
  37. return false
  38. }
  39. emitter.buffer[emitter.buffer_pos] = value
  40. emitter.buffer_pos++
  41. emitter.column++
  42. return true
  43. }
  44. // Put a line break to the output buffer.
  45. func put_break(emitter *yaml_emitter_t) bool {
  46. if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
  47. return false
  48. }
  49. switch emitter.line_break {
  50. case yaml_CR_BREAK:
  51. emitter.buffer[emitter.buffer_pos] = '\r'
  52. emitter.buffer_pos += 1
  53. case yaml_LN_BREAK:
  54. emitter.buffer[emitter.buffer_pos] = '\n'
  55. emitter.buffer_pos += 1
  56. case yaml_CRLN_BREAK:
  57. emitter.buffer[emitter.buffer_pos+0] = '\r'
  58. emitter.buffer[emitter.buffer_pos+1] = '\n'
  59. emitter.buffer_pos += 2
  60. default:
  61. panic("unknown line break setting")
  62. }
  63. if emitter.column == 0 {
  64. emitter.space_above = true
  65. }
  66. emitter.column = 0
  67. emitter.line++
  68. // [Go] Do this here and below and drop from everywhere else (see commented lines).
  69. emitter.indention = true
  70. return true
  71. }
  72. // Copy a character from a string into buffer.
  73. func write(emitter *yaml_emitter_t, s []byte, i *int) bool {
  74. if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
  75. return false
  76. }
  77. p := emitter.buffer_pos
  78. w := width(s[*i])
  79. switch w {
  80. case 4:
  81. emitter.buffer[p+3] = s[*i+3]
  82. fallthrough
  83. case 3:
  84. emitter.buffer[p+2] = s[*i+2]
  85. fallthrough
  86. case 2:
  87. emitter.buffer[p+1] = s[*i+1]
  88. fallthrough
  89. case 1:
  90. emitter.buffer[p+0] = s[*i+0]
  91. default:
  92. panic("unknown character width")
  93. }
  94. emitter.column++
  95. emitter.buffer_pos += w
  96. *i += w
  97. return true
  98. }
  99. // Write a whole string into buffer.
  100. func write_all(emitter *yaml_emitter_t, s []byte) bool {
  101. for i := 0; i < len(s); {
  102. if !write(emitter, s, &i) {
  103. return false
  104. }
  105. }
  106. return true
  107. }
  108. // Copy a line break character from a string into buffer.
  109. func write_break(emitter *yaml_emitter_t, s []byte, i *int) bool {
  110. if s[*i] == '\n' {
  111. if !put_break(emitter) {
  112. return false
  113. }
  114. *i++
  115. } else {
  116. if !write(emitter, s, i) {
  117. return false
  118. }
  119. if emitter.column == 0 {
  120. emitter.space_above = true
  121. }
  122. emitter.column = 0
  123. emitter.line++
  124. // [Go] Do this here and above and drop from everywhere else (see commented lines).
  125. emitter.indention = true
  126. }
  127. return true
  128. }
  129. // Set an emitter error and return false.
  130. func yaml_emitter_set_emitter_error(emitter *yaml_emitter_t, problem string) bool {
  131. emitter.error = yaml_EMITTER_ERROR
  132. emitter.problem = problem
  133. return false
  134. }
  135. // Emit an event.
  136. func yaml_emitter_emit(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  137. emitter.events = append(emitter.events, *event)
  138. for !yaml_emitter_need_more_events(emitter) {
  139. event := &emitter.events[emitter.events_head]
  140. if !yaml_emitter_analyze_event(emitter, event) {
  141. return false
  142. }
  143. if !yaml_emitter_state_machine(emitter, event) {
  144. return false
  145. }
  146. yaml_event_delete(event)
  147. emitter.events_head++
  148. }
  149. return true
  150. }
  151. // Check if we need to accumulate more events before emitting.
  152. //
  153. // We accumulate extra
  154. // - 1 event for DOCUMENT-START
  155. // - 2 events for SEQUENCE-START
  156. // - 3 events for MAPPING-START
  157. func yaml_emitter_need_more_events(emitter *yaml_emitter_t) bool {
  158. if emitter.events_head == len(emitter.events) {
  159. return true
  160. }
  161. var accumulate int
  162. switch emitter.events[emitter.events_head].typ {
  163. case yaml_DOCUMENT_START_EVENT:
  164. accumulate = 1
  165. break
  166. case yaml_SEQUENCE_START_EVENT:
  167. accumulate = 2
  168. break
  169. case yaml_MAPPING_START_EVENT:
  170. accumulate = 3
  171. break
  172. default:
  173. return false
  174. }
  175. if len(emitter.events)-emitter.events_head > accumulate {
  176. return false
  177. }
  178. var level int
  179. for i := emitter.events_head; i < len(emitter.events); i++ {
  180. switch emitter.events[i].typ {
  181. case yaml_STREAM_START_EVENT, yaml_DOCUMENT_START_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT:
  182. level++
  183. case yaml_STREAM_END_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_END_EVENT, yaml_MAPPING_END_EVENT:
  184. level--
  185. }
  186. if level == 0 {
  187. return false
  188. }
  189. }
  190. return true
  191. }
  192. // Append a directive to the directives stack.
  193. func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_directive_t, allow_duplicates bool) bool {
  194. for i := 0; i < len(emitter.tag_directives); i++ {
  195. if bytes.Equal(value.handle, emitter.tag_directives[i].handle) {
  196. if allow_duplicates {
  197. return true
  198. }
  199. return yaml_emitter_set_emitter_error(emitter, "duplicate %TAG directive")
  200. }
  201. }
  202. // [Go] Do we actually need to copy this given garbage collection
  203. // and the lack of deallocating destructors?
  204. tag_copy := yaml_tag_directive_t{
  205. handle: make([]byte, len(value.handle)),
  206. prefix: make([]byte, len(value.prefix)),
  207. }
  208. copy(tag_copy.handle, value.handle)
  209. copy(tag_copy.prefix, value.prefix)
  210. emitter.tag_directives = append(emitter.tag_directives, tag_copy)
  211. return true
  212. }
  213. // Increase the indentation level.
  214. func yaml_emitter_increase_indent_compact(emitter *yaml_emitter_t, flow, indentless bool, compact_seq bool) bool {
  215. emitter.indents = append(emitter.indents, emitter.indent)
  216. if emitter.indent < 0 {
  217. if flow {
  218. emitter.indent = emitter.best_indent
  219. } else {
  220. emitter.indent = 0
  221. }
  222. } else if !indentless {
  223. // [Go] This was changed so that indentations are more regular.
  224. if emitter.states[len(emitter.states)-1] == yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE {
  225. // The first indent inside a sequence will just skip the "- " indicator.
  226. emitter.indent += 2
  227. } else {
  228. // Everything else aligns to the chosen indentation.
  229. emitter.indent = emitter.best_indent * ((emitter.indent + emitter.best_indent) / emitter.best_indent)
  230. if compact_seq {
  231. // The value compact_seq passed in is almost always set to `false` when this function is called,
  232. // except when we are dealing with sequence nodes. So this gets triggered to subtract 2 only when we
  233. // are increasing the indent to account for sequence nodes, which will be correct because we need to
  234. // subtract 2 to account for the - at the beginning of the sequence node.
  235. emitter.indent = emitter.indent - 2
  236. }
  237. }
  238. }
  239. return true
  240. }
  241. // State dispatcher.
  242. func yaml_emitter_state_machine(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  243. switch emitter.state {
  244. default:
  245. case yaml_EMIT_STREAM_START_STATE:
  246. return yaml_emitter_emit_stream_start(emitter, event)
  247. case yaml_EMIT_FIRST_DOCUMENT_START_STATE:
  248. return yaml_emitter_emit_document_start(emitter, event, true)
  249. case yaml_EMIT_DOCUMENT_START_STATE:
  250. return yaml_emitter_emit_document_start(emitter, event, false)
  251. case yaml_EMIT_DOCUMENT_CONTENT_STATE:
  252. return yaml_emitter_emit_document_content(emitter, event)
  253. case yaml_EMIT_DOCUMENT_END_STATE:
  254. return yaml_emitter_emit_document_end(emitter, event)
  255. case yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE:
  256. return yaml_emitter_emit_flow_sequence_item(emitter, event, true, false)
  257. case yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE:
  258. return yaml_emitter_emit_flow_sequence_item(emitter, event, false, true)
  259. case yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE:
  260. return yaml_emitter_emit_flow_sequence_item(emitter, event, false, false)
  261. case yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE:
  262. return yaml_emitter_emit_flow_mapping_key(emitter, event, true, false)
  263. case yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE:
  264. return yaml_emitter_emit_flow_mapping_key(emitter, event, false, true)
  265. case yaml_EMIT_FLOW_MAPPING_KEY_STATE:
  266. return yaml_emitter_emit_flow_mapping_key(emitter, event, false, false)
  267. case yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE:
  268. return yaml_emitter_emit_flow_mapping_value(emitter, event, true)
  269. case yaml_EMIT_FLOW_MAPPING_VALUE_STATE:
  270. return yaml_emitter_emit_flow_mapping_value(emitter, event, false)
  271. case yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE:
  272. return yaml_emitter_emit_block_sequence_item(emitter, event, true)
  273. case yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE:
  274. return yaml_emitter_emit_block_sequence_item(emitter, event, false)
  275. case yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE:
  276. return yaml_emitter_emit_block_mapping_key(emitter, event, true)
  277. case yaml_EMIT_BLOCK_MAPPING_KEY_STATE:
  278. return yaml_emitter_emit_block_mapping_key(emitter, event, false)
  279. case yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE:
  280. return yaml_emitter_emit_block_mapping_value(emitter, event, true)
  281. case yaml_EMIT_BLOCK_MAPPING_VALUE_STATE:
  282. return yaml_emitter_emit_block_mapping_value(emitter, event, false)
  283. case yaml_EMIT_END_STATE:
  284. return yaml_emitter_set_emitter_error(emitter, "expected nothing after STREAM-END")
  285. }
  286. panic("invalid emitter state")
  287. }
  288. // Expect STREAM-START.
  289. func yaml_emitter_emit_stream_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  290. if event.typ != yaml_STREAM_START_EVENT {
  291. return yaml_emitter_set_emitter_error(emitter, "expected STREAM-START")
  292. }
  293. if emitter.encoding == yaml_ANY_ENCODING {
  294. emitter.encoding = event.encoding
  295. if emitter.encoding == yaml_ANY_ENCODING {
  296. emitter.encoding = yaml_UTF8_ENCODING
  297. }
  298. }
  299. if emitter.best_indent < 2 || emitter.best_indent > 9 {
  300. emitter.best_indent = 2
  301. }
  302. if emitter.best_width >= 0 && emitter.best_width <= emitter.best_indent*2 {
  303. emitter.best_width = 80
  304. }
  305. if emitter.best_width < 0 {
  306. emitter.best_width = 1<<31 - 1
  307. }
  308. if emitter.line_break == yaml_ANY_BREAK {
  309. emitter.line_break = yaml_LN_BREAK
  310. }
  311. emitter.indent = -1
  312. emitter.line = 0
  313. emitter.column = 0
  314. emitter.whitespace = true
  315. emitter.indention = true
  316. emitter.space_above = true
  317. emitter.foot_indent = -1
  318. if emitter.encoding != yaml_UTF8_ENCODING {
  319. if !yaml_emitter_write_bom(emitter) {
  320. return false
  321. }
  322. }
  323. emitter.state = yaml_EMIT_FIRST_DOCUMENT_START_STATE
  324. return true
  325. }
  326. // Expect DOCUMENT-START or STREAM-END.
  327. func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
  328. if event.typ == yaml_DOCUMENT_START_EVENT {
  329. if event.version_directive != nil {
  330. if !yaml_emitter_analyze_version_directive(emitter, event.version_directive) {
  331. return false
  332. }
  333. }
  334. for i := 0; i < len(event.tag_directives); i++ {
  335. tag_directive := &event.tag_directives[i]
  336. if !yaml_emitter_analyze_tag_directive(emitter, tag_directive) {
  337. return false
  338. }
  339. if !yaml_emitter_append_tag_directive(emitter, tag_directive, false) {
  340. return false
  341. }
  342. }
  343. for i := 0; i < len(default_tag_directives); i++ {
  344. tag_directive := &default_tag_directives[i]
  345. if !yaml_emitter_append_tag_directive(emitter, tag_directive, true) {
  346. return false
  347. }
  348. }
  349. implicit := event.implicit
  350. if !first || emitter.canonical {
  351. implicit = false
  352. }
  353. if emitter.open_ended && (event.version_directive != nil || len(event.tag_directives) > 0) {
  354. if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
  355. return false
  356. }
  357. if !yaml_emitter_write_indent(emitter) {
  358. return false
  359. }
  360. }
  361. if event.version_directive != nil {
  362. implicit = false
  363. if !yaml_emitter_write_indicator(emitter, []byte("%YAML"), true, false, false) {
  364. return false
  365. }
  366. if !yaml_emitter_write_indicator(emitter, []byte("1.1"), true, false, false) {
  367. return false
  368. }
  369. if !yaml_emitter_write_indent(emitter) {
  370. return false
  371. }
  372. }
  373. if len(event.tag_directives) > 0 {
  374. implicit = false
  375. for i := 0; i < len(event.tag_directives); i++ {
  376. tag_directive := &event.tag_directives[i]
  377. if !yaml_emitter_write_indicator(emitter, []byte("%TAG"), true, false, false) {
  378. return false
  379. }
  380. if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) {
  381. return false
  382. }
  383. if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) {
  384. return false
  385. }
  386. if !yaml_emitter_write_indent(emitter) {
  387. return false
  388. }
  389. }
  390. }
  391. if yaml_emitter_check_empty_document(emitter) {
  392. implicit = false
  393. }
  394. if !implicit {
  395. if !yaml_emitter_write_indent(emitter) {
  396. return false
  397. }
  398. if !yaml_emitter_write_indicator(emitter, []byte("---"), true, false, false) {
  399. return false
  400. }
  401. if emitter.canonical || true {
  402. if !yaml_emitter_write_indent(emitter) {
  403. return false
  404. }
  405. }
  406. }
  407. if len(emitter.head_comment) > 0 {
  408. if !yaml_emitter_process_head_comment(emitter) {
  409. return false
  410. }
  411. if !put_break(emitter) {
  412. return false
  413. }
  414. }
  415. emitter.state = yaml_EMIT_DOCUMENT_CONTENT_STATE
  416. return true
  417. }
  418. if event.typ == yaml_STREAM_END_EVENT {
  419. if emitter.open_ended {
  420. if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
  421. return false
  422. }
  423. if !yaml_emitter_write_indent(emitter) {
  424. return false
  425. }
  426. }
  427. if !yaml_emitter_flush(emitter) {
  428. return false
  429. }
  430. emitter.state = yaml_EMIT_END_STATE
  431. return true
  432. }
  433. return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-START or STREAM-END")
  434. }
  435. // yaml_emitter_increase_indent preserves the original signature and delegates to
  436. // yaml_emitter_increase_indent_compact without compact-sequence indentation
  437. func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool {
  438. return yaml_emitter_increase_indent_compact(emitter, flow, indentless, false)
  439. }
  440. // yaml_emitter_process_line_comment preserves the original signature and delegates to
  441. // yaml_emitter_process_line_comment_linebreak passing false for linebreak
  442. func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
  443. return yaml_emitter_process_line_comment_linebreak(emitter, false)
  444. }
  445. // Expect the root node.
  446. func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  447. emitter.states = append(emitter.states, yaml_EMIT_DOCUMENT_END_STATE)
  448. if !yaml_emitter_process_head_comment(emitter) {
  449. return false
  450. }
  451. if !yaml_emitter_emit_node(emitter, event, true, false, false, false) {
  452. return false
  453. }
  454. if !yaml_emitter_process_line_comment(emitter) {
  455. return false
  456. }
  457. if !yaml_emitter_process_foot_comment(emitter) {
  458. return false
  459. }
  460. return true
  461. }
  462. // Expect DOCUMENT-END.
  463. func yaml_emitter_emit_document_end(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  464. if event.typ != yaml_DOCUMENT_END_EVENT {
  465. return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-END")
  466. }
  467. // [Go] Force document foot separation.
  468. emitter.foot_indent = 0
  469. if !yaml_emitter_process_foot_comment(emitter) {
  470. return false
  471. }
  472. emitter.foot_indent = -1
  473. if !yaml_emitter_write_indent(emitter) {
  474. return false
  475. }
  476. if !event.implicit {
  477. // [Go] Allocate the slice elsewhere.
  478. if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
  479. return false
  480. }
  481. if !yaml_emitter_write_indent(emitter) {
  482. return false
  483. }
  484. }
  485. if !yaml_emitter_flush(emitter) {
  486. return false
  487. }
  488. emitter.state = yaml_EMIT_DOCUMENT_START_STATE
  489. emitter.tag_directives = emitter.tag_directives[:0]
  490. return true
  491. }
  492. // Expect a flow item node.
  493. func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first, trail bool) bool {
  494. if first {
  495. if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) {
  496. return false
  497. }
  498. if !yaml_emitter_increase_indent(emitter, true, false) {
  499. return false
  500. }
  501. emitter.flow_level++
  502. }
  503. if event.typ == yaml_SEQUENCE_END_EVENT {
  504. if emitter.canonical && !first && !trail {
  505. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  506. return false
  507. }
  508. }
  509. emitter.flow_level--
  510. emitter.indent = emitter.indents[len(emitter.indents)-1]
  511. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  512. if emitter.column == 0 || emitter.canonical && !first {
  513. if !yaml_emitter_write_indent(emitter) {
  514. return false
  515. }
  516. }
  517. if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) {
  518. return false
  519. }
  520. if !yaml_emitter_process_line_comment(emitter) {
  521. return false
  522. }
  523. if !yaml_emitter_process_foot_comment(emitter) {
  524. return false
  525. }
  526. emitter.state = emitter.states[len(emitter.states)-1]
  527. emitter.states = emitter.states[:len(emitter.states)-1]
  528. return true
  529. }
  530. if !first && !trail {
  531. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  532. return false
  533. }
  534. }
  535. if !yaml_emitter_process_head_comment(emitter) {
  536. return false
  537. }
  538. if emitter.column == 0 {
  539. if !yaml_emitter_write_indent(emitter) {
  540. return false
  541. }
  542. }
  543. if emitter.canonical || emitter.column > emitter.best_width {
  544. if !yaml_emitter_write_indent(emitter) {
  545. return false
  546. }
  547. }
  548. if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
  549. emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE)
  550. } else {
  551. emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE)
  552. }
  553. if !yaml_emitter_emit_node(emitter, event, false, true, false, false) {
  554. return false
  555. }
  556. if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
  557. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  558. return false
  559. }
  560. }
  561. if !yaml_emitter_process_line_comment(emitter) {
  562. return false
  563. }
  564. if !yaml_emitter_process_foot_comment(emitter) {
  565. return false
  566. }
  567. return true
  568. }
  569. // Expect a flow key node.
  570. func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first, trail bool) bool {
  571. if first {
  572. if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) {
  573. return false
  574. }
  575. if !yaml_emitter_increase_indent(emitter, true, false) {
  576. return false
  577. }
  578. emitter.flow_level++
  579. }
  580. if event.typ == yaml_MAPPING_END_EVENT {
  581. if (emitter.canonical || len(emitter.head_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0) && !first && !trail {
  582. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  583. return false
  584. }
  585. }
  586. if !yaml_emitter_process_head_comment(emitter) {
  587. return false
  588. }
  589. emitter.flow_level--
  590. emitter.indent = emitter.indents[len(emitter.indents)-1]
  591. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  592. if emitter.canonical && !first {
  593. if !yaml_emitter_write_indent(emitter) {
  594. return false
  595. }
  596. }
  597. if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) {
  598. return false
  599. }
  600. if !yaml_emitter_process_line_comment(emitter) {
  601. return false
  602. }
  603. if !yaml_emitter_process_foot_comment(emitter) {
  604. return false
  605. }
  606. emitter.state = emitter.states[len(emitter.states)-1]
  607. emitter.states = emitter.states[:len(emitter.states)-1]
  608. return true
  609. }
  610. if !first && !trail {
  611. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  612. return false
  613. }
  614. }
  615. if !yaml_emitter_process_head_comment(emitter) {
  616. return false
  617. }
  618. if emitter.column == 0 {
  619. if !yaml_emitter_write_indent(emitter) {
  620. return false
  621. }
  622. }
  623. if emitter.canonical || emitter.column > emitter.best_width {
  624. if !yaml_emitter_write_indent(emitter) {
  625. return false
  626. }
  627. }
  628. if !emitter.canonical && yaml_emitter_check_simple_key(emitter) {
  629. emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)
  630. return yaml_emitter_emit_node(emitter, event, false, false, true, true)
  631. }
  632. if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, false) {
  633. return false
  634. }
  635. emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_VALUE_STATE)
  636. return yaml_emitter_emit_node(emitter, event, false, false, true, false)
  637. }
  638. // Expect a flow value node.
  639. func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool {
  640. if simple {
  641. if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) {
  642. return false
  643. }
  644. } else {
  645. if emitter.canonical || emitter.column > emitter.best_width {
  646. if !yaml_emitter_write_indent(emitter) {
  647. return false
  648. }
  649. }
  650. if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, false) {
  651. return false
  652. }
  653. }
  654. if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
  655. emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE)
  656. } else {
  657. emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_KEY_STATE)
  658. }
  659. if !yaml_emitter_emit_node(emitter, event, false, false, true, false) {
  660. return false
  661. }
  662. if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
  663. if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
  664. return false
  665. }
  666. }
  667. if !yaml_emitter_process_line_comment(emitter) {
  668. return false
  669. }
  670. if !yaml_emitter_process_foot_comment(emitter) {
  671. return false
  672. }
  673. return true
  674. }
  675. // Expect a block item node.
  676. func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
  677. if first {
  678. // emitter.mapping context tells us if we are currently in a mapping context.
  679. // emiiter.column tells us which column we are in in the yaml output. 0 is the first char of the column.
  680. // emitter.indentation tells us if the last character was an indentation character.
  681. // emitter.compact_sequence_indent tells us if '- ' is considered part of the indentation for sequence elements.
  682. // So, `seq` means that we are in a mapping context, and we are either at the first char of the column or
  683. // the last character was not an indentation character, and we consider '- ' part of the indentation
  684. // for sequence elements.
  685. seq := emitter.mapping_context && (emitter.column == 0 || !emitter.indention) &&
  686. emitter.compact_sequence_indent
  687. if !yaml_emitter_increase_indent_compact(emitter, false, false, seq) {
  688. return false
  689. }
  690. }
  691. if event.typ == yaml_SEQUENCE_END_EVENT {
  692. emitter.indent = emitter.indents[len(emitter.indents)-1]
  693. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  694. emitter.state = emitter.states[len(emitter.states)-1]
  695. emitter.states = emitter.states[:len(emitter.states)-1]
  696. return true
  697. }
  698. if !yaml_emitter_process_head_comment(emitter) {
  699. return false
  700. }
  701. if !yaml_emitter_write_indent(emitter) {
  702. return false
  703. }
  704. if !yaml_emitter_write_indicator(emitter, []byte{'-'}, true, false, true) {
  705. return false
  706. }
  707. emitter.states = append(emitter.states, yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE)
  708. if !yaml_emitter_emit_node(emitter, event, false, true, false, false) {
  709. return false
  710. }
  711. if !yaml_emitter_process_line_comment(emitter) {
  712. return false
  713. }
  714. if !yaml_emitter_process_foot_comment(emitter) {
  715. return false
  716. }
  717. return true
  718. }
  719. // Expect a block key node.
  720. func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
  721. if first {
  722. if !yaml_emitter_increase_indent(emitter, false, false) {
  723. return false
  724. }
  725. }
  726. if !yaml_emitter_process_head_comment(emitter) {
  727. return false
  728. }
  729. if event.typ == yaml_MAPPING_END_EVENT {
  730. emitter.indent = emitter.indents[len(emitter.indents)-1]
  731. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  732. emitter.state = emitter.states[len(emitter.states)-1]
  733. emitter.states = emitter.states[:len(emitter.states)-1]
  734. return true
  735. }
  736. if !yaml_emitter_write_indent(emitter) {
  737. return false
  738. }
  739. if len(emitter.line_comment) > 0 {
  740. // [Go] A line comment was provided for the key. That's unusual as the
  741. // scanner associates line comments with the value. Either way,
  742. // save the line comment and render it appropriately later.
  743. emitter.key_line_comment = emitter.line_comment
  744. emitter.line_comment = nil
  745. }
  746. if yaml_emitter_check_simple_key(emitter) {
  747. emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE)
  748. return yaml_emitter_emit_node(emitter, event, false, false, true, true)
  749. }
  750. if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, true) {
  751. return false
  752. }
  753. emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_VALUE_STATE)
  754. return yaml_emitter_emit_node(emitter, event, false, false, true, false)
  755. }
  756. // Expect a block value node.
  757. func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool {
  758. if simple {
  759. if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) {
  760. return false
  761. }
  762. } else {
  763. if !yaml_emitter_write_indent(emitter) {
  764. return false
  765. }
  766. if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, true) {
  767. return false
  768. }
  769. }
  770. if len(emitter.key_line_comment) > 0 {
  771. // [Go] Line comments are generally associated with the value, but when there's
  772. // no value on the same line as a mapping key they end up attached to the
  773. // key itself.
  774. if event.typ == yaml_SCALAR_EVENT {
  775. if len(emitter.line_comment) == 0 {
  776. // A scalar is coming and it has no line comments by itself yet,
  777. // so just let it handle the line comment as usual. If it has a
  778. // line comment, we can't have both so the one from the key is lost.
  779. emitter.line_comment = emitter.key_line_comment
  780. emitter.key_line_comment = nil
  781. }
  782. } else if event.sequence_style() != yaml_FLOW_SEQUENCE_STYLE && (event.typ == yaml_MAPPING_START_EVENT || event.typ == yaml_SEQUENCE_START_EVENT) {
  783. // An indented block follows, so write the comment right now.
  784. emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
  785. if !yaml_emitter_process_line_comment(emitter) {
  786. return false
  787. }
  788. emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
  789. }
  790. }
  791. emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_KEY_STATE)
  792. if !yaml_emitter_emit_node(emitter, event, false, false, true, false) {
  793. return false
  794. }
  795. if !yaml_emitter_process_line_comment(emitter) {
  796. return false
  797. }
  798. if !yaml_emitter_process_foot_comment(emitter) {
  799. return false
  800. }
  801. return true
  802. }
  803. func yaml_emitter_silent_nil_event(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  804. return event.typ == yaml_SCALAR_EVENT && event.implicit && !emitter.canonical && len(emitter.scalar_data.value) == 0
  805. }
  806. // Expect a node.
  807. func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t,
  808. root bool, sequence bool, mapping bool, simple_key bool) bool {
  809. emitter.root_context = root
  810. emitter.sequence_context = sequence
  811. emitter.mapping_context = mapping
  812. emitter.simple_key_context = simple_key
  813. switch event.typ {
  814. case yaml_ALIAS_EVENT:
  815. return yaml_emitter_emit_alias(emitter, event)
  816. case yaml_SCALAR_EVENT:
  817. return yaml_emitter_emit_scalar(emitter, event)
  818. case yaml_SEQUENCE_START_EVENT:
  819. return yaml_emitter_emit_sequence_start(emitter, event)
  820. case yaml_MAPPING_START_EVENT:
  821. return yaml_emitter_emit_mapping_start(emitter, event)
  822. default:
  823. return yaml_emitter_set_emitter_error(emitter,
  824. fmt.Sprintf("expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS, but got %v", event.typ))
  825. }
  826. }
  827. // Expect ALIAS.
  828. func yaml_emitter_emit_alias(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  829. if !yaml_emitter_process_anchor(emitter) {
  830. return false
  831. }
  832. emitter.state = emitter.states[len(emitter.states)-1]
  833. emitter.states = emitter.states[:len(emitter.states)-1]
  834. return true
  835. }
  836. // Expect SCALAR.
  837. func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  838. if !yaml_emitter_select_scalar_style(emitter, event) {
  839. return false
  840. }
  841. if !yaml_emitter_process_anchor(emitter) {
  842. return false
  843. }
  844. if !yaml_emitter_process_tag(emitter) {
  845. return false
  846. }
  847. if !yaml_emitter_increase_indent(emitter, true, false) {
  848. return false
  849. }
  850. if !yaml_emitter_process_scalar(emitter) {
  851. return false
  852. }
  853. emitter.indent = emitter.indents[len(emitter.indents)-1]
  854. emitter.indents = emitter.indents[:len(emitter.indents)-1]
  855. emitter.state = emitter.states[len(emitter.states)-1]
  856. emitter.states = emitter.states[:len(emitter.states)-1]
  857. return true
  858. }
  859. // Expect SEQUENCE-START.
  860. func yaml_emitter_emit_sequence_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  861. if !yaml_emitter_process_anchor(emitter) {
  862. return false
  863. }
  864. if !yaml_emitter_process_tag(emitter) {
  865. return false
  866. }
  867. if emitter.flow_level > 0 || emitter.canonical || event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE ||
  868. yaml_emitter_check_empty_sequence(emitter) {
  869. emitter.state = yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE
  870. } else {
  871. emitter.state = yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE
  872. }
  873. return true
  874. }
  875. // Expect MAPPING-START.
  876. func yaml_emitter_emit_mapping_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  877. if !yaml_emitter_process_anchor(emitter) {
  878. return false
  879. }
  880. if !yaml_emitter_process_tag(emitter) {
  881. return false
  882. }
  883. if emitter.flow_level > 0 || emitter.canonical || event.mapping_style() == yaml_FLOW_MAPPING_STYLE ||
  884. yaml_emitter_check_empty_mapping(emitter) {
  885. emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE
  886. } else {
  887. emitter.state = yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE
  888. }
  889. return true
  890. }
  891. // Check if the document content is an empty scalar.
  892. func yaml_emitter_check_empty_document(emitter *yaml_emitter_t) bool {
  893. return false // [Go] Huh?
  894. }
  895. // Check if the next events represent an empty sequence.
  896. func yaml_emitter_check_empty_sequence(emitter *yaml_emitter_t) bool {
  897. if len(emitter.events)-emitter.events_head < 2 {
  898. return false
  899. }
  900. return emitter.events[emitter.events_head].typ == yaml_SEQUENCE_START_EVENT &&
  901. emitter.events[emitter.events_head+1].typ == yaml_SEQUENCE_END_EVENT
  902. }
  903. // Check if the next events represent an empty mapping.
  904. func yaml_emitter_check_empty_mapping(emitter *yaml_emitter_t) bool {
  905. if len(emitter.events)-emitter.events_head < 2 {
  906. return false
  907. }
  908. return emitter.events[emitter.events_head].typ == yaml_MAPPING_START_EVENT &&
  909. emitter.events[emitter.events_head+1].typ == yaml_MAPPING_END_EVENT
  910. }
  911. // Check if the next node can be expressed as a simple key.
  912. func yaml_emitter_check_simple_key(emitter *yaml_emitter_t) bool {
  913. length := 0
  914. switch emitter.events[emitter.events_head].typ {
  915. case yaml_ALIAS_EVENT:
  916. length += len(emitter.anchor_data.anchor)
  917. case yaml_SCALAR_EVENT:
  918. if emitter.scalar_data.multiline {
  919. return false
  920. }
  921. length += len(emitter.anchor_data.anchor) +
  922. len(emitter.tag_data.handle) +
  923. len(emitter.tag_data.suffix) +
  924. len(emitter.scalar_data.value)
  925. case yaml_SEQUENCE_START_EVENT:
  926. if !yaml_emitter_check_empty_sequence(emitter) {
  927. return false
  928. }
  929. length += len(emitter.anchor_data.anchor) +
  930. len(emitter.tag_data.handle) +
  931. len(emitter.tag_data.suffix)
  932. case yaml_MAPPING_START_EVENT:
  933. if !yaml_emitter_check_empty_mapping(emitter) {
  934. return false
  935. }
  936. length += len(emitter.anchor_data.anchor) +
  937. len(emitter.tag_data.handle) +
  938. len(emitter.tag_data.suffix)
  939. default:
  940. return false
  941. }
  942. return length <= 128
  943. }
  944. // Determine an acceptable scalar style.
  945. func yaml_emitter_select_scalar_style(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  946. no_tag := len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0
  947. if no_tag && !event.implicit && !event.quoted_implicit {
  948. return yaml_emitter_set_emitter_error(emitter, "neither tag nor implicit flags are specified")
  949. }
  950. style := event.scalar_style()
  951. if style == yaml_ANY_SCALAR_STYLE {
  952. style = yaml_PLAIN_SCALAR_STYLE
  953. }
  954. if emitter.canonical {
  955. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  956. }
  957. if emitter.simple_key_context && emitter.scalar_data.multiline {
  958. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  959. }
  960. if style == yaml_PLAIN_SCALAR_STYLE {
  961. if emitter.flow_level > 0 && !emitter.scalar_data.flow_plain_allowed ||
  962. emitter.flow_level == 0 && !emitter.scalar_data.block_plain_allowed {
  963. style = yaml_SINGLE_QUOTED_SCALAR_STYLE
  964. }
  965. if len(emitter.scalar_data.value) == 0 && (emitter.flow_level > 0 || emitter.simple_key_context) {
  966. style = yaml_SINGLE_QUOTED_SCALAR_STYLE
  967. }
  968. if no_tag && !event.implicit {
  969. style = yaml_SINGLE_QUOTED_SCALAR_STYLE
  970. }
  971. }
  972. if style == yaml_SINGLE_QUOTED_SCALAR_STYLE {
  973. if !emitter.scalar_data.single_quoted_allowed {
  974. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  975. }
  976. }
  977. if style == yaml_LITERAL_SCALAR_STYLE || style == yaml_FOLDED_SCALAR_STYLE {
  978. if !emitter.scalar_data.block_allowed || emitter.flow_level > 0 || emitter.simple_key_context {
  979. style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
  980. }
  981. }
  982. if no_tag && !event.quoted_implicit && style != yaml_PLAIN_SCALAR_STYLE {
  983. emitter.tag_data.handle = []byte{'!'}
  984. }
  985. emitter.scalar_data.style = style
  986. return true
  987. }
  988. // Write an anchor.
  989. func yaml_emitter_process_anchor(emitter *yaml_emitter_t) bool {
  990. if emitter.anchor_data.anchor == nil {
  991. return true
  992. }
  993. c := []byte{'&'}
  994. if emitter.anchor_data.alias {
  995. c[0] = '*'
  996. }
  997. if !yaml_emitter_write_indicator(emitter, c, true, false, false) {
  998. return false
  999. }
  1000. return yaml_emitter_write_anchor(emitter, emitter.anchor_data.anchor)
  1001. }
  1002. // Write a tag.
  1003. func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool {
  1004. if len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0 {
  1005. return true
  1006. }
  1007. if len(emitter.tag_data.handle) > 0 {
  1008. if !yaml_emitter_write_tag_handle(emitter, emitter.tag_data.handle) {
  1009. return false
  1010. }
  1011. if len(emitter.tag_data.suffix) > 0 {
  1012. if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
  1013. return false
  1014. }
  1015. }
  1016. } else {
  1017. // [Go] Allocate these slices elsewhere.
  1018. if !yaml_emitter_write_indicator(emitter, []byte("!<"), true, false, false) {
  1019. return false
  1020. }
  1021. if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
  1022. return false
  1023. }
  1024. if !yaml_emitter_write_indicator(emitter, []byte{'>'}, false, false, false) {
  1025. return false
  1026. }
  1027. }
  1028. return true
  1029. }
  1030. // Write a scalar.
  1031. func yaml_emitter_process_scalar(emitter *yaml_emitter_t) bool {
  1032. switch emitter.scalar_data.style {
  1033. case yaml_PLAIN_SCALAR_STYLE:
  1034. return yaml_emitter_write_plain_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
  1035. case yaml_SINGLE_QUOTED_SCALAR_STYLE:
  1036. return yaml_emitter_write_single_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
  1037. case yaml_DOUBLE_QUOTED_SCALAR_STYLE:
  1038. return yaml_emitter_write_double_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
  1039. case yaml_LITERAL_SCALAR_STYLE:
  1040. return yaml_emitter_write_literal_scalar(emitter, emitter.scalar_data.value)
  1041. case yaml_FOLDED_SCALAR_STYLE:
  1042. return yaml_emitter_write_folded_scalar(emitter, emitter.scalar_data.value)
  1043. }
  1044. panic("unknown scalar style")
  1045. }
  1046. // Write a head comment.
  1047. func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool {
  1048. if len(emitter.tail_comment) > 0 {
  1049. if !yaml_emitter_write_indent(emitter) {
  1050. return false
  1051. }
  1052. if !yaml_emitter_write_comment(emitter, emitter.tail_comment) {
  1053. return false
  1054. }
  1055. emitter.tail_comment = emitter.tail_comment[:0]
  1056. emitter.foot_indent = emitter.indent
  1057. if emitter.foot_indent < 0 {
  1058. emitter.foot_indent = 0
  1059. }
  1060. }
  1061. if len(emitter.head_comment) == 0 {
  1062. return true
  1063. }
  1064. if !yaml_emitter_write_indent(emitter) {
  1065. return false
  1066. }
  1067. if !yaml_emitter_write_comment(emitter, emitter.head_comment) {
  1068. return false
  1069. }
  1070. emitter.head_comment = emitter.head_comment[:0]
  1071. return true
  1072. }
  1073. // Write an line comment.
  1074. func yaml_emitter_process_line_comment_linebreak(emitter *yaml_emitter_t, linebreak bool) bool {
  1075. if len(emitter.line_comment) == 0 {
  1076. // The next 3 lines are needed to resolve an issue with leading newlines
  1077. // See https://github.com/go-yaml/yaml/issues/755
  1078. // When linebreak is set to true, put_break will be called and will add
  1079. // the needed newline.
  1080. if linebreak && !put_break(emitter) {
  1081. return false
  1082. }
  1083. return true
  1084. }
  1085. if !emitter.whitespace {
  1086. if !put(emitter, ' ') {
  1087. return false
  1088. }
  1089. }
  1090. if !yaml_emitter_write_comment(emitter, emitter.line_comment) {
  1091. return false
  1092. }
  1093. emitter.line_comment = emitter.line_comment[:0]
  1094. return true
  1095. }
  1096. // Write a foot comment.
  1097. func yaml_emitter_process_foot_comment(emitter *yaml_emitter_t) bool {
  1098. if len(emitter.foot_comment) == 0 {
  1099. return true
  1100. }
  1101. if !yaml_emitter_write_indent(emitter) {
  1102. return false
  1103. }
  1104. if !yaml_emitter_write_comment(emitter, emitter.foot_comment) {
  1105. return false
  1106. }
  1107. emitter.foot_comment = emitter.foot_comment[:0]
  1108. emitter.foot_indent = emitter.indent
  1109. if emitter.foot_indent < 0 {
  1110. emitter.foot_indent = 0
  1111. }
  1112. return true
  1113. }
  1114. // Check if a %YAML directive is valid.
  1115. func yaml_emitter_analyze_version_directive(emitter *yaml_emitter_t, version_directive *yaml_version_directive_t) bool {
  1116. if version_directive.major != 1 || version_directive.minor != 1 {
  1117. return yaml_emitter_set_emitter_error(emitter, "incompatible %YAML directive")
  1118. }
  1119. return true
  1120. }
  1121. // Check if a %TAG directive is valid.
  1122. func yaml_emitter_analyze_tag_directive(emitter *yaml_emitter_t, tag_directive *yaml_tag_directive_t) bool {
  1123. handle := tag_directive.handle
  1124. prefix := tag_directive.prefix
  1125. if len(handle) == 0 {
  1126. return yaml_emitter_set_emitter_error(emitter, "tag handle must not be empty")
  1127. }
  1128. if handle[0] != '!' {
  1129. return yaml_emitter_set_emitter_error(emitter, "tag handle must start with '!'")
  1130. }
  1131. if handle[len(handle)-1] != '!' {
  1132. return yaml_emitter_set_emitter_error(emitter, "tag handle must end with '!'")
  1133. }
  1134. for i := 1; i < len(handle)-1; i += width(handle[i]) {
  1135. if !is_alpha(handle, i) {
  1136. return yaml_emitter_set_emitter_error(emitter, "tag handle must contain alphanumerical characters only")
  1137. }
  1138. }
  1139. if len(prefix) == 0 {
  1140. return yaml_emitter_set_emitter_error(emitter, "tag prefix must not be empty")
  1141. }
  1142. return true
  1143. }
  1144. // Check if an anchor is valid.
  1145. func yaml_emitter_analyze_anchor(emitter *yaml_emitter_t, anchor []byte, alias bool) bool {
  1146. if len(anchor) == 0 {
  1147. problem := "anchor value must not be empty"
  1148. if alias {
  1149. problem = "alias value must not be empty"
  1150. }
  1151. return yaml_emitter_set_emitter_error(emitter, problem)
  1152. }
  1153. for i := 0; i < len(anchor); i += width(anchor[i]) {
  1154. if !is_alpha(anchor, i) {
  1155. problem := "anchor value must contain alphanumerical characters only"
  1156. if alias {
  1157. problem = "alias value must contain alphanumerical characters only"
  1158. }
  1159. return yaml_emitter_set_emitter_error(emitter, problem)
  1160. }
  1161. }
  1162. emitter.anchor_data.anchor = anchor
  1163. emitter.anchor_data.alias = alias
  1164. return true
  1165. }
  1166. // Check if a tag is valid.
  1167. func yaml_emitter_analyze_tag(emitter *yaml_emitter_t, tag []byte) bool {
  1168. if len(tag) == 0 {
  1169. return yaml_emitter_set_emitter_error(emitter, "tag value must not be empty")
  1170. }
  1171. for i := 0; i < len(emitter.tag_directives); i++ {
  1172. tag_directive := &emitter.tag_directives[i]
  1173. if bytes.HasPrefix(tag, tag_directive.prefix) {
  1174. emitter.tag_data.handle = tag_directive.handle
  1175. emitter.tag_data.suffix = tag[len(tag_directive.prefix):]
  1176. return true
  1177. }
  1178. }
  1179. emitter.tag_data.suffix = tag
  1180. return true
  1181. }
  1182. // Check if a scalar is valid.
  1183. func yaml_emitter_analyze_scalar(emitter *yaml_emitter_t, value []byte) bool {
  1184. var (
  1185. block_indicators = false
  1186. flow_indicators = false
  1187. line_breaks = false
  1188. special_characters = false
  1189. tab_characters = false
  1190. leading_space = false
  1191. leading_break = false
  1192. trailing_space = false
  1193. trailing_break = false
  1194. break_space = false
  1195. space_break = false
  1196. preceded_by_whitespace = false
  1197. followed_by_whitespace = false
  1198. previous_space = false
  1199. previous_break = false
  1200. )
  1201. emitter.scalar_data.value = value
  1202. if len(value) == 0 {
  1203. emitter.scalar_data.multiline = false
  1204. emitter.scalar_data.flow_plain_allowed = false
  1205. emitter.scalar_data.block_plain_allowed = true
  1206. emitter.scalar_data.single_quoted_allowed = true
  1207. emitter.scalar_data.block_allowed = false
  1208. return true
  1209. }
  1210. if len(value) >= 3 && ((value[0] == '-' && value[1] == '-' && value[2] == '-') || (value[0] == '.' && value[1] == '.' && value[2] == '.')) {
  1211. block_indicators = true
  1212. flow_indicators = true
  1213. }
  1214. preceded_by_whitespace = true
  1215. for i, w := 0, 0; i < len(value); i += w {
  1216. w = width(value[i])
  1217. followed_by_whitespace = i+w >= len(value) || is_blank(value, i+w)
  1218. if i == 0 {
  1219. switch value[i] {
  1220. case '#', ',', '[', ']', '{', '}', '&', '*', '!', '|', '>', '\'', '"', '%', '@', '`':
  1221. flow_indicators = true
  1222. block_indicators = true
  1223. case '?', ':':
  1224. flow_indicators = true
  1225. if followed_by_whitespace {
  1226. block_indicators = true
  1227. }
  1228. case '-':
  1229. if followed_by_whitespace {
  1230. flow_indicators = true
  1231. block_indicators = true
  1232. }
  1233. }
  1234. } else {
  1235. switch value[i] {
  1236. case ',', '?', '[', ']', '{', '}':
  1237. flow_indicators = true
  1238. case ':':
  1239. flow_indicators = true
  1240. if followed_by_whitespace {
  1241. block_indicators = true
  1242. }
  1243. case '#':
  1244. if preceded_by_whitespace {
  1245. flow_indicators = true
  1246. block_indicators = true
  1247. }
  1248. }
  1249. }
  1250. if value[i] == '\t' {
  1251. tab_characters = true
  1252. } else if !is_printable(value, i) || !is_ascii(value, i) && !emitter.unicode {
  1253. special_characters = true
  1254. }
  1255. if is_space(value, i) {
  1256. if i == 0 {
  1257. leading_space = true
  1258. }
  1259. if i+width(value[i]) == len(value) {
  1260. trailing_space = true
  1261. }
  1262. if previous_break {
  1263. break_space = true
  1264. }
  1265. previous_space = true
  1266. previous_break = false
  1267. } else if is_break(value, i) {
  1268. line_breaks = true
  1269. if i == 0 {
  1270. leading_break = true
  1271. }
  1272. if i+width(value[i]) == len(value) {
  1273. trailing_break = true
  1274. }
  1275. if previous_space {
  1276. space_break = true
  1277. }
  1278. previous_space = false
  1279. previous_break = true
  1280. } else {
  1281. previous_space = false
  1282. previous_break = false
  1283. }
  1284. // [Go]: Why 'z'? Couldn't be the end of the string as that's the loop condition.
  1285. preceded_by_whitespace = is_blankz(value, i)
  1286. }
  1287. emitter.scalar_data.multiline = line_breaks
  1288. emitter.scalar_data.flow_plain_allowed = true
  1289. emitter.scalar_data.block_plain_allowed = true
  1290. emitter.scalar_data.single_quoted_allowed = true
  1291. emitter.scalar_data.block_allowed = true
  1292. if leading_space || leading_break || trailing_space || trailing_break {
  1293. emitter.scalar_data.flow_plain_allowed = false
  1294. emitter.scalar_data.block_plain_allowed = false
  1295. }
  1296. if trailing_space {
  1297. emitter.scalar_data.block_allowed = false
  1298. }
  1299. if break_space {
  1300. emitter.scalar_data.flow_plain_allowed = false
  1301. emitter.scalar_data.block_plain_allowed = false
  1302. emitter.scalar_data.single_quoted_allowed = false
  1303. }
  1304. if space_break || tab_characters || special_characters {
  1305. emitter.scalar_data.flow_plain_allowed = false
  1306. emitter.scalar_data.block_plain_allowed = false
  1307. emitter.scalar_data.single_quoted_allowed = false
  1308. }
  1309. if space_break || special_characters {
  1310. emitter.scalar_data.block_allowed = false
  1311. }
  1312. if line_breaks {
  1313. emitter.scalar_data.flow_plain_allowed = false
  1314. emitter.scalar_data.block_plain_allowed = false
  1315. }
  1316. if flow_indicators {
  1317. emitter.scalar_data.flow_plain_allowed = false
  1318. }
  1319. if block_indicators {
  1320. emitter.scalar_data.block_plain_allowed = false
  1321. }
  1322. return true
  1323. }
  1324. // Check if the event data is valid.
  1325. func yaml_emitter_analyze_event(emitter *yaml_emitter_t, event *yaml_event_t) bool {
  1326. emitter.anchor_data.anchor = nil
  1327. emitter.tag_data.handle = nil
  1328. emitter.tag_data.suffix = nil
  1329. emitter.scalar_data.value = nil
  1330. if len(event.head_comment) > 0 {
  1331. emitter.head_comment = event.head_comment
  1332. }
  1333. if len(event.line_comment) > 0 {
  1334. emitter.line_comment = event.line_comment
  1335. }
  1336. if len(event.foot_comment) > 0 {
  1337. emitter.foot_comment = event.foot_comment
  1338. }
  1339. if len(event.tail_comment) > 0 {
  1340. emitter.tail_comment = event.tail_comment
  1341. }
  1342. switch event.typ {
  1343. case yaml_ALIAS_EVENT:
  1344. if !yaml_emitter_analyze_anchor(emitter, event.anchor, true) {
  1345. return false
  1346. }
  1347. case yaml_SCALAR_EVENT:
  1348. if len(event.anchor) > 0 {
  1349. if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
  1350. return false
  1351. }
  1352. }
  1353. if len(event.tag) > 0 && (emitter.canonical || (!event.implicit && !event.quoted_implicit)) {
  1354. if !yaml_emitter_analyze_tag(emitter, event.tag) {
  1355. return false
  1356. }
  1357. }
  1358. if !yaml_emitter_analyze_scalar(emitter, event.value) {
  1359. return false
  1360. }
  1361. case yaml_SEQUENCE_START_EVENT:
  1362. if len(event.anchor) > 0 {
  1363. if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
  1364. return false
  1365. }
  1366. }
  1367. if len(event.tag) > 0 && (emitter.canonical || !event.implicit) {
  1368. if !yaml_emitter_analyze_tag(emitter, event.tag) {
  1369. return false
  1370. }
  1371. }
  1372. case yaml_MAPPING_START_EVENT:
  1373. if len(event.anchor) > 0 {
  1374. if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
  1375. return false
  1376. }
  1377. }
  1378. if len(event.tag) > 0 && (emitter.canonical || !event.implicit) {
  1379. if !yaml_emitter_analyze_tag(emitter, event.tag) {
  1380. return false
  1381. }
  1382. }
  1383. }
  1384. return true
  1385. }
  1386. // Write the BOM character.
  1387. func yaml_emitter_write_bom(emitter *yaml_emitter_t) bool {
  1388. if !flush(emitter) {
  1389. return false
  1390. }
  1391. pos := emitter.buffer_pos
  1392. emitter.buffer[pos+0] = '\xEF'
  1393. emitter.buffer[pos+1] = '\xBB'
  1394. emitter.buffer[pos+2] = '\xBF'
  1395. emitter.buffer_pos += 3
  1396. return true
  1397. }
  1398. func yaml_emitter_write_indent(emitter *yaml_emitter_t) bool {
  1399. indent := emitter.indent
  1400. if indent < 0 {
  1401. indent = 0
  1402. }
  1403. if !emitter.indention || emitter.column > indent || (emitter.column == indent && !emitter.whitespace) {
  1404. if !put_break(emitter) {
  1405. return false
  1406. }
  1407. }
  1408. if emitter.foot_indent == indent {
  1409. if !put_break(emitter) {
  1410. return false
  1411. }
  1412. }
  1413. for emitter.column < indent {
  1414. if !put(emitter, ' ') {
  1415. return false
  1416. }
  1417. }
  1418. emitter.whitespace = true
  1419. //emitter.indention = true
  1420. emitter.space_above = false
  1421. emitter.foot_indent = -1
  1422. return true
  1423. }
  1424. func yaml_emitter_write_indicator(emitter *yaml_emitter_t, indicator []byte, need_whitespace, is_whitespace, is_indention bool) bool {
  1425. if need_whitespace && !emitter.whitespace {
  1426. if !put(emitter, ' ') {
  1427. return false
  1428. }
  1429. }
  1430. if !write_all(emitter, indicator) {
  1431. return false
  1432. }
  1433. emitter.whitespace = is_whitespace
  1434. emitter.indention = (emitter.indention && is_indention)
  1435. emitter.open_ended = false
  1436. return true
  1437. }
  1438. func yaml_emitter_write_anchor(emitter *yaml_emitter_t, value []byte) bool {
  1439. if !write_all(emitter, value) {
  1440. return false
  1441. }
  1442. emitter.whitespace = false
  1443. emitter.indention = false
  1444. return true
  1445. }
  1446. func yaml_emitter_write_tag_handle(emitter *yaml_emitter_t, value []byte) bool {
  1447. if !emitter.whitespace {
  1448. if !put(emitter, ' ') {
  1449. return false
  1450. }
  1451. }
  1452. if !write_all(emitter, value) {
  1453. return false
  1454. }
  1455. emitter.whitespace = false
  1456. emitter.indention = false
  1457. return true
  1458. }
  1459. func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_whitespace bool) bool {
  1460. if need_whitespace && !emitter.whitespace {
  1461. if !put(emitter, ' ') {
  1462. return false
  1463. }
  1464. }
  1465. for i := 0; i < len(value); {
  1466. var must_write bool
  1467. switch value[i] {
  1468. case ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')', '[', ']':
  1469. must_write = true
  1470. default:
  1471. must_write = is_alpha(value, i)
  1472. }
  1473. if must_write {
  1474. if !write(emitter, value, &i) {
  1475. return false
  1476. }
  1477. } else {
  1478. w := width(value[i])
  1479. for k := 0; k < w; k++ {
  1480. octet := value[i]
  1481. i++
  1482. if !put(emitter, '%') {
  1483. return false
  1484. }
  1485. c := octet >> 4
  1486. if c < 10 {
  1487. c += '0'
  1488. } else {
  1489. c += 'A' - 10
  1490. }
  1491. if !put(emitter, c) {
  1492. return false
  1493. }
  1494. c = octet & 0x0f
  1495. if c < 10 {
  1496. c += '0'
  1497. } else {
  1498. c += 'A' - 10
  1499. }
  1500. if !put(emitter, c) {
  1501. return false
  1502. }
  1503. }
  1504. }
  1505. }
  1506. emitter.whitespace = false
  1507. emitter.indention = false
  1508. return true
  1509. }
  1510. func yaml_emitter_write_plain_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
  1511. if len(value) > 0 && !emitter.whitespace {
  1512. if !put(emitter, ' ') {
  1513. return false
  1514. }
  1515. }
  1516. spaces := false
  1517. breaks := false
  1518. for i := 0; i < len(value); {
  1519. if is_space(value, i) {
  1520. if allow_breaks && !spaces && emitter.column > emitter.best_width && !is_space(value, i+1) {
  1521. if !yaml_emitter_write_indent(emitter) {
  1522. return false
  1523. }
  1524. i += width(value[i])
  1525. } else {
  1526. if !write(emitter, value, &i) {
  1527. return false
  1528. }
  1529. }
  1530. spaces = true
  1531. } else if is_break(value, i) {
  1532. if !breaks && value[i] == '\n' {
  1533. if !put_break(emitter) {
  1534. return false
  1535. }
  1536. }
  1537. if !write_break(emitter, value, &i) {
  1538. return false
  1539. }
  1540. //emitter.indention = true
  1541. breaks = true
  1542. } else {
  1543. if breaks {
  1544. if !yaml_emitter_write_indent(emitter) {
  1545. return false
  1546. }
  1547. }
  1548. if !write(emitter, value, &i) {
  1549. return false
  1550. }
  1551. emitter.indention = false
  1552. spaces = false
  1553. breaks = false
  1554. }
  1555. }
  1556. if len(value) > 0 {
  1557. emitter.whitespace = false
  1558. }
  1559. emitter.indention = false
  1560. if emitter.root_context {
  1561. emitter.open_ended = true
  1562. }
  1563. return true
  1564. }
  1565. func yaml_emitter_write_single_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
  1566. if !yaml_emitter_write_indicator(emitter, []byte{'\''}, true, false, false) {
  1567. return false
  1568. }
  1569. spaces := false
  1570. breaks := false
  1571. for i := 0; i < len(value); {
  1572. if is_space(value, i) {
  1573. if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 && !is_space(value, i+1) {
  1574. if !yaml_emitter_write_indent(emitter) {
  1575. return false
  1576. }
  1577. i += width(value[i])
  1578. } else {
  1579. if !write(emitter, value, &i) {
  1580. return false
  1581. }
  1582. }
  1583. spaces = true
  1584. } else if is_break(value, i) {
  1585. if !breaks && value[i] == '\n' {
  1586. if !put_break(emitter) {
  1587. return false
  1588. }
  1589. }
  1590. if !write_break(emitter, value, &i) {
  1591. return false
  1592. }
  1593. //emitter.indention = true
  1594. breaks = true
  1595. } else {
  1596. if breaks {
  1597. if !yaml_emitter_write_indent(emitter) {
  1598. return false
  1599. }
  1600. }
  1601. if value[i] == '\'' {
  1602. if !put(emitter, '\'') {
  1603. return false
  1604. }
  1605. }
  1606. if !write(emitter, value, &i) {
  1607. return false
  1608. }
  1609. emitter.indention = false
  1610. spaces = false
  1611. breaks = false
  1612. }
  1613. }
  1614. if !yaml_emitter_write_indicator(emitter, []byte{'\''}, false, false, false) {
  1615. return false
  1616. }
  1617. emitter.whitespace = false
  1618. emitter.indention = false
  1619. return true
  1620. }
  1621. func yaml_emitter_write_double_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
  1622. spaces := false
  1623. if !yaml_emitter_write_indicator(emitter, []byte{'"'}, true, false, false) {
  1624. return false
  1625. }
  1626. for i := 0; i < len(value); {
  1627. if !is_printable(value, i) || (!emitter.unicode && !is_ascii(value, i)) ||
  1628. is_bom(value, i) || is_break(value, i) ||
  1629. value[i] == '"' || value[i] == '\\' {
  1630. octet := value[i]
  1631. var w int
  1632. var v rune
  1633. switch {
  1634. case octet&0x80 == 0x00:
  1635. w, v = 1, rune(octet&0x7F)
  1636. case octet&0xE0 == 0xC0:
  1637. w, v = 2, rune(octet&0x1F)
  1638. case octet&0xF0 == 0xE0:
  1639. w, v = 3, rune(octet&0x0F)
  1640. case octet&0xF8 == 0xF0:
  1641. w, v = 4, rune(octet&0x07)
  1642. }
  1643. for k := 1; k < w; k++ {
  1644. octet = value[i+k]
  1645. v = (v << 6) + (rune(octet) & 0x3F)
  1646. }
  1647. i += w
  1648. if !put(emitter, '\\') {
  1649. return false
  1650. }
  1651. var ok bool
  1652. switch v {
  1653. case 0x00:
  1654. ok = put(emitter, '0')
  1655. case 0x07:
  1656. ok = put(emitter, 'a')
  1657. case 0x08:
  1658. ok = put(emitter, 'b')
  1659. case 0x09:
  1660. ok = put(emitter, 't')
  1661. case 0x0A:
  1662. ok = put(emitter, 'n')
  1663. case 0x0b:
  1664. ok = put(emitter, 'v')
  1665. case 0x0c:
  1666. ok = put(emitter, 'f')
  1667. case 0x0d:
  1668. ok = put(emitter, 'r')
  1669. case 0x1b:
  1670. ok = put(emitter, 'e')
  1671. case 0x22:
  1672. ok = put(emitter, '"')
  1673. case 0x5c:
  1674. ok = put(emitter, '\\')
  1675. case 0x85:
  1676. ok = put(emitter, 'N')
  1677. case 0xA0:
  1678. ok = put(emitter, '_')
  1679. case 0x2028:
  1680. ok = put(emitter, 'L')
  1681. case 0x2029:
  1682. ok = put(emitter, 'P')
  1683. default:
  1684. if v <= 0xFF {
  1685. ok = put(emitter, 'x')
  1686. w = 2
  1687. } else if v <= 0xFFFF {
  1688. ok = put(emitter, 'u')
  1689. w = 4
  1690. } else {
  1691. ok = put(emitter, 'U')
  1692. w = 8
  1693. }
  1694. for k := (w - 1) * 4; ok && k >= 0; k -= 4 {
  1695. digit := byte((v >> uint(k)) & 0x0F)
  1696. if digit < 10 {
  1697. ok = put(emitter, digit+'0')
  1698. } else {
  1699. ok = put(emitter, digit+'A'-10)
  1700. }
  1701. }
  1702. }
  1703. if !ok {
  1704. return false
  1705. }
  1706. spaces = false
  1707. } else if is_space(value, i) {
  1708. if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 {
  1709. if !yaml_emitter_write_indent(emitter) {
  1710. return false
  1711. }
  1712. if is_space(value, i+1) {
  1713. if !put(emitter, '\\') {
  1714. return false
  1715. }
  1716. }
  1717. i += width(value[i])
  1718. } else if !write(emitter, value, &i) {
  1719. return false
  1720. }
  1721. spaces = true
  1722. } else {
  1723. if !write(emitter, value, &i) {
  1724. return false
  1725. }
  1726. spaces = false
  1727. }
  1728. }
  1729. if !yaml_emitter_write_indicator(emitter, []byte{'"'}, false, false, false) {
  1730. return false
  1731. }
  1732. emitter.whitespace = false
  1733. emitter.indention = false
  1734. return true
  1735. }
  1736. func yaml_emitter_write_block_scalar_hints(emitter *yaml_emitter_t, value []byte) bool {
  1737. if is_space(value, 0) || is_break(value, 0) {
  1738. indent_hint := []byte{'0' + byte(emitter.best_indent)}
  1739. if !yaml_emitter_write_indicator(emitter, indent_hint, false, false, false) {
  1740. return false
  1741. }
  1742. }
  1743. emitter.open_ended = false
  1744. var chomp_hint [1]byte
  1745. if len(value) == 0 {
  1746. chomp_hint[0] = '-'
  1747. } else {
  1748. i := len(value) - 1
  1749. for value[i]&0xC0 == 0x80 {
  1750. i--
  1751. }
  1752. if !is_break(value, i) {
  1753. chomp_hint[0] = '-'
  1754. } else if i == 0 {
  1755. chomp_hint[0] = '+'
  1756. emitter.open_ended = true
  1757. } else {
  1758. i--
  1759. for value[i]&0xC0 == 0x80 {
  1760. i--
  1761. }
  1762. if is_break(value, i) {
  1763. chomp_hint[0] = '+'
  1764. emitter.open_ended = true
  1765. }
  1766. }
  1767. }
  1768. if chomp_hint[0] != 0 {
  1769. if !yaml_emitter_write_indicator(emitter, chomp_hint[:], false, false, false) {
  1770. return false
  1771. }
  1772. }
  1773. return true
  1774. }
  1775. func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bool {
  1776. if !yaml_emitter_write_indicator(emitter, []byte{'|'}, true, false, false) {
  1777. return false
  1778. }
  1779. if !yaml_emitter_write_block_scalar_hints(emitter, value) {
  1780. return false
  1781. }
  1782. if !yaml_emitter_process_line_comment_linebreak(emitter, true) {
  1783. return false
  1784. }
  1785. //emitter.indention = true
  1786. emitter.whitespace = true
  1787. breaks := true
  1788. for i := 0; i < len(value); {
  1789. if is_break(value, i) {
  1790. if !write_break(emitter, value, &i) {
  1791. return false
  1792. }
  1793. //emitter.indention = true
  1794. breaks = true
  1795. } else {
  1796. if breaks {
  1797. if !yaml_emitter_write_indent(emitter) {
  1798. return false
  1799. }
  1800. }
  1801. if !write(emitter, value, &i) {
  1802. return false
  1803. }
  1804. emitter.indention = false
  1805. breaks = false
  1806. }
  1807. }
  1808. return true
  1809. }
  1810. func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) bool {
  1811. if !yaml_emitter_write_indicator(emitter, []byte{'>'}, true, false, false) {
  1812. return false
  1813. }
  1814. if !yaml_emitter_write_block_scalar_hints(emitter, value) {
  1815. return false
  1816. }
  1817. if !yaml_emitter_process_line_comment_linebreak(emitter, true) {
  1818. return false
  1819. }
  1820. //emitter.indention = true
  1821. emitter.whitespace = true
  1822. breaks := true
  1823. leading_spaces := true
  1824. for i := 0; i < len(value); {
  1825. if is_break(value, i) {
  1826. if !breaks && !leading_spaces && value[i] == '\n' {
  1827. k := 0
  1828. for is_break(value, k) {
  1829. k += width(value[k])
  1830. }
  1831. if !is_blankz(value, k) {
  1832. if !put_break(emitter) {
  1833. return false
  1834. }
  1835. }
  1836. }
  1837. if !write_break(emitter, value, &i) {
  1838. return false
  1839. }
  1840. //emitter.indention = true
  1841. breaks = true
  1842. } else {
  1843. if breaks {
  1844. if !yaml_emitter_write_indent(emitter) {
  1845. return false
  1846. }
  1847. leading_spaces = is_blank(value, i)
  1848. }
  1849. if !breaks && is_space(value, i) && !is_space(value, i+1) && emitter.column > emitter.best_width {
  1850. if !yaml_emitter_write_indent(emitter) {
  1851. return false
  1852. }
  1853. i += width(value[i])
  1854. } else {
  1855. if !write(emitter, value, &i) {
  1856. return false
  1857. }
  1858. }
  1859. emitter.indention = false
  1860. breaks = false
  1861. }
  1862. }
  1863. return true
  1864. }
  1865. func yaml_emitter_write_comment(emitter *yaml_emitter_t, comment []byte) bool {
  1866. breaks := false
  1867. pound := false
  1868. for i := 0; i < len(comment); {
  1869. if is_break(comment, i) {
  1870. if !write_break(emitter, comment, &i) {
  1871. return false
  1872. }
  1873. //emitter.indention = true
  1874. breaks = true
  1875. pound = false
  1876. } else {
  1877. if breaks && !yaml_emitter_write_indent(emitter) {
  1878. return false
  1879. }
  1880. if !pound {
  1881. if comment[i] != '#' && (!put(emitter, '#') || !put(emitter, ' ')) {
  1882. return false
  1883. }
  1884. pound = true
  1885. }
  1886. if !write(emitter, comment, &i) {
  1887. return false
  1888. }
  1889. emitter.indention = false
  1890. breaks = false
  1891. }
  1892. }
  1893. if !breaks && !put_break(emitter) {
  1894. return false
  1895. }
  1896. emitter.whitespace = true
  1897. //emitter.indention = true
  1898. return true
  1899. }