all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Wolfgang Jenkner <wjenkner@inode.at>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 20783@debbugs.gnu.org
Subject: bug#20783: 25.0.50; [PATCH] byte-to-position has internal off-by-one bug
Date: Thu, 11 Jun 2015 17:24:42 +0200	[thread overview]
Message-ID: <85oakmxn4i.fsf@iznogoud.viz> (raw)
In-Reply-To: <85fv5za8vv.fsf@iznogoud.viz>

On Wed, Jun 10 2015, Eli Zaretskii wrote:

> Wouldn't it be better to handle this use case in Fbyte_to_position?
> The BYTE_TO_CHAR macro is called an awful lot in the Emacs innermost
> loops, and it's _always_ called with a byte position that's on a
> character boundary.

I see.  How about something like the patch below?  The loop could be
improved a bit by doing pointer arithmetic like in DEC_POS but it's
perhaps not worth complicating things for the case where bytepos is not
at a character boundary.

-- >8 --
Subject: [PATCH] * editfns.c (Fbyte_to_position): Fix bytepos not at char
 boundary.

The behavior now matches the description in the manual.  (Bug#20783)
---
 src/editfns.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/editfns.c b/src/editfns.c
index cddb0d4..94715fe 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -1025,10 +1025,18 @@ DEFUN ("byte-to-position", Fbyte_to_position, Sbyte_to_position, 1, 1, 0,
 If BYTEPOS is out of range, the value is nil.  */)
   (Lisp_Object bytepos)
 {
+  ptrdiff_t pos_byte;
+
   CHECK_NUMBER (bytepos);
-  if (XINT (bytepos) < BEG_BYTE || XINT (bytepos) > Z_BYTE)
+  pos_byte = XINT (bytepos);
+  if (pos_byte < BEG_BYTE || pos_byte > Z_BYTE)
     return Qnil;
-  return make_number (BYTE_TO_CHAR (XINT (bytepos)));
+  if (Z != Z_BYTE)
+    /* There are multibyte characters in the buffer.
+       Search for the start of the current character. */
+    while (!CHAR_HEAD_P (FETCH_BYTE (pos_byte)))
+      pos_byte--;
+  return make_number (BYTE_TO_CHAR (pos_byte));
 }
 \f
 DEFUN ("following-char", Ffollowing_char, Sfollowing_char, 0, 0, 0,
-- 
2.4.2






  reply	other threads:[~2015-06-11 15:24 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=85oakmxn4i.fsf@iznogoud.viz \
    --to=wjenkner@inode.at \
    --cc=20783@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.