unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#20783: 25.0.50; [PATCH] byte-to-position has internal off-by-one bug
@ 2015-06-10 15:13 Wolfgang Jenkner
  2015-06-10 17:17 ` Eli Zaretskii
  0 siblings, 1 reply; 11+ messages in thread
From: Wolfgang Jenkner @ 2015-06-10 15:13 UTC (permalink / raw)
  To: 20783

Here's a test case for the bug:

(with-temp-buffer
  (insert "éé")
  (let ((i 1) pos res)
    (while (setq pos (byte-to-position i))
      (push pos res)
      (setq i (1+ i)))
    (nreverse res)))

=> (1 2 2 2 3)

while the correct result is

=> (1 1 2 2 3)

I found that this bug had been noticed before in

http://stackoverflow.com/questions/17588117/emacs-byte-to-position-function-is-not-consistent-with-document

Here's a patch.  The fix may look a bit clumsy but it's actually meant
to avoid pessimizing the presumably common case where the initial
bytepos is at a character boundary.

-- >8 --
Subject: [PATCH] * src/marker.c (buf_bytepos_to_charpos): Fix best_below_byte
 count.

If bytepos is not after a character boundary the preceding loop
overshoots by one character position.
---
 src/marker.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/marker.c b/src/marker.c
index 73928ba..94d676b 100644
--- a/src/marker.c
+++ b/src/marker.c
@@ -341,6 +341,12 @@ buf_bytepos_to_charpos (struct buffer *b, ptrdiff_t bytepos)
 	  BUF_INC_POS (b, best_below_byte);
 	}
 
+      if (best_below_byte != bytepos)
+	{
+	  best_below--;
+	  BUF_DEC_POS (b, best_below_byte);
+	}
+
       /* If this position is quite far from the nearest known position,
 	 cache the correspondence by creating a marker here.
 	 It will last until the next GC.
-- 
2.4.2






^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2015-06-17 16:57 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-10 15:13 bug#20783: 25.0.50; [PATCH] byte-to-position has internal off-by-one bug Wolfgang Jenkner
2015-06-10 17:17 ` Eli Zaretskii
2015-06-11 15:24   ` Wolfgang Jenkner
2015-06-11 16:04     ` Eli Zaretskii
2015-06-11 16:41       ` Wolfgang Jenkner
2015-06-11 19:08         ` Eli Zaretskii
2015-06-17 12:19       ` Wolfgang Jenkner
2015-06-17 16:57         ` Eli Zaretskii
2015-06-16 15:40     ` Wolfgang Jenkner
2015-06-16 16:08       ` Eli Zaretskii
2015-06-16 16:31         ` Wolfgang Jenkner

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).