From 37b1bd4eef902785613e6aa9bf1a7b937b5126a3 Mon Sep 17 00:00:00 2001 From: Phil Turnbull Date: Tue, 7 Aug 2018 16:38:54 -0400 Subject: [PATCH] Serialize as many tags as possible --- src/scanner.cc | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/scanner.cc b/src/scanner.cc index dc1be41..f4b83a5 100644 --- a/src/scanner.cc +++ b/src/scanner.cc @@ -26,25 +26,26 @@ struct Scanner { Scanner() {} unsigned serialize(char *buffer) { - unsigned i = 0; - unsigned n = tags.size(); - std::memcpy(buffer, &n, sizeof(n)); - i += sizeof(n); - for (unsigned j = 0; j < n; j++) { - Tag &tag = tags[j]; + unsigned tag_count = 0; + unsigned i = sizeof(tag_count); + + for (unsigned n = tags.size(); tag_count < n; tag_count++) { + Tag &tag = tags[tag_count]; if (tag.type == CUSTOM) { unsigned name_length = tag.custom_tag_name.size(); - if (name_length > UINT8_MAX) return 0; - if (i + 2 + name_length >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) return 0; + if (name_length > UINT8_MAX) break; + if (i + 2 + name_length >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) break; buffer[i++] = static_cast(tag.type); buffer[i++] = name_length; tag.custom_tag_name.copy(&buffer[i], name_length); i += name_length; } else { - if (i + 1 >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) return 0; + if (i + 1 >= TREE_SITTER_SERIALIZATION_BUFFER_SIZE) break; buffer[i++] = static_cast(tag.type); } } + + std::memcpy(buffer, &tag_count, sizeof(tag_count)); return i; }