[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 5990da6290: Simplify str_to_multibyte and related code
From: |
Mattias Engdegård |
Subject: |
master 5990da6290: Simplify str_to_multibyte and related code |
Date: |
Mon, 11 Jul 2022 07:52:09 -0400 (EDT) |
branch: master
commit 5990da629074b09212d7dea31811d0429e3e2fb8
Author: Mattias Engdegård <mattiase@acm.org>
Commit: Mattias Engdegård <mattiase@acm.org>
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.
---
lisp/emacs-lisp/byte-opt.el | 2 --
src/character.c | 22 ++++++++++----------
src/character.h | 4 ++--
src/fns.c | 50 +++++++++++++--------------------------------
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 352ac40663..b7147a7f50 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 841e46c091..968daccafa 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 36e2b06ee1..6d0f035c2b 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 7d8f957ef9..eb83471649 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 9a31e386f5..b5a621f80a 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;
}
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- master 5990da6290: Simplify str_to_multibyte and related code,
Mattias Engdegård <=