summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2025-11-26 20:07:43 +0100
committerAlbert Cervin <albert@acervin.com>2025-11-26 20:07:43 +0100
commit8de2328d2e712fc892d6f02d92feb88fa857e85b (patch)
tree22a710957ed42cee9bb3afef93f42d2e72106d9f
parenta4056eb9de575463ccc9f53db8952e49801a8cf3 (diff)
downloaddged-8de2328d2e712fc892d6f02d92feb88fa857e85b.tar.gz
dged-8de2328d2e712fc892d6f02d92feb88fa857e85b.tar.xz
dged-8de2328d2e712fc892d6f02d92feb88fa857e85b.zip
Fix find_prev_in_line
It did not consider the starting location which made it not find the char it was standing on. This resulted in buffer_word_at returning the wrong thing.
-rw-r--r--src/dged/buffer.c3
-rw-r--r--test/buffer.c11
2 files changed, 10 insertions, 4 deletions
diff --git a/src/dged/buffer.c b/src/dged/buffer.c
index 6337096..fed8f87 100644
--- a/src/dged/buffer.c
+++ b/src/dged/buffer.c
@@ -304,7 +304,8 @@ find_prev_in_line(struct buffer *buffer, struct location start,
text_line_codepoint_iterator(buffer->text, start.line);
uint32_t coli = 0, tab_width = get_tab_width(buffer), found_at;
struct codepoint *codepoint;
- while (coli < start.col && (codepoint = utf8_next_codepoint(&iter)) != NULL) {
+ while (coli <= start.col &&
+ (codepoint = utf8_next_codepoint(&iter)) != NULL) {
if (predicate(codepoint)) {
found = true;
found_at = coli;
diff --git a/test/buffer.c b/test/buffer.c
index a8268da..7fc60b7 100644
--- a/test/buffer.c
+++ b/test/buffer.c
@@ -113,7 +113,7 @@ static void test_word_at(void) {
ASSERT(word1.begin.col == 0 && word1.end.col == 5,
"Expected only word to end at col 5");
- const char *txt2 = " (word2). Another";
+ const char *txt2 = " (word2). word3. Another";
buffer_add(&b, at, (uint8_t *)txt2, strlen(txt2));
word1 = buffer_word_at(&b, (struct location){.line = 0, .col = 0});
ASSERT(region_has_size(word1), "expected 0,0 to be a word");
@@ -131,8 +131,13 @@ static void test_word_at(void) {
// test that clamping works correctly
struct region word3 = buffer_word_at(&b, buffer_clamp(&b, 0, 100));
ASSERT(region_has_size(word3), "expected 0,100 to be in the last word");
- ASSERT(word3.begin.col == 15 && word3.end.col == 22,
- "Expected word to span cols 15..22");
+ ASSERT(word3.begin.col == 22 && word3.end.col == 29,
+ "Expected word to span cols 22..29");
+
+ // test that the dot is not considered a word
+ struct region word4 =
+ buffer_word_at(&b, (struct location){.line = 0, .col = 20});
+ ASSERT(!region_has_size(word4), "dot should not be a word");
buffer_destroy(&b);
}