From 16bd3ede6241a4790246ad7fcbde21e8c1d626a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 10 Sep 2014 20:50:10 +0200 Subject: text-motions: fix no match case of text_find_char_{next,prev} These functions should return the original position in case no match was found. --- text-motions.c | 13 +++++++++---- text-motions.h | 3 ++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/text-motions.c b/text-motions.c index f152c8a..25ff87c 100644 --- a/text-motions.c +++ b/text-motions.c @@ -51,7 +51,7 @@ size_t text_find_char_next(Text *txt, size_t pos, const char *s, size_t len) { matched = 0; text_iterator_byte_next(&it, NULL); } - return it.pos - (matched == len ? len : 0); + return matched == len ? it.pos - len : pos; } size_t text_find_char_prev(Text *txt, size_t pos, const char *s, size_t len) { @@ -62,14 +62,15 @@ size_t text_find_char_prev(Text *txt, size_t pos, const char *s, size_t len) { return pos; while (text_iterator_byte_get(&it, &c)) { if (c == s[matched]) { - if (matched-- == 0) + if (matched == 0) break; + matched--; } else { matched = len - 1; } text_iterator_byte_prev(&it, NULL); } - return it.pos; + return matched == 0 ? it.pos : pos; } size_t text_line_begin(Text *txt, size_t pos) { @@ -110,7 +111,11 @@ size_t text_line_finish(Text *txt, size_t pos) { } size_t text_line_end(Text *txt, size_t pos) { - return text_find_char_next(txt, pos, "\n", 1); + char c; + Iterator it = text_iterator_get(txt, pos); + while (text_iterator_byte_get(&it, &c) && c != '\n') + text_iterator_byte_next(&it, NULL); + return it.pos; } size_t text_line_next(Text *txt, size_t pos) { diff --git a/text-motions.h b/text-motions.h index 84f6ecc..e549402 100644 --- a/text-motions.h +++ b/text-motions.h @@ -16,7 +16,8 @@ size_t text_char_next(Text*, size_t pos); size_t text_char_prev(Text*, size_t pos); /* find the given substring either in forward or backward direction. - * does not wrap around at file start / end. */ + * does not wrap around at file start / end. if no match is found return + * original position */ size_t text_find_char_next(Text*, size_t pos, const char *s, size_t len); size_t text_find_char_prev(Text*, size_t pos, const char *s, size_t len); -- cgit v1.2.3