Fix bug in split and add line->byte lookup.

This commit is contained in:
2026-07-28 19:18:31 +01:00
parent 784d2736e2
commit d8a59a5f5c
4 changed files with 33 additions and 3 deletions
+2
View File
@@ -105,4 +105,6 @@ struct Vase {
flatten(b->right, out);
}
}
uint32_t offset_of(uint32_t line_number, uint32_t col);
};
+3 -1
View File
@@ -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))
+2 -2
View File
@@ -125,13 +125,13 @@ std::pair<Shard *, Shard *> 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
);
+26
View File
@@ -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<Branch *>(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<Petal *>(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;
}