+ PARSE_MULTIBYTE_SEQ (ptr, endp - ptr, bytes);
+ ptr += bytes;
+ chars++;
+ }
+
+ return chars;
+}
+
+/* Parse unibyte text at STR of LEN bytes as multibyte text, and
+ count the numbers of characters and bytes in it. On counting
+ bytes, pay attention to the fact that 8-bit characters in the range
+ 0x80..0x9F are represented by 2 bytes in multibyte text. */
+void
+parse_str_as_multibyte (str, len, nchars, nbytes)
+ const unsigned char *str;
+ int len, *nchars, *nbytes;
+{
+ const unsigned char *endp = str + len;
+ int n, chars = 0, bytes = 0;
+
+ while (str < endp)
+ {
+ if (UNIBYTE_STR_AS_MULTIBYTE_P (str, endp - str, n))
+ str += n, bytes += n;
+ else
+ str++, bytes += 2;
+ chars++;
+ }
+ *nchars = chars;
+ *nbytes = bytes;
+ return;
+}
+
+/* Arrange unibyte text at STR of NBYTES bytes as multibyte text.
+ It actually converts only 8-bit characters in the range 0x80..0x9F
+ that don't contruct multibyte characters to multibyte forms. If
+ NCHARS is nonzero, set *NCHARS to the number of characters in the
+ text. It is assured that we can use LEN bytes at STR as a work
+ area and that is enough. Return the number of bytes of the
+ resulting text. */
+
+int
+str_as_multibyte (str, len, nbytes, nchars)
+ unsigned char *str;
+ int len, nbytes, *nchars;
+{
+ unsigned char *p = str, *endp = str + nbytes;
+ unsigned char *to;
+ int chars = 0;
+ int n;
+
+ while (p < endp && UNIBYTE_STR_AS_MULTIBYTE_P (p, endp - p, n))
+ p += n, chars++;
+ if (nchars)
+ *nchars = chars;
+ if (p == endp)
+ return nbytes;
+
+ to = p;
+ nbytes = endp - p;
+ endp = str + len;
+ safe_bcopy (p, endp - nbytes, nbytes);
+ p = endp - nbytes;
+ while (p < endp)
+ {
+ if (UNIBYTE_STR_AS_MULTIBYTE_P (p, endp - p, n))