aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGerd Möllmann2025-04-26 21:20:22 +0200
committerGerd Möllmann2025-04-27 04:51:18 +0200
commitc38027f0b8568cbf5d2f60ef56c5c5e076ebb0bc (patch)
tree5737a67fce0a1d8e7cd35654f959220c982a22e3
parentd4879b59a2e777cede1d041c8f7240da80b9a72d (diff)
downloademacs-c38027f0b8568cbf5d2f60ef56c5c5e076ebb0bc.tar.gz
emacs-c38027f0b8568cbf5d2f60ef56c5c5e076ebb0bc.zip
Don't optimize prematurely in text_index_charpos_to_bytepos
* src/text-index.c (text_index_bytepos_to_charpos): Add a const. (bytepos_of_head): Removed. (text_index_charpos_to_bytepos): Don't be clever about choosing to scan forward or backward. (bytepos_forward_to_charpos): Use char_start_bytepos.
-rw-r--r--src/text-index.c26
1 files changed, 8 insertions, 18 deletions
diff --git a/src/text-index.c b/src/text-index.c
index edb2f0d592f..a04d7e39419 100644
--- a/src/text-index.c
+++ b/src/text-index.c
@@ -457,7 +457,7 @@ bytepos_forward_to_charpos (struct buffer *b, const struct text_pos from,
457 ptrdiff_t to_charpos) 457 ptrdiff_t to_charpos)
458{ 458{
459 eassert (from.charpos < to_charpos); 459 eassert (from.charpos < to_charpos);
460 ptrdiff_t bytepos = from.bytepos; 460 ptrdiff_t bytepos = char_start_bytepos (b, from.bytepos);
461 ptrdiff_t charpos = from.charpos; 461 ptrdiff_t charpos = from.charpos;
462 while (charpos < to_charpos) 462 while (charpos < to_charpos)
463 { 463 {
@@ -620,7 +620,7 @@ buf_bytepos_to_charpos (struct buffer *b, const ptrdiff_t bytepos)
620 620
621 /* Scan forward if the distance to the previous known position is 621 /* Scan forward if the distance to the previous known position is
622 smaller than the distance to the next known position. */ 622 smaller than the distance to the next known position. */
623 ptrdiff_t charpos 623 const ptrdiff_t charpos
624 = (bytepos - prev.bytepos < next.bytepos - bytepos) 624 = (bytepos - prev.bytepos < next.bytepos - bytepos)
625 ? charpos_forward_to_bytepos (b, prev, bytepos) 625 ? charpos_forward_to_bytepos (b, prev, bytepos)
626 : charpos_backward_to_bytepos (b, next, bytepos); 626 : charpos_backward_to_bytepos (b, next, bytepos);
@@ -629,14 +629,6 @@ buf_bytepos_to_charpos (struct buffer *b, const ptrdiff_t bytepos)
629 return charpos; 629 return charpos;
630} 630}
631 631
632static ptrdiff_t
633bytepos_of_head (struct buffer *b, ptrdiff_t bytepos)
634{
635 while (!CHAR_HEAD_P (BUF_FETCH_BYTE (b, bytepos)))
636 --bytepos;
637 return bytepos;
638}
639
640/* Return the byte position in buffer B corresponding to character 632/* Return the byte position in buffer B corresponding to character
641 position CHARPOS. */ 633 position CHARPOS. */
642 634
@@ -684,14 +676,12 @@ buf_charpos_to_bytepos (struct buffer *b, const ptrdiff_t charpos)
684 return prev.bytepos + (charpos - prev.charpos); /* ASCII-only! */ 676 return prev.bytepos + (charpos - prev.charpos); /* ASCII-only! */
685 } 677 }
686 678
687 /* Don't scan forward if CHARPOS is exactly on the previous known 679 /* Scan forward if the distance to the previous known position is
688 position because the index bytepos can be in the middle of a 680 smaller than the distance to the next known position. */
689 character, which is found by scanning backwards. */ 681 const ptrdiff_t bytepos
690 ptrdiff_t bytepos 682 = (charpos - prev.charpos < next.charpos - charpos
691 = (charpos == prev.charpos ? bytepos_of_head (b, prev.bytepos) 683 ? bytepos_forward_to_charpos (b, prev, charpos)
692 : (charpos - prev.charpos < next.charpos - charpos 684 : bytepos_backward_to_charpos (b, next, charpos));
693 ? bytepos_forward_to_charpos (b, prev, charpos)
694 : bytepos_backward_to_charpos (b, next, charpos)));
695 685
696 cache (ti, charpos, bytepos); 686 cache (ti, charpos, bytepos);
697 return bytepos; 687 return bytepos;