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
+39 -30
View File
@@ -1,7 +1,7 @@
#include "internal/vase/vase.h"
namespace bed::internal::vase {
std::vector<Vase::ReplacePart> Vase::parse_replace(std::string_view s) {
std::vector<ReplacePart> parse_replace(AppendStorage *ap, std::string_view s) {
std::vector<ReplacePart> parts;
std::string constant;
auto flush_constant = [&]() {
@@ -13,11 +13,11 @@ std::vector<Vase::ReplacePart> Vase::parse_replace(std::string_view s) {
++lines;
++p;
}
uint64_t pos = append->append(constant.data(), (uint64_t)constant.size());
uint64_t pos = ap->append(constant.data(), (uint64_t)constant.size());
parts.push_back(
ReplacePart{
.type = ReplacePart::PartType::Constant,
.value = new Petal((uint64_t)constant.size(), lines, append, pos)
.value = new Petal((uint64_t)constant.size(), lines, ap, pos)
}
);
constant.clear();
@@ -61,15 +61,31 @@ std::vector<Vase::ReplacePart> Vase::parse_replace(std::string_view s) {
return parts;
}
void Vase::regex_search_replace(
std::string_view pattern, Range range,
Shard *substitute(
AppendStorage *ap, Shard *root,
std::string_view pattern, uint64_t start, uint64_t end,
std::string_view replace, std::string_view options
) {
std::vector<RegexMatch> matches = _regex_search(pattern, range, options);
if (matches.empty())
return;
if (!start || !end)
throw ed_error("Invalid range.");
start--;
end--;
if (!root)
throw ed_error("line range out of bounds");
uint64_t line_count = root->lines + 1;
if (start > end || end >= line_count)
throw ed_error("line range out of bounds");
uint64_t start_offset = offset_of(root, start);
uint64_t end_offset =
(end + 1 == line_count)
? root->length
: offset_of(root, end + 1);
std::vector<ReplacePart> replace_parts = parse_replace(replace);
std::vector<RegexMatch> matches = _regex_search(root, pattern, start_offset, end_offset, options);
if (matches.empty())
return root;
std::vector<ReplacePart> replace_parts = parse_replace(ap, replace);
std::vector<Shard *> pieces;
pieces.reserve(matches.size() * 2 + 1);
@@ -141,23 +157,13 @@ void Vase::regex_search_replace(
Shard *new_root = compact.empty() ? nullptr : Shard::build(compact.data(), 0, compact.size());
Shard::release(root);
root = new_root;
return new_root;
}
std::vector<Range> Vase::regex_search(
std::string_view pattern, Range range, std::string_view options
) {
std::vector<RegexMatch> matches = _regex_search(pattern, range, options);
if (matches.empty())
return {};
std::vector<Range> result;
result.reserve(matches.size());
for (auto match : matches)
result.push_back({point_of(match.start), point_of(match.end)});
return result;
}
uint64_t Vase::find_next(std::string_view pattern, uint64_t start) {
uint64_t find_next(Shard *root, std::string_view pattern, uint64_t start) {
if (start == 0 || start > root->lines)
throw ed_error("Invalid line number.");
start--;
std::vector<RegexMatch> results;
int errornumber;
PCRE2_SIZE erroroffset;
@@ -176,7 +182,7 @@ uint64_t Vase::find_next(std::string_view pattern, uint64_t start) {
pcre2_code_free(re);
throw ed_error("Can't create regex match data.");
}
uint64_t at = (start + 1) % lines();
uint64_t at = (start + 1) % (root->lines + 1);
LineIterator it(root, at, Direction::Forward);
std::string line;
while (it.next(&line)) {
@@ -196,7 +202,7 @@ uint64_t Vase::find_next(std::string_view pattern, uint64_t start) {
at = 0;
LineIterator it2(root, at, Direction::Forward);
while (it2.next(&line)) {
if (at >= start)
if (at > start)
break;
int rc = pcre2_match(re, (PCRE2_SPTR)line.data(), line.size(), 0, 0, match_data, nullptr);
if (rc >= 0) {
@@ -216,7 +222,10 @@ uint64_t Vase::find_next(std::string_view pattern, uint64_t start) {
throw ed_error("No line matched.");
}
uint64_t Vase::find_prev(std::string_view pattern, uint64_t start) {
uint64_t find_prev(Shard *root, std::string_view pattern, uint64_t start) {
if (start == 0 || start > root->lines)
throw ed_error("Invalid line number.");
start--;
std::vector<RegexMatch> results;
int errornumber;
PCRE2_SIZE erroroffset;
@@ -235,7 +244,7 @@ uint64_t Vase::find_prev(std::string_view pattern, uint64_t start) {
pcre2_code_free(re);
throw ed_error("Can't create regex match data.");
}
uint64_t at = (start == 0 ? lines() : start) - 1;
uint64_t at = (start == 0 ? root->lines : start - 1);
LineIterator it(root, at, Direction::Backward);
std::string line;
while (it.next(&line)) {
@@ -252,10 +261,10 @@ uint64_t Vase::find_prev(std::string_view pattern, uint64_t start) {
}
at--;
}
at = lines() - 1;
at = root->lines;
LineIterator it2(root, at, Direction::Backward);
while (it2.next(&line)) {
if (at <= start)
if (at < start)
break;
int rc = pcre2_match(re, (PCRE2_SPTR)line.data(), line.size(), 0, 0, match_data, nullptr);
if (rc >= 0) {
+2 -5
View File
@@ -1,8 +1,8 @@
#include "internal/vase/vase.h"
namespace bed::internal::vase {
std::vector<Vase::RegexMatch> Vase::_regex_search(
std::string_view pattern, Range range, std::string_view options
std::vector<RegexMatch> _regex_search(
Shard *root, std::string_view pattern, uint64_t start_offset, uint64_t end_offset, std::string_view options
) {
bool global = false;
uint64_t flags = PCRE2_MULTILINE | PCRE2_UTF;
@@ -52,9 +52,6 @@ std::vector<Vase::RegexMatch> Vase::_regex_search(
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL);
uint64_t start_offset = offset_of(range.start);
uint64_t end_offset = offset_of(range.end);
PetalIterator it(root, Direction::Forward);
it.seek_offset(start_offset);
+95 -32
View File
@@ -14,6 +14,7 @@ void Shard::release(Shard *n) {
release(((Branch *)n)->right);
delete (Branch *)n;
} else {
((Petal *)n)->source->release();
delete (Petal *)n;
}
}
@@ -200,28 +201,27 @@ Shard *Shard::build(Shard **pieces, uint64_t lo, uint64_t hi) {
return node;
}
Shard *Shard::from_command(const char *cmd, OriginalBuffer *o, bool posix_ending) {
Shard *Shard::from_command(const char *cmd, bool posix_ending) {
auto o = new OriginalStorage("/tmp");
int dest_fd = o->fd;
if (dest_fd == -1)
if (dest_fd == -1) {
delete o;
return nullptr;
}
FILE *pipe = popen(cmd, "r");
if (!pipe)
if (!pipe) {
delete o;
return nullptr;
}
std::vector<Shard *> pieces;
pieces.reserve(16);
uint64_t pos = 0;
char buf[PETAL_SIZE_MAX];
uint64_t buf_cursor = 0;
char ending[2] = {'\0', '\0'};
while (true) {
size_t got = fread(buf + buf_cursor, 1, sizeof(buf) - buf_cursor, pipe);
buf_cursor += got;
if (buf_cursor == PETAL_SIZE_MAX || feof(pipe)) {
if (buf_cursor == 0)
break;
@@ -244,6 +244,7 @@ Shard *Shard::from_command(const char *cmd, OriginalBuffer *o, bool posix_ending
}
if (!write_all(dest_fd, buf, buf_cursor)) {
pclose(pipe);
delete o;
return nullptr;
}
pieces.push_back(new Petal(buf_cursor, lines, o, pos));
@@ -251,19 +252,24 @@ Shard *Shard::from_command(const char *cmd, OriginalBuffer *o, bool posix_ending
}
if (feof(pipe))
break;
if (ferror(pipe))
if (ferror(pipe)) {
delete o;
return nullptr;
}
}
int status = pclose(pipe);
if (status == -1)
if (status == -1) {
delete o;
return nullptr;
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
delete o;
return nullptr;
if (pieces.empty())
}
if (pieces.empty()) {
delete o;
return nullptr;
}
if (posix_ending) {
if (ending[1] == '\n') {
Petal *last = (Petal *)pieces.back();
@@ -280,52 +286,58 @@ Shard *Shard::from_command(const char *cmd, OriginalBuffer *o, bool posix_ending
last->length--;
}
}
o->initialize();
if (pieces.size() == 1)
return pieces[0];
return build(pieces.data(), 0, pieces.size());
}
Shard *Shard::from_file(std::filesystem::path &path, OriginalBuffer *o, bool posix_ending) {
Shard *Shard::from_file(const std::filesystem::path &path, bool posix_ending) {
auto o = new OriginalStorage("/tmp");
int dest_fd = o->fd;
if (dest_fd == -1)
if (dest_fd == -1) {
delete o;
return nullptr;
}
int src_fd = open(path.c_str(), O_RDONLY);
if (src_fd == -1)
if (src_fd == -1) {
delete o;
return nullptr;
}
uint64_t total = std::filesystem::file_size(path);
if (posix_ending && total > 0) {
char last;
if (pread(src_fd, &last, 1, (off_t)(total - 1)) != 1)
if (pread(src_fd, &last, 1, (off_t)(total - 1)) != 1) {
delete o;
return nullptr;
}
if (last == '\n') {
total--;
if (total > 0) {
char s_last;
if (pread(src_fd, &s_last, 1, (off_t)(total - 1)) != 1)
if (pread(src_fd, &s_last, 1, (off_t)(total - 1)) != 1) {
delete o;
return nullptr;
}
if (s_last == '\r')
total--;
}
}
}
if (total == 0)
if (total == 0) {
delete o;
return nullptr;
}
std::vector<Shard *> pieces;
uint64_t pos = 0;
pieces.reserve((total + PETAL_SIZE_MAX - 1) / PETAL_SIZE_MAX);
char buf[PETAL_SIZE_MAX];
while (pos < total) {
uint64_t want = std::min(PETAL_SIZE_MAX, total - pos);
ssize_t got = pread(src_fd, buf, want, pos);
if (got <= 0) {
close(src_fd);
delete o;
return nullptr;
}
uint64_t take = (uint64_t)got;
@@ -341,19 +353,70 @@ Shard *Shard::from_file(std::filesystem::path &path, OriginalBuffer *o, bool pos
}
if (!write_all(dest_fd, buf, take)) {
close(src_fd);
delete o;
return nullptr;
}
pieces.push_back(new Petal(take, lines, o, pos));
pos += take;
}
close(src_fd);
if (pieces.empty())
if (pieces.empty()) {
delete o;
return nullptr;
}
o->initialize();
if (pieces.size() == 1)
return pieces[0];
return build(pieces.data(), 0, pieces.size());
}
Shard *Shard::from_string(const char *data, uint64_t len, bool posix_ending) {
auto o = new OriginalStorage("/tmp");
int dest_fd = o->fd;
if (dest_fd == -1 || data == nullptr) {
delete o;
return nullptr;
}
uint64_t total = len;
if (posix_ending && total > 0) {
if (data[total - 1] == '\n') {
total--;
if (total > 0 && data[total - 1] == '\r')
total--;
}
}
if (total == 0) {
delete o;
return nullptr;
}
std::vector<Shard *> pieces;
uint64_t pos = 0;
pieces.reserve((total + PETAL_SIZE_MAX - 1) / PETAL_SIZE_MAX);
while (pos < total) {
uint64_t take = std::min(PETAL_SIZE_MAX, total - pos);
const char *buf = data + pos;
uint64_t lines = 0;
const char *p = buf;
const char *end = buf + take;
while (p < end) {
const void *nl = memchr(p, '\n', end - p);
if (!nl)
break;
lines++;
p = (const char *)nl + 1;
}
if (!write_all(dest_fd, buf, take)) {
delete o;
return nullptr;
}
pieces.push_back(new Petal(take, lines, o, pos));
pos += take;
}
if (pieces.empty()) {
delete o;
return nullptr;
}
o->initialize();
if (pieces.size() == 1)
return pieces[0];
return build(pieces.data(), 0, pieces.size());
@@ -1,7 +1,7 @@
#include "internal/vase/vase.h"
namespace bed::internal::vase {
AppendBuffer::AppendBuffer(std::filesystem::path base_dir) {
AppendStorage::AppendStorage(std::filesystem::path base_dir) {
base_dir /= "tapp.XXXXXX";
char *s = strdup(base_dir.c_str());
fd = mkstemp(s);
@@ -17,14 +17,14 @@ AppendBuffer::AppendBuffer(std::filesystem::path base_dir) {
throw std::runtime_error("mmap failed");
}
AppendBuffer::~AppendBuffer() {
AppendStorage::~AppendStorage() {
if (buf && buf != MAP_FAILED)
munmap(buf, allocated_capacity);
if (fd != -1)
close(fd);
}
void AppendBuffer::grow(uint64_t len) {
void AppendStorage::grow(uint64_t len) {
if (current_size + len > allocated_capacity) {
uint64_t new_capacity = allocated_capacity * 2;
if (new_capacity < current_size + len)
@@ -46,13 +46,13 @@ void AppendBuffer::grow(uint64_t len) {
}
}
uint64_t AppendBuffer::append(const char c) {
uint64_t AppendStorage::append(const char c) {
grow(1);
buf[current_size++] = c;
return current_size - 1;
}
uint64_t AppendBuffer::append(const char *text, uint64_t len) {
uint64_t AppendStorage::append(const char *text, uint64_t len) {
grow(len);
memcpy(buf + current_size, text, len);
uint64_t old_pos = current_size;
@@ -60,13 +60,13 @@ uint64_t AppendBuffer::append(const char *text, uint64_t len) {
return old_pos;
}
const char *AppendBuffer::read(uint64_t pos) {
const char *AppendStorage::read(uint64_t pos) {
if (pos >= current_size)
return nullptr;
return buf + pos;
}
uint64_t AppendBuffer::length() {
uint64_t AppendStorage::length() {
return current_size;
}
} // namespace bed::internal::vase
@@ -1,7 +1,7 @@
#include "internal/vase/vase.h"
namespace bed::internal::vase {
OriginalBuffer::OriginalBuffer(std::filesystem::path base_dir) {
OriginalStorage::OriginalStorage(std::filesystem::path base_dir) {
if (!std::filesystem::exists(base_dir) || !std::filesystem::is_directory(base_dir))
throw std::runtime_error("Swap directory does not exist or is not a directory.");
base_dir /= "tbuf.XXXXXX";
@@ -13,14 +13,14 @@ OriginalBuffer::OriginalBuffer(std::filesystem::path base_dir) {
free(s);
}
OriginalBuffer::~OriginalBuffer() {
OriginalStorage::~OriginalStorage() {
if (buf)
munmap((char *)buf, len);
if (fd != -1)
close(fd);
}
void OriginalBuffer::initialize() {
void OriginalStorage::initialize() {
struct stat st;
if (fstat(fd, &st) == -1)
throw std::runtime_error("fstat failed");
@@ -35,13 +35,13 @@ void OriginalBuffer::initialize() {
fd = -1;
}
const char *OriginalBuffer::read(uint64_t pos) {
const char *OriginalStorage::read(uint64_t pos) {
if (pos >= len)
return nullptr;
return buf + pos;
}
uint64_t OriginalBuffer::length() {
uint64_t OriginalStorage::length() {
return len;
}
} // namespace bed::internal::vase
+135 -397
View File
@@ -1,108 +1,7 @@
#include "internal/vase/vase.h"
namespace bed::internal::vase {
Vase::Vase(std::filesystem::path path, std::filesystem::path swapdir)
: path(path), swapdir(swapdir) {
if (!std::filesystem::exists(swapdir) || !std::filesystem::is_directory(swapdir))
throw std::runtime_error("Swap directory does not exist or is not a directory.");
append = new AppendBuffer(swapdir);
original = new OriginalBuffer(swapdir);
if (std::filesystem::is_regular_file(path))
root = Shard::from_file(path, original, posix_ending);
else
root = nullptr;
history_top = 0;
history.push_back(root);
Shard::retain(root);
}
Vase::Vase(std::string cmd, std::filesystem::path swapdir)
: path(""), swapdir(swapdir) {
if (!std::filesystem::exists(swapdir) || !std::filesystem::is_directory(swapdir))
throw std::runtime_error("Swap directory does not exist or is not a directory.");
append = new AppendBuffer(swapdir);
original = new OriginalBuffer(swapdir);
root = Shard::from_command(cmd.c_str(), original, posix_ending);
history_top = 0;
history.push_back(root);
Shard::retain(root);
}
Vase::Vase(std::filesystem::path swapdir)
: path(""), swapdir(swapdir) {
if (!std::filesystem::exists(swapdir) || !std::filesystem::is_directory(swapdir))
throw std::runtime_error("Swap directory does not exist or is not a directory.");
append = new AppendBuffer(swapdir);
original = new OriginalBuffer(swapdir);
root = nullptr;
history_top = 0;
history.push_back(root);
Shard::retain(root);
}
Vase::~Vase() {
Shard::release(root);
for (auto s : history)
Shard::release(s);
if (original)
delete original;
if (append)
delete append;
}
Vase::Vase(Vase &&other) noexcept
: original(other.original),
append(other.append),
root(other.root),
posix_ending(other.posix_ending),
using_crlf(other.using_crlf),
path(std::move(other.path)),
swapdir(std::move(other.swapdir)),
history(std::move(other.history)),
history_top(other.history_top) {
other.original = nullptr;
other.append = nullptr;
other.root = nullptr;
other.history_top = 0;
}
Vase &Vase::operator=(Vase &&other) noexcept {
if (this == &other)
return *this;
Shard::release(root);
for (auto s : history)
Shard::release(s);
delete original;
delete append;
original = other.original;
append = other.append;
root = other.root;
posix_ending = other.posix_ending;
using_crlf = other.using_crlf;
path = std::move(other.path);
swapdir = std::move(other.swapdir);
history = std::move(other.history);
history_top = other.history_top;
other.original = nullptr;
other.append = nullptr;
other.root = nullptr;
other.history_top = 0;
return *this;
}
uint64_t Vase::length() {
if (!root)
return 0;
return root->length + posix_ending;
}
uint64_t Vase::lines() {
if (!root)
return 0;
return root->lines + 1;
}
std::string Vase::to_string() {
std::string to_string(Shard *root) {
std::string out;
if (!root)
return out;
@@ -112,23 +11,19 @@ std::string Vase::to_string() {
uint64_t len;
while (it.next(&data, &len))
out.append(data, len);
if (posix_ending)
out.append("\n");
return out;
}
std::string Vase::to_string(Range range) {
clamp(&range.start);
clamp(&range.end);
std::string to_string(Shard *root, Range range) {
std::string out;
if (!root)
return out;
PetalIterator it(root, Direction::Forward);
uint64_t start = offset_of(range.start);
uint64_t start = offset_of(root, range.start);
it.seek_offset(start);
const char *data;
uint64_t len;
uint64_t remaining = offset_of(range.end) - start;
uint64_t remaining = offset_of(root, range.end) - start;
while (remaining && it.next(&data, &len)) {
uint64_t n = std::min(len, remaining);
out.append(data, n);
@@ -137,85 +32,10 @@ std::string Vase::to_string(Range range) {
return out;
}
Iterator Vase::iterate(uint64_t line, Direction dir) {
return Iterator(root, line, dir);
}
bool Vase::undo() {
if (history_top == 0)
return false;
Shard::release(root);
history_top--;
root = history[history_top];
Shard::retain(root);
return true;
}
bool Vase::redo() {
if (history_top + 1 >= history.size())
return false;
Shard::release(root);
history_top++;
root = history[history_top];
Shard::retain(root);
return true;
}
void Vase::snapshot() {
if (history[history_top] == root)
return;
while (history.size() > history_top + 1) {
Shard::release(history.back());
history.pop_back();
}
Shard::retain(root);
history.push_back(root);
history_top++;
}
void Vase::prune_history(uint64_t n) {
uint64_t keep = std::min(history.size(), n + 1);
if (keep == history.size())
return;
uint64_t remove = history.size() - keep;
for (uint64_t i = 0; i < remove; ++i)
Shard::release(history[i]);
history.erase(history.begin(), history.begin() + remove);
history_top -= remove;
}
bool Vase::save() {
if (!root)
return true;
if (path == "")
return false;
std::ofstream file(path, std::ios::binary);
if (!file)
return false;
PetalIterator it(root, Direction::Forward);
it.seek_offset(0);
const char *data;
uint64_t len;
while (it.next(&data, &len)) {
file.write(data, len);
if (!file)
return false;
}
if (posix_ending)
file.write("\n", 1);
if (!file)
return false;
return true;
}
bool Vase::save_swap() {
return false;
}
void Vase::insert(Point *point, char key) {
uint64_t pos = append->append(key);
Shard *inserted = new Petal(1, key == '\n', append, pos);
auto [left, right] = Shard::split(root, offset_of(*point));
Shard *insert(AppendStorage *ap, Shard *root, Point *point, char key) {
uint64_t pos = ap->append(key);
Shard *inserted = new Petal(1, key == '\n', ap, pos);
auto [left, right] = Shard::split(root, offset_of(root, *point));
Shard *left2 = Shard::append(left, inserted);
Shard::release(left);
Shard::release(inserted);
@@ -223,31 +43,28 @@ void Vase::insert(Point *point, char key) {
Shard::release(left2);
Shard::release(right);
Shard::release(root);
root = new_root;
if (key == '\n')
*point = {point->row + 1, 0};
else
point->col++;
return new_root;
}
void Vase::insert(Point *point, std::string_view str) {
insert(point, str.data(), str.size());
}
void Vase::insert(Point *point, const char *data, uint64_t len) {
Shard *insert(AppendStorage *ap, Shard *root, Point *point, const char *data, uint64_t len) {
while (len) {
uint64_t chunk_size = std::min<uint64_t>(len, PETAL_SIZE_MAX);
_insert(point, data, chunk_size);
_insert(ap, &root, point, data, chunk_size);
len -= chunk_size;
data += chunk_size;
}
return root;
}
void Vase::_insert(Point *point, const char *data, uint64_t len) {
void _insert(AppendStorage *ap, Shard **root, Point *point, const char *data, uint64_t len) {
if (len == 0)
return;
uint64_t offset = offset_of(*point);
uint64_t pos = append->append(data, len);
uint64_t offset = offset_of(*root, *point);
uint64_t pos = ap->append(data, len);
uint64_t lines = 0;
const char *start = data;
const char *last_line = start;
@@ -270,50 +87,23 @@ void Vase::_insert(Point *point, const char *data, uint64_t len) {
} else {
point->col += col;
}
Shard *inserted = new Petal(len, lines, append, pos);
auto [left, right] = Shard::split(root, offset);
Shard *inserted = new Petal(len, lines, ap, pos);
auto [left, right] = Shard::split(*root, offset);
Shard *left2 = Shard::append(left, inserted);
Shard::release(left);
Shard::release(inserted);
Shard *new_root = Shard::concat(left2, right);
Shard::release(left2);
Shard::release(right);
Shard::release(root);
root = new_root;
Shard::release(*root);
*root = new_root;
}
void Vase::erase(Point *point, uint64_t amount, Direction dir) {
if (amount == 0)
return;
Point start = *point;
Point end = *point;
if (dir == Direction::Forward)
move_clusters(&end, amount, Direction::Forward);
else
move_clusters(&start, amount, Direction::Backward);
uint64_t start_offset = offset_of(start);
uint64_t end_offset = offset_of(end);
if (start_offset > end_offset)
std::swap(start_offset, end_offset);
uint64_t count = end_offset - start_offset;
auto [a, b] = Shard::split(root, start_offset);
auto [d, c] = Shard::split(b, count);
Shard *new_root = Shard::concat(a, c);
Shard::release(a);
Shard::release(b);
Shard::release(c);
Shard::release(d);
Shard::release(root);
root = new_root;
if (dir == Direction::Backward)
*point = start;
}
void Vase::erase(Range range) {
Shard *erase(Shard *root, Range range) {
Point start = range.start;
Point end = range.end;
uint64_t start_offset = offset_of(start);
uint64_t end_offset = offset_of(end);
uint64_t start_offset = offset_of(root, start);
uint64_t end_offset = offset_of(root, end);
uint64_t count = end_offset - start_offset;
auto [a, b] = Shard::split(root, start_offset);
auto [d, c] = Shard::split(b, count);
@@ -323,188 +113,136 @@ void Vase::erase(Range range) {
Shard::release(c);
Shard::release(d);
Shard::release(root);
root = new_root;
return new_root;
}
void Vase::replace(Range range, std::string_view str) {
replace(range, str.data(), str.size());
Shard *replace(AppendStorage *ap, Shard *root, Range range, const char *data, uint64_t len) {
root = erase(root, range);
return insert(ap, root, &range.start, data, len);
}
void Vase::replace(Range range, const char *data, uint64_t len) {
erase(range);
insert(&range.start, data, len);
uint64_t offset_of(Shard *root, Point point) {
return offset_of(root, point.row) + point.col;
}
uint64_t Vase::offset_of(Point point) {
clamp(&point);
LineIterator it(root, point.row, Direction::Forward);
std::string line;
uint64_t offset_of(Shard *root, uint64_t line) {
if (!root)
return 0;
if (line > root->lines)
throw ed_error("line out of range");
uint64_t offset = 0;
if (it.next(&line)) {
const char *ptr = line.data();
uint64_t remaining = line.length();
while (point.col && remaining) {
uint64_t next_len = grapheme_next_character_break_utf8(ptr, remaining);
remaining -= next_len;
ptr += next_len;
offset += next_len;
point.col--;
Shard *curr = root;
while (curr->kind == Shard::Kind::Branch) {
auto *b = (Branch *)curr;
if (line <= b->left->lines) {
curr = b->left;
} else {
line -= b->left->lines;
offset += b->left->length;
curr = b->right;
}
}
return it.byte_offset() + offset;
auto *petal = (Petal *)curr;
if (line == 0)
return offset;
const char *text = petal->source->read(petal->pos);
uint64_t local = 0;
while (line--) {
const char *nl = (const char *)memchr(text + local, '\n', petal->length - local);
if (!nl)
throw std::runtime_error("leaf line count is wrong.");
local = (nl - text) + 1;
}
return offset + local;
}
Point Vase::point_of(uint64_t offset) {
PetalIterator it(root, Direction::Backward);
it.seek_offset(offset);
Point p;
p.row = it.global_line;
std::string line;
const char *chunk;
uint64_t len = 0;
if (!it.next(&chunk, &len))
return p;
while (true) {
#if defined(__GLIBC__) || defined(__APPLE__)
const char *nl = (const char *)memrchr(chunk, '\n', len);
#else
const char *nl = nullptr;
const char *p = chunk + len;
while (!nl && p != chunk)
if (*(--p) == '\n')
nl = p;
#endif
if (!nl) {
if (len && chunk[len - 1] == '\r')
--len;
line.insert(0, chunk, len);
if (!it.next(&chunk, &len))
break;
continue;
}
const char *end = chunk + len;
const char *start = nl + 1;
const char *line_end = end;
if (line_end > start && *(line_end - 1) == '\r')
--line_end;
line.insert(0, start, line_end - start);
break;
}
const char *ptr = line.data();
uint64_t remaining = line.length();
while (remaining) {
uint64_t next_len = grapheme_next_character_break_utf8(ptr, remaining);
remaining -= next_len;
ptr += next_len;
p.col++;
}
clamp(&p);
return p;
static Shard *newline(AppendStorage *ap) {
uint64_t pos = ap->append('\n');
return new Petal(1, true, ap, pos);
}
void Vase::move_clusters(Point *point, uint64_t amount, Direction dir) {
if (amount == 0)
return;
if (dir == Direction::Backward) {
LineIterator it(root, point->row, Direction::Backward);
while (amount) {
std::string line;
if (!it.next(&line))
return;
std::vector<uint64_t> clusters;
const char *ptr = line.data();
uint64_t remaining = line.size();
uint64_t byte = 0;
while (remaining) {
clusters.push_back(byte);
uint64_t len =
grapheme_next_character_break_utf8(ptr, remaining);
ptr += len;
remaining -= len;
byte += len;
}
while (amount && point->col) {
point->col--;
amount--;
}
if (amount == 0)
return;
if (point->row == 0)
return;
point->row--;
point->col = clusters.size();
amount--;
}
} else {
LineIterator it(root, point->row, Direction::Forward);
while (amount) {
std::string line;
if (!it.next(&line))
return;
if (point->col || amount) {
const char *ptr = line.data();
uint64_t remaining = line.size();
uint64_t col = 0;
while (col < point->col && remaining) {
uint64_t len = grapheme_next_character_break_utf8(ptr, remaining);
ptr += len;
remaining -= len;
col++;
}
while (amount && remaining) {
uint64_t len = grapheme_next_character_break_utf8(ptr, remaining);
ptr += len;
remaining -= len;
point->col++;
amount--;
}
}
if (amount) {
point->row++;
point->col = 0;
amount--;
}
}
}
clamp(point);
}
void Vase::clamp(Point *point) {
Shard *insert(AppendStorage *ap, Shard *root, Shard *text, uint64_t line) {
if (!root) {
point->row = 0;
point->col = 0;
return;
if (line != 0)
throw ed_error("line out of range");
Shard::retain(text);
return text;
}
if (point->row > root->lines) {
point->row = root->lines;
point->col = UINT64_MAX;
if (line > root->lines + 1)
throw ed_error("line out of range");
Shard::retain(text);
Shard *nl = newline(ap);
if (line <= root->lines) {
uint64_t offset = offset_of(root, line);
auto [left, right] = Shard::split(root, offset);
Shard *middle = Shard::concat(text, nl);
Shard::release(text);
Shard::release(nl);
Shard *new_root = Shard::concat(left, middle);
Shard::release(left);
Shard::release(middle);
middle = Shard::concat(new_root, right);
Shard::release(new_root);
Shard::release(right);
Shard::release(root);
return middle;
}
LineIterator it(root, point->row, Direction::Forward);
std::string line;
uint64_t clusters = 0;
if (it.next(&line)) {
const char *ptr = line.data();
uint64_t remaining = line.length();
while (remaining) {
uint64_t next_len = grapheme_next_character_break_utf8(ptr, remaining);
remaining -= next_len;
ptr += next_len;
clusters++;
}
}
if (point->col > clusters)
point->col = clusters;
Shard *new_root = Shard::concat(root, nl);
Shard::release(root);
Shard::release(nl);
Shard *result = Shard::concat(new_root, text);
Shard::release(new_root);
Shard::release(text);
return result;
}
void Vase::move_lines(Point *point, uint64_t amount, Direction dir) {
if (dir == Direction::Forward) {
point->row += amount;
} else {
if (amount > point->row)
point->row = 0;
else
point->row -= amount;
}
clamp(point);
Shard *erase(Shard *root, uint64_t start, uint64_t end) {
if (!start || !end)
throw ed_error("Invalid range.");
start--;
end--;
if (!root)
throw ed_error("line range out of bounds");
uint64_t line_count = root->lines + 1;
if (start > end || end >= line_count)
throw ed_error("line range out of bounds");
uint64_t start_offset = offset_of(root, start);
uint64_t end_offset =
(end + 1 == line_count)
? root->length
: offset_of(root, end + 1);
auto [left, rest] = Shard::split(root, start_offset);
auto [middle, right] = Shard::split(rest, end_offset - start_offset);
Shard *new_root = Shard::concat(left, right);
Shard::release(left);
Shard::release(rest);
Shard::release(middle);
Shard::release(right);
Shard::release(root);
return new_root;
}
Shard *copy(Shard *root, uint64_t start, uint64_t end) {
if (!start || !end)
throw ed_error("Invalid range.");
start--;
end--;
if (!root)
throw ed_error("line range out of bounds");
uint64_t line_count = root->lines + 1;
if (start > end || end >= line_count)
throw ed_error("line range out of bounds");
uint64_t start_offset = offset_of(root, start);
uint64_t end_offset =
(end + 1 == line_count)
? root->length
: offset_of(root, end + 1);
auto [left, rest] = Shard::split(root, start_offset);
auto [middle, right] = Shard::split(rest, end_offset - start_offset);
Shard::release(left);
Shard::release(rest);
Shard::release(right);
Shard::release(root);
return middle;
}
} // namespace bed::internal::vase