From: Stefan Monnier Date: Tue, 2 Jul 2024 20:28:08 +0000 (-0400) Subject: (buf_*pos_to_*pos): Fix minor off-by one X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=301a6f00db02d39a543660a18dec001cef03e3ee;p=emacs.git (buf_*pos_to_*pos): Fix minor off-by one * src/marker.c (buf_charpos_to_bytepos, buf_bytepos_to_charpos): Slightly restructure the markers loop so we check `distance` also before "consider"ing the first marker. (cherry picked from commit 0385c0c0820361b71a74988f23d0445238f30473) --- diff --git a/src/marker.c b/src/marker.c index 9aaa2e7402c..16fa3bcef3e 100644 --- a/src/marker.c +++ b/src/marker.c @@ -202,19 +202,15 @@ buf_charpos_to_bytepos (struct buffer *b, ptrdiff_t charpos) if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff) CONSIDER (cached_charpos, cached_bytepos); - for (tail = BUF_MARKERS (b); tail; tail = tail->next) - { - CONSIDER (tail->charpos, tail->bytepos); - - /* If we are down to a range of 50 chars, - don't bother checking any other markers; - scan the intervening chars directly now. */ - if (best_above - charpos < distance - || charpos - best_below < distance) - break; - else - distance += BYTECHAR_DISTANCE_INCREMENT; - } + for (tail = BUF_MARKERS (b); + /* If we are down to a range of DISTANCE chars, + don't bother checking any other markers; + scan the intervening chars directly now. */ + tail && !(best_above - charpos < distance + || charpos - best_below < distance); + tail = tail->next, + distance += BYTECHAR_DISTANCE_INCREMENT) + CONSIDER (tail->charpos, tail->bytepos); /* We get here if we did not exactly hit one of the known places. We have one known above and one known below. @@ -354,19 +350,15 @@ buf_bytepos_to_charpos (struct buffer *b, ptrdiff_t bytepos) if (b == cached_buffer && BUF_MODIFF (b) == cached_modiff) CONSIDER (cached_bytepos, cached_charpos); - for (tail = BUF_MARKERS (b); tail; tail = tail->next) - { - CONSIDER (tail->bytepos, tail->charpos); - - /* If we are down to a range of DISTANCE bytes, - don't bother checking any other markers; - scan the intervening chars directly now. */ - if (best_above_byte - bytepos < distance - || bytepos - best_below_byte < distance) - break; - else - distance += BYTECHAR_DISTANCE_INCREMENT; - } + for (tail = BUF_MARKERS (b); + /* If we are down to a range of DISTANCE bytes, + don't bother checking any other markers; + scan the intervening chars directly now. */ + tail && !(best_above_byte - bytepos < distance + || bytepos - best_below_byte < distance); + tail = tail->next, + distance += BYTECHAR_DISTANCE_INCREMENT) + CONSIDER (tail->bytepos, tail->charpos); /* We get here if we did not exactly hit one of the known places. We have one known above and one known below.