changeset 28387:9a8814cc543c

(Freplace_match): Adjust multibyteness of the current buffer and NEWTEXT. Free allocated memory before signaling an error.
author Kenichi Handa <handa@m17n.org>
date Wed, 29 Mar 2000 11:31:23 +0000
parents ca59dff6ec37
children 5b49a098d0cc
files src/search.c
diffstat 1 files changed, 41 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/search.c	Wed Mar 29 11:29:23 2000 +0000
+++ b/src/search.c	Wed Mar 29 11:31:23 2000 +0000
@@ -2434,12 +2434,22 @@
       int length = STRING_BYTES (XSTRING (newtext));
       unsigned char *substed;
       int substed_alloc_size, substed_len;
+      int buf_multibyte = !NILP (current_buffer->enable_multibyte_characters);
+      int str_multibyte = STRING_MULTIBYTE (newtext);
+      Lisp_Object rev_tbl;
+
+      rev_tbl= (!buf_multibyte && CHAR_TABLE_P (Vnonascii_translation_table)
+		? Fchar_table_extra_slot (Vnonascii_translation_table,
+					  make_number (0))
+		: Qnil);
 
       substed_alloc_size = length * 2 + 100;
       substed = (unsigned char *) xmalloc (substed_alloc_size + 1);
       substed_len = 0;
 
-      /* Go thru NEWTEXT, producing the actual text to insert in SUBSTED.  */
+      /* Go thru NEWTEXT, producing the actual text to insert in
+	 SUBSTED while adjusting multibyteness to that of the current
+	 buffer.  */
 
       for (pos_byte = 0, pos = 0; pos_byte < length;)
 	{
@@ -2448,7 +2458,19 @@
 	  int add_len;
 	  int idx = -1;
 
-	  FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
+	  if (str_multibyte)
+	    {
+	      FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
+	      if (!buf_multibyte)
+		c = multibyte_char_to_unibyte (c, rev_tbl);
+	    }
+	  else
+	    {
+	      /* Note that we don't have to increment POS.  */
+	      c = XSTRING (newtext)->data[pos_byte++];
+	      if (buf_multibyte)
+		c = unibyte_char_to_multibyte (c);
+	    }
 
 	  /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED,
 	     or set IDX to a match index, which means put that part
@@ -2456,7 +2478,19 @@
 
 	  if (c == '\\')
 	    {
-	      FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
+	      if (str_multibyte)
+		{
+		  FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
+		  if (!buf_multibyte && !SINGLE_BYTE_CHAR_P (c))
+		    c = multibyte_char_to_unibyte (c, rev_tbl);
+		}
+	      else
+		{
+		  c = XSTRING (newtext)->data[pos_byte++];
+		  if (buf_multibyte)
+		    c = unibyte_char_to_multibyte (c);
+		}
+
 	      if (c == '&')
 		idx = sub;
 	      else if (c >= '1' && c <= '9' && c <= search_regs.num_regs + '0')
@@ -2467,7 +2501,10 @@
 	      else if (c == '\\')
 		add_len = 1, add_stuff = "\\";
 	      else
-		error ("Invalid use of `\\' in replacement text");
+		{
+		  xfree (substed);
+		  error ("Invalid use of `\\' in replacement text");
+		}
 	    }
 	  else
 	    {