comparison src/editfns.c @ 22698:ea6ef56295b4

(Fformat): Pay attention to the byte combining problem.
author Kenichi Handa <handa@m17n.org>
date Mon, 06 Jul 1998 01:47:34 +0000
parents ea5a8ef23b45
children 6f129ed55108
comparison
equal deleted inserted replaced
22697:578f11200ed5 22698:ea6ef56295b4
2318 register unsigned char *format, *end; 2318 register unsigned char *format, *end;
2319 int length, nchars; 2319 int length, nchars;
2320 /* Nonzero if the output should be a multibyte string, 2320 /* Nonzero if the output should be a multibyte string,
2321 which is true if any of the inputs is one. */ 2321 which is true if any of the inputs is one. */
2322 int multibyte = 0; 2322 int multibyte = 0;
2323 /* When we make a multibyte string, we must pay attention to the
2324 byte combining problem, i.e., a byte may be combined with a
2325 multibyte charcter of the previous string. This flag tells if we
2326 must consider such a situation or not. */
2327 int maybe_combine_byte;
2323 unsigned char *this_format; 2328 unsigned char *this_format;
2324 int longest_format; 2329 int longest_format;
2325 Lisp_Object val; 2330 Lisp_Object val;
2326 2331
2327 extern char *index (); 2332 extern char *index ();
2474 nchars = 0; 2479 nchars = 0;
2475 n = 0; 2480 n = 0;
2476 2481
2477 /* Scan the format and store result in BUF. */ 2482 /* Scan the format and store result in BUF. */
2478 format = XSTRING (args[0])->data; 2483 format = XSTRING (args[0])->data;
2484 maybe_combine_byte = 0;
2479 while (format != end) 2485 while (format != end)
2480 { 2486 {
2481 if (*format == '%') 2487 if (*format == '%')
2482 { 2488 {
2483 int minlen; 2489 int minlen;
2517 { 2523 {
2518 *p++ = ' '; 2524 *p++ = ' ';
2519 nchars++; 2525 nchars++;
2520 } 2526 }
2521 2527
2528 if (p > buf
2529 && multibyte
2530 && *((unsigned char *) p - 1) >= 0x80
2531 && STRING_MULTIBYTE (args[n])
2532 && XSTRING (args[n])->data[0] >= 0xA0)
2533 maybe_combine_byte = 1;
2522 nbytes = copy_text (XSTRING (args[n])->data, p, 2534 nbytes = copy_text (XSTRING (args[n])->data, p,
2523 STRING_BYTES (XSTRING (args[n])), 2535 STRING_BYTES (XSTRING (args[n])),
2524 STRING_MULTIBYTE (args[n]), multibyte); 2536 STRING_MULTIBYTE (args[n]), multibyte);
2525 p += nbytes; 2537 p += nbytes;
2526 nchars += XSTRING (args[n])->size; 2538 nchars += XSTRING (args[n])->size;
2543 if (INTEGERP (args[n])) 2555 if (INTEGERP (args[n]))
2544 sprintf (p, this_format, XINT (args[n])); 2556 sprintf (p, this_format, XINT (args[n]));
2545 else 2557 else
2546 sprintf (p, this_format, XFLOAT (args[n])->data); 2558 sprintf (p, this_format, XFLOAT (args[n])->data);
2547 2559
2560 if (p > buf
2561 && multibyte
2562 && *((unsigned char *) p - 1) >= 0x80
2563 && *((unsigned char *) p) >= 0xA0)
2564 maybe_combine_byte = 1;
2548 this_nchars = strlen (p); 2565 this_nchars = strlen (p);
2549 p += this_nchars; 2566 p += this_nchars;
2550 nchars += this_nchars; 2567 nchars += this_nchars;
2551 } 2568 }
2552 } 2569 }
2553 else if (STRING_MULTIBYTE (args[0])) 2570 else if (STRING_MULTIBYTE (args[0]))
2554 { 2571 {
2555 /* Copy a whole multibyte character. */ 2572 /* Copy a whole multibyte character. */
2573 if (p > buf
2574 && multibyte
2575 && *((unsigned char *) p - 1) >= 0x80
2576 && *format >= 0xA0)
2577 maybe_combine_byte = 1;
2556 *p++ = *format++; 2578 *p++ = *format++;
2557 while (! CHAR_HEAD_P (*format)) *p++ = *format++; 2579 while (! CHAR_HEAD_P (*format)) *p++ = *format++;
2558 nchars++; 2580 nchars++;
2559 } 2581 }
2560 else if (multibyte) 2582 else if (multibyte)
2568 } 2590 }
2569 else 2591 else
2570 *p++ = *format++, nchars++; 2592 *p++ = *format++, nchars++;
2571 } 2593 }
2572 2594
2595 if (maybe_combine_byte)
2596 nchars = multibyte_chars_in_text (buf, p - buf);
2573 val = make_specified_string (buf, nchars, p - buf, multibyte); 2597 val = make_specified_string (buf, nchars, p - buf, multibyte);
2574 2598
2575 /* If we allocated BUF with malloc, free it too. */ 2599 /* If we allocated BUF with malloc, free it too. */
2576 if (total >= 1000) 2600 if (total >= 1000)
2577 xfree (buf); 2601 xfree (buf);