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)
}
-/* 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
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,
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;
}
return i;
}
\f
-/* 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. */
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;
}
{
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,
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;
}
}