From d8a59a5f5c4af06ecec4d69b7c2f0a5d80a484fc Mon Sep 17 00:00:00 2001 From: Syed Daanish Date: Tue, 28 Jul 2026 19:18:31 +0100 Subject: [PATCH] Fix bug in split and add line->byte lookup. --- include/vase/vase.h | 2 ++ src/main.cc | 4 +++- src/vase/shard.cc | 4 ++-- src/vase/vase.cc | 26 ++++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/vase/vase.cc diff --git a/include/vase/vase.h b/include/vase/vase.h index c8299fa..aae65b8 100644 --- a/include/vase/vase.h +++ b/include/vase/vase.h @@ -105,4 +105,6 @@ struct Vase { flatten(b->right, out); } } + + uint32_t offset_of(uint32_t line_number, uint32_t col); }; diff --git a/src/main.cc b/src/main.cc index 74c8ba0..ba37426 100644 --- a/src/main.cc +++ b/src/main.cc @@ -26,10 +26,12 @@ int main() { vase.type(7, '3'); vase.type(8, '4'); - vase.erase(9, -2); + vase.erase(vase.offset_of(7, 3), -3); print_shard(vase.root); + std::cout << (int)vase.offset_of(6, 0) << "\n\n"; + LineIterator it(vase.root, 3); std::string line; while (it.next(line)) diff --git a/src/vase/shard.cc b/src/vase/shard.cc index e949b60..4b4ca69 100644 --- a/src/vase/shard.cc +++ b/src/vase/shard.cc @@ -125,13 +125,13 @@ std::pair split_shard(Shard *n, uint32_t offset) { Petal *p = (Petal *)n; auto left = new Petal( offset, - p->source->count_lines(0, offset), + p->source->count_lines(p->pos, offset), p->source, p->pos ); auto right = new Petal( p->length - offset, - p->source->count_lines(offset, p->length), + p->source->count_lines(p->pos + offset, p->length - offset), p->source, p->pos + offset ); diff --git a/src/vase/vase.cc b/src/vase/vase.cc new file mode 100644 index 0000000..97c744a --- /dev/null +++ b/src/vase/vase.cc @@ -0,0 +1,26 @@ +#include "vase/vase.h" + +uint32_t Vase::offset_of(uint32_t line_number, uint32_t col) { + if (line_number == 0) + return col; + + Shard *s = root; + uint32_t nth = line_number; + uint32_t base_offset = 0; + + while (s->kind == Shard::ShardKind::Branch) { + auto *b = static_cast(s); + if (nth <= b->left->lines) { + s = b->left; + } else { + nth -= b->left->lines; + base_offset += b->left->length; + s = b->right; + } + } + + auto *petal = static_cast(s); + uint32_t nl_pos = petal->source->nth_newline(petal->pos, nth); + uint32_t offset_in_petal = (nl_pos - petal->pos) + 1; + return base_offset + offset_in_petal + col; +}