Function: string-to-unibyte
string-to-unibyte is a function defined in fns.c.
Signature
(string-to-unibyte STRING)
Documentation
Return a unibyte string with the same individual chars as STRING.
If STRING is unibyte, the result is STRING itself.
Otherwise it is a newly created string, with no text properties,
where each eight-bit character is converted to the corresponding byte.
If STRING contains a non-ASCII, non-eight-bit character,
an error is signaled.
Probably introduced at or before Emacs version 23.1.
Source Code
// Defined in /usr/src/emacs/src/fns.c
{
CHECK_STRING (string);
if (!STRING_MULTIBYTE (string))
return string;
ptrdiff_t chars = SCHARS (string);
Lisp_Object ret = make_uninit_string (chars);
unsigned char *src = SDATA (string);
unsigned char *dst = SDATA (ret);
for (ptrdiff_t i = 0; i < chars; i++)
{
unsigned char b = *src++;
if (b <= 0x7f)
*dst++ = b; /* ASCII */
else if (CHAR_BYTE8_HEAD_P (b))
*dst++ = 0x80 | (b & 1) << 6 | (*src++ & 0x3f); /* raw byte */
else
error ("Cannot convert character at index %"pD"d to unibyte", i);
}
return ret;
}