Basic cursor movement, handling end of buffer, end of line in drawing

This commit is contained in:
Krzosa Karol
2024-06-22 16:09:26 +02:00
parent 7b818c5cde
commit 5ba9975c10
3 changed files with 85 additions and 33 deletions

View File

@@ -252,17 +252,22 @@ Line FindLine(Buffer &buffer, int64_t pos) {
return {};
}
int64_t AdjustUTF8Pos(const Buffer &buffer, int64_t pos, int64_t direction = 1) {
int64_t result = pos;
for (; result >= 0 && result < buffer.len;) {
char c = GetChar(buffer, pos);
if (IsUTF8ContinuationByte(c)) {
result += direction;
int64_t AdjustUTF8Pos(String string, int64_t pos, int64_t direction) {
for (; pos >= 0 && pos < string.len;) {
if (IsUTF8ContinuationByte(string.data[pos])) {
pos += direction;
} else {
break;
}
}
return result;
return pos;
}
int64_t AdjustUTF8Pos(const Buffer &buffer, int64_t pos, int64_t direction = 1, bool clamp = true) {
String string = GetString(buffer);
pos = AdjustUTF8Pos(string, pos, direction);
if (clamp) pos = Clamp(buffer, pos);
return pos;
}
uint32_t GetUTF32(Buffer &buffer, int64_t pos, int64_t *codepoint_size) {
@@ -317,7 +322,7 @@ void Advance(BufferIter *iter) {
if (iter->direction == ITERATE_FORWARD) {
iter->pos += iter->utf8_codepoint_size;
} else {
iter->pos = AdjustUTF8Pos(*iter->buffer, iter->pos - 1, ITERATE_BACKWARD);
iter->pos = AdjustUTF8Pos(*iter->buffer, iter->pos - 1, ITERATE_BACKWARD, false);
}
if (!IsValid(*iter)) return;