diff --git a/src/scanner.cc b/src/scanner.cc index 058e48f..9f96b12 100644 --- a/src/scanner.cc +++ b/src/scanner.cc @@ -26,41 +26,44 @@ struct Scanner { unsigned serialize(char *buffer) { unsigned i = 0; - - for (unsigned j = 0, n = tags.size(); j < n; j++) { + unsigned n = tags.size(); + buffer[i++] = n; + for (unsigned j = 0; j < n; j++) { Tag &tag = tags[j]; - buffer[i] = static_cast(tag.type); - i++; - + buffer[i++] = static_cast(tag.type); if (tag.type == CUSTOM) { - buffer[i++] = tag.custom_tag_name.size(); - tag.custom_tag_name.copy(&buffer[i], tag.custom_tag_name.size()); - i += tag.custom_tag_name.size(); + unsigned name_length = tag.custom_tag_name.size(); + buffer[i++] = name_length; + tag.custom_tag_name.copy(&buffer[i], name_length); + i += name_length; } } - return i; } void deserialize(const char *buffer, unsigned length) { tags.clear(); - - unsigned i = 0; - while (i < length) { - Tag tag(static_cast(buffer[i]), ""); - i++; - if (tag.type == CUSTOM) { - unsigned length = buffer[i++]; - tag.custom_tag_name.assign(&buffer[i], &buffer[i + length]); - i += length; + if (length > 0) { + unsigned i = 0; + unsigned n = buffer[i++]; + tags.resize(n); + for (unsigned j = 0; j < n; j++) { + Tag &tag = tags[j]; + tag.type = static_cast(buffer[i++]); + if (tag.type == CUSTOM) { + unsigned name_length = buffer[i++]; + tag.custom_tag_name.assign(&buffer[i], &buffer[i + name_length]); + i += name_length; + } } - tags.push_back(tag); } } string scan_tag_name(TSLexer *lexer) { string tag_name; - while (iswalnum(lexer->lookahead) || lexer->lookahead == '-' || lexer->lookahead == ':') { + while (iswalnum(lexer->lookahead) || + lexer->lookahead == '-' || + lexer->lookahead == ':') { tag_name += towupper(lexer->lookahead); lexer->advance(lexer, false); } diff --git a/src/tag.h b/src/tag.h index 0dc4440..429ebb2 100644 --- a/src/tag.h +++ b/src/tag.h @@ -310,6 +310,7 @@ struct Tag { TagType type; string custom_tag_name; + Tag() : type(DIV) {} Tag(TagType type, const string &name) : type(type), custom_tag_name(name) {} bool operator==(const Tag &other) const {