forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/gopkg.in/yaml.v3/emitterc.go

3726 lines
56 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright (c) 2011-2019 Canonical Ltd
2024-02-18 10:42:21 +00:00
// Copyright (c) 2006-2010 Kirill Simonov
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Permission is hereby granted, free of charge, to any person obtaining a copy of
2024-02-18 10:42:21 +00:00
// this software and associated documentation files (the "Software"), to deal in
2024-02-18 10:42:21 +00:00
// the Software without restriction, including without limitation the rights to
2024-02-18 10:42:21 +00:00
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
2024-02-18 10:42:21 +00:00
// of the Software, and to permit persons to whom the Software is furnished to do
2024-02-18 10:42:21 +00:00
// so, subject to the following conditions:
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The above copyright notice and this permission notice shall be included in all
2024-02-18 10:42:21 +00:00
// copies or substantial portions of the Software.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2024-02-18 10:42:21 +00:00
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2024-02-18 10:42:21 +00:00
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2024-02-18 10:42:21 +00:00
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2024-02-18 10:42:21 +00:00
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2024-02-18 10:42:21 +00:00
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2024-02-18 10:42:21 +00:00
// SOFTWARE.
package yaml
import (
"bytes"
"fmt"
)
// Flush the buffer if needed.
2024-02-18 10:42:21 +00:00
func flush(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if emitter.buffer_pos+5 >= len(emitter.buffer) {
2024-02-18 10:42:21 +00:00
return yaml_emitter_flush(emitter)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Put a character to the output buffer.
2024-02-18 10:42:21 +00:00
func put(emitter *yaml_emitter_t, value byte) bool {
2024-02-18 10:42:21 +00:00
if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.buffer[emitter.buffer_pos] = value
2024-02-18 10:42:21 +00:00
emitter.buffer_pos++
2024-02-18 10:42:21 +00:00
emitter.column++
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Put a line break to the output buffer.
2024-02-18 10:42:21 +00:00
func put_break(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
switch emitter.line_break {
2024-02-18 10:42:21 +00:00
case yaml_CR_BREAK:
2024-02-18 10:42:21 +00:00
emitter.buffer[emitter.buffer_pos] = '\r'
2024-02-18 10:42:21 +00:00
emitter.buffer_pos += 1
2024-02-18 10:42:21 +00:00
case yaml_LN_BREAK:
2024-02-18 10:42:21 +00:00
emitter.buffer[emitter.buffer_pos] = '\n'
2024-02-18 10:42:21 +00:00
emitter.buffer_pos += 1
2024-02-18 10:42:21 +00:00
case yaml_CRLN_BREAK:
2024-02-18 10:42:21 +00:00
emitter.buffer[emitter.buffer_pos+0] = '\r'
2024-02-18 10:42:21 +00:00
emitter.buffer[emitter.buffer_pos+1] = '\n'
2024-02-18 10:42:21 +00:00
emitter.buffer_pos += 2
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
panic("unknown line break setting")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.column == 0 {
2024-02-18 10:42:21 +00:00
emitter.space_above = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.column = 0
2024-02-18 10:42:21 +00:00
emitter.line++
2024-02-18 10:42:21 +00:00
// [Go] Do this here and below and drop from everywhere else (see commented lines).
2024-02-18 10:42:21 +00:00
emitter.indention = true
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Copy a character from a string into buffer.
2024-02-18 10:42:21 +00:00
func write(emitter *yaml_emitter_t, s []byte, i *int) bool {
2024-02-18 10:42:21 +00:00
if emitter.buffer_pos+5 >= len(emitter.buffer) && !yaml_emitter_flush(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p := emitter.buffer_pos
2024-02-18 10:42:21 +00:00
w := width(s[*i])
2024-02-18 10:42:21 +00:00
switch w {
2024-02-18 10:42:21 +00:00
case 4:
2024-02-18 10:42:21 +00:00
emitter.buffer[p+3] = s[*i+3]
2024-02-18 10:42:21 +00:00
fallthrough
2024-02-18 10:42:21 +00:00
case 3:
2024-02-18 10:42:21 +00:00
emitter.buffer[p+2] = s[*i+2]
2024-02-18 10:42:21 +00:00
fallthrough
2024-02-18 10:42:21 +00:00
case 2:
2024-02-18 10:42:21 +00:00
emitter.buffer[p+1] = s[*i+1]
2024-02-18 10:42:21 +00:00
fallthrough
2024-02-18 10:42:21 +00:00
case 1:
2024-02-18 10:42:21 +00:00
emitter.buffer[p+0] = s[*i+0]
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
panic("unknown character width")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.column++
2024-02-18 10:42:21 +00:00
emitter.buffer_pos += w
2024-02-18 10:42:21 +00:00
*i += w
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Write a whole string into buffer.
2024-02-18 10:42:21 +00:00
func write_all(emitter *yaml_emitter_t, s []byte) bool {
2024-02-18 10:42:21 +00:00
for i := 0; i < len(s); {
2024-02-18 10:42:21 +00:00
if !write(emitter, s, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Copy a line break character from a string into buffer.
2024-02-18 10:42:21 +00:00
func write_break(emitter *yaml_emitter_t, s []byte, i *int) bool {
2024-02-18 10:42:21 +00:00
if s[*i] == '\n' {
2024-02-18 10:42:21 +00:00
if !put_break(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
*i++
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if !write(emitter, s, i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.column == 0 {
2024-02-18 10:42:21 +00:00
emitter.space_above = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.column = 0
2024-02-18 10:42:21 +00:00
emitter.line++
2024-02-18 10:42:21 +00:00
// [Go] Do this here and above and drop from everywhere else (see commented lines).
2024-02-18 10:42:21 +00:00
emitter.indention = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Set an emitter error and return false.
2024-02-18 10:42:21 +00:00
func yaml_emitter_set_emitter_error(emitter *yaml_emitter_t, problem string) bool {
2024-02-18 10:42:21 +00:00
emitter.error = yaml_EMITTER_ERROR
2024-02-18 10:42:21 +00:00
emitter.problem = problem
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
// Emit an event.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
emitter.events = append(emitter.events, *event)
2024-02-18 10:42:21 +00:00
for !yaml_emitter_need_more_events(emitter) {
2024-02-18 10:42:21 +00:00
event := &emitter.events[emitter.events_head]
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_event(emitter, event) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_state_machine(emitter, event) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
yaml_event_delete(event)
2024-02-18 10:42:21 +00:00
emitter.events_head++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Check if we need to accumulate more events before emitting.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// We accumulate extra
2024-06-14 08:41:36 +00:00
// - 1 event for DOCUMENT-START
2024-06-14 08:41:36 +00:00
// - 2 events for SEQUENCE-START
2024-06-14 08:41:36 +00:00
// - 3 events for MAPPING-START
2024-02-18 10:42:21 +00:00
func yaml_emitter_need_more_events(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if emitter.events_head == len(emitter.events) {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var accumulate int
2024-02-18 10:42:21 +00:00
switch emitter.events[emitter.events_head].typ {
2024-02-18 10:42:21 +00:00
case yaml_DOCUMENT_START_EVENT:
2024-02-18 10:42:21 +00:00
accumulate = 1
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
case yaml_SEQUENCE_START_EVENT:
2024-02-18 10:42:21 +00:00
accumulate = 2
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
case yaml_MAPPING_START_EVENT:
2024-02-18 10:42:21 +00:00
accumulate = 3
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.events)-emitter.events_head > accumulate {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var level int
2024-02-18 10:42:21 +00:00
for i := emitter.events_head; i < len(emitter.events); i++ {
2024-02-18 10:42:21 +00:00
switch emitter.events[i].typ {
2024-02-18 10:42:21 +00:00
case yaml_STREAM_START_EVENT, yaml_DOCUMENT_START_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT:
2024-02-18 10:42:21 +00:00
level++
2024-02-18 10:42:21 +00:00
case yaml_STREAM_END_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_END_EVENT, yaml_MAPPING_END_EVENT:
2024-02-18 10:42:21 +00:00
level--
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if level == 0 {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Append a directive to the directives stack.
2024-02-18 10:42:21 +00:00
func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_directive_t, allow_duplicates bool) bool {
2024-02-18 10:42:21 +00:00
for i := 0; i < len(emitter.tag_directives); i++ {
2024-02-18 10:42:21 +00:00
if bytes.Equal(value.handle, emitter.tag_directives[i].handle) {
2024-02-18 10:42:21 +00:00
if allow_duplicates {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "duplicate %TAG directive")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// [Go] Do we actually need to copy this given garbage collection
2024-02-18 10:42:21 +00:00
// and the lack of deallocating destructors?
2024-02-18 10:42:21 +00:00
tag_copy := yaml_tag_directive_t{
2024-02-18 10:42:21 +00:00
handle: make([]byte, len(value.handle)),
2024-02-18 10:42:21 +00:00
prefix: make([]byte, len(value.prefix)),
}
2024-02-18 10:42:21 +00:00
copy(tag_copy.handle, value.handle)
2024-02-18 10:42:21 +00:00
copy(tag_copy.prefix, value.prefix)
2024-02-18 10:42:21 +00:00
emitter.tag_directives = append(emitter.tag_directives, tag_copy)
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Increase the indentation level.
2024-02-18 10:42:21 +00:00
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool {
2024-02-18 10:42:21 +00:00
emitter.indents = append(emitter.indents, emitter.indent)
2024-02-18 10:42:21 +00:00
if emitter.indent < 0 {
2024-02-18 10:42:21 +00:00
if flow {
2024-02-18 10:42:21 +00:00
emitter.indent = emitter.best_indent
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
emitter.indent = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else if !indentless {
2024-02-18 10:42:21 +00:00
// [Go] This was changed so that indentations are more regular.
2024-02-18 10:42:21 +00:00
if emitter.states[len(emitter.states)-1] == yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE {
2024-02-18 10:42:21 +00:00
// The first indent inside a sequence will just skip the "- " indicator.
2024-02-18 10:42:21 +00:00
emitter.indent += 2
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// Everything else aligns to the chosen indentation.
2024-06-14 08:41:36 +00:00
emitter.indent = emitter.best_indent * ((emitter.indent + emitter.best_indent) / emitter.best_indent)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// State dispatcher.
2024-02-18 10:42:21 +00:00
func yaml_emitter_state_machine(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
switch emitter.state {
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
case yaml_EMIT_STREAM_START_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_stream_start(emitter, event)
case yaml_EMIT_FIRST_DOCUMENT_START_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_document_start(emitter, event, true)
case yaml_EMIT_DOCUMENT_START_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_document_start(emitter, event, false)
case yaml_EMIT_DOCUMENT_CONTENT_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_document_content(emitter, event)
case yaml_EMIT_DOCUMENT_END_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_document_end(emitter, event)
case yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_flow_sequence_item(emitter, event, true, false)
case yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_flow_sequence_item(emitter, event, false, true)
case yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_flow_sequence_item(emitter, event, false, false)
case yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_flow_mapping_key(emitter, event, true, false)
case yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_flow_mapping_key(emitter, event, false, true)
case yaml_EMIT_FLOW_MAPPING_KEY_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_flow_mapping_key(emitter, event, false, false)
case yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_flow_mapping_value(emitter, event, true)
case yaml_EMIT_FLOW_MAPPING_VALUE_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_flow_mapping_value(emitter, event, false)
case yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_block_sequence_item(emitter, event, true)
case yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_block_sequence_item(emitter, event, false)
case yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_block_mapping_key(emitter, event, true)
case yaml_EMIT_BLOCK_MAPPING_KEY_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_block_mapping_key(emitter, event, false)
case yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_block_mapping_value(emitter, event, true)
case yaml_EMIT_BLOCK_MAPPING_VALUE_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_block_mapping_value(emitter, event, false)
case yaml_EMIT_END_STATE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "expected nothing after STREAM-END")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
panic("invalid emitter state")
2024-02-18 10:42:21 +00:00
}
// Expect STREAM-START.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_stream_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
if event.typ != yaml_STREAM_START_EVENT {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "expected STREAM-START")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.encoding == yaml_ANY_ENCODING {
2024-02-18 10:42:21 +00:00
emitter.encoding = event.encoding
2024-02-18 10:42:21 +00:00
if emitter.encoding == yaml_ANY_ENCODING {
2024-02-18 10:42:21 +00:00
emitter.encoding = yaml_UTF8_ENCODING
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.best_indent < 2 || emitter.best_indent > 9 {
2024-02-18 10:42:21 +00:00
emitter.best_indent = 2
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.best_width >= 0 && emitter.best_width <= emitter.best_indent*2 {
2024-02-18 10:42:21 +00:00
emitter.best_width = 80
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.best_width < 0 {
2024-02-18 10:42:21 +00:00
emitter.best_width = 1<<31 - 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.line_break == yaml_ANY_BREAK {
2024-02-18 10:42:21 +00:00
emitter.line_break = yaml_LN_BREAK
2024-02-18 10:42:21 +00:00
}
emitter.indent = -1
2024-02-18 10:42:21 +00:00
emitter.line = 0
2024-02-18 10:42:21 +00:00
emitter.column = 0
2024-02-18 10:42:21 +00:00
emitter.whitespace = true
2024-02-18 10:42:21 +00:00
emitter.indention = true
2024-02-18 10:42:21 +00:00
emitter.space_above = true
2024-02-18 10:42:21 +00:00
emitter.foot_indent = -1
if emitter.encoding != yaml_UTF8_ENCODING {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_bom(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.state = yaml_EMIT_FIRST_DOCUMENT_START_STATE
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Expect DOCUMENT-START or STREAM-END.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
if event.typ == yaml_DOCUMENT_START_EVENT {
if event.version_directive != nil {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_version_directive(emitter, event.version_directive) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
for i := 0; i < len(event.tag_directives); i++ {
2024-02-18 10:42:21 +00:00
tag_directive := &event.tag_directives[i]
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_tag_directive(emitter, tag_directive) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_append_tag_directive(emitter, tag_directive, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
for i := 0; i < len(default_tag_directives); i++ {
2024-02-18 10:42:21 +00:00
tag_directive := &default_tag_directives[i]
2024-02-18 10:42:21 +00:00
if !yaml_emitter_append_tag_directive(emitter, tag_directive, true) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
implicit := event.implicit
2024-02-18 10:42:21 +00:00
if !first || emitter.canonical {
2024-02-18 10:42:21 +00:00
implicit = false
2024-02-18 10:42:21 +00:00
}
if emitter.open_ended && (event.version_directive != nil || len(event.tag_directives) > 0) {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if event.version_directive != nil {
2024-02-18 10:42:21 +00:00
implicit = false
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte("%YAML"), true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte("1.1"), true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if len(event.tag_directives) > 0 {
2024-02-18 10:42:21 +00:00
implicit = false
2024-02-18 10:42:21 +00:00
for i := 0; i < len(event.tag_directives); i++ {
2024-02-18 10:42:21 +00:00
tag_directive := &event.tag_directives[i]
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte("%TAG"), true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if yaml_emitter_check_empty_document(emitter) {
2024-02-18 10:42:21 +00:00
implicit = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !implicit {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte("---"), true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.canonical || true {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if len(emitter.head_comment) > 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_head_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !put_break(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
emitter.state = yaml_EMIT_DOCUMENT_CONTENT_STATE
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
if event.typ == yaml_STREAM_END_EVENT {
2024-02-18 10:42:21 +00:00
if emitter.open_ended {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_flush(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.state = yaml_EMIT_END_STATE
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-START or STREAM-END")
2024-02-18 10:42:21 +00:00
}
// Expect the root node.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_document_content(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_DOCUMENT_END_STATE)
if !yaml_emitter_process_head_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_emit_node(emitter, event, true, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_foot_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Expect DOCUMENT-END.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_document_end(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
if event.typ != yaml_DOCUMENT_END_EVENT {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "expected DOCUMENT-END")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// [Go] Force document foot separation.
2024-02-18 10:42:21 +00:00
emitter.foot_indent = 0
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_foot_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.foot_indent = -1
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !event.implicit {
2024-02-18 10:42:21 +00:00
// [Go] Allocate the slice elsewhere.
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_flush(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.state = yaml_EMIT_DOCUMENT_START_STATE
2024-02-18 10:42:21 +00:00
emitter.tag_directives = emitter.tag_directives[:0]
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Expect a flow item node.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first, trail bool) bool {
2024-02-18 10:42:21 +00:00
if first {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_increase_indent(emitter, true, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.flow_level++
2024-02-18 10:42:21 +00:00
}
if event.typ == yaml_SEQUENCE_END_EVENT {
2024-02-18 10:42:21 +00:00
if emitter.canonical && !first && !trail {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.flow_level--
2024-02-18 10:42:21 +00:00
emitter.indent = emitter.indents[len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
emitter.indents = emitter.indents[:len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
if emitter.column == 0 || emitter.canonical && !first {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{']'}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_foot_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.state = emitter.states[len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
emitter.states = emitter.states[:len(emitter.states)-1]
return true
2024-02-18 10:42:21 +00:00
}
if !first && !trail {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if !yaml_emitter_process_head_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.column == 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if emitter.canonical || emitter.column > emitter.best_width {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_TRAIL_ITEM_STATE)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_FLOW_SEQUENCE_ITEM_STATE)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_emit_node(emitter, event, false, true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_foot_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Expect a flow key node.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first, trail bool) bool {
2024-02-18 10:42:21 +00:00
if first {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_increase_indent(emitter, true, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.flow_level++
2024-02-18 10:42:21 +00:00
}
if event.typ == yaml_MAPPING_END_EVENT {
2024-02-18 10:42:21 +00:00
if (emitter.canonical || len(emitter.head_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0) && !first && !trail {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_head_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.flow_level--
2024-02-18 10:42:21 +00:00
emitter.indent = emitter.indents[len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
emitter.indents = emitter.indents[:len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
if emitter.canonical && !first {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'}'}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_foot_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.state = emitter.states[len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
emitter.states = emitter.states[:len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
if !first && !trail {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if !yaml_emitter_process_head_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
if emitter.column == 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if emitter.canonical || emitter.column > emitter.best_width {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if !emitter.canonical && yaml_emitter_check_simple_key(emitter) {
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_SIMPLE_VALUE_STATE)
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_node(emitter, event, false, false, true, true)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_VALUE_STATE)
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_node(emitter, event, false, false, true, false)
2024-02-18 10:42:21 +00:00
}
// Expect a flow value node.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool {
2024-02-18 10:42:21 +00:00
if simple {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if emitter.canonical || emitter.column > emitter.best_width {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_TRAIL_KEY_STATE)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_FLOW_MAPPING_KEY_STATE)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_emit_node(emitter, event, false, false, true, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.line_comment)+len(emitter.foot_comment)+len(emitter.tail_comment) > 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{','}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_foot_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Expect a block item node.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
2024-02-18 10:42:21 +00:00
if first {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_increase_indent(emitter, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if event.typ == yaml_SEQUENCE_END_EVENT {
2024-02-18 10:42:21 +00:00
emitter.indent = emitter.indents[len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
emitter.indents = emitter.indents[:len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
emitter.state = emitter.states[len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
emitter.states = emitter.states[:len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_head_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'-'}, true, false, true) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE)
2024-02-18 10:42:21 +00:00
if !yaml_emitter_emit_node(emitter, event, false, true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_foot_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Expect a block key node.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
2024-02-18 10:42:21 +00:00
if first {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_increase_indent(emitter, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_head_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if event.typ == yaml_MAPPING_END_EVENT {
2024-02-18 10:42:21 +00:00
emitter.indent = emitter.indents[len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
emitter.indents = emitter.indents[:len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
emitter.state = emitter.states[len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
emitter.states = emitter.states[:len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.line_comment) > 0 {
2024-02-18 10:42:21 +00:00
// [Go] A line comment was provided for the key. That's unusual as the
2024-02-18 10:42:21 +00:00
// scanner associates line comments with the value. Either way,
2024-02-18 10:42:21 +00:00
// save the line comment and render it appropriately later.
2024-02-18 10:42:21 +00:00
emitter.key_line_comment = emitter.line_comment
2024-02-18 10:42:21 +00:00
emitter.line_comment = nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if yaml_emitter_check_simple_key(emitter) {
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_SIMPLE_VALUE_STATE)
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_node(emitter, event, false, false, true, true)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'?'}, true, false, true) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_VALUE_STATE)
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_node(emitter, event, false, false, true, false)
2024-02-18 10:42:21 +00:00
}
// Expect a block value node.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_block_mapping_value(emitter *yaml_emitter_t, event *yaml_event_t, simple bool) bool {
2024-02-18 10:42:21 +00:00
if simple {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{':'}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{':'}, true, false, true) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.key_line_comment) > 0 {
2024-02-18 10:42:21 +00:00
// [Go] Line comments are generally associated with the value, but when there's
2024-02-18 10:42:21 +00:00
// no value on the same line as a mapping key they end up attached to the
2024-02-18 10:42:21 +00:00
// key itself.
2024-02-18 10:42:21 +00:00
if event.typ == yaml_SCALAR_EVENT {
2024-02-18 10:42:21 +00:00
if len(emitter.line_comment) == 0 {
2024-02-18 10:42:21 +00:00
// A scalar is coming and it has no line comments by itself yet,
2024-02-18 10:42:21 +00:00
// so just let it handle the line comment as usual. If it has a
2024-02-18 10:42:21 +00:00
// line comment, we can't have both so the one from the key is lost.
2024-02-18 10:42:21 +00:00
emitter.line_comment = emitter.key_line_comment
2024-02-18 10:42:21 +00:00
emitter.key_line_comment = nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else if event.sequence_style() != yaml_FLOW_SEQUENCE_STYLE && (event.typ == yaml_MAPPING_START_EVENT || event.typ == yaml_SEQUENCE_START_EVENT) {
2024-02-18 10:42:21 +00:00
// An indented block follows, so write the comment right now.
2024-02-18 10:42:21 +00:00
emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.line_comment, emitter.key_line_comment = emitter.key_line_comment, emitter.line_comment
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.states = append(emitter.states, yaml_EMIT_BLOCK_MAPPING_KEY_STATE)
2024-02-18 10:42:21 +00:00
if !yaml_emitter_emit_node(emitter, event, false, false, true, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_foot_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_silent_nil_event(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
return event.typ == yaml_SCALAR_EVENT && event.implicit && !emitter.canonical && len(emitter.scalar_data.value) == 0
2024-02-18 10:42:21 +00:00
}
// Expect a node.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_node(emitter *yaml_emitter_t, event *yaml_event_t,
2024-02-18 10:42:21 +00:00
root bool, sequence bool, mapping bool, simple_key bool) bool {
emitter.root_context = root
2024-02-18 10:42:21 +00:00
emitter.sequence_context = sequence
2024-02-18 10:42:21 +00:00
emitter.mapping_context = mapping
2024-02-18 10:42:21 +00:00
emitter.simple_key_context = simple_key
switch event.typ {
2024-02-18 10:42:21 +00:00
case yaml_ALIAS_EVENT:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_alias(emitter, event)
2024-02-18 10:42:21 +00:00
case yaml_SCALAR_EVENT:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_scalar(emitter, event)
2024-02-18 10:42:21 +00:00
case yaml_SEQUENCE_START_EVENT:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_sequence_start(emitter, event)
2024-02-18 10:42:21 +00:00
case yaml_MAPPING_START_EVENT:
2024-02-18 10:42:21 +00:00
return yaml_emitter_emit_mapping_start(emitter, event)
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter,
2024-02-18 10:42:21 +00:00
fmt.Sprintf("expected SCALAR, SEQUENCE-START, MAPPING-START, or ALIAS, but got %v", event.typ))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Expect ALIAS.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_alias(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_anchor(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.state = emitter.states[len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
emitter.states = emitter.states[:len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Expect SCALAR.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_select_scalar_style(emitter, event) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_anchor(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_tag(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_increase_indent(emitter, true, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_scalar(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.indent = emitter.indents[len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
emitter.indents = emitter.indents[:len(emitter.indents)-1]
2024-02-18 10:42:21 +00:00
emitter.state = emitter.states[len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
emitter.states = emitter.states[:len(emitter.states)-1]
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Expect SEQUENCE-START.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_sequence_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_anchor(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_tag(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.flow_level > 0 || emitter.canonical || event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE ||
2024-02-18 10:42:21 +00:00
yaml_emitter_check_empty_sequence(emitter) {
2024-02-18 10:42:21 +00:00
emitter.state = yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
emitter.state = yaml_EMIT_BLOCK_SEQUENCE_FIRST_ITEM_STATE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Expect MAPPING-START.
2024-02-18 10:42:21 +00:00
func yaml_emitter_emit_mapping_start(emitter *yaml_emitter_t, event *yaml_event_t) bool {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_anchor(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_tag(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.flow_level > 0 || emitter.canonical || event.mapping_style() == yaml_FLOW_MAPPING_STYLE ||
2024-02-18 10:42:21 +00:00
yaml_emitter_check_empty_mapping(emitter) {
2024-02-18 10:42:21 +00:00
emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
emitter.state = yaml_EMIT_BLOCK_MAPPING_FIRST_KEY_STATE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Check if the document content is an empty scalar.
2024-02-18 10:42:21 +00:00
func yaml_emitter_check_empty_document(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
return false // [Go] Huh?
2024-02-18 10:42:21 +00:00
}
// Check if the next events represent an empty sequence.
2024-02-18 10:42:21 +00:00
func yaml_emitter_check_empty_sequence(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if len(emitter.events)-emitter.events_head < 2 {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return emitter.events[emitter.events_head].typ == yaml_SEQUENCE_START_EVENT &&
2024-02-18 10:42:21 +00:00
emitter.events[emitter.events_head+1].typ == yaml_SEQUENCE_END_EVENT
2024-02-18 10:42:21 +00:00
}
// Check if the next events represent an empty mapping.
2024-02-18 10:42:21 +00:00
func yaml_emitter_check_empty_mapping(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if len(emitter.events)-emitter.events_head < 2 {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return emitter.events[emitter.events_head].typ == yaml_MAPPING_START_EVENT &&
2024-02-18 10:42:21 +00:00
emitter.events[emitter.events_head+1].typ == yaml_MAPPING_END_EVENT
2024-02-18 10:42:21 +00:00
}
// Check if the next node can be expressed as a simple key.
2024-02-18 10:42:21 +00:00
func yaml_emitter_check_simple_key(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
length := 0
2024-02-18 10:42:21 +00:00
switch emitter.events[emitter.events_head].typ {
2024-02-18 10:42:21 +00:00
case yaml_ALIAS_EVENT:
2024-02-18 10:42:21 +00:00
length += len(emitter.anchor_data.anchor)
2024-02-18 10:42:21 +00:00
case yaml_SCALAR_EVENT:
2024-02-18 10:42:21 +00:00
if emitter.scalar_data.multiline {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
length += len(emitter.anchor_data.anchor) +
2024-02-18 10:42:21 +00:00
len(emitter.tag_data.handle) +
2024-02-18 10:42:21 +00:00
len(emitter.tag_data.suffix) +
2024-02-18 10:42:21 +00:00
len(emitter.scalar_data.value)
2024-02-18 10:42:21 +00:00
case yaml_SEQUENCE_START_EVENT:
2024-02-18 10:42:21 +00:00
if !yaml_emitter_check_empty_sequence(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
length += len(emitter.anchor_data.anchor) +
2024-02-18 10:42:21 +00:00
len(emitter.tag_data.handle) +
2024-02-18 10:42:21 +00:00
len(emitter.tag_data.suffix)
2024-02-18 10:42:21 +00:00
case yaml_MAPPING_START_EVENT:
2024-02-18 10:42:21 +00:00
if !yaml_emitter_check_empty_mapping(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
length += len(emitter.anchor_data.anchor) +
2024-02-18 10:42:21 +00:00
len(emitter.tag_data.handle) +
2024-02-18 10:42:21 +00:00
len(emitter.tag_data.suffix)
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return length <= 128
2024-02-18 10:42:21 +00:00
}
// Determine an acceptable scalar style.
2024-02-18 10:42:21 +00:00
func yaml_emitter_select_scalar_style(emitter *yaml_emitter_t, event *yaml_event_t) bool {
no_tag := len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0
2024-02-18 10:42:21 +00:00
if no_tag && !event.implicit && !event.quoted_implicit {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "neither tag nor implicit flags are specified")
2024-02-18 10:42:21 +00:00
}
style := event.scalar_style()
2024-02-18 10:42:21 +00:00
if style == yaml_ANY_SCALAR_STYLE {
2024-02-18 10:42:21 +00:00
style = yaml_PLAIN_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.canonical {
2024-02-18 10:42:21 +00:00
style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.simple_key_context && emitter.scalar_data.multiline {
2024-02-18 10:42:21 +00:00
style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
if style == yaml_PLAIN_SCALAR_STYLE {
2024-02-18 10:42:21 +00:00
if emitter.flow_level > 0 && !emitter.scalar_data.flow_plain_allowed ||
2024-02-18 10:42:21 +00:00
emitter.flow_level == 0 && !emitter.scalar_data.block_plain_allowed {
2024-02-18 10:42:21 +00:00
style = yaml_SINGLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.scalar_data.value) == 0 && (emitter.flow_level > 0 || emitter.simple_key_context) {
2024-02-18 10:42:21 +00:00
style = yaml_SINGLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if no_tag && !event.implicit {
2024-02-18 10:42:21 +00:00
style = yaml_SINGLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if style == yaml_SINGLE_QUOTED_SCALAR_STYLE {
2024-02-18 10:42:21 +00:00
if !emitter.scalar_data.single_quoted_allowed {
2024-02-18 10:42:21 +00:00
style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if style == yaml_LITERAL_SCALAR_STYLE || style == yaml_FOLDED_SCALAR_STYLE {
2024-02-18 10:42:21 +00:00
if !emitter.scalar_data.block_allowed || emitter.flow_level > 0 || emitter.simple_key_context {
2024-02-18 10:42:21 +00:00
style = yaml_DOUBLE_QUOTED_SCALAR_STYLE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if no_tag && !event.quoted_implicit && style != yaml_PLAIN_SCALAR_STYLE {
2024-02-18 10:42:21 +00:00
emitter.tag_data.handle = []byte{'!'}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.scalar_data.style = style
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Write an anchor.
2024-02-18 10:42:21 +00:00
func yaml_emitter_process_anchor(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if emitter.anchor_data.anchor == nil {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
c := []byte{'&'}
2024-02-18 10:42:21 +00:00
if emitter.anchor_data.alias {
2024-02-18 10:42:21 +00:00
c[0] = '*'
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, c, true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return yaml_emitter_write_anchor(emitter, emitter.anchor_data.anchor)
2024-02-18 10:42:21 +00:00
}
// Write a tag.
2024-02-18 10:42:21 +00:00
func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if len(emitter.tag_data.handle) == 0 && len(emitter.tag_data.suffix) == 0 {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.tag_data.handle) > 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_tag_handle(emitter, emitter.tag_data.handle) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(emitter.tag_data.suffix) > 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
// [Go] Allocate these slices elsewhere.
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte("!<"), true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'>'}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Write a scalar.
2024-02-18 10:42:21 +00:00
func yaml_emitter_process_scalar(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
switch emitter.scalar_data.style {
2024-02-18 10:42:21 +00:00
case yaml_PLAIN_SCALAR_STYLE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_write_plain_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
case yaml_SINGLE_QUOTED_SCALAR_STYLE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_write_single_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
case yaml_DOUBLE_QUOTED_SCALAR_STYLE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_write_double_quoted_scalar(emitter, emitter.scalar_data.value, !emitter.simple_key_context)
case yaml_LITERAL_SCALAR_STYLE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_write_literal_scalar(emitter, emitter.scalar_data.value)
case yaml_FOLDED_SCALAR_STYLE:
2024-02-18 10:42:21 +00:00
return yaml_emitter_write_folded_scalar(emitter, emitter.scalar_data.value)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
panic("unknown scalar style")
2024-02-18 10:42:21 +00:00
}
// Write a head comment.
2024-02-18 10:42:21 +00:00
func yaml_emitter_process_head_comment(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if len(emitter.tail_comment) > 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_comment(emitter, emitter.tail_comment) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.tail_comment = emitter.tail_comment[:0]
2024-02-18 10:42:21 +00:00
emitter.foot_indent = emitter.indent
2024-02-18 10:42:21 +00:00
if emitter.foot_indent < 0 {
2024-02-18 10:42:21 +00:00
emitter.foot_indent = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if len(emitter.head_comment) == 0 {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_comment(emitter, emitter.head_comment) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.head_comment = emitter.head_comment[:0]
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Write an line comment.
2024-02-18 10:42:21 +00:00
func yaml_emitter_process_line_comment(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if len(emitter.line_comment) == 0 {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !emitter.whitespace {
2024-02-18 10:42:21 +00:00
if !put(emitter, ' ') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_comment(emitter, emitter.line_comment) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.line_comment = emitter.line_comment[:0]
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Write a foot comment.
2024-02-18 10:42:21 +00:00
func yaml_emitter_process_foot_comment(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if len(emitter.foot_comment) == 0 {
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_comment(emitter, emitter.foot_comment) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.foot_comment = emitter.foot_comment[:0]
2024-02-18 10:42:21 +00:00
emitter.foot_indent = emitter.indent
2024-02-18 10:42:21 +00:00
if emitter.foot_indent < 0 {
2024-02-18 10:42:21 +00:00
emitter.foot_indent = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Check if a %YAML directive is valid.
2024-02-18 10:42:21 +00:00
func yaml_emitter_analyze_version_directive(emitter *yaml_emitter_t, version_directive *yaml_version_directive_t) bool {
2024-02-18 10:42:21 +00:00
if version_directive.major != 1 || version_directive.minor != 1 {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "incompatible %YAML directive")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Check if a %TAG directive is valid.
2024-02-18 10:42:21 +00:00
func yaml_emitter_analyze_tag_directive(emitter *yaml_emitter_t, tag_directive *yaml_tag_directive_t) bool {
2024-02-18 10:42:21 +00:00
handle := tag_directive.handle
2024-02-18 10:42:21 +00:00
prefix := tag_directive.prefix
2024-02-18 10:42:21 +00:00
if len(handle) == 0 {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "tag handle must not be empty")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if handle[0] != '!' {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "tag handle must start with '!'")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if handle[len(handle)-1] != '!' {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "tag handle must end with '!'")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for i := 1; i < len(handle)-1; i += width(handle[i]) {
2024-02-18 10:42:21 +00:00
if !is_alpha(handle, i) {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "tag handle must contain alphanumerical characters only")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(prefix) == 0 {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "tag prefix must not be empty")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Check if an anchor is valid.
2024-02-18 10:42:21 +00:00
func yaml_emitter_analyze_anchor(emitter *yaml_emitter_t, anchor []byte, alias bool) bool {
2024-02-18 10:42:21 +00:00
if len(anchor) == 0 {
2024-02-18 10:42:21 +00:00
problem := "anchor value must not be empty"
2024-02-18 10:42:21 +00:00
if alias {
2024-02-18 10:42:21 +00:00
problem = "alias value must not be empty"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, problem)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for i := 0; i < len(anchor); i += width(anchor[i]) {
2024-02-18 10:42:21 +00:00
if !is_alpha(anchor, i) {
2024-02-18 10:42:21 +00:00
problem := "anchor value must contain alphanumerical characters only"
2024-02-18 10:42:21 +00:00
if alias {
2024-02-18 10:42:21 +00:00
problem = "alias value must contain alphanumerical characters only"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, problem)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.anchor_data.anchor = anchor
2024-02-18 10:42:21 +00:00
emitter.anchor_data.alias = alias
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Check if a tag is valid.
2024-02-18 10:42:21 +00:00
func yaml_emitter_analyze_tag(emitter *yaml_emitter_t, tag []byte) bool {
2024-02-18 10:42:21 +00:00
if len(tag) == 0 {
2024-02-18 10:42:21 +00:00
return yaml_emitter_set_emitter_error(emitter, "tag value must not be empty")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for i := 0; i < len(emitter.tag_directives); i++ {
2024-02-18 10:42:21 +00:00
tag_directive := &emitter.tag_directives[i]
2024-02-18 10:42:21 +00:00
if bytes.HasPrefix(tag, tag_directive.prefix) {
2024-02-18 10:42:21 +00:00
emitter.tag_data.handle = tag_directive.handle
2024-02-18 10:42:21 +00:00
emitter.tag_data.suffix = tag[len(tag_directive.prefix):]
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.tag_data.suffix = tag
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Check if a scalar is valid.
2024-02-18 10:42:21 +00:00
func yaml_emitter_analyze_scalar(emitter *yaml_emitter_t, value []byte) bool {
2024-02-18 10:42:21 +00:00
var (
block_indicators = false
flow_indicators = false
line_breaks = false
2024-02-18 10:42:21 +00:00
special_characters = false
tab_characters = false
leading_space = false
leading_break = false
2024-02-18 10:42:21 +00:00
trailing_space = false
2024-02-18 10:42:21 +00:00
trailing_break = false
break_space = false
space_break = false
2024-02-18 10:42:21 +00:00
preceded_by_whitespace = false
2024-02-18 10:42:21 +00:00
followed_by_whitespace = false
previous_space = false
previous_break = false
2024-02-18 10:42:21 +00:00
)
emitter.scalar_data.value = value
if len(value) == 0 {
2024-02-18 10:42:21 +00:00
emitter.scalar_data.multiline = false
2024-02-18 10:42:21 +00:00
emitter.scalar_data.flow_plain_allowed = false
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_plain_allowed = true
2024-02-18 10:42:21 +00:00
emitter.scalar_data.single_quoted_allowed = true
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_allowed = false
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
if len(value) >= 3 && ((value[0] == '-' && value[1] == '-' && value[2] == '-') || (value[0] == '.' && value[1] == '.' && value[2] == '.')) {
2024-02-18 10:42:21 +00:00
block_indicators = true
2024-02-18 10:42:21 +00:00
flow_indicators = true
2024-02-18 10:42:21 +00:00
}
preceded_by_whitespace = true
2024-02-18 10:42:21 +00:00
for i, w := 0, 0; i < len(value); i += w {
2024-02-18 10:42:21 +00:00
w = width(value[i])
2024-02-18 10:42:21 +00:00
followed_by_whitespace = i+w >= len(value) || is_blank(value, i+w)
if i == 0 {
2024-02-18 10:42:21 +00:00
switch value[i] {
2024-02-18 10:42:21 +00:00
case '#', ',', '[', ']', '{', '}', '&', '*', '!', '|', '>', '\'', '"', '%', '@', '`':
2024-02-18 10:42:21 +00:00
flow_indicators = true
2024-02-18 10:42:21 +00:00
block_indicators = true
2024-02-18 10:42:21 +00:00
case '?', ':':
2024-02-18 10:42:21 +00:00
flow_indicators = true
2024-02-18 10:42:21 +00:00
if followed_by_whitespace {
2024-02-18 10:42:21 +00:00
block_indicators = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case '-':
2024-02-18 10:42:21 +00:00
if followed_by_whitespace {
2024-02-18 10:42:21 +00:00
flow_indicators = true
2024-02-18 10:42:21 +00:00
block_indicators = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
switch value[i] {
2024-02-18 10:42:21 +00:00
case ',', '?', '[', ']', '{', '}':
2024-02-18 10:42:21 +00:00
flow_indicators = true
2024-02-18 10:42:21 +00:00
case ':':
2024-02-18 10:42:21 +00:00
flow_indicators = true
2024-02-18 10:42:21 +00:00
if followed_by_whitespace {
2024-02-18 10:42:21 +00:00
block_indicators = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
case '#':
2024-02-18 10:42:21 +00:00
if preceded_by_whitespace {
2024-02-18 10:42:21 +00:00
flow_indicators = true
2024-02-18 10:42:21 +00:00
block_indicators = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if value[i] == '\t' {
2024-02-18 10:42:21 +00:00
tab_characters = true
2024-02-18 10:42:21 +00:00
} else if !is_printable(value, i) || !is_ascii(value, i) && !emitter.unicode {
2024-02-18 10:42:21 +00:00
special_characters = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if is_space(value, i) {
2024-02-18 10:42:21 +00:00
if i == 0 {
2024-02-18 10:42:21 +00:00
leading_space = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i+width(value[i]) == len(value) {
2024-02-18 10:42:21 +00:00
trailing_space = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if previous_break {
2024-02-18 10:42:21 +00:00
break_space = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
previous_space = true
2024-02-18 10:42:21 +00:00
previous_break = false
2024-02-18 10:42:21 +00:00
} else if is_break(value, i) {
2024-02-18 10:42:21 +00:00
line_breaks = true
2024-02-18 10:42:21 +00:00
if i == 0 {
2024-02-18 10:42:21 +00:00
leading_break = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i+width(value[i]) == len(value) {
2024-02-18 10:42:21 +00:00
trailing_break = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if previous_space {
2024-02-18 10:42:21 +00:00
space_break = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
previous_space = false
2024-02-18 10:42:21 +00:00
previous_break = true
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
previous_space = false
2024-02-18 10:42:21 +00:00
previous_break = false
2024-02-18 10:42:21 +00:00
}
// [Go]: Why 'z'? Couldn't be the end of the string as that's the loop condition.
2024-02-18 10:42:21 +00:00
preceded_by_whitespace = is_blankz(value, i)
2024-02-18 10:42:21 +00:00
}
emitter.scalar_data.multiline = line_breaks
2024-02-18 10:42:21 +00:00
emitter.scalar_data.flow_plain_allowed = true
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_plain_allowed = true
2024-02-18 10:42:21 +00:00
emitter.scalar_data.single_quoted_allowed = true
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_allowed = true
if leading_space || leading_break || trailing_space || trailing_break {
2024-02-18 10:42:21 +00:00
emitter.scalar_data.flow_plain_allowed = false
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_plain_allowed = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if trailing_space {
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_allowed = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if break_space {
2024-02-18 10:42:21 +00:00
emitter.scalar_data.flow_plain_allowed = false
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_plain_allowed = false
2024-02-18 10:42:21 +00:00
emitter.scalar_data.single_quoted_allowed = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if space_break || tab_characters || special_characters {
2024-02-18 10:42:21 +00:00
emitter.scalar_data.flow_plain_allowed = false
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_plain_allowed = false
2024-02-18 10:42:21 +00:00
emitter.scalar_data.single_quoted_allowed = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if space_break || special_characters {
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_allowed = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if line_breaks {
2024-02-18 10:42:21 +00:00
emitter.scalar_data.flow_plain_allowed = false
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_plain_allowed = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if flow_indicators {
2024-02-18 10:42:21 +00:00
emitter.scalar_data.flow_plain_allowed = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if block_indicators {
2024-02-18 10:42:21 +00:00
emitter.scalar_data.block_plain_allowed = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Check if the event data is valid.
2024-02-18 10:42:21 +00:00
func yaml_emitter_analyze_event(emitter *yaml_emitter_t, event *yaml_event_t) bool {
emitter.anchor_data.anchor = nil
2024-02-18 10:42:21 +00:00
emitter.tag_data.handle = nil
2024-02-18 10:42:21 +00:00
emitter.tag_data.suffix = nil
2024-02-18 10:42:21 +00:00
emitter.scalar_data.value = nil
if len(event.head_comment) > 0 {
2024-02-18 10:42:21 +00:00
emitter.head_comment = event.head_comment
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(event.line_comment) > 0 {
2024-02-18 10:42:21 +00:00
emitter.line_comment = event.line_comment
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(event.foot_comment) > 0 {
2024-02-18 10:42:21 +00:00
emitter.foot_comment = event.foot_comment
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(event.tail_comment) > 0 {
2024-02-18 10:42:21 +00:00
emitter.tail_comment = event.tail_comment
2024-02-18 10:42:21 +00:00
}
switch event.typ {
2024-02-18 10:42:21 +00:00
case yaml_ALIAS_EVENT:
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_anchor(emitter, event.anchor, true) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
case yaml_SCALAR_EVENT:
2024-02-18 10:42:21 +00:00
if len(event.anchor) > 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(event.tag) > 0 && (emitter.canonical || (!event.implicit && !event.quoted_implicit)) {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_tag(emitter, event.tag) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_scalar(emitter, event.value) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
case yaml_SEQUENCE_START_EVENT:
2024-02-18 10:42:21 +00:00
if len(event.anchor) > 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(event.tag) > 0 && (emitter.canonical || !event.implicit) {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_tag(emitter, event.tag) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
case yaml_MAPPING_START_EVENT:
2024-02-18 10:42:21 +00:00
if len(event.anchor) > 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(event.tag) > 0 && (emitter.canonical || !event.implicit) {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_analyze_tag(emitter, event.tag) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
// Write the BOM character.
2024-02-18 10:42:21 +00:00
func yaml_emitter_write_bom(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
if !flush(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pos := emitter.buffer_pos
2024-02-18 10:42:21 +00:00
emitter.buffer[pos+0] = '\xEF'
2024-02-18 10:42:21 +00:00
emitter.buffer[pos+1] = '\xBB'
2024-02-18 10:42:21 +00:00
emitter.buffer[pos+2] = '\xBF'
2024-02-18 10:42:21 +00:00
emitter.buffer_pos += 3
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_indent(emitter *yaml_emitter_t) bool {
2024-02-18 10:42:21 +00:00
indent := emitter.indent
2024-02-18 10:42:21 +00:00
if indent < 0 {
2024-02-18 10:42:21 +00:00
indent = 0
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !emitter.indention || emitter.column > indent || (emitter.column == indent && !emitter.whitespace) {
2024-02-18 10:42:21 +00:00
if !put_break(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if emitter.foot_indent == indent {
2024-02-18 10:42:21 +00:00
if !put_break(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for emitter.column < indent {
2024-02-18 10:42:21 +00:00
if !put(emitter, ' ') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.whitespace = true
2024-02-18 10:42:21 +00:00
//emitter.indention = true
2024-02-18 10:42:21 +00:00
emitter.space_above = false
2024-02-18 10:42:21 +00:00
emitter.foot_indent = -1
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_indicator(emitter *yaml_emitter_t, indicator []byte, need_whitespace, is_whitespace, is_indention bool) bool {
2024-02-18 10:42:21 +00:00
if need_whitespace && !emitter.whitespace {
2024-02-18 10:42:21 +00:00
if !put(emitter, ' ') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !write_all(emitter, indicator) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.whitespace = is_whitespace
2024-02-18 10:42:21 +00:00
emitter.indention = (emitter.indention && is_indention)
2024-02-18 10:42:21 +00:00
emitter.open_ended = false
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_anchor(emitter *yaml_emitter_t, value []byte) bool {
2024-02-18 10:42:21 +00:00
if !write_all(emitter, value) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.whitespace = false
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_tag_handle(emitter *yaml_emitter_t, value []byte) bool {
2024-02-18 10:42:21 +00:00
if !emitter.whitespace {
2024-02-18 10:42:21 +00:00
if !put(emitter, ' ') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !write_all(emitter, value) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.whitespace = false
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_whitespace bool) bool {
2024-02-18 10:42:21 +00:00
if need_whitespace && !emitter.whitespace {
2024-02-18 10:42:21 +00:00
if !put(emitter, ' ') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for i := 0; i < len(value); {
2024-02-18 10:42:21 +00:00
var must_write bool
2024-02-18 10:42:21 +00:00
switch value[i] {
2024-02-18 10:42:21 +00:00
case ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')', '[', ']':
2024-02-18 10:42:21 +00:00
must_write = true
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
must_write = is_alpha(value, i)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if must_write {
2024-02-18 10:42:21 +00:00
if !write(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
w := width(value[i])
2024-02-18 10:42:21 +00:00
for k := 0; k < w; k++ {
2024-02-18 10:42:21 +00:00
octet := value[i]
2024-02-18 10:42:21 +00:00
i++
2024-02-18 10:42:21 +00:00
if !put(emitter, '%') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
c := octet >> 4
2024-02-18 10:42:21 +00:00
if c < 10 {
2024-02-18 10:42:21 +00:00
c += '0'
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
c += 'A' - 10
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !put(emitter, c) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
c = octet & 0x0f
2024-02-18 10:42:21 +00:00
if c < 10 {
2024-02-18 10:42:21 +00:00
c += '0'
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
c += 'A' - 10
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !put(emitter, c) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.whitespace = false
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_plain_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
2024-02-18 10:42:21 +00:00
if len(value) > 0 && !emitter.whitespace {
2024-02-18 10:42:21 +00:00
if !put(emitter, ' ') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
spaces := false
2024-02-18 10:42:21 +00:00
breaks := false
2024-02-18 10:42:21 +00:00
for i := 0; i < len(value); {
2024-02-18 10:42:21 +00:00
if is_space(value, i) {
2024-02-18 10:42:21 +00:00
if allow_breaks && !spaces && emitter.column > emitter.best_width && !is_space(value, i+1) {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i += width(value[i])
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if !write(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
spaces = true
2024-02-18 10:42:21 +00:00
} else if is_break(value, i) {
2024-02-18 10:42:21 +00:00
if !breaks && value[i] == '\n' {
2024-02-18 10:42:21 +00:00
if !put_break(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !write_break(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
//emitter.indention = true
2024-02-18 10:42:21 +00:00
breaks = true
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if breaks {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !write(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
spaces = false
2024-02-18 10:42:21 +00:00
breaks = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
if len(value) > 0 {
2024-02-18 10:42:21 +00:00
emitter.whitespace = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
if emitter.root_context {
2024-02-18 10:42:21 +00:00
emitter.open_ended = true
2024-02-18 10:42:21 +00:00
}
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_single_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
if !yaml_emitter_write_indicator(emitter, []byte{'\''}, true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
spaces := false
2024-02-18 10:42:21 +00:00
breaks := false
2024-02-18 10:42:21 +00:00
for i := 0; i < len(value); {
2024-02-18 10:42:21 +00:00
if is_space(value, i) {
2024-02-18 10:42:21 +00:00
if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 && !is_space(value, i+1) {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i += width(value[i])
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if !write(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
spaces = true
2024-02-18 10:42:21 +00:00
} else if is_break(value, i) {
2024-02-18 10:42:21 +00:00
if !breaks && value[i] == '\n' {
2024-02-18 10:42:21 +00:00
if !put_break(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !write_break(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
//emitter.indention = true
2024-02-18 10:42:21 +00:00
breaks = true
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if breaks {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if value[i] == '\'' {
2024-02-18 10:42:21 +00:00
if !put(emitter, '\'') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !write(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
spaces = false
2024-02-18 10:42:21 +00:00
breaks = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'\''}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.whitespace = false
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_double_quoted_scalar(emitter *yaml_emitter_t, value []byte, allow_breaks bool) bool {
2024-02-18 10:42:21 +00:00
spaces := false
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'"'}, true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
for i := 0; i < len(value); {
2024-02-18 10:42:21 +00:00
if !is_printable(value, i) || (!emitter.unicode && !is_ascii(value, i)) ||
2024-02-18 10:42:21 +00:00
is_bom(value, i) || is_break(value, i) ||
2024-02-18 10:42:21 +00:00
value[i] == '"' || value[i] == '\\' {
octet := value[i]
var w int
2024-02-18 10:42:21 +00:00
var v rune
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case octet&0x80 == 0x00:
2024-02-18 10:42:21 +00:00
w, v = 1, rune(octet&0x7F)
2024-02-18 10:42:21 +00:00
case octet&0xE0 == 0xC0:
2024-02-18 10:42:21 +00:00
w, v = 2, rune(octet&0x1F)
2024-02-18 10:42:21 +00:00
case octet&0xF0 == 0xE0:
2024-02-18 10:42:21 +00:00
w, v = 3, rune(octet&0x0F)
2024-02-18 10:42:21 +00:00
case octet&0xF8 == 0xF0:
2024-02-18 10:42:21 +00:00
w, v = 4, rune(octet&0x07)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for k := 1; k < w; k++ {
2024-02-18 10:42:21 +00:00
octet = value[i+k]
2024-02-18 10:42:21 +00:00
v = (v << 6) + (rune(octet) & 0x3F)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i += w
if !put(emitter, '\\') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
var ok bool
2024-02-18 10:42:21 +00:00
switch v {
2024-02-18 10:42:21 +00:00
case 0x00:
2024-02-18 10:42:21 +00:00
ok = put(emitter, '0')
2024-02-18 10:42:21 +00:00
case 0x07:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'a')
2024-02-18 10:42:21 +00:00
case 0x08:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'b')
2024-02-18 10:42:21 +00:00
case 0x09:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 't')
2024-02-18 10:42:21 +00:00
case 0x0A:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'n')
2024-02-18 10:42:21 +00:00
case 0x0b:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'v')
2024-02-18 10:42:21 +00:00
case 0x0c:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'f')
2024-02-18 10:42:21 +00:00
case 0x0d:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'r')
2024-02-18 10:42:21 +00:00
case 0x1b:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'e')
2024-02-18 10:42:21 +00:00
case 0x22:
2024-02-18 10:42:21 +00:00
ok = put(emitter, '"')
2024-02-18 10:42:21 +00:00
case 0x5c:
2024-02-18 10:42:21 +00:00
ok = put(emitter, '\\')
2024-02-18 10:42:21 +00:00
case 0x85:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'N')
2024-02-18 10:42:21 +00:00
case 0xA0:
2024-02-18 10:42:21 +00:00
ok = put(emitter, '_')
2024-02-18 10:42:21 +00:00
case 0x2028:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'L')
2024-02-18 10:42:21 +00:00
case 0x2029:
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'P')
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
if v <= 0xFF {
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'x')
2024-02-18 10:42:21 +00:00
w = 2
2024-02-18 10:42:21 +00:00
} else if v <= 0xFFFF {
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'u')
2024-02-18 10:42:21 +00:00
w = 4
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
ok = put(emitter, 'U')
2024-02-18 10:42:21 +00:00
w = 8
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for k := (w - 1) * 4; ok && k >= 0; k -= 4 {
2024-02-18 10:42:21 +00:00
digit := byte((v >> uint(k)) & 0x0F)
2024-02-18 10:42:21 +00:00
if digit < 10 {
2024-02-18 10:42:21 +00:00
ok = put(emitter, digit+'0')
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
ok = put(emitter, digit+'A'-10)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
spaces = false
2024-02-18 10:42:21 +00:00
} else if is_space(value, i) {
2024-02-18 10:42:21 +00:00
if allow_breaks && !spaces && emitter.column > emitter.best_width && i > 0 && i < len(value)-1 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if is_space(value, i+1) {
2024-02-18 10:42:21 +00:00
if !put(emitter, '\\') {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i += width(value[i])
2024-02-18 10:42:21 +00:00
} else if !write(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
spaces = true
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if !write(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
spaces = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'"'}, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.whitespace = false
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_block_scalar_hints(emitter *yaml_emitter_t, value []byte) bool {
2024-02-18 10:42:21 +00:00
if is_space(value, 0) || is_break(value, 0) {
2024-02-18 10:42:21 +00:00
indent_hint := []byte{'0' + byte(emitter.best_indent)}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, indent_hint, false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
emitter.open_ended = false
var chomp_hint [1]byte
2024-02-18 10:42:21 +00:00
if len(value) == 0 {
2024-02-18 10:42:21 +00:00
chomp_hint[0] = '-'
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
i := len(value) - 1
2024-02-18 10:42:21 +00:00
for value[i]&0xC0 == 0x80 {
2024-02-18 10:42:21 +00:00
i--
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !is_break(value, i) {
2024-02-18 10:42:21 +00:00
chomp_hint[0] = '-'
2024-02-18 10:42:21 +00:00
} else if i == 0 {
2024-02-18 10:42:21 +00:00
chomp_hint[0] = '+'
2024-02-18 10:42:21 +00:00
emitter.open_ended = true
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
i--
2024-02-18 10:42:21 +00:00
for value[i]&0xC0 == 0x80 {
2024-02-18 10:42:21 +00:00
i--
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if is_break(value, i) {
2024-02-18 10:42:21 +00:00
chomp_hint[0] = '+'
2024-02-18 10:42:21 +00:00
emitter.open_ended = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if chomp_hint[0] != 0 {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, chomp_hint[:], false, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_literal_scalar(emitter *yaml_emitter_t, value []byte) bool {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'|'}, true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
//emitter.indention = true
2024-02-18 10:42:21 +00:00
emitter.whitespace = true
2024-02-18 10:42:21 +00:00
breaks := true
2024-02-18 10:42:21 +00:00
for i := 0; i < len(value); {
2024-02-18 10:42:21 +00:00
if is_break(value, i) {
2024-02-18 10:42:21 +00:00
if !write_break(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
//emitter.indention = true
2024-02-18 10:42:21 +00:00
breaks = true
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if breaks {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !write(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
breaks = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_folded_scalar(emitter *yaml_emitter_t, value []byte) bool {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indicator(emitter, []byte{'>'}, true, false, false) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_block_scalar_hints(emitter, value) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !yaml_emitter_process_line_comment(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
//emitter.indention = true
2024-02-18 10:42:21 +00:00
emitter.whitespace = true
breaks := true
2024-02-18 10:42:21 +00:00
leading_spaces := true
2024-02-18 10:42:21 +00:00
for i := 0; i < len(value); {
2024-02-18 10:42:21 +00:00
if is_break(value, i) {
2024-02-18 10:42:21 +00:00
if !breaks && !leading_spaces && value[i] == '\n' {
2024-02-18 10:42:21 +00:00
k := 0
2024-02-18 10:42:21 +00:00
for is_break(value, k) {
2024-02-18 10:42:21 +00:00
k += width(value[k])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !is_blankz(value, k) {
2024-02-18 10:42:21 +00:00
if !put_break(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !write_break(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
//emitter.indention = true
2024-02-18 10:42:21 +00:00
breaks = true
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if breaks {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
leading_spaces = is_blank(value, i)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !breaks && is_space(value, i) && !is_space(value, i+1) && emitter.column > emitter.best_width {
2024-02-18 10:42:21 +00:00
if !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i += width(value[i])
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if !write(emitter, value, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
breaks = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}
func yaml_emitter_write_comment(emitter *yaml_emitter_t, comment []byte) bool {
2024-02-18 10:42:21 +00:00
breaks := false
2024-02-18 10:42:21 +00:00
pound := false
2024-02-18 10:42:21 +00:00
for i := 0; i < len(comment); {
2024-02-18 10:42:21 +00:00
if is_break(comment, i) {
2024-02-18 10:42:21 +00:00
if !write_break(emitter, comment, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
//emitter.indention = true
2024-02-18 10:42:21 +00:00
breaks = true
2024-02-18 10:42:21 +00:00
pound = false
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
if breaks && !yaml_emitter_write_indent(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !pound {
2024-02-18 10:42:21 +00:00
if comment[i] != '#' && (!put(emitter, '#') || !put(emitter, ' ')) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pound = true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !write(emitter, comment, &i) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
emitter.indention = false
2024-02-18 10:42:21 +00:00
breaks = false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if !breaks && !put_break(emitter) {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
emitter.whitespace = true
2024-02-18 10:42:21 +00:00
//emitter.indention = true
2024-02-18 10:42:21 +00:00
return true
2024-02-18 10:42:21 +00:00
}