Major updates:

- A lotta cleanup
- BEd command parser rewrite
- Vase class removed
- Proper IO system.
- A lot more.
This commit is contained in:
2026-08-29 22:30:01 +01:00
parent d7278251ec
commit b8eac7b2da
42 changed files with 2153 additions and 1733 deletions
-19
View File
@@ -1,19 +0,0 @@
#pragma once
#include "buffer.h"
#include "pch.h"
namespace bed::internal::vase {
struct OriginalBuffer : Buffer {
const char *buf;
uint64_t len;
int fd = -1;
OriginalBuffer(std::filesystem::path base_dir);
~OriginalBuffer();
void initialize();
const char *read(uint64_t pos) override;
uint64_t length() override;
};
} // namespace bed::internal::vase
+11 -8
View File
@@ -1,9 +1,9 @@
#pragma once
#include "buffer/buffer.h"
#include "buffer/original.h"
#include "constants.h"
#include "pch.h"
#include "storage/original.h"
#include "storage/storage.h"
namespace bed::internal::vase {
struct Shard {
@@ -24,9 +24,10 @@ struct Shard {
static void retain(Shard *n);
static void release(Shard *n);
static Shard *from_file(std::filesystem::path &path, OriginalBuffer *b, bool posix_ending);
static Shard *from_command(const char *cmd, OriginalBuffer *o, bool posix_ending);
static std::vector<Shard *> from_swap(std::filesystem::path &path, OriginalBuffer *b);
static Shard *from_file(const std::filesystem::path &path, bool posix_ending);
static Shard *from_string(const char *data, uint64_t len, bool posix_ending);
static Shard *from_command(const char *cmd, bool posix_ending);
// static std::vector<Shard *> from_swap(std::filesystem::path &path, OriginalStorage *b);
static std::pair<Shard *, Shard *> split(Shard *n, uint64_t offset);
static Shard *concat(Shard *a, Shard *b);
@@ -54,12 +55,14 @@ struct Branch : Shard {
};
struct Petal : Shard {
Buffer *source;
Storage *source;
uint64_t pos;
Petal(uint64_t length, uint64_t lines, Buffer *source, uint64_t pos)
Petal(uint64_t length, uint64_t lines, Storage *source, uint64_t pos)
: Shard(Kind::Petal, length, lines, 1),
source(source), pos(pos) {};
source(source), pos(pos) {
source->retain();
};
};
} // namespace bed::internal::vase
@@ -1,24 +1,27 @@
#pragma once
#include "../constants.h"
#include "buffer.h"
#include "pch.h"
#include "storage.h"
namespace bed::internal::vase {
struct AppendBuffer : Buffer {
struct AppendStorage : Storage {
char *buf = nullptr;
uint64_t allocated_capacity = 0;
uint64_t current_size = 0;
int fd = -1;
AppendBuffer(std::filesystem::path base_dir);
~AppendBuffer();
AppendStorage(std::filesystem::path base_dir);
~AppendStorage();
uint64_t append(const char c);
uint64_t append(const char *text, uint64_t len);
const char *read(uint64_t pos) override;
uint64_t length() override;
void retain() override {}
void release() override {}
private:
void grow(uint64_t len);
};
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include "pch.h"
#include "storage.h"
namespace bed::internal::vase {
struct OriginalStorage : Storage {
std::atomic_uint64_t refs{0};
const char *buf = nullptr;
uint64_t len = 0;
int fd = -1;
OriginalStorage(std::filesystem::path base_dir);
~OriginalStorage();
void initialize();
const char *read(uint64_t pos) override;
uint64_t length() override;
void retain() override {
refs++;
}
void release() override {
if (--refs > 0)
return;
delete this;
}
};
} // namespace bed::internal::vase
@@ -3,9 +3,11 @@
#include "pch.h"
namespace bed::internal::vase {
struct Buffer {
struct Storage {
virtual const char *read(uint64_t pos) = 0;
virtual uint64_t length() = 0;
virtual ~Buffer() = default;
virtual ~Storage() = default;
virtual void retain() = 0;
virtual void release() = 0;
};
} // namespace bed::internal::vase
+54 -100
View File
@@ -1,12 +1,12 @@
#pragma once
#include "buffer/append.h"
#include "buffer/original.h"
#include "constants.h"
#include "definitions.h"
#include "iterators/line.h"
#include "pch.h"
#include "shard.h"
#include "storage/append.h"
#include "storage/original.h"
namespace bed::internal::vase {
struct Point {
@@ -19,103 +19,57 @@ struct Range {
Point end;
};
struct Vase {
struct RegexGroup {
uint64_t start{0};
uint64_t end{0};
};
struct RegexMatch {
uint64_t start;
uint64_t end;
RegexGroup groups[9]{};
};
struct ReplacePart {
enum struct PartType {
FullMatch,
CaptureGroup,
Constant
} type;
std::variant<uint8_t, Shard *> value;
};
OriginalBuffer *original;
AppendBuffer *append;
Shard *root;
#ifdef _WIN32
bool posix_ending = false;
bool using_crlf = true;
#else
bool posix_ending = true;
bool using_crlf = false;
#endif
std::filesystem::path path;
std::filesystem::path swapdir;
Vase(std::filesystem::path path, std::filesystem::path swapdir);
Vase(std::string cmd, std::filesystem::path swapdir);
Vase(std::filesystem::path swapdir);
~Vase();
Vase(Vase &&other) noexcept;
Vase &operator=(Vase &&other) noexcept;
Vase(const Vase &) = delete;
Vase &operator=(const Vase &) = delete;
uint64_t length();
uint64_t lines();
std::string to_string();
std::string to_string(Range range);
Iterator iterate(uint64_t line, Direction dir);
void insert(Point *point, char key);
void insert(Point *point, std::string_view str);
void insert(Point *point, const char *data, uint64_t len);
void erase(Point *point, uint64_t amount, Direction dir);
void erase(Range range);
void replace(Range range, std::string_view str);
void replace(Range range, const char *data, uint64_t len);
void move_clusters(Point *point, uint64_t amount, Direction dir);
void move_lines(Point *point, uint64_t amount, Direction dir);
void clamp(Point *point);
void regex_search_replace(
std::string_view pattern, Range range,
std::string_view replace, std::string_view options
);
std::vector<Range> regex_search(
std::string_view pattern, Range range, std::string_view options
);
uint64_t find_next(std::string_view pattern, uint64_t start);
uint64_t find_prev(std::string_view pattern, uint64_t start);
bool undo();
bool redo();
void snapshot();
void prune_history(uint64_t n);
bool save();
bool save_swap();
uint64_t offset_of(Point point);
Point point_of(uint64_t offset);
private:
std::vector<Shard *> history;
uint64_t history_top;
void _insert(Point *point, const char *data, uint64_t len);
std::vector<ReplacePart> parse_replace(std::string_view s);
std::vector<RegexMatch> _regex_search(
std::string_view pattern, Range range, std::string_view options
);
struct RegexGroup {
uint64_t start{0};
uint64_t end{0};
};
struct RegexMatch {
uint64_t start;
uint64_t end;
RegexGroup groups[9]{};
};
struct ReplacePart {
enum struct PartType {
FullMatch,
CaptureGroup,
Constant
} type;
std::variant<uint8_t, Shard *> value;
};
uint64_t offset_of(Shard *root, uint64_t line);
uint64_t offset_of(Shard *root, Point point);
Point point_of(Shard *root, uint64_t offset);
std::string to_string(Shard *root);
std::string to_string(Shard *root, Range range);
Shard *insert(AppendStorage *ap, Shard *root, Shard *text, uint64_t line);
Shard *erase(Shard *root, uint64_t start, uint64_t end);
Shard *copy(Shard *root, uint64_t start, uint64_t end);
uint64_t find_next(Shard *root, std::string_view pattern, uint64_t start);
uint64_t find_prev(Shard *root, std::string_view pattern, uint64_t start);
Shard *substitute(
AppendStorage *ap, Shard *root,
std::string_view pattern, uint64_t start, uint64_t end,
std::string_view replace, std::string_view options
);
// internal
void _insert(AppendStorage *ap, Shard **root, Point *point, const char *data, uint64_t len);
Shard *insert(AppendStorage *ap, Shard *root, Point *point, char key);
Shard *insert(AppendStorage *ap, Shard *root, Point *point, const char *data, uint64_t len);
Shard *erase(Shard *root, Range range);
Shard *replace(AppendStorage *ap, Shard *root, Range range, const char *data, uint64_t len);
std::vector<ReplacePart> parse_replace(AppendStorage *ap, std::string_view s);
std::vector<RegexMatch> _regex_search(
Shard *root, std::string_view pattern, uint64_t start_offset, uint64_t end_offset, std::string_view options
);
} // namespace bed::internal::vase