From 13002885275be0499d0131d4d1823d5e5a6a1be6 Mon Sep 17 00:00:00 2001 From: Dmitry Antipov Date: Thu, 20 Dec 2012 20:09:05 +0400 Subject: [PATCH] Avoid calls to CHAR_TO_BYTE if byte position is known. * editfns.c (make_buffer_string_both): Use move_gap_both. (Fbuffer_string): Use make_buffer_string_both. * marker.c (buf_charpos_to_bytepos): Convert to eassert. Adjust comment. (buf_bytepos_to_charpos): Likewise. (charpos_to_bytepos): Remove. * fileio.c (Finsert_file_contents): Use move_gap_both. * search.c (Freplace_match): Likewise. * process.c (process_send_region): Likewise. Use convenient names for byte positions. * lisp.h (charpos_to_bytepos): Remove prototype. * indent.c (scan_for_column): Use CHAR_TO_BYTE. * insdel.c (move_gap): Likewise. --- src/ChangeLog | 17 +++++++++++++++++ src/editfns.c | 4 ++-- src/fileio.c | 2 +- src/indent.c | 3 ++- src/insdel.c | 2 +- src/lisp.h | 1 - src/marker.c | 21 ++++++--------------- src/process.c | 16 ++++++++-------- src/search.c | 2 +- 9 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 7d1b78a510..f9c629c1c5 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,20 @@ +2012-12-20 Dmitry Antipov + + Avoid calls to CHAR_TO_BYTE if byte position is known. + * editfns.c (make_buffer_string_both): Use move_gap_both. + (Fbuffer_string): Use make_buffer_string_both. + * marker.c (buf_charpos_to_bytepos): Convert to eassert. + Adjust comment. + (buf_bytepos_to_charpos): Likewise. + (charpos_to_bytepos): Remove. + * fileio.c (Finsert_file_contents): Use move_gap_both. + * search.c (Freplace_match): Likewise. + * process.c (process_send_region): Likewise. Use convenient + names for byte positions. + * lisp.h (charpos_to_bytepos): Remove prototype. + * indent.c (scan_for_column): Use CHAR_TO_BYTE. + * insdel.c (move_gap): Likewise. + 2012-12-20 Paul Eggert * xdisp.c (redisplay_internal): Remove now-unused local. diff --git a/src/editfns.c b/src/editfns.c index 911cd416e8..d7fe1c1c4c 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -2501,7 +2501,7 @@ make_buffer_string_both (ptrdiff_t start, ptrdiff_t start_byte, Lisp_Object result, tem, tem1; if (start < GPT && GPT < end) - move_gap (start); + move_gap_both (start, start_byte); if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) result = make_uninit_multibyte_string (end - start, end_byte - start_byte); @@ -2599,7 +2599,7 @@ If narrowing is in effect, this function returns only the visible part of the buffer. */) (void) { - return make_buffer_string (BEGV, ZV, 1); + return make_buffer_string_both (BEGV, BEGV_BYTE, ZV, ZV_BYTE, 1); } DEFUN ("insert-buffer-substring", Finsert_buffer_substring, Sinsert_buffer_substring, diff --git a/src/fileio.c b/src/fileio.c index 26150a7e55..45f3b77d72 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -4131,7 +4131,7 @@ variable `last-coding-system-used' to the coding system actually used. */) prepare_to_modify_buffer (GPT, GPT, NULL); } - move_gap (PT); + move_gap_both (PT, PT_BYTE); if (GAP_SIZE < total) make_gap (total - GAP_SIZE); diff --git a/src/indent.c b/src/indent.c index 3dbf372cf1..c1ddbfd236 100644 --- a/src/indent.c +++ b/src/indent.c @@ -571,7 +571,8 @@ scan_for_column (ptrdiff_t *endpos, EMACS_INT *goalcol, ptrdiff_t *prevcol) col += width; if (endp > scan) /* Avoid infinite loops with 0-width overlays. */ { - scan = endp; scan_byte = charpos_to_bytepos (scan); + scan = endp; + scan_byte = CHAR_TO_BYTE (scan); continue; } } diff --git a/src/insdel.c b/src/insdel.c index 74e938c4b8..f9eff889bd 100644 --- a/src/insdel.c +++ b/src/insdel.c @@ -90,7 +90,7 @@ check_markers (void) void move_gap (ptrdiff_t charpos) { - move_gap_both (charpos, charpos_to_bytepos (charpos)); + move_gap_both (charpos, CHAR_TO_BYTE (charpos)); } /* Move gap to byte position BYTEPOS, which is also char position CHARPOS. diff --git a/src/lisp.h b/src/lisp.h index 9af52c0978..cfdff6cf51 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -3176,7 +3176,6 @@ extern void keys_of_buffer (void); extern ptrdiff_t marker_position (Lisp_Object); extern ptrdiff_t marker_byte_position (Lisp_Object); extern void clear_charpos_cache (struct buffer *); -extern ptrdiff_t charpos_to_bytepos (ptrdiff_t); extern ptrdiff_t buf_charpos_to_bytepos (struct buffer *, ptrdiff_t); extern ptrdiff_t buf_bytepos_to_charpos (struct buffer *, ptrdiff_t); extern void unchain_marker (struct Lisp_Marker *marker); diff --git a/src/marker.c b/src/marker.c index 69be4faec3..0df03e0173 100644 --- a/src/marker.c +++ b/src/marker.c @@ -82,9 +82,7 @@ clear_charpos_cache (struct buffer *b) and everywhere there is a marker. So we find the one of these places that is closest to the specified position, and scan from there. */ -/* charpos_to_bytepos returns the byte position corresponding to CHARPOS. */ - -/* This macro is a subroutine of charpos_to_bytepos. +/* This macro is a subroutine of buf_charpos_to_bytepos. Note that it is desirable that BYTEPOS is not evaluated except when we really want its value. */ @@ -128,11 +126,7 @@ clear_charpos_cache (struct buffer *b) } \ } -ptrdiff_t -charpos_to_bytepos (ptrdiff_t charpos) -{ - return buf_charpos_to_bytepos (current_buffer, charpos); -} +/* Return the byte position corresponding to CHARPOS in B. */ ptrdiff_t buf_charpos_to_bytepos (struct buffer *b, ptrdiff_t charpos) @@ -141,8 +135,7 @@ buf_charpos_to_bytepos (struct buffer *b, ptrdiff_t charpos) ptrdiff_t best_above, best_above_byte; ptrdiff_t best_below, best_below_byte; - if (charpos < BUF_BEG (b) || charpos > BUF_Z (b)) - emacs_abort (); + eassert (BUF_BEG (b) <= charpos && charpos <= BUF_Z (b)); best_above = BUF_Z (b); best_above_byte = BUF_Z_BYTE (b); @@ -242,9 +235,6 @@ buf_charpos_to_bytepos (struct buffer *b, ptrdiff_t charpos) #undef CONSIDER -/* buf_bytepos_to_charpos returns the char position corresponding to - BYTEPOS. */ - /* This macro is a subroutine of buf_bytepos_to_charpos. It is used when BYTEPOS is actually the byte position. */ @@ -288,6 +278,8 @@ buf_charpos_to_bytepos (struct buffer *b, ptrdiff_t charpos) } \ } +/* Return the character position corresponding to BYTEPOS in B. */ + ptrdiff_t buf_bytepos_to_charpos (struct buffer *b, ptrdiff_t bytepos) { @@ -295,8 +287,7 @@ buf_bytepos_to_charpos (struct buffer *b, ptrdiff_t bytepos) ptrdiff_t best_above, best_above_byte; ptrdiff_t best_below, best_below_byte; - if (bytepos < BUF_BEG_BYTE (b) || bytepos > BUF_Z_BYTE (b)) - emacs_abort (); + eassert (BUF_BEG_BYTE (b) <= bytepos && bytepos <= BUF_Z_BYTE (b)); best_above = BUF_Z (b); best_above_byte = BUF_Z_BYTE (b); diff --git a/src/process.c b/src/process.c index 92d5e692d0..7ab651a237 100644 --- a/src/process.c +++ b/src/process.c @@ -5556,19 +5556,19 @@ it is sent in several bunches. This may happen even for shorter regions. Output from processes can arrive in between bunches. */) (Lisp_Object process, Lisp_Object start, Lisp_Object end) { - Lisp_Object proc; - ptrdiff_t start1, end1; + Lisp_Object proc = get_process (process); + ptrdiff_t start_byte, end_byte; - proc = get_process (process); validate_region (&start, &end); + start_byte = CHAR_TO_BYTE (XINT (start)); + end_byte = CHAR_TO_BYTE (XINT (end)); + if (XINT (start) < GPT && XINT (end) > GPT) - move_gap (XINT (start)); + move_gap_both (XINT (start), start_byte); - start1 = CHAR_TO_BYTE (XINT (start)); - end1 = CHAR_TO_BYTE (XINT (end)); - send_process (proc, (char *) BYTE_POS_ADDR (start1), end1 - start1, - Fcurrent_buffer ()); + send_process (proc, (char *) BYTE_POS_ADDR (start_byte), + end_byte - start_byte, Fcurrent_buffer ()); return Qnil; } diff --git a/src/search.c b/src/search.c index 7f26601cc6..394012b276 100644 --- a/src/search.c +++ b/src/search.c @@ -2599,7 +2599,7 @@ since only regular expressions have distinguished subexpressions. */) ptrdiff_t begbyte = CHAR_TO_BYTE (search_regs.start[idx]); add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte; if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx]) - move_gap (search_regs.start[idx]); + move_gap_both (search_regs.start[idx], begbyte); add_stuff = BYTE_POS_ADDR (begbyte); } -- 2.20.1