Major fixes:

- ChunkIterator is now bidirectional
- LineIterator is now also bidirectional
- Undo/redo history operations
- Replace operation
- Range based api functions
- And more minor bug fixes
This commit is contained in:
2026-08-03 20:26:02 +01:00
parent 557edb5191
commit 8c61f186cf
18 changed files with 443 additions and 188 deletions
+1 -3
View File
@@ -29,12 +29,10 @@ inline int read_file(const char *path, char **text, uint32_t *len) {
if (read != *len) {
free(*text);
*text = NULL;
*text = nullptr;
*len = 0;
return 0;
}
(*text)[*len] = '\0'; // handy if treating it as a C string
return 1;
}
-5
View File
@@ -1,8 +1,3 @@
#pragma once
#include "pch.h"
struct Point {
uint32_t row;
uint32_t col;
};
+2 -5
View File
@@ -1,12 +1,11 @@
#pragma once
#include "../constants.h"
#include "buffer.h"
#include "pch.h"
struct AppendBuffer : Buffer {
static constexpr uint32_t CHUNK_SIZE = 1 << 16;
using TChunk = char[CHUNK_SIZE];
using TChunk = char[APPEND_CHUNK_SIZE];
std::vector<TChunk *> buf;
TChunk *t_current;
uint32_t current_offset{0};
@@ -21,6 +20,4 @@ struct AppendBuffer : Buffer {
const char *read(uint32_t pos, uint32_t *out_len);
inline uint32_t length();
inline void new_text_chunk();
};
+2 -2
View File
@@ -7,8 +7,8 @@ struct OriginalBuffer : Buffer {
char *buf;
uint32_t len;
// TODO: replace with file io or even lazy loaded file io for large files/logs etc. later.
OriginalBuffer(char *buf, uint32_t len);
// Add 2 modes here, normal and lazy, lazy copies the file to a safe location and uses mmap.
OriginalBuffer(std::string path);
~OriginalBuffer();
+11
View File
@@ -0,0 +1,11 @@
#pragma once
#include "pch.h"
constexpr uint32_t APPEND_CHUNK_SIZE = 64 * 1024;
constexpr uint32_t PETAL_SIZE_MAX = 32 * 1024;
static_assert(
APPEND_CHUNK_SIZE > PETAL_SIZE_MAX,
"PETAL_SIZE_MAX must be smaller than APPEND_CHUNK_SIZE"
);
+8 -2
View File
@@ -3,14 +3,20 @@
#include "../shard.h"
#include "pch.h"
enum struct Direction : uint8_t {
Forward,
Backward
};
struct ChunkIterator {
Direction dir;
Shard *root = nullptr;
std::vector<Shard *> stack;
Petal *petal = nullptr;
uint32_t petal_offset = 0;
uint32_t global_offset;
uint32_t global_offset = 0;
ChunkIterator(Shard *r);
ChunkIterator(Shard *r, Direction dir);
~ChunkIterator();
+4 -6
View File
@@ -6,14 +6,12 @@
struct LineIterator {
ChunkIterator it;
Direction dir;
const char *cursor = nullptr;
bool eof = false;
uint32_t remaining = 0;
uint32_t line_offset = 0;
LineIterator(Shard *r, uint32_t start_line);
const char *chunk;
uint32_t len;
LineIterator(Shard *r, uint32_t start_line, Direction dir);
~LineIterator() = default;
bool next(std::string &line);
+1
View File
@@ -2,6 +2,7 @@
#include "buffer/buffer.h"
#include "buffer/original.h"
#include "constants.h"
#include "pch.h"
#include "utils/utils.h"
+33 -11
View File
@@ -2,14 +2,29 @@
#include "buffer/append.h"
#include "buffer/original.h"
#include "constants.h"
#include "iterators/line.h"
#include "pch.h"
#include "search.h"
#include "shard.h"
#include "utils/utils.h"
struct Point {
uint32_t row;
uint32_t col;
};
struct Range {
Point start;
Point end;
};
struct Vase {
Vase(char *data, uint32_t length);
OriginalBuffer original;
AppendBuffer append;
Shard *root;
Vase(std::string path);
~Vase();
@@ -20,26 +35,33 @@ struct Vase {
LineIterator iterate(uint32_t line);
void input(Point *point, char key);
void insert(Point *point, char key);
void insert(Point *point, const char *data, uint32_t len);
void erase(Point *point, int64_t amount);
bool jump(Point *point, int64_t amount);
bool clamp(Point *point);
void erase(Range range);
void replace(Range range, const char *data, uint32_t len);
void move_clusters(Point *point, int64_t amount);
void move_lines(Point *point, int64_t amount);
void clamp(Point *point);
void regex_search_replace(
std::string_view pattern,
Point start, Point end,
std::string_view replace, std::string_view options
);
private:
OriginalBuffer original;
AppendBuffer append;
Shard *root;
bool undo();
bool redo();
/*std::vector<Shard *> undo; // TODO: later
uint16_t top; // of the undo stack.
uint16_t max; // for redo when no edits have been done after some undo.*/
void snapshot();
void prune_history(uint32_t n);
private:
std::vector<Shard *> history;
uint32_t history_top;
uint32_t offset_of(Point point);
static void flatten(Shard *s, std::string &out);
void _insert(Point *point, const char *data, uint32_t len);
};