Serialize as many tags as possible

This commit is contained in:
Phil Turnbull 2018-08-07 16:38:54 -04:00
parent 5877d9a30d
commit 37b1bd4eef
No known key found for this signature in database
GPG Key ID: D81B30C00789D262
1 changed files with 10 additions and 9 deletions

View File

@ -26,25 +26,26 @@ struct Scanner {
Scanner() {} Scanner() {}
unsigned serialize(char *buffer) { unsigned serialize(char *buffer) {
unsigned i = 0; unsigned tag_count = 0;
unsigned n = tags.size(); unsigned i = sizeof(tag_count);
std::memcpy(buffer, &n, sizeof(n));
i += sizeof(n); for (unsigned n = tags.size(); tag_count < n; tag_count++) {
for (unsigned j = 0; j < n; j++) { Tag &tag = tags[tag_count];
Tag &tag = tags[j];
if (tag.type == CUSTOM) { if (tag.type == CUSTOM) {
unsigned name_length = tag.custom_tag_name.size(); unsigned name_length = tag.custom_tag_name.size();
if (name_length > UINT8_MAX) return 0; if (name_length > UINT8_MAX) break;
if (i + 2 + name_length >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) return 0; if (i + 2 + name_length >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) break;
buffer[i++] = static_cast<char>(tag.type); buffer[i++] = static_cast<char>(tag.type);
buffer[i++] = name_length; buffer[i++] = name_length;
tag.custom_tag_name.copy(&buffer[i], name_length); tag.custom_tag_name.copy(&buffer[i], name_length);
i += name_length; i += name_length;
} else { } else {
if (i + 1 >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) return 0; if (i + 1 >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) break;
buffer[i++] = static_cast<char>(tag.type); buffer[i++] = static_cast<char>(tag.type);
} }
} }
std::memcpy(buffer, &tag_count, sizeof(tag_count));
return i; return i;
} }