Major changes to api and structure.

- Remove newline caching
- limit petal sizes
- make public facing api use points only
- fix iterators (chunk & line)
This commit is contained in:
2026-08-02 11:07:04 +01:00
parent 89b24a7488
commit 51e193d28b
16 changed files with 355 additions and 422 deletions
+10 -130
View File
@@ -2,140 +2,49 @@
AppendBuffer::AppendBuffer() {
new_text_chunk();
new_line_chunk();
}
AppendBuffer::~AppendBuffer() {
for (auto p : buf)
free(p);
for (auto p : newlines)
free(p);
}
uint32_t AppendBuffer::key(char c) {
if (t_offset == CHUNK_SIZE)
new_text_chunk();
(*t_current)[t_offset++] = c;
if (c == '\n') {
if (l_offset == CHUNK_SIZE)
new_line_chunk();
(*l_current)[l_offset++] = current_offset;
}
++current_offset;
return current_offset - 1;
return current_offset++;
}
uint32_t AppendBuffer::append(const char *text, uint32_t length, uint32_t *line_count) {
uint32_t AppendBuffer::append(const char *text, uint32_t length) {
uint32_t start = current_offset;
while (length) {
uint32_t space = CHUNK_SIZE - t_offset;
if (space == 0) {
while (length > 0) {
if (t_offset == CHUNK_SIZE)
new_text_chunk();
space = CHUNK_SIZE;
}
uint32_t copy = length < space ? length : space;
*line_count += _append(text, copy);
uint32_t copy = std::min(length, CHUNK_SIZE - t_offset);
memcpy((*t_current) + t_offset, text, copy);
t_offset += copy;
current_offset += copy;
text += copy;
length -= copy;
}
return start;
}
const char *AppendBuffer::read(uint32_t pos, uint32_t *out_len) {
if (pos >= current_offset)
return nullptr;
uint32_t local_offset = pos % CHUNK_SIZE;
if (out_len) {
uint32_t remaining = current_offset - pos;
uint32_t until_chunk_end = CHUNK_SIZE - local_offset;
*out_len = std::min(remaining, until_chunk_end);
}
return &(*buf[pos / CHUNK_SIZE])[local_offset];
}
uint32_t AppendBuffer::find_newline(uint32_t target, uint32_t low, uint32_t high) {
if (low >= high)
return low;
uint32_t first_chunk = low / CHUNK_SIZE;
uint32_t last_chunk = (high - 1) / CHUNK_SIZE;
uint32_t left = first_chunk;
uint32_t right = last_chunk;
while (left < right) {
uint32_t mid = left + (right - left) / 2;
uint32_t first_value = (*newlines[mid])[0];
if (first_value < target)
left = mid + 1;
else
right = mid;
}
uint32_t chunk = left;
if (chunk > first_chunk) {
LChunk &prev = *newlines[chunk - 1];
uint32_t prev_size =
(chunk - 1 == newlines.size() - 1) ? l_offset : CHUNK_SIZE;
if (prev[prev_size - 1] >= target)
chunk--;
}
LChunk &c = *newlines[chunk];
uint32_t chunk_size =
(chunk == newlines.size() - 1) ? l_offset : CHUNK_SIZE;
uint32_t l = 0;
uint32_t r = chunk_size;
while (l < r) {
uint32_t m = l + (r - l) / 2;
if (c[m] < target)
l = m + 1;
else
r = m;
}
return chunk * CHUNK_SIZE + l;
}
inline uint32_t AppendBuffer::newline_value_at(uint32_t flat_index) {
uint32_t chunk = flat_index / CHUNK_SIZE;
uint32_t off = flat_index % CHUNK_SIZE;
return (*newlines[chunk])[off];
}
uint32_t AppendBuffer::next_newline(uint32_t pos) {
uint32_t total = CHUNK_SIZE * (uint32_t)(newlines.size() - 1) + l_offset;
uint32_t idx = find_newline(pos, 0, total);
return idx >= total ? UINT32_MAX : newline_value_at(idx);
}
uint32_t AppendBuffer::nth_newline(uint32_t pos, uint32_t n) {
uint32_t total = CHUNK_SIZE * (uint32_t)(newlines.size() - 1) + l_offset;
uint32_t idx0 = find_newline(pos, 0, total);
uint32_t idx = idx0 + (n - 1);
return idx >= total ? UINT32_MAX : newline_value_at(idx);
}
uint32_t AppendBuffer::count_lines(uint32_t pos, uint32_t length) {
if (newlines.empty())
return 0;
uint32_t total_newlines = CHUNK_SIZE * (newlines.size() - 1) + l_offset;
uint32_t pos_index = find_newline(pos, 0, total_newlines);
uint32_t end_index = find_newline(pos + length, pos_index, total_newlines);
return end_index - pos_index;
inline uint32_t AppendBuffer::length() {
return current_offset;
}
inline void AppendBuffer::new_text_chunk() {
@@ -143,32 +52,3 @@ inline void AppendBuffer::new_text_chunk() {
buf.push_back(t_current);
t_offset = 0;
}
inline void AppendBuffer::new_line_chunk() {
l_current = (LChunk *)malloc(sizeof(LChunk));
newlines.push_back(l_current);
l_offset = 0;
}
inline uint32_t AppendBuffer::_append(const char *text, uint32_t length) {
memcpy((*t_current) + t_offset, text, length);
const char *p = text;
const char *end = text + length;
uint32_t start_index = l_offset;
while (p < end) {
p = (const char *)memchr(p, '\n', end - p);
if (!p)
break;
if (l_offset == CHUNK_SIZE)
new_line_chunk();
(*l_current)[l_offset++] = current_offset + uint32_t(p - text);
p++;
}
t_offset += length;
current_offset += length;
return l_offset - start_index;
}