comparison src/editfns.c @ 21837:ea78758c282e

(Fcompare_buffer_substrings): Rewrite to loop by chars.
author Richard M. Stallman <rms@gnu.org>
date Wed, 29 Apr 1998 06:50:40 +0000
parents 9e82920b194d
children c2e75fe68665
comparison
equal deleted inserted replaced
21836:1173cfe8b7bd 21837:ea78758c282e
1684 The value of `case-fold-search' in the current buffer\n\ 1684 The value of `case-fold-search' in the current buffer\n\
1685 determines whether case is significant or ignored.") 1685 determines whether case is significant or ignored.")
1686 (buffer1, start1, end1, buffer2, start2, end2) 1686 (buffer1, start1, end1, buffer2, start2, end2)
1687 Lisp_Object buffer1, start1, end1, buffer2, start2, end2; 1687 Lisp_Object buffer1, start1, end1, buffer2, start2, end2;
1688 { 1688 {
1689 register int begp1, endp1, begp2, endp2, temp, len1, len2, length, i; 1689 register int begp1, endp1, begp2, endp2, temp;
1690 register struct buffer *bp1, *bp2; 1690 register struct buffer *bp1, *bp2;
1691 register Lisp_Object *trt 1691 register Lisp_Object *trt
1692 = (!NILP (current_buffer->case_fold_search) 1692 = (!NILP (current_buffer->case_fold_search)
1693 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0); 1693 ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0);
1694 int chars = 0; 1694 int chars = 0;
1695 int beg1_byte, beg2_byte; 1695 int i1, i2, i1_byte, i2_byte;
1696 1696
1697 /* Find the first buffer and its substring. */ 1697 /* Find the first buffer and its substring. */
1698 1698
1699 if (NILP (buffer1)) 1699 if (NILP (buffer1))
1700 bp1 = current_buffer; 1700 bp1 = current_buffer;
1768 if (!(BUF_BEGV (bp2) <= begp2 1768 if (!(BUF_BEGV (bp2) <= begp2
1769 && begp2 <= endp2 1769 && begp2 <= endp2
1770 && endp2 <= BUF_ZV (bp2))) 1770 && endp2 <= BUF_ZV (bp2)))
1771 args_out_of_range (start2, end2); 1771 args_out_of_range (start2, end2);
1772 1772
1773 beg1_byte = buf_charpos_to_bytepos (bp1, begp1); 1773 i1 = begp1;
1774 beg2_byte = buf_charpos_to_bytepos (bp2, begp2); 1774 i2 = begp2;
1775 len1 = buf_charpos_to_bytepos (bp1, endp1) - begp1; 1775 i1_byte = buf_charpos_to_bytepos (bp1, i1);
1776 len2 = buf_charpos_to_bytepos (bp2, endp2) - begp2; 1776 i2_byte = buf_charpos_to_bytepos (bp2, i2);
1777 length = len1; 1777
1778 if (len2 < length) 1778 while (i1 < endp1 && i2 < endp2)
1779 length = len2; 1779 {
1780 1780 /* When we find a mismatch, we must compare the
1781 for (i = 0; i < length; i++) 1781 characters, not just the bytes. */
1782 { 1782 int c1, c2;
1783 unsigned char *p1 = BUF_BYTE_ADDRESS (bp1, beg1_byte + i); 1783
1784 int c1 = *p1; 1784 if (! NILP (bp1->enable_multibyte_characters))
1785 int c2 = *BUF_BYTE_ADDRESS (bp2, beg2_byte + i); 1785 {
1786 1786 c1 = BUF_FETCH_MULTIBYTE_CHAR (bp1, i1_byte);
1787 /* If a character begins here, 1787 BUF_INC_POS (bp1, i1_byte);
1788 count the previous character now. */ 1788 i1++;
1789 if (i > 0 1789 }
1790 && (NILP (current_buffer->enable_multibyte_characters) 1790 else
1791 || CHAR_HEAD_P (*p1))) 1791 {
1792 chars++; 1792 c1 = BUF_FETCH_BYTE (bp1, i1);
1793 c1 = unibyte_char_to_multibyte (c1);
1794 i1++;
1795 }
1796
1797 if (! NILP (bp2->enable_multibyte_characters))
1798 {
1799 c2 = BUF_FETCH_MULTIBYTE_CHAR (bp2, i2_byte);
1800 BUF_INC_POS (bp2, i2_byte);
1801 i2++;
1802 }
1803 else
1804 {
1805 c2 = BUF_FETCH_BYTE (bp2, i2);
1806 c2 = unibyte_char_to_multibyte (c2);
1807 i2++;
1808 }
1793 1809
1794 if (trt) 1810 if (trt)
1795 { 1811 {
1796 c1 = XINT (trt[c1]); 1812 c1 = XINT (trt[c1]);
1797 c2 = XINT (trt[c2]); 1813 c2 = XINT (trt[c2]);
1798 } 1814 }
1799 if (c1 < c2) 1815 if (c1 < c2)
1800 return make_number (- 1 - chars); 1816 return make_number (- 1 - chars);
1801 if (c1 > c2) 1817 if (c1 > c2)
1802 return make_number (chars + 1); 1818 return make_number (chars + 1);
1819
1820 chars++;
1803 } 1821 }
1804 1822
1805 /* The strings match as far as they go. 1823 /* The strings match as far as they go.
1806 If one is shorter, that one is less. */ 1824 If one is shorter, that one is less. */
1807 if (length < len1) 1825 if (chars < endp1 - begp1)
1808 return make_number (chars + 1); 1826 return make_number (chars + 1);
1809 else if (length < len2) 1827 else if (chars < endp2 - begp2)
1810 return make_number (- chars - 1); 1828 return make_number (- chars - 1);
1811 1829
1812 /* Same length too => they are equal. */ 1830 /* Same length too => they are equal. */
1813 return make_number (0); 1831 return make_number (0);
1814 } 1832 }