diff options
author | Mattias EngdegÄrd <mattiase@acm.org> | 2022-07-11 13:43:34 +0200 |
---|---|---|
committer | Mattias EngdegÄrd <mattiase@acm.org> | 2022-07-11 13:52:01 +0200 |
commit | 5990da629074b09212d7dea31811d0429e3e2fb8 (patch) | |
tree | 4e2081294c1efc440d98ee06187b8a00c3e89c49 | |
parent | 050252043fe85e12412de311a08f0159cd89e92a (diff) | |
download | emacs-5990da629074b09212d7dea31811d0429e3e2fb8.tar.gz emacs-5990da629074b09212d7dea31811d0429e3e2fb8.tar.bz2 emacs-5990da629074b09212d7dea31811d0429e3e2fb8.zip |
Simplify str_to_multibyte and related code
* src/character.h (str_to_multibyte):
* src/character.c (str_to_multibyte): Remove `nbytes` argument; return
it instead. Copy forwards.
* src/fns.c (concat_to_string, Fstring_make_multibyte):
Use str_to_multibyte.
(string_make_multibyte): Remove.
(string_to_multibyte):
* src/print.c (print_string): Adapt calls.
-rw-r--r-- | lisp/emacs-lisp/byte-opt.el | 2 | ||||
-rw-r--r-- | src/character.c | 22 | ||||
-rw-r--r-- | src/character.h | 4 | ||||
-rw-r--r-- | src/fns.c | 50 | ||||
-rw-r--r-- | src/print.c | 2 |
5 files changed, 28 insertions, 52 deletions
diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el index 352ac40663c..b7147a7f50f 100644 --- a/lisp/emacs-lisp/byte-opt.el +++ b/lisp/emacs-lisp/byte-opt.el @@ -1288,8 +1288,6 @@ See Info node `(elisp) Integer Basics'." form)) ;; Fixme: delete-char -> delete-region (byte-coded) -;; optimize string-as-unibyte, string-as-multibyte, string-make-unibyte, -;; string-make-multibyte for constant args. (put 'set 'byte-optimizer #'byte-optimize-set) (defun byte-optimize-set (form) diff --git a/src/character.c b/src/character.c index 841e46c0917..968daccafa7 100644 --- a/src/character.c +++ b/src/character.c @@ -666,26 +666,26 @@ count_size_as_multibyte (const unsigned char *str, ptrdiff_t len) } -/* Convert unibyte text at SRC of NCHARS bytes to a multibyte text - at DST of NBYTES bytes, that contains the same single-byte characters. */ -void +/* Convert unibyte text at SRC of NCHARS chars to a multibyte text + at DST, that contains the same single-byte characters. + Return the number of bytes written at DST. */ +ptrdiff_t str_to_multibyte (unsigned char *dst, const unsigned char *src, - ptrdiff_t nchars, ptrdiff_t nbytes) + ptrdiff_t nchars) { - const unsigned char *s = src + nchars; - unsigned char *d = dst + nbytes; + unsigned char *d = dst; for (ptrdiff_t i = 0; i < nchars; i++) { - unsigned char c = *--s; + unsigned char c = src[i]; if (c <= 0x7f) - *--d = c; + *d++ = c; else { - *--d = 0x80 + (c & 0x3f); - *--d = 0xc0 + ((c >> 6) & 1); + *d++ = 0xc0 + ((c >> 6) & 1); + *d++ = 0x80 + (c & 0x3f); } } - eassert (d == dst && s == src); + return d - dst; } /* Arrange multibyte text at STR of LEN bytes as a unibyte text. It diff --git a/src/character.h b/src/character.h index 36e2b06ee1b..6d0f035c2bb 100644 --- a/src/character.h +++ b/src/character.h @@ -567,8 +567,8 @@ extern int translate_char (Lisp_Object, int c); extern ptrdiff_t count_size_as_multibyte (const unsigned char *, ptrdiff_t); extern ptrdiff_t str_as_multibyte (unsigned char *, ptrdiff_t, ptrdiff_t, ptrdiff_t *); -extern void str_to_multibyte (unsigned char *dst, const unsigned char *src, - ptrdiff_t nchars, ptrdiff_t nbytes); +extern ptrdiff_t str_to_multibyte (unsigned char *dst, const unsigned char *src, + ptrdiff_t nchars); extern ptrdiff_t str_as_unibyte (unsigned char *, ptrdiff_t); extern ptrdiff_t strwidth (const char *, ptrdiff_t); extern ptrdiff_t c_string_width (const unsigned char *, ptrdiff_t, int, diff --git a/src/fns.c b/src/fns.c index 7d8f957ef98..eb83471649e 100644 --- a/src/fns.c +++ b/src/fns.c @@ -856,9 +856,8 @@ concat_to_string (ptrdiff_t nargs, Lisp_Object *args) else { /* Copy a single-byte string to a multibyte string. */ - toindex_byte += copy_text (SDATA (arg), - SDATA (result) + toindex_byte, - nchars, 0, 1); + toindex_byte += str_to_multibyte (SDATA (result) + toindex_byte, + SDATA (arg), nchars); } toindex += nchars; } @@ -1205,37 +1204,6 @@ string_byte_to_char (Lisp_Object string, ptrdiff_t byte_index) return i; } -/* Convert STRING to a multibyte string. */ - -static Lisp_Object -string_make_multibyte (Lisp_Object string) -{ - unsigned char *buf; - ptrdiff_t nbytes; - Lisp_Object ret; - USE_SAFE_ALLOCA; - - if (STRING_MULTIBYTE (string)) - return string; - - nbytes = count_size_as_multibyte (SDATA (string), - SCHARS (string)); - /* If all the chars are ASCII, they won't need any more bytes - once converted. In that case, we can return STRING itself. */ - if (nbytes == SBYTES (string)) - return string; - - buf = SAFE_ALLOCA (nbytes); - copy_text (SDATA (string), buf, SBYTES (string), - 0, 1); - - ret = make_multibyte_string ((char *) buf, SCHARS (string), nbytes); - SAFE_FREE (); - - return ret; -} - - /* Convert STRING (if unibyte) to a multibyte string without changing the number of characters. Characters 0x80..0xff are interpreted as raw bytes. */ @@ -1254,7 +1222,7 @@ string_to_multibyte (Lisp_Object string) return make_multibyte_string (SSDATA (string), nbytes, nbytes); Lisp_Object ret = make_uninit_multibyte_string (nchars, nbytes); - str_to_multibyte (SDATA (ret), SDATA (string), nchars, nbytes); + str_to_multibyte (SDATA (ret), SDATA (string), nchars); return ret; } @@ -1299,7 +1267,17 @@ string the same way whether it is unibyte or multibyte.) */) { CHECK_STRING (string); - return string_make_multibyte (string); + if (STRING_MULTIBYTE (string)) + return string; + + ptrdiff_t nchars = SCHARS (string); + ptrdiff_t nbytes = count_size_as_multibyte (SDATA (string), nchars); + if (nbytes == nchars) + return string; + + Lisp_Object ret = make_uninit_multibyte_string (nchars, nbytes); + str_to_multibyte (SDATA (ret), SDATA (string), nchars); + return ret; } DEFUN ("string-make-unibyte", Fstring_make_unibyte, Sstring_make_unibyte, diff --git a/src/print.c b/src/print.c index 9a31e386f5e..b5a621f80aa 100644 --- a/src/print.c +++ b/src/print.c @@ -467,7 +467,7 @@ print_string (Lisp_Object string, Lisp_Object printcharfun) if (chars < bytes) { newstr = make_uninit_multibyte_string (chars, bytes); - str_to_multibyte (SDATA (newstr), SDATA (string), chars, bytes); + str_to_multibyte (SDATA (newstr), SDATA (string), chars); string = newstr; } } |