aboutsummaryrefslogtreecommitdiffstats
path: root/src/character.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/character.c')
-rw-r--r--src/character.c86
1 files changed, 50 insertions, 36 deletions
diff --git a/src/character.c b/src/character.c
index c4a3a008bcd..d80f8139486 100644
--- a/src/character.c
+++ b/src/character.c
@@ -378,11 +378,12 @@ usage: (char-width CHAR) */)
378 characters and bytes of the substring in *NCHARS and *NBYTES 378 characters and bytes of the substring in *NCHARS and *NBYTES
379 respectively. */ 379 respectively. */
380 380
381int 381EMACS_INT
382c_string_width (const unsigned char *str, int len, int precision, int *nchars, int *nbytes) 382c_string_width (const unsigned char *str, EMACS_INT len, int precision,
383 EMACS_INT *nchars, EMACS_INT *nbytes)
383{ 384{
384 int i = 0, i_byte = 0; 385 EMACS_INT i = 0, i_byte = 0;
385 int width = 0; 386 EMACS_INT width = 0;
386 struct Lisp_Char_Table *dp = buffer_display_table (); 387 struct Lisp_Char_Table *dp = buffer_display_table ();
387 388
388 while (i_byte < len) 389 while (i_byte < len)
@@ -429,8 +430,8 @@ c_string_width (const unsigned char *str, int len, int precision, int *nchars, i
429 current buffer. The width is measured by how many columns it 430 current buffer. The width is measured by how many columns it
430 occupies on the screen. */ 431 occupies on the screen. */
431 432
432int 433EMACS_INT
433strwidth (const unsigned char *str, int len) 434strwidth (const unsigned char *str, EMACS_INT len)
434{ 435{
435 return c_string_width (str, len, -1, NULL, NULL); 436 return c_string_width (str, len, -1, NULL, NULL);
436} 437}
@@ -442,17 +443,18 @@ strwidth (const unsigned char *str, int len)
442 PRECISION, and set number of characters and bytes of the substring 443 PRECISION, and set number of characters and bytes of the substring
443 in *NCHARS and *NBYTES respectively. */ 444 in *NCHARS and *NBYTES respectively. */
444 445
445int 446EMACS_INT
446lisp_string_width (Lisp_Object string, int precision, int *nchars, int *nbytes) 447lisp_string_width (Lisp_Object string, int precision,
448 EMACS_INT *nchars, EMACS_INT *nbytes)
447{ 449{
448 int len = SCHARS (string); 450 EMACS_INT len = SCHARS (string);
449 /* This set multibyte to 0 even if STRING is multibyte when it 451 /* This set multibyte to 0 even if STRING is multibyte when it
450 contains only ascii and eight-bit-graphic, but that's 452 contains only ascii and eight-bit-graphic, but that's
451 intentional. */ 453 intentional. */
452 int multibyte = len < SBYTES (string); 454 int multibyte = len < SBYTES (string);
453 unsigned char *str = SDATA (string); 455 unsigned char *str = SDATA (string);
454 int i = 0, i_byte = 0; 456 EMACS_INT i = 0, i_byte = 0;
455 int width = 0; 457 EMACS_INT width = 0;
456 struct Lisp_Char_Table *dp = buffer_display_table (); 458 struct Lisp_Char_Table *dp = buffer_display_table ();
457 459
458 while (i < len) 460 while (i < len)
@@ -570,11 +572,11 @@ EMACS_INT
570multibyte_chars_in_text (const unsigned char *ptr, EMACS_INT nbytes) 572multibyte_chars_in_text (const unsigned char *ptr, EMACS_INT nbytes)
571{ 573{
572 const unsigned char *endp = ptr + nbytes; 574 const unsigned char *endp = ptr + nbytes;
573 int chars = 0; 575 EMACS_INT chars = 0;
574 576
575 while (ptr < endp) 577 while (ptr < endp)
576 { 578 {
577 int len = MULTIBYTE_LENGTH (ptr, endp); 579 EMACS_INT len = MULTIBYTE_LENGTH (ptr, endp);
578 580
579 if (len == 0) 581 if (len == 0)
580 abort (); 582 abort ();
@@ -592,10 +594,11 @@ multibyte_chars_in_text (const unsigned char *ptr, EMACS_INT nbytes)
592 represented by 2-byte in a multibyte text. */ 594 represented by 2-byte in a multibyte text. */
593 595
594void 596void
595parse_str_as_multibyte (const unsigned char *str, int len, int *nchars, int *nbytes) 597parse_str_as_multibyte (const unsigned char *str, EMACS_INT len,
598 EMACS_INT *nchars, EMACS_INT *nbytes)
596{ 599{
597 const unsigned char *endp = str + len; 600 const unsigned char *endp = str + len;
598 int n, chars = 0, bytes = 0; 601 EMACS_INT n, chars = 0, bytes = 0;
599 602
600 if (len >= MAX_MULTIBYTE_LENGTH) 603 if (len >= MAX_MULTIBYTE_LENGTH)
601 { 604 {
@@ -633,12 +636,13 @@ parse_str_as_multibyte (const unsigned char *str, int len, int *nchars, int *nby
633 area and that is enough. Return the number of bytes of the 636 area and that is enough. Return the number of bytes of the
634 resulting text. */ 637 resulting text. */
635 638
636int 639EMACS_INT
637str_as_multibyte (unsigned char *str, int len, int nbytes, int *nchars) 640str_as_multibyte (unsigned char *str, EMACS_INT len, EMACS_INT nbytes,
641 EMACS_INT *nchars)
638{ 642{
639 unsigned char *p = str, *endp = str + nbytes; 643 unsigned char *p = str, *endp = str + nbytes;
640 unsigned char *to; 644 unsigned char *to;
641 int chars = 0; 645 EMACS_INT chars = 0;
642 int n; 646 int n;
643 647
644 if (nbytes >= MAX_MULTIBYTE_LENGTH) 648 if (nbytes >= MAX_MULTIBYTE_LENGTH)
@@ -709,11 +713,11 @@ str_as_multibyte (unsigned char *str, int len, int nbytes, int *nchars)
709 bytes it may ocupy when converted to multibyte string by 713 bytes it may ocupy when converted to multibyte string by
710 `str_to_multibyte'. */ 714 `str_to_multibyte'. */
711 715
712int 716EMACS_INT
713parse_str_to_multibyte (const unsigned char *str, int len) 717parse_str_to_multibyte (const unsigned char *str, EMACS_INT len)
714{ 718{
715 const unsigned char *endp = str + len; 719 const unsigned char *endp = str + len;
716 int bytes; 720 EMACS_INT bytes;
717 721
718 for (bytes = 0; str < endp; str++) 722 for (bytes = 0; str < endp; str++)
719 bytes += (*str < 0x80) ? 1 : 2; 723 bytes += (*str < 0x80) ? 1 : 2;
@@ -727,8 +731,8 @@ parse_str_to_multibyte (const unsigned char *str, int len)
727 that we can use LEN bytes at STR as a work area and that is 731 that we can use LEN bytes at STR as a work area and that is
728 enough. */ 732 enough. */
729 733
730int 734EMACS_INT
731str_to_multibyte (unsigned char *str, int len, int bytes) 735str_to_multibyte (unsigned char *str, EMACS_INT len, EMACS_INT bytes)
732{ 736{
733 unsigned char *p = str, *endp = str + bytes; 737 unsigned char *p = str, *endp = str + bytes;
734 unsigned char *to; 738 unsigned char *to;
@@ -756,8 +760,8 @@ str_to_multibyte (unsigned char *str, int len, int bytes)
756 actually converts characters in the range 0x80..0xFF to 760 actually converts characters in the range 0x80..0xFF to
757 unibyte. */ 761 unibyte. */
758 762
759int 763EMACS_INT
760str_as_unibyte (unsigned char *str, int bytes) 764str_as_unibyte (unsigned char *str, EMACS_INT bytes)
761{ 765{
762 const unsigned char *p = str, *endp = str + bytes; 766 const unsigned char *p = str, *endp = str + bytes;
763 unsigned char *to; 767 unsigned char *to;
@@ -818,14 +822,14 @@ str_to_unibyte (const unsigned char *src, unsigned char *dst, EMACS_INT chars, i
818} 822}
819 823
820 824
821int 825EMACS_INT
822string_count_byte8 (Lisp_Object string) 826string_count_byte8 (Lisp_Object string)
823{ 827{
824 int multibyte = STRING_MULTIBYTE (string); 828 int multibyte = STRING_MULTIBYTE (string);
825 int nbytes = SBYTES (string); 829 EMACS_INT nbytes = SBYTES (string);
826 unsigned char *p = SDATA (string); 830 unsigned char *p = SDATA (string);
827 unsigned char *pend = p + nbytes; 831 unsigned char *pend = p + nbytes;
828 int count = 0; 832 EMACS_INT count = 0;
829 int c, len; 833 int c, len;
830 834
831 if (multibyte) 835 if (multibyte)
@@ -851,10 +855,10 @@ string_count_byte8 (Lisp_Object string)
851Lisp_Object 855Lisp_Object
852string_escape_byte8 (Lisp_Object string) 856string_escape_byte8 (Lisp_Object string)
853{ 857{
854 int nchars = SCHARS (string); 858 EMACS_INT nchars = SCHARS (string);
855 int nbytes = SBYTES (string); 859 EMACS_INT nbytes = SBYTES (string);
856 int multibyte = STRING_MULTIBYTE (string); 860 int multibyte = STRING_MULTIBYTE (string);
857 int byte8_count; 861 EMACS_INT byte8_count;
858 const unsigned char *src, *src_end; 862 const unsigned char *src, *src_end;
859 unsigned char *dst; 863 unsigned char *dst;
860 Lisp_Object val; 864 Lisp_Object val;
@@ -869,12 +873,22 @@ string_escape_byte8 (Lisp_Object string)
869 return string; 873 return string;
870 874
871 if (multibyte) 875 if (multibyte)
872 /* Convert 2-byte sequence of byte8 chars to 4-byte octal. */ 876 {
873 val = make_uninit_multibyte_string (nchars + byte8_count * 3, 877 if ((MOST_POSITIVE_FIXNUM - nchars) / 3 < byte8_count
874 nbytes + byte8_count * 2); 878 || (MOST_POSITIVE_FIXNUM - nbytes) / 2 < byte8_count)
879 error ("Maximum string size exceeded");
880
881 /* Convert 2-byte sequence of byte8 chars to 4-byte octal. */
882 val = make_uninit_multibyte_string (nchars + byte8_count * 3,
883 nbytes + byte8_count * 2);
884 }
875 else 885 else
876 /* Convert 1-byte sequence of byte8 chars to 4-byte octal. */ 886 {
877 val = make_uninit_string (nbytes + byte8_count * 3); 887 if ((MOST_POSITIVE_FIXNUM - nchars) / 3 < byte8_count)
888 error ("Maximum string size exceeded");
889 /* Convert 1-byte sequence of byte8 chars to 4-byte octal. */
890 val = make_uninit_string (nbytes + byte8_count * 3);
891 }
878 892
879 src = SDATA (string); 893 src = SDATA (string);
880 src_end = src + nbytes; 894 src_end = src + nbytes;