Skip to content

Commit

Permalink
Merge pull request #51 from go-faster/fix/tag-emit
Browse files Browse the repository at this point in the history
fix: escape shorthand tags if needed
  • Loading branch information
ernado committed Apr 10, 2023
2 parents ed711fb + 41fb464 commit 29cddc6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 7 additions & 5 deletions emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func yaml_emitter_emit_document_start(emitter *yaml_emitter_t, event *yaml_event
if !yaml_emitter_write_tag_handle(emitter, tag_directive.handle) {
return false
}
if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true) {
if !yaml_emitter_write_tag_content(emitter, tag_directive.prefix, true, true) {
return false
}
if !yaml_emitter_write_indent(emitter) {
Expand Down Expand Up @@ -1080,7 +1080,7 @@ func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool {
return false
}
if len(emitter.tag_data.suffix) > 0 {
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, need_brackets, false) {
return false
}
}
Expand All @@ -1094,7 +1094,7 @@ func yaml_emitter_process_tag(emitter *yaml_emitter_t) bool {
if !yaml_emitter_write_indicator(emitter, []byte("!<"), true, false, false) {
return false
}
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false) {
if !yaml_emitter_write_tag_content(emitter, emitter.tag_data.suffix, false, false) {
return false
}
if !yaml_emitter_write_indicator(emitter, []byte{'>'}, false, false, false) {
Expand Down Expand Up @@ -1562,7 +1562,7 @@ func yaml_emitter_write_tag_handle(emitter *yaml_emitter_t, value []byte) bool {
return true
}

func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_whitespace bool) bool {
func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, allow_uri, need_whitespace bool) bool {
if need_whitespace && !emitter.whitespace {
if !put(emitter, ' ') {
return false
Expand All @@ -1571,8 +1571,10 @@ func yaml_emitter_write_tag_content(emitter *yaml_emitter_t, value []byte, need_
for i := 0; i < len(value); {
var must_write bool
switch value[i] {
case ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\'', '(', ')', '[', ']':
case ';', '/', '?', ':', '@', '&', '=', '+', '$', '_', '.', '~', '*', '\'', '(', ')':
must_write = true
case '{', '}', '[', ']', ',':
must_write = allow_uri
default:
must_write = is_alpha(value, i)
}
Expand Down
15 changes: 15 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,21 @@ var marshalTests = []struct {
},
"!<!!>\n",
},
// https://github.com/go-faster/yaml/issues/50
{
yaml.Node{
Kind: yaml.ScalarNode,
Tag: "[",
},
"!<%5B>\n",
},
{
yaml.Node{
Kind: yaml.ScalarNode,
Tag: "]",
},
"!<%5D>\n",
},

// Enforced tagging with shorthand notation (issue #616).
{
Expand Down

0 comments on commit 29cddc6

Please sign in to comment.