Cheap sanitization: - Reduce huge buffer sizes. - Drop some of the duplicated allocation. - Drop sub-structs that used to compose a union, and inline fields.
diff --git a/api_c.go b/api_c.go index 6d75786..ded1b99 100644 --- a/api_c.go +++ b/api_c.go
@@ -27,14 +27,8 @@ // Create a new parser object. func yaml_parser_initialize(parser *yaml_parser_t) bool { *parser = yaml_parser_t{ - raw_buffer: make([]byte, 0, input_raw_buffer_size), - buffer: make([]byte, 0, input_buffer_size), - //tokens: make([]yaml_token_t, 0, initial_queue_size), - //indents: make([]int, 0, initial_stack_size), - //simple_keys: make([]yaml_simple_key_t, 0, initial_stack_size), - //states: make([]yaml_parser_state_t, 0, initial_stack_size), - //marks: make([]yaml_mark_t, 0, initial_stack_size), - //tag_directives: make([]yaml_tag_directive_t, 0, initial_stack_size), + raw_buffer: make([]byte, 0, input_raw_buffer_size), + buffer: make([]byte, 0, input_buffer_size), } return true } @@ -88,14 +82,11 @@ // Create a new emitter object. func yaml_emitter_initialize(emitter *yaml_emitter_t) bool { - // [Go] These should be initialized lazily instead. *emitter = yaml_emitter_t{ buffer: make([]byte, output_buffer_size), raw_buffer: make([]byte, 0, output_raw_buffer_size), states: make([]yaml_emitter_state_t, 0, initial_stack_size), events: make([]yaml_event_t, 0, initial_queue_size), - indents: make([]int, 0, initial_stack_size), - tag_directives: make([]yaml_tag_directive_t, 0, initial_stack_size), } return true } @@ -263,9 +254,9 @@ // Create STREAM-START. func yaml_stream_start_event_initialize(event *yaml_event_t, encoding yaml_encoding_t) bool { *event = yaml_event_t{ - typ: yaml_STREAM_START_EVENT, + typ: yaml_STREAM_START_EVENT, + encoding: encoding, } - event.stream_start.encoding = encoding return true } @@ -280,34 +271,21 @@ // Create DOCUMENT-START. func yaml_document_start_event_initialize(event *yaml_event_t, version_directive *yaml_version_directive_t, tag_directives []yaml_tag_directive_t, implicit bool) bool { - - // [Go] It doesn't sound necessary to perform these copies - // given that with garbage collection ownership is handled. - var version_directive_copy *yaml_version_directive_t - var tag_directives_copy []yaml_tag_directive_t - if version_directive != nil { - copy := *version_directive - version_directive_copy = © - } - if len(tag_directives) > 0 { - tag_directives_copy = append([]yaml_tag_directive_t(nil), tag_directives...) - } - *event = yaml_event_t{ - typ: yaml_DOCUMENT_START_EVENT, + typ: yaml_DOCUMENT_START_EVENT, + version_directive: version_directive, + tag_directives: tag_directives, + implicit: implicit, } - event.document_start.version_directive = version_directive_copy - event.document_start.tag_directives = tag_directives_copy - event.document_start.implicit = implicit return true } // Create DOCUMENT-END. func yaml_document_end_event_initialize(event *yaml_event_t, implicit bool) bool { *event = yaml_event_t{ - typ: yaml_DOCUMENT_END_EVENT, + typ: yaml_DOCUMENT_END_EVENT, + implicit: implicit, } - event.document_end.implicit = implicit return true } @@ -336,56 +314,28 @@ //} // Create SCALAR. -func yaml_scalar_event_initialize(event *yaml_event_t, anchor, tag, value []byte, - plain_implicit, quoted_implicit bool, style yaml_scalar_style_t) bool { - var anchor_copy, tag_copy, value_copy []byte - - // [Go] These copies are probably not necessary in Go, where - // ownership of data is more flexible due to garbage collection. - if len(anchor) > 0 { - //if !yaml_check_utf8(anchor) { return false } - anchor_copy = append([]byte(nil), anchor...) - } - if len(tag) > 0 { - //if !yaml_check_utf8(tag) { return false } - tag_copy = append([]byte(nil), tag...) - } - //if !yaml_check_utf8(value) { return false } - value_copy = append([]byte(nil), value...) - +func yaml_scalar_event_initialize(event *yaml_event_t, anchor, tag, value []byte, plain_implicit, quoted_implicit bool, style yaml_scalar_style_t) bool { *event = yaml_event_t{ - typ: yaml_SCALAR_EVENT, + typ: yaml_SCALAR_EVENT, + anchor: anchor, + tag: tag, + value: value, + implicit: plain_implicit, + quoted_implicit: quoted_implicit, + style: yaml_style_t(style), } - event.scalar.anchor = anchor_copy - event.scalar.tag = tag_copy - event.scalar.value = value_copy - event.scalar.plain_implicit = plain_implicit - event.scalar.quoted_implicit = quoted_implicit - event.scalar.style = style return true } // Create SEQUENCE-START. func yaml_sequence_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_sequence_style_t) bool { - // [Go] These copies are probably not necessary in Go, where - // ownership of data is more flexible due to garbage collection. - var anchor_copy, tag_copy []byte - if len(anchor) > 0 { - //if !yaml_check_utf8(anchor) { return false } - anchor_copy = append([]byte(nil), anchor...) - } - if len(tag) > 0 { - //if !yaml_check_utf8(tag) { return false } - tag_copy = append([]byte(nil), tag...) - } - *event = yaml_event_t{ - typ: yaml_SEQUENCE_START_EVENT, + typ: yaml_SEQUENCE_START_EVENT, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(style), } - event.sequence_start.anchor = anchor_copy - event.sequence_start.tag = tag_copy - event.sequence_start.implicit = implicit - event.sequence_start.style = style return true } @@ -399,25 +349,13 @@ // Create MAPPING-START. func yaml_mapping_start_event_initialize(event *yaml_event_t, anchor, tag []byte, implicit bool, style yaml_mapping_style_t) bool { - // [Go] These copies are probably not necessary in Go, where - // ownership of data is more flexible due to garbage collection. - var anchor_copy, tag_copy []byte - if len(anchor) > 0 { - //if !yaml_check_utf8(anchor) { return false } - anchor_copy = append([]byte(nil), anchor...) - } - if len(tag) > 0 { - //if !yaml_check_utf8(tag) { return false } - tag_copy = append([]byte(nil), tag...) - } - *event = yaml_event_t{ - typ: yaml_MAPPING_START_EVENT, + typ: yaml_MAPPING_START_EVENT, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(style), } - event.mapping_start.anchor = anchor_copy - event.mapping_start.tag = tag_copy - event.mapping_start.implicit = implicit - event.mapping_start.style = style return true }
diff --git a/decode.go b/decode.go index 6179e66..e7fb29b 100644 --- a/decode.go +++ b/decode.go
@@ -143,25 +143,24 @@ func (p *parser) alias() *node { n := p.node(aliasNode) - n.value = string(p.event.alias.anchor) + n.value = string(p.event.anchor) p.skip() return n } func (p *parser) scalar() *node { - scalar := p.event.scalar n := p.node(scalarNode) - n.value = string(scalar.value) - n.tag = string(scalar.tag) - n.implicit = scalar.plain_implicit - p.anchor(n, scalar.anchor) + n.value = string(p.event.value) + n.tag = string(p.event.tag) + n.implicit = p.event.implicit + p.anchor(n, p.event.anchor) p.skip() return n } func (p *parser) sequence() *node { n := p.node(sequenceNode) - p.anchor(n, p.event.sequence_start.anchor) + p.anchor(n, p.event.anchor) p.skip() for p.event.typ != yaml_SEQUENCE_END_EVENT { n.children = append(n.children, p.parse()) @@ -172,7 +171,7 @@ func (p *parser) mapping() *node { n := p.node(mappingNode) - p.anchor(n, p.event.mapping_start.anchor) + p.anchor(n, p.event.anchor) p.skip() for p.event.typ != yaml_MAPPING_END_EVENT { n.children = append(n.children, p.parse(), p.parse())
diff --git a/decode_test.go b/decode_test.go index 89a9fdb..8970b98 100644 --- a/decode_test.go +++ b/decode_test.go
@@ -1,6 +1,7 @@ package goyaml_test import ( + "io/ioutil" . "launchpad.net/gocheck" "launchpad.net/goyaml" "math" @@ -437,31 +438,31 @@ c.Assert(m["ghi"].value, Equals, 3) } -//var data []byte -//func init() { -// var err error -// data, err = ioutil.ReadFile("/tmp/file.yaml") -// if err != nil { -// panic(err) -// } -//} -// -//func (s *S) BenchmarkUnmarshal(c *C) { -// var err error -// for i := 0; i < c.N; i++ { -// var v map[string]interface{} -// err = goyaml.Unmarshal(data, &v) -// } -// if err != nil { -// panic(err) -// } -//} -// -//func (s *S) BenchmarkMarshal(c *C) { -// var v map[string]interface{} -// goyaml.Unmarshal(data, &v) -// c.ResetTimer() -// for i := 0; i < c.N; i++ { -// goyaml.Marshal(&v) -// } -//} +var data []byte +func init() { + var err error + data, err = ioutil.ReadFile("/tmp/file.yaml") + if err != nil { + panic(err) + } +} + +func (s *S) BenchmarkUnmarshal(c *C) { + var err error + for i := 0; i < c.N; i++ { + var v map[string]interface{} + err = goyaml.Unmarshal(data, &v) + } + if err != nil { + panic(err) + } +} + +func (s *S) BenchmarkMarshal(c *C) { + var v map[string]interface{} + goyaml.Unmarshal(data, &v) + c.ResetTimer() + for i := 0; i < c.N; i++ { + goyaml.Marshal(&v) + } +}
diff --git a/emitter_c.go b/emitter_c.go index 507a2fd..ac63f28 100644 --- a/emitter_c.go +++ b/emitter_c.go
@@ -274,7 +274,7 @@ return yaml_emitter_set_emitter_error(emitter, "expected STREAM-START") } if emitter.encoding == yaml_ANY_ENCODING { - emitter.encoding = event.stream_start.encoding + emitter.encoding = event.encoding if emitter.encoding == yaml_ANY_ENCODING { emitter.encoding = yaml_UTF8_ENCODING } @@ -312,14 +312,14 @@ if event.typ == yaml_DOCUMENT_START_EVENT { - if event.document_start.version_directive != nil { - if !yaml_emitter_analyze_version_directive(emitter, event.document_start.version_directive) { + if event.version_directive != nil { + if !yaml_emitter_analyze_version_directive(emitter, event.version_directive) { return false } } - for i := 0; i < len(event.document_start.tag_directives); i++ { - tag_directive := &event.document_start.tag_directives[i] + for i := 0; i < len(event.tag_directives); i++ { + tag_directive := &event.tag_directives[i] if !yaml_emitter_analyze_tag_directive(emitter, tag_directive) { return false } @@ -335,12 +335,12 @@ } } - implicit := event.document_start.implicit + implicit := event.implicit if !first || emitter.canonical { implicit = false } - if emitter.open_ended && (event.document_start.version_directive != nil || len(event.document_start.tag_directives) > 0) { + if emitter.open_ended && (event.version_directive != nil || len(event.tag_directives) > 0) { if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { return false } @@ -349,7 +349,7 @@ } } - if event.document_start.version_directive != nil { + if event.version_directive != nil { implicit = false if !yaml_emitter_write_indicator(emitter, []byte("%YAML"), true, false, false) { return false @@ -362,10 +362,10 @@ } } - if len(event.document_start.tag_directives) > 0 { + if len(event.tag_directives) > 0 { implicit = false - for i := 0; i < len(event.document_start.tag_directives); i++ { - tag_directive := &event.document_start.tag_directives[i] + for i := 0; i < len(event.tag_directives); i++ { + tag_directive := &event.tag_directives[i] if !yaml_emitter_write_indicator(emitter, []byte("%TAG"), true, false, false) { return false } @@ -435,7 +435,7 @@ if !yaml_emitter_write_indent(emitter) { return false } - if !event.document_end.implicit { + if !event.implicit { // [Go] Allocate the slice elsewhere. if !yaml_emitter_write_indicator(emitter, []byte("..."), true, false, false) { return false @@ -711,8 +711,7 @@ if !yaml_emitter_process_tag(emitter) { return false } - if emitter.flow_level > 0 || emitter.canonical || - event.sequence_start.style == yaml_FLOW_SEQUENCE_STYLE || + if emitter.flow_level > 0 || emitter.canonical || event.sequence_style() == yaml_FLOW_SEQUENCE_STYLE || yaml_emitter_check_empty_sequence(emitter) { emitter.state = yaml_EMIT_FLOW_SEQUENCE_FIRST_ITEM_STATE } else { @@ -729,8 +728,7 @@ if !yaml_emitter_process_tag(emitter) { return false } - if emitter.flow_level > 0 || emitter.canonical || - event.mapping_start.style == yaml_FLOW_MAPPING_STYLE || + if emitter.flow_level > 0 || emitter.canonical || event.mapping_style() == yaml_FLOW_MAPPING_STYLE || yaml_emitter_check_empty_mapping(emitter) { emitter.state = yaml_EMIT_FLOW_MAPPING_FIRST_KEY_STATE } else { @@ -800,11 +798,11 @@ 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 - if no_tag && !event.scalar.plain_implicit && !event.scalar.quoted_implicit { + if no_tag && !event.implicit && !event.quoted_implicit { return yaml_emitter_set_emitter_error(emitter, "neither tag nor implicit flags are specified") } - style := event.scalar.style + style := event.scalar_style() if style == yaml_ANY_SCALAR_STYLE { style = yaml_PLAIN_SCALAR_STYLE } @@ -823,7 +821,7 @@ if len(emitter.scalar_data.value) == 0 && (emitter.flow_level > 0 || emitter.simple_key_context) { style = yaml_SINGLE_QUOTED_SCALAR_STYLE } - if no_tag && !event.scalar.plain_implicit { + if no_tag && !event.implicit { style = yaml_SINGLE_QUOTED_SCALAR_STYLE } } @@ -838,7 +836,7 @@ } } - if no_tag && !event.scalar.quoted_implicit && style != yaml_PLAIN_SCALAR_STYLE { + if no_tag && !event.quoted_implicit && style != yaml_PLAIN_SCALAR_STYLE { emitter.tag_data.handle = []byte{'!'} } emitter.scalar_data.style = style @@ -1141,45 +1139,45 @@ switch event.typ { case yaml_ALIAS_EVENT: - if !yaml_emitter_analyze_anchor(emitter, event.alias.anchor, true) { + if !yaml_emitter_analyze_anchor(emitter, event.anchor, true) { return false } case yaml_SCALAR_EVENT: - if len(event.scalar.anchor) > 0 { - if !yaml_emitter_analyze_anchor(emitter, event.scalar.anchor, false) { + if len(event.anchor) > 0 { + if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) { return false } } - if len(event.scalar.tag) > 0 && (emitter.canonical || (!event.scalar.plain_implicit && !event.scalar.quoted_implicit)) { - if !yaml_emitter_analyze_tag(emitter, event.scalar.tag) { + if len(event.tag) > 0 && (emitter.canonical || (!event.implicit && !event.quoted_implicit)) { + if !yaml_emitter_analyze_tag(emitter, event.tag) { return false } } - if !yaml_emitter_analyze_scalar(emitter, event.scalar.value) { + if !yaml_emitter_analyze_scalar(emitter, event.value) { return false } case yaml_SEQUENCE_START_EVENT: - if len(event.sequence_start.anchor) > 0 { - if !yaml_emitter_analyze_anchor(emitter, event.sequence_start.anchor, false) { + if len(event.anchor) > 0 { + if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) { return false } } - if len(event.sequence_start.tag) > 0 && (emitter.canonical || !event.sequence_start.implicit) { - if !yaml_emitter_analyze_tag(emitter, event.sequence_start.tag) { + if len(event.tag) > 0 && (emitter.canonical || !event.implicit) { + if !yaml_emitter_analyze_tag(emitter, event.tag) { return false } } case yaml_MAPPING_START_EVENT: - if len(event.mapping_start.anchor) > 0 { - if !yaml_emitter_analyze_anchor(emitter, event.mapping_start.anchor, false) { + if len(event.anchor) > 0 { + if !yaml_emitter_analyze_anchor(emitter, event.anchor, false) { return false } } - if len(event.mapping_start.tag) > 0 && (emitter.canonical || !event.mapping_start.implicit) { - if !yaml_emitter_analyze_tag(emitter, event.mapping_start.tag) { + if len(event.tag) > 0 && (emitter.canonical || !event.implicit) { + if !yaml_emitter_analyze_tag(emitter, event.tag) { return false } }
diff --git a/parser_c.go b/parser_c.go index 5210b4f..0fdfa4e 100644 --- a/parser_c.go +++ b/parser_c.go
@@ -185,8 +185,8 @@ typ: yaml_STREAM_START_EVENT, start_mark: token.start_mark, end_mark: token.end_mark, + encoding: token.encoding, } - event.stream_start.encoding = token.stream_start.encoding skip_token(parser) return true } @@ -253,13 +253,13 @@ end_mark := token.end_mark *event = yaml_event_t{ - typ: yaml_DOCUMENT_START_EVENT, - start_mark: start_mark, - end_mark: end_mark, + typ: yaml_DOCUMENT_START_EVENT, + start_mark: start_mark, + end_mark: end_mark, + version_directive: version_directive, + tag_directives: tag_directives, + implicit: false, } - event.document_start.version_directive = version_directive - event.document_start.tag_directives = tag_directives - event.document_start.implicit = false skip_token(parser) } else { @@ -326,8 +326,8 @@ typ: yaml_DOCUMENT_END_EVENT, start_mark: start_mark, end_mark: end_mark, + implicit: implicit, } - event.document_start.implicit = implicit return true } @@ -372,8 +372,8 @@ typ: yaml_ALIAS_EVENT, start_mark: token.start_mark, end_mark: token.end_mark, + anchor: token.value, } - event.alias.anchor = token.alias.value skip_token(parser) return true } @@ -385,7 +385,7 @@ var tag_handle, tag_suffix, anchor []byte var tag_mark yaml_mark_t if token.typ == yaml_ANCHOR_TOKEN { - anchor = token.anchor.value + anchor = token.value start_mark = token.start_mark end_mark = token.end_mark skip_token(parser) @@ -395,8 +395,8 @@ } if token.typ == yaml_TAG_TOKEN { tag_token = true - tag_handle = token.tag.handle - tag_suffix = token.tag.suffix + tag_handle = token.value + tag_suffix = token.suffix tag_mark = token.start_mark end_mark = token.end_mark skip_token(parser) @@ -407,8 +407,8 @@ } } else if token.typ == yaml_TAG_TOKEN { tag_token = true - tag_handle = token.tag.handle - tag_suffix = token.tag.suffix + tag_handle = token.value + tag_suffix = token.suffix start_mark = token.start_mark tag_mark = token.start_mark end_mark = token.end_mark @@ -418,7 +418,7 @@ return false } if token.typ == yaml_ANCHOR_TOKEN { - anchor = token.anchor.value + anchor = token.value end_mark = token.end_mark skip_token(parser) token = peek_token(parser) @@ -458,17 +458,17 @@ typ: yaml_SEQUENCE_START_EVENT, start_mark: start_mark, end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE), } - event.sequence_start.anchor = anchor - event.sequence_start.tag = tag - event.sequence_start.implicit = implicit - event.sequence_start.style = yaml_BLOCK_SEQUENCE_STYLE return true } if token.typ == yaml_SCALAR_TOKEN { var plain_implicit, quoted_implicit bool end_mark = token.end_mark - if (len(tag) == 0 && token.scalar.style == yaml_PLAIN_SCALAR_STYLE) || (len(tag) == 1 && tag[0] == '!') { + if (len(tag) == 0 && token.style == yaml_PLAIN_SCALAR_STYLE) || (len(tag) == 1 && tag[0] == '!') { plain_implicit = true } else if len(tag) == 0 { quoted_implicit = true @@ -477,16 +477,16 @@ parser.states = parser.states[:len(parser.states)-1] *event = yaml_event_t{ - typ: yaml_SCALAR_EVENT, - start_mark: start_mark, - end_mark: end_mark, + typ: yaml_SCALAR_EVENT, + start_mark: start_mark, + end_mark: end_mark, + anchor: anchor, + tag: tag, + value: token.value, + implicit: plain_implicit, + quoted_implicit: quoted_implicit, + style: yaml_style_t(token.style), } - event.scalar.anchor = anchor - event.scalar.tag = tag - event.scalar.value = token.scalar.value - event.scalar.plain_implicit = plain_implicit - event.scalar.quoted_implicit = quoted_implicit - event.scalar.style = token.scalar.style skip_token(parser) return true } @@ -498,11 +498,11 @@ typ: yaml_SEQUENCE_START_EVENT, start_mark: start_mark, end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_FLOW_SEQUENCE_STYLE), } - event.sequence_start.anchor = anchor - event.sequence_start.tag = tag - event.sequence_start.implicit = implicit - event.sequence_start.style = yaml_FLOW_SEQUENCE_STYLE return true } if token.typ == yaml_FLOW_MAPPING_START_TOKEN { @@ -512,11 +512,11 @@ typ: yaml_MAPPING_START_EVENT, start_mark: start_mark, end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_FLOW_MAPPING_STYLE), } - event.mapping_start.anchor = anchor - event.mapping_start.tag = tag - event.mapping_start.implicit = implicit - event.mapping_start.style = yaml_FLOW_MAPPING_STYLE return true } if block && token.typ == yaml_BLOCK_SEQUENCE_START_TOKEN { @@ -526,11 +526,11 @@ typ: yaml_SEQUENCE_START_EVENT, start_mark: start_mark, end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_BLOCK_SEQUENCE_STYLE), } - event.sequence_start.anchor = anchor - event.sequence_start.tag = tag - event.sequence_start.implicit = implicit - event.sequence_start.style = yaml_BLOCK_SEQUENCE_STYLE return true } if block && token.typ == yaml_BLOCK_MAPPING_START_TOKEN { @@ -540,11 +540,11 @@ typ: yaml_MAPPING_START_EVENT, start_mark: start_mark, end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + style: yaml_style_t(yaml_BLOCK_MAPPING_STYLE), } - event.mapping_start.anchor = anchor - event.mapping_start.tag = tag - event.mapping_start.implicit = implicit - event.mapping_start.style = yaml_BLOCK_MAPPING_STYLE return true } if len(anchor) > 0 || len(tag) > 0 { @@ -552,15 +552,15 @@ parser.states = parser.states[:len(parser.states)-1] *event = yaml_event_t{ - typ: yaml_SCALAR_EVENT, - start_mark: start_mark, - end_mark: end_mark, + typ: yaml_SCALAR_EVENT, + start_mark: start_mark, + end_mark: end_mark, + anchor: anchor, + tag: tag, + implicit: implicit, + quoted_implicit: false, + style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE), } - event.scalar.anchor = anchor - event.scalar.tag = tag - event.scalar.plain_implicit = implicit - event.scalar.quoted_implicit = false - event.scalar.style = yaml_PLAIN_SCALAR_STYLE return true } @@ -801,9 +801,9 @@ typ: yaml_MAPPING_START_EVENT, start_mark: token.start_mark, end_mark: token.end_mark, + implicit: true, + style: yaml_style_t(yaml_FLOW_MAPPING_STYLE), } - event.mapping_start.implicit = true - event.mapping_start.style = yaml_FLOW_MAPPING_STYLE skip_token(parser) return true } else if token.typ != yaml_FLOW_SEQUENCE_END_TOKEN { @@ -998,10 +998,10 @@ typ: yaml_SCALAR_EVENT, start_mark: mark, end_mark: mark, + value: nil, // Empty + implicit: true, + style: yaml_style_t(yaml_PLAIN_SCALAR_STYLE), } - event.scalar.plain_implicit = true - event.scalar.style = yaml_PLAIN_SCALAR_STYLE - // Empty means len(event.scalar.value) == 0 return true } @@ -1030,19 +1030,19 @@ "found duplicate %YAML directive", token.start_mark) return false } - if token.version_directive.major != 1 || token.version_directive.minor != 1 { + if token.major != 1 || token.minor != 1 { yaml_parser_set_parser_error(parser, "found incompatible YAML document", token.start_mark) return false } version_directive = &yaml_version_directive_t{ - major: token.version_directive.major, - minor: token.version_directive.minor, + major: token.major, + minor: token.minor, } } else if token.typ == yaml_TAG_DIRECTIVE_TOKEN { value := yaml_tag_directive_t{ - handle: token.tag_directive.handle, - prefix: token.tag_directive.prefix, + handle: token.value, + prefix: token.prefix, } if !yaml_parser_append_tag_directive(parser, value, false, token.start_mark) { return false
diff --git a/scanner_c.go b/scanner_c.go index c0e611a..3589318 100644 --- a/scanner_c.go +++ b/scanner_c.go
@@ -990,8 +990,8 @@ typ: yaml_STREAM_START_TOKEN, start_mark: parser.mark, end_mark: parser.mark, + encoding: parser.encoding, } - token.stream_start.encoding = parser.encoding yaml_insert_token(parser, -1, &token) return true } @@ -1494,7 +1494,7 @@ // Is it a YAML directive? if bytes.Equal(name, []byte("YAML")) { // Scan the VERSION directive value. - var major, minor int + var major, minor int8 if !yaml_parser_scan_version_directive_value(parser, start_mark, &major, &minor) { return false } @@ -1505,9 +1505,9 @@ typ: yaml_VERSION_DIRECTIVE_TOKEN, start_mark: start_mark, end_mark: end_mark, + major: major, + minor: minor, } - token.version_directive.major = major - token.version_directive.minor = minor // Is it a TAG directive? } else if bytes.Equal(name, []byte("TAG")) { @@ -1523,9 +1523,9 @@ typ: yaml_TAG_DIRECTIVE_TOKEN, start_mark: start_mark, end_mark: end_mark, + value: handle, + prefix: prefix, } - token.tag_directive.handle = handle - token.tag_directive.prefix = prefix // Unknown directive. } else { @@ -1619,7 +1619,7 @@ // Scope: // %YAML 1.1 # a comment \n // ^^^^^^ -func yaml_parser_scan_version_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, major, minor *int) bool { +func yaml_parser_scan_version_directive_value(parser *yaml_parser_t, start_mark yaml_mark_t, major, minor *int8) bool { // Eat whitespaces. if !cache(parser, 1) { return false @@ -1651,7 +1651,7 @@ return true } -const max_number_length = 9 +const max_number_length = 2 // Scan the version number of VERSION-DIRECTIVE. // @@ -1660,13 +1660,13 @@ // ^ // %YAML 1.1 # a comment \n // ^ -func yaml_parser_scan_version_directive_number(parser *yaml_parser_t, start_mark yaml_mark_t, number *int) bool { +func yaml_parser_scan_version_directive_number(parser *yaml_parser_t, start_mark yaml_mark_t, number *int8) bool { // Repeat while the next character is digit. if !cache(parser, 1) { return false } - var value, length int + var value, length int8 for is_digit(parser.buffer, parser.buffer_pos) { // Check if the number is too long. length++ @@ -1674,7 +1674,7 @@ return yaml_parser_set_scanner_error(parser, "while scanning a %YAML directive", start_mark, "found extremely long version number") } - value = value*10 + as_digit(parser.buffer, parser.buffer_pos) + value = value*10 + int8(as_digit(parser.buffer, parser.buffer_pos)) skip(parser) if !cache(parser, 1) { return false @@ -1804,12 +1804,7 @@ typ: typ, start_mark: start_mark, end_mark: end_mark, - } - // [Go] Just use a single field instead. - if typ == yaml_ANCHOR_TOKEN { - token.anchor.value = s - } else { - token.alias.value = s + value: s, } return true @@ -1897,9 +1892,9 @@ typ: yaml_TAG_TOKEN, start_mark: start_mark, end_mark: end_mark, + value: handle, + suffix: suffix, } - token.tag.handle = handle - token.tag.suffix = suffix return true } @@ -2241,14 +2236,12 @@ typ: yaml_SCALAR_TOKEN, start_mark: start_mark, end_mark: end_mark, + value: s, + style: yaml_LITERAL_SCALAR_STYLE, } - token.scalar.value = s - if literal { - token.scalar.style = yaml_LITERAL_SCALAR_STYLE - } else { - token.scalar.style = yaml_FOLDED_SCALAR_STYLE + if !literal { + token.style = yaml_FOLDED_SCALAR_STYLE } - return true } @@ -2566,12 +2559,11 @@ typ: yaml_SCALAR_TOKEN, start_mark: start_mark, end_mark: end_mark, + value: s, + style: yaml_SINGLE_QUOTED_SCALAR_STYLE, } - token.scalar.value = s - if single { - token.scalar.style = yaml_SINGLE_QUOTED_SCALAR_STYLE - } else { - token.scalar.style = yaml_DOUBLE_QUOTED_SCALAR_STYLE + if !single { + token.style = yaml_DOUBLE_QUOTED_SCALAR_STYLE } return true } @@ -2726,9 +2718,9 @@ typ: yaml_SCALAR_TOKEN, start_mark: start_mark, end_mark: end_mark, + value: s, + style: yaml_PLAIN_SCALAR_STYLE, } - token.scalar.value = s - token.scalar.style = yaml_PLAIN_SCALAR_STYLE // Note that we change the 'simple_key_allowed' flag. if leading_blanks {
diff --git a/yaml_h.go b/yaml_h.go index eb8931c..7733b86 100644 --- a/yaml_h.go +++ b/yaml_h.go
@@ -6,8 +6,8 @@ // The version directive data. type yaml_version_directive_t struct { - major int // The major version number. - minor int // The minor version number. + major int8 // The major version number. + minor int8 // The minor version number. } // The tag directive data. @@ -65,7 +65,9 @@ // Node Styles -type yaml_scalar_style_t int +type yaml_style_t int8 + +type yaml_scalar_style_t yaml_style_t // Scalar styles. const ( @@ -79,7 +81,7 @@ yaml_FOLDED_SCALAR_STYLE // The folded scalar style. ) -type yaml_sequence_style_t int +type yaml_sequence_style_t yaml_style_t // Sequence styles. const ( @@ -90,7 +92,7 @@ yaml_FLOW_SEQUENCE_STYLE // The flow sequence style. ) -type yaml_mapping_style_t int +type yaml_mapping_style_t yaml_style_t // Mapping styles. const ( @@ -190,64 +192,35 @@ // The token structure. type yaml_token_t struct { - // The token type. typ yaml_token_type_t - // The token data. + // The start/end of the token. + start_mark, end_mark yaml_mark_t - // [Go] These structs should all be flattened onto the outer - // struct, as many of them are naturally shared among the various - // token types. This will reduce the impact of the lack of a union. + // The stream encoding (for yaml_STREAM_START_TOKEN). + encoding yaml_encoding_t - // The stream start (for yaml_STREAM_START_TOKEN). - stream_start struct { - encoding yaml_encoding_t // The stream encoding. - } + // The alias/anchor/scalar value or tag/tag directive handle + // (for yaml_ALIAS_TOKEN, yaml_ANCHOR_TOKEN, yaml_SCALAR_TOKEN, yaml_TAG_TOKEN, yaml_TAG_DIRECTIVE_TOKEN). + value []byte - // The alias (for yaml_ALIAS_TOKEN). - alias struct { - value []byte // The alias value. - } + // The tag suffix (for yaml_TAG_TOKEN). + suffix []byte - // The anchor (for yaml_ANCHOR_TOKEN). - anchor struct { - value []byte // The anchor value. - } + // The tag directive prefix (for yaml_TAG_DIRECTIVE_TOKEN). + prefix []byte - // The tag (for yaml_TAG_TOKEN). - tag struct { - handle []byte // The tag handle. - suffix []byte // The tag suffix. - } + // The scalar style (for yaml_SCALAR_TOKEN). + style yaml_scalar_style_t - // The scalar value (for yaml_SCALAR_TOKEN). - scalar struct { - value []byte // The scalar value. - style yaml_scalar_style_t // The scalar style. - } - - // The version directive (for yaml_VERSION_DIRECTIVE_TOKEN). - version_directive struct { - major int // The major version number. - minor int // The minor version number. - } - - // The tag directive (for yaml_TAG_DIRECTIVE_TOKEN). - tag_directive struct { - handle []byte // The tag handle. - prefix []byte // The tag prefix. - } - - // The beginning of the token. - start_mark yaml_mark_t - // The end of the token. - end_mark yaml_mark_t + // The version directive major/minor (for yaml_VERSION_DIRECTIVE_TOKEN). + major, minor int8 } // Events -type yaml_event_type_t int +type yaml_event_type_t int8 // Event types. const ( @@ -272,64 +245,42 @@ // The event type. typ yaml_event_type_t - // The event data. + // The start and end of the event. + start_mark, end_mark yaml_mark_t - // The stream parameters (for yaml_STREAM_START_EVENT). - stream_start struct { - encoding yaml_encoding_t // The document encoding. - } + // The document encoding (for yaml_STREAM_START_EVENT). + encoding yaml_encoding_t - // The document parameters (for yaml_DOCUMENT_START_EVENT). - document_start struct { - version_directive *yaml_version_directive_t // The version directive. + // The version directive (for yaml_DOCUMENT_START_EVENT). + version_directive *yaml_version_directive_t - // The list of tag directives. - tag_directives []yaml_tag_directive_t - implicit bool // Is the document indicator implicit? - } + // The list of tag directives (for yaml_DOCUMENT_START_EVENT). + tag_directives []yaml_tag_directive_t - // The document end parameters (for yaml_DOCUMENT_END_EVENT). - document_end struct { - implicit bool // Is the document end indicator implicit? - } + // The anchor (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT, yaml_ALIAS_EVENT). + anchor []byte - // The alias parameters (for yaml_ALIAS_EVENT). - alias struct { - anchor []byte // The anchor. - } + // The tag (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT). + tag []byte - // The scalar parameters (for yaml_SCALAR_EVENT). - scalar struct { - anchor []byte // The anchor. - tag []byte // The tag. - value []byte // The scalar value. - length int // The length of the scalar value. - plain_implicit bool // Is the tag optional for the plain style? - quoted_implicit bool // Is the tag optional for any non-plain style? - style yaml_scalar_style_t // The scalar style. - } + // The scalar value (for yaml_SCALAR_EVENT). + value []byte - // The sequence parameters (for yaml_SEQUENCE_START_EVENT). - sequence_start struct { - anchor []byte // The anchor. - tag []byte // The tag. - implicit bool // Is the tag optional? - style yaml_sequence_style_t // The sequence style. - } + // Is the document start/end indicator implicit, or the tag optional? + // (for yaml_DOCUMENT_START_EVENT, yaml_DOCUMENT_END_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT, yaml_SCALAR_EVENT). + implicit bool - // The mapping parameters (for yaml_MAPPING_START_EVENT). - mapping_start struct { - anchor []byte // The anchor. - tag []byte // The tag. - implicit bool // Is the tag optional? - style yaml_mapping_style_t // The mapping style. - } + // Is the tag optional for any non-plain style? (for yaml_SCALAR_EVENT). + quoted_implicit bool - start_mark yaml_mark_t // The beginning of the event. - end_mark yaml_mark_t // The end of the event. - + // The style (for yaml_SCALAR_EVENT, yaml_SEQUENCE_START_EVENT, yaml_MAPPING_START_EVENT). + style yaml_style_t } +func (e *yaml_event_t) scalar_style() yaml_scalar_style_t { return yaml_scalar_style_t(e.style) } +func (e *yaml_event_t) sequence_style() yaml_sequence_style_t { return yaml_sequence_style_t(e.style) } +func (e *yaml_event_t) mapping_style() yaml_mapping_style_t { return yaml_mapping_style_t(e.style) } + // Nodes const ( @@ -420,9 +371,8 @@ start_implicit int // Is the document start indicator implicit? end_implicit int // Is the document end indicator implicit? - start_mark yaml_mark_t // The beginning of the document. - end_mark yaml_mark_t // The end of the document. - + // The start/end of the document. + start_mark, end_mark yaml_mark_t } // The prototype of a read handler.
diff --git a/yaml_private_h.go b/yaml_private_h.go index 781166e..9dd382f 100644 --- a/yaml_private_h.go +++ b/yaml_private_h.go
@@ -2,14 +2,14 @@ const ( // The size of the input raw buffer. - input_raw_buffer_size = 16384 + input_raw_buffer_size = 512 // The size of the input buffer. // It should be possible to decode the whole raw buffer. input_buffer_size = input_raw_buffer_size * 3 // The size of the output buffer. - output_buffer_size = 16384 + output_buffer_size = 128 // The size of the output raw buffer. // It should be possible to encode the whole output buffer.