Fix bug in split and add line->byte lookup.
This commit is contained in:
@@ -105,4 +105,6 @@ struct Vase {
|
||||
flatten(b->right, out);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t offset_of(uint32_t line_number, uint32_t col);
|
||||
};
|
||||
|
||||
+3
-1
@@ -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
@@ -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
|
||||
);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user