Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 25c4ec8

Browse files
authored
Merge pull request #272 from rogpeppe/005-cleaner-tag-scan
Make tag scanning code slightly cleaner.
2 parents 670d4cf + e9bfed5 commit 25c4ec8

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

decode_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ type inlineC struct {
610610
}
611611

612612
func (s *S) TestUnmarshal(c *C) {
613-
for _, item := range unmarshalTests {
613+
for i, item := range unmarshalTests {
614+
c.Logf("test %d: %q", i, item.data)
614615
t := reflect.ValueOf(item.value).Type()
615616
var value interface{}
616617
switch t.Kind() {
@@ -654,6 +655,7 @@ var unmarshalErrorTests = []struct {
654655
{"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
655656
{"{[.]}", `yaml: invalid map key: \[\]interface \{\}\{"\."\}`},
656657
{"{{.}}", `yaml: invalid map key: map\[interface\ \{\}\]interface \{\}\{".":interface \{\}\(nil\)\}`},
658+
{"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
657659
}
658660

659661
func (s *S) TestUnmarshalErrors(c *C) {

scannerc.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ func yaml_parser_scan_tag_handle(parser *yaml_parser_t, directive bool, start_ma
19441944
} else {
19451945
// It's either the '!' tag or not really a tag handle. If it's a %TAG
19461946
// directive, it's an error. If it's a tag token, it must be a part of URI.
1947-
if directive && !(s[0] == '!' && s[1] == 0) {
1947+
if directive && string(s) != "!" {
19481948
yaml_parser_set_scanner_tag_error(parser, directive,
19491949
start_mark, "did not find expected '!'")
19501950
return false
@@ -1959,12 +1959,12 @@ func yaml_parser_scan_tag_handle(parser *yaml_parser_t, directive bool, start_ma
19591959
func yaml_parser_scan_tag_uri(parser *yaml_parser_t, directive bool, head []byte, start_mark yaml_mark_t, uri *[]byte) bool {
19601960
//size_t length = head ? strlen((char *)head) : 0
19611961
var s []byte
1962-
length := len(head)
1962+
hasTag := len(head) > 0
19631963

19641964
// Copy the head if needed.
19651965
//
19661966
// Note that we don't copy the leading '!' character.
1967-
if length > 0 {
1967+
if len(head) > 1 {
19681968
s = append(s, head[1:]...)
19691969
}
19701970

@@ -1997,15 +1997,14 @@ func yaml_parser_scan_tag_uri(parser *yaml_parser_t, directive bool, head []byte
19971997
}
19981998
} else {
19991999
s = read(parser, s)
2000-
length++
20012000
}
20022001
if parser.unread < 1 && !yaml_parser_update_buffer(parser, 1) {
20032002
return false
20042003
}
2004+
hasTag = true
20052005
}
20062006

2007-
// Check if the tag is non-empty.
2008-
if length == 0 {
2007+
if !hasTag {
20092008
yaml_parser_set_scanner_tag_error(parser, directive,
20102009
start_mark, "did not find expected tag URI")
20112010
return false

0 commit comments

Comments
 (0)