Commit 3bc25e52 authored by Kenichi Handa's avatar Kenichi Handa
Browse files

(Freplace_match): Adjust multibyteness of the current

buffer and NEWTEXT.  Free allocated memory before signaling an
error.
parent fdce64ff
......@@ -2434,12 +2434,22 @@ since only regular expressions have distinguished subexpressions.")
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 @@ since only regular expressions have distinguished subexpressions.")
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 @@ since only regular expressions have distinguished subexpressions.")
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 @@ since only regular expressions have distinguished subexpressions.")
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
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment