diff options
| author | Paul Eggert | 2020-04-04 18:26:21 -0700 |
|---|---|---|
| committer | Paul Eggert | 2020-04-04 18:27:11 -0700 |
| commit | e6b5cd0edacb5663859f6a6f93d82a0a4d486e83 (patch) | |
| tree | ff481dd6e0b51173ff7f026cb35f7ca5b9a26c23 /src | |
| parent | c09457e6ed85da8bdc287851a05c4e3f95a44033 (diff) | |
| download | emacs-e6b5cd0edacb5663859f6a6f93d82a0a4d486e83.tar.gz emacs-e6b5cd0edacb5663859f6a6f93d82a0a4d486e83.zip | |
Avoid SAFE_ALLOCA in Fstring, Funibyte_string
* src/character.c (Fstring, Funibyte_string):
Redo to avoid the need for a temporary array allocation
and then a copying from that array to the destination.
Diffstat (limited to 'src')
| -rw-r--r-- | src/character.c | 39 |
1 files changed, 15 insertions, 24 deletions
diff --git a/src/character.c b/src/character.c index d71cb3f145c..a566cacb023 100644 --- a/src/character.c +++ b/src/character.c | |||
| @@ -849,24 +849,22 @@ Concatenate all the argument characters and make the result a string. | |||
| 849 | usage: (string &rest CHARACTERS) */) | 849 | usage: (string &rest CHARACTERS) */) |
| 850 | (ptrdiff_t n, Lisp_Object *args) | 850 | (ptrdiff_t n, Lisp_Object *args) |
| 851 | { | 851 | { |
| 852 | ptrdiff_t i; | 852 | ptrdiff_t nbytes = 0; |
| 853 | int c; | 853 | for (ptrdiff_t i = 0; i < n; i++) |
| 854 | unsigned char *buf, *p; | ||
| 855 | Lisp_Object str; | ||
| 856 | USE_SAFE_ALLOCA; | ||
| 857 | |||
| 858 | SAFE_NALLOCA (buf, MAX_MULTIBYTE_LENGTH, n); | ||
| 859 | p = buf; | ||
| 860 | |||
| 861 | for (i = 0; i < n; i++) | ||
| 862 | { | 854 | { |
| 863 | CHECK_CHARACTER (args[i]); | 855 | CHECK_CHARACTER (args[i]); |
| 864 | c = XFIXNUM (args[i]); | 856 | nbytes += CHAR_BYTES (XFIXNUM (args[i])); |
| 857 | } | ||
| 858 | if (nbytes == n) | ||
| 859 | return Funibyte_string (n, args); | ||
| 860 | Lisp_Object str = make_uninit_multibyte_string (n, nbytes); | ||
| 861 | unsigned char *p = SDATA (str); | ||
| 862 | for (ptrdiff_t i = 0; i < n; i++) | ||
| 863 | { | ||
| 864 | eassume (CHARACTERP (args[i])); | ||
| 865 | int c = XFIXNUM (args[i]); | ||
| 865 | p += CHAR_STRING (c, p); | 866 | p += CHAR_STRING (c, p); |
| 866 | } | 867 | } |
| 867 | |||
| 868 | str = make_string_from_bytes ((char *) buf, n, p - buf); | ||
| 869 | SAFE_FREE (); | ||
| 870 | return str; | 868 | return str; |
| 871 | } | 869 | } |
| 872 | 870 | ||
| @@ -875,20 +873,13 @@ DEFUN ("unibyte-string", Funibyte_string, Sunibyte_string, 0, MANY, 0, | |||
| 875 | usage: (unibyte-string &rest BYTES) */) | 873 | usage: (unibyte-string &rest BYTES) */) |
| 876 | (ptrdiff_t n, Lisp_Object *args) | 874 | (ptrdiff_t n, Lisp_Object *args) |
| 877 | { | 875 | { |
| 878 | ptrdiff_t i; | 876 | Lisp_Object str = make_uninit_string (n); |
| 879 | Lisp_Object str; | 877 | unsigned char *p = SDATA (str); |
| 880 | USE_SAFE_ALLOCA; | 878 | for (ptrdiff_t i = 0; i < n; i++) |
| 881 | unsigned char *buf = SAFE_ALLOCA (n); | ||
| 882 | unsigned char *p = buf; | ||
| 883 | |||
| 884 | for (i = 0; i < n; i++) | ||
| 885 | { | 879 | { |
| 886 | CHECK_RANGED_INTEGER (args[i], 0, 255); | 880 | CHECK_RANGED_INTEGER (args[i], 0, 255); |
| 887 | *p++ = XFIXNUM (args[i]); | 881 | *p++ = XFIXNUM (args[i]); |
| 888 | } | 882 | } |
| 889 | |||
| 890 | str = make_string_from_bytes ((char *) buf, n, p - buf); | ||
| 891 | SAFE_FREE (); | ||
| 892 | return str; | 883 | return str; |
| 893 | } | 884 | } |
| 894 | 885 | ||