aboutsummaryrefslogtreecommitdiffstats
path: root/src/coding.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/coding.c')
-rw-r--r--src/coding.c111
1 files changed, 68 insertions, 43 deletions
diff --git a/src/coding.c b/src/coding.c
index e18c6f6c511..e4d2fda89cf 100644
--- a/src/coding.c
+++ b/src/coding.c
@@ -5542,28 +5542,6 @@ get_translation_table (attrs, encodep)
5542} 5542}
5543 5543
5544 5544
5545static void
5546translate_chars (coding, table)
5547 struct coding_system *coding;
5548 Lisp_Object table;
5549{
5550 int *charbuf = coding->charbuf;
5551 int *charbuf_end = charbuf + coding->charbuf_used;
5552 int c;
5553
5554 if (coding->chars_at_source)
5555 return;
5556
5557 while (charbuf < charbuf_end)
5558 {
5559 c = *charbuf;
5560 if (c < 0)
5561 charbuf += -c;
5562 else
5563 *charbuf++ = translate_char (table, c);
5564 }
5565}
5566
5567static Lisp_Object 5545static Lisp_Object
5568get_translation (val, buf, buf_end, last_block, from_nchars, to_nchars) 5546get_translation (val, buf, buf_end, last_block, from_nchars, to_nchars)
5569 Lisp_Object val; 5547 Lisp_Object val;
@@ -5571,24 +5549,38 @@ get_translation (val, buf, buf_end, last_block, from_nchars, to_nchars)
5571 int last_block; 5549 int last_block;
5572 int *from_nchars, *to_nchars; 5550 int *from_nchars, *to_nchars;
5573{ 5551{
5574 /* VAL is TO-CHAR, [TO-CHAR ...], ([FROM-CHAR ...] . TO-CHAR), or 5552 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
5575 ([FROM-CHAR ...] . [TO-CHAR ...]). */ 5553 [TO-CHAR ...]. */
5576 if (CONSP (val)) 5554 if (CONSP (val))
5577 { 5555 {
5578 Lisp_Object from; 5556 Lisp_Object from, tail;
5579 int i, len; 5557 int i, len;
5580 5558
5581 from = XCAR (val); 5559 for (tail = val; CONSP (tail); tail = XCDR (tail))
5582 val = XCDR (val);
5583 len = ASIZE (from);
5584 for (i = 0; i < len; i++)
5585 { 5560 {
5586 if (buf + i == buf_end) 5561 val = XCAR (tail);
5587 return (last_block ? Qnil : Qt); 5562 from = XCAR (val);
5588 if (XINT (AREF (from, i)) != buf[i]) 5563 len = ASIZE (from);
5589 return Qnil; 5564 for (i = 0; i < len; i++)
5565 {
5566 if (buf + i == buf_end)
5567 {
5568 if (! last_block)
5569 return Qt;
5570 break;
5571 }
5572 if (XINT (AREF (from, i)) != buf[i])
5573 break;
5574 }
5575 if (i == len)
5576 {
5577 val = XCDR (val);
5578 *from_nchars = len;
5579 break;
5580 }
5590 } 5581 }
5591 *from_nchars = len; 5582 if (! CONSP (tail))
5583 return Qnil;
5592 } 5584 }
5593 if (VECTORP (val)) 5585 if (VECTORP (val))
5594 *buf = XINT (AREF (val, 0)), *to_nchars = ASIZE (val); 5586 *buf = XINT (AREF (val, 0)), *to_nchars = ASIZE (val);
@@ -5648,8 +5640,10 @@ produce_chars (coding, translation_table, last_block)
5648 dst_end = coding->destination + coding->dst_bytes; 5640 dst_end = coding->destination + coding->dst_bytes;
5649 } 5641 }
5650 5642
5651 for (i = 0; i < to_nchars; i++, c = XINT (AREF (trans, i))) 5643 for (i = 0; i < to_nchars; i++)
5652 { 5644 {
5645 if (i > 0)
5646 c = XINT (AREF (trans, i));
5653 if (coding->dst_multibyte 5647 if (coding->dst_multibyte
5654 || ! CHAR_BYTE8_P (c)) 5648 || ! CHAR_BYTE8_P (c))
5655 CHAR_STRING_ADVANCE (c, dst); 5649 CHAR_STRING_ADVANCE (c, dst);
@@ -6182,8 +6176,9 @@ handle_charset_annotation (pos, limit, coding, buf, stop)
6182 6176
6183 6177
6184static void 6178static void
6185consume_chars (coding) 6179consume_chars (coding, translation_table)
6186 struct coding_system *coding; 6180 struct coding_system *coding;
6181 Lisp_Object translation_table;
6187{ 6182{
6188 int *buf = coding->charbuf; 6183 int *buf = coding->charbuf;
6189 int *buf_end = coding->charbuf + coding->charbuf_size; 6184 int *buf_end = coding->charbuf + coding->charbuf_size;
@@ -6195,6 +6190,13 @@ consume_chars (coding)
6195 Lisp_Object eol_type; 6190 Lisp_Object eol_type;
6196 int c; 6191 int c;
6197 EMACS_INT stop, stop_composition, stop_charset; 6192 EMACS_INT stop, stop_composition, stop_charset;
6193 int max_lookup = 0, *lookup_buf = NULL;
6194
6195 if (! NILP (translation_table))
6196 {
6197 max_lookup = XINT (XCHAR_TABLE (translation_table)->extras[1]);
6198 lookup_buf = alloca (sizeof (int) * max_lookup);
6199 }
6198 6200
6199 eol_type = CODING_ID_EOL_TYPE (coding->id); 6201 eol_type = CODING_ID_EOL_TYPE (coding->id);
6200 if (VECTORP (eol_type)) 6202 if (VECTORP (eol_type))
@@ -6221,6 +6223,8 @@ consume_chars (coding)
6221 buf_end -= 1 + MAX_ANNOTATION_LENGTH; 6223 buf_end -= 1 + MAX_ANNOTATION_LENGTH;
6222 while (buf < buf_end) 6224 while (buf < buf_end)
6223 { 6225 {
6226 Lisp_Object trans;
6227
6224 if (pos == stop) 6228 if (pos == stop)
6225 { 6229 {
6226 if (pos == end_pos) 6230 if (pos == end_pos)
@@ -6259,7 +6263,32 @@ consume_chars (coding)
6259 c = '\r'; 6263 c = '\r';
6260 } 6264 }
6261 } 6265 }
6262 *buf++ = c; 6266
6267 if (NILP (translation_table)
6268 || NILP (trans = CHAR_TABLE_REF (translation_table, c)))
6269 *buf++ = c;
6270 else
6271 {
6272 int from_nchars = 1, to_nchars = 1;
6273 int *lookup_buf_end;
6274 const unsigned char *p = src;
6275 int i;
6276
6277 lookup_buf[0] = c;
6278 for (i = 1; i < max_lookup && p < src_end; i++)
6279 lookup_buf[i] = STRING_CHAR_ADVANCE (p);
6280 lookup_buf_end = lookup_buf + i;
6281 trans = get_translation (trans, lookup_buf, lookup_buf_end, 1,
6282 &from_nchars, &to_nchars);
6283 if (EQ (trans, Qt)
6284 || buf + to_nchars > buf_end)
6285 break;
6286 *buf++ = *lookup_buf;
6287 for (i = 1; i < to_nchars; i++)
6288 *buf++ = XINT (AREF (trans, i));
6289 for (i = 1; i < from_nchars; i++, pos++)
6290 src += MULTIBYTE_LENGTH_NO_CHECK (src);
6291 }
6263 } 6292 }
6264 6293
6265 coding->consumed = src - coding->source; 6294 coding->consumed = src - coding->source;
@@ -6316,11 +6345,7 @@ encode_coding (coding)
6316 6345
6317 do { 6346 do {
6318 coding_set_source (coding); 6347 coding_set_source (coding);
6319 consume_chars (coding); 6348 consume_chars (coding, translation_table);
6320
6321 if (!NILP (translation_table))
6322 translate_chars (coding, translation_table);
6323
6324 coding_set_destination (coding); 6349 coding_set_destination (coding);
6325 (*(coding->encoder)) (coding); 6350 (*(coding->encoder)) (coding);
6326 } while (coding->consumed_char < coding->src_chars); 6351 } while (coding->consumed_char < coding->src_chars);
@@ -8989,7 +9014,7 @@ syms_of_coding ()
8989 Qchar_table_extra_slots = intern ("char-table-extra-slots"); 9014 Qchar_table_extra_slots = intern ("char-table-extra-slots");
8990 9015
8991 DEFSYM (Qtranslation_table, "translation-table"); 9016 DEFSYM (Qtranslation_table, "translation-table");
8992 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (1)); 9017 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
8993 DEFSYM (Qtranslation_table_id, "translation-table-id"); 9018 DEFSYM (Qtranslation_table_id, "translation-table-id");
8994 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode"); 9019 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode");
8995 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode"); 9020 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode");