Major refractor
- Add regex search and replace - Add chunk iterator + update line iterator. - Cleanup/formatting.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
#define PCRE_WORKSPACE_SIZE 512
|
||||
|
||||
#include <mruby.h>
|
||||
#include <mruby/array.h>
|
||||
|
||||
@@ -9,21 +9,9 @@ struct OriginalBuffer : Buffer {
|
||||
std::vector<uint32_t> newlines;
|
||||
|
||||
// TODO: replace with file io or even lazy loaded file io for large files/logs etc. later.
|
||||
OriginalBuffer(char *buf, uint32_t length) : buf(buf), length(length) {
|
||||
const char *p = buf;
|
||||
const char *end = buf + length;
|
||||
while (p < end) {
|
||||
p = (const char *)memchr(p, '\n', end - p);
|
||||
if (!p)
|
||||
break;
|
||||
newlines.push_back(uint32_t(p - buf));
|
||||
p++;
|
||||
}
|
||||
}
|
||||
OriginalBuffer(char *buf, uint32_t length);
|
||||
|
||||
~OriginalBuffer() {
|
||||
free(buf);
|
||||
}
|
||||
~OriginalBuffer();
|
||||
|
||||
const char *read(uint32_t pos, uint32_t *out_len);
|
||||
uint32_t count_lines(uint32_t pos, uint32_t length);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "../shard.h"
|
||||
#include "pch.h"
|
||||
|
||||
struct ChunkIterator {
|
||||
Shard *root = nullptr;
|
||||
std::vector<Shard *> stack;
|
||||
Petal *petal = nullptr;
|
||||
uint32_t petal_offset = 0;
|
||||
|
||||
ChunkIterator(Shard *r, uint32_t offset = 0);
|
||||
|
||||
~ChunkIterator();
|
||||
|
||||
void seek(uint32_t offset);
|
||||
bool next(const char **data, uint32_t *out_len);
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "../shard.h"
|
||||
#include "chunk.h"
|
||||
#include "pch.h"
|
||||
|
||||
struct LineIterator {
|
||||
uint32_t offset;
|
||||
ChunkIterator it;
|
||||
|
||||
const char *cursor = nullptr;
|
||||
uint32_t remaining = 0;
|
||||
bool eof = false;
|
||||
|
||||
LineIterator(Shard *r, uint32_t start_line);
|
||||
|
||||
~LineIterator();
|
||||
|
||||
bool next(std::string &line);
|
||||
};
|
||||
@@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
#include "pch.h"
|
||||
#include "shard.h"
|
||||
|
||||
struct LineIterator {
|
||||
Shard *root = nullptr;
|
||||
std::vector<Branch *> stack;
|
||||
Petal *petal = nullptr;
|
||||
uint32_t petal_offset = 0;
|
||||
|
||||
LineIterator(Shard *r, uint32_t start_line);
|
||||
|
||||
~LineIterator();
|
||||
|
||||
LineIterator(const LineIterator &) = delete;
|
||||
LineIterator &operator=(const LineIterator &) = delete;
|
||||
LineIterator(LineIterator &&) = default;
|
||||
LineIterator &operator=(LineIterator &&) = default;
|
||||
|
||||
void seek_line(uint32_t line_index);
|
||||
bool next(std::string &line);
|
||||
|
||||
private:
|
||||
void descend(Shard *s);
|
||||
bool advance_petal();
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "pch.h"
|
||||
#include "vase/shard.h"
|
||||
|
||||
struct RegexGroup {
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
bool matched;
|
||||
};
|
||||
|
||||
struct RegexMatch {
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
std::vector<RegexGroup> groups{};
|
||||
};
|
||||
|
||||
const std::vector<RegexMatch> regex_search(Shard *root, std::string_view pattern_str, uint32_t start_offset, uint32_t end_offset, uint32_t flags, bool global);
|
||||
void print_regex(const std::vector<RegexMatch> &matches);
|
||||
@@ -70,4 +70,6 @@ Shard *merge(Shard *a, Shard *b);
|
||||
Shard *merge_leaves(Shard *a, Shard *b);
|
||||
Shard *append_leaf(Shard *root, Shard *leaf);
|
||||
|
||||
uint32_t offset_of(Shard *s, uint32_t line_number, uint32_t col);
|
||||
|
||||
void print_shard(const Shard *shard, int depth = 0);
|
||||
|
||||
+12
-87
@@ -2,8 +2,10 @@
|
||||
|
||||
#include "buffer/append.h"
|
||||
#include "buffer/original.h"
|
||||
#include "line_iter.h"
|
||||
#include "iterators/chunk.h"
|
||||
#include "iterators/line.h"
|
||||
#include "pch.h"
|
||||
#include "search.h"
|
||||
#include "shard.h"
|
||||
|
||||
struct Vase {
|
||||
@@ -16,95 +18,18 @@ struct Vase {
|
||||
|
||||
Shard *root;
|
||||
|
||||
Vase(char *data, uint32_t length) : original(data, length), append() {
|
||||
root = new Petal(length, original.newlines.size(), &original, 0);
|
||||
}
|
||||
Vase(char *data, uint32_t length);
|
||||
|
||||
~Vase() {
|
||||
Shard::release(root);
|
||||
}
|
||||
~Vase();
|
||||
|
||||
uint32_t length() {
|
||||
return root->length;
|
||||
}
|
||||
uint32_t length();
|
||||
|
||||
std::string to_string() {
|
||||
std::string out;
|
||||
flatten(root, out);
|
||||
return out;
|
||||
}
|
||||
std::string to_string();
|
||||
static void flatten(Shard *s, std::string &out);
|
||||
|
||||
void type(uint32_t offset, char key) {
|
||||
uint32_t pos = append.key(key);
|
||||
Shard *inserted = new Petal(1, key == '\n', &append, pos);
|
||||
auto [left, right] = split_shard(root, offset);
|
||||
Shard *left2 = append_leaf(left, inserted);
|
||||
Shard *new_root = concat_shard(left2, right);
|
||||
Shard::release(left);
|
||||
Shard::release(right);
|
||||
Shard::release(left2);
|
||||
Shard::release(inserted);
|
||||
Shard::release(root);
|
||||
root = new_root;
|
||||
}
|
||||
void type(uint32_t offset, char key);
|
||||
void insert(uint32_t offset, const char *data, uint32_t len);
|
||||
void erase(uint32_t cursor, int64_t amount);
|
||||
|
||||
void insert(uint32_t offset, const char *data, uint32_t len) {
|
||||
uint32_t lines = 0;
|
||||
uint32_t pos = append.append(data, len, &lines);
|
||||
Shard *inserted = new Petal(len, lines, &append, pos);
|
||||
auto [left, right] = split_shard(root, offset);
|
||||
Shard *left2 = append_leaf(left, inserted);
|
||||
Shard *new_root = concat_shard(left2, right);
|
||||
Shard::release(left);
|
||||
Shard::release(right);
|
||||
Shard::release(left2);
|
||||
Shard::release(inserted);
|
||||
Shard::release(root);
|
||||
root = new_root;
|
||||
}
|
||||
|
||||
void erase(uint32_t cursor, int64_t amount) {
|
||||
if (amount == 0)
|
||||
return;
|
||||
uint32_t start;
|
||||
uint32_t count;
|
||||
if (amount < 0) {
|
||||
count = std::min<uint32_t>(-amount, cursor);
|
||||
start = cursor - count;
|
||||
} else {
|
||||
start = cursor;
|
||||
count = amount;
|
||||
}
|
||||
auto [a, b] = split_shard(root, start);
|
||||
auto [d, c] = split_shard(b, count);
|
||||
Shard *new_root = concat_shard(a, c);
|
||||
Shard::release(a);
|
||||
Shard::release(b);
|
||||
Shard::release(c);
|
||||
Shard::release(d);
|
||||
Shard::release(root);
|
||||
root = new_root;
|
||||
}
|
||||
|
||||
void flatten(Shard *s, std::string &out) {
|
||||
if (s->kind == Shard::ShardKind::Petal) {
|
||||
auto *p = static_cast<Petal *>(s);
|
||||
uint32_t remaining = p->length;
|
||||
uint32_t pos = p->pos;
|
||||
while (remaining) {
|
||||
uint32_t got;
|
||||
const char *data = p->source->read(pos, &got);
|
||||
uint32_t take = std::min(got, remaining);
|
||||
out.append(data, take);
|
||||
remaining -= take;
|
||||
pos += take;
|
||||
}
|
||||
} else {
|
||||
auto *b = static_cast<Branch *>(s);
|
||||
flatten(b->left, out);
|
||||
flatten(b->right, out);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t offset_of(uint32_t line_number, uint32_t col);
|
||||
void regex_search_replace(std::string_view pattern, uint32_t start_offset, uint32_t end_offset, std::string_view replace, std::string_view options);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user