Function: string-as-multibyte

string-as-multibyte is a function defined in fns.c.

This function is obsolete since 26.1; use decode-coding-string.

Signature

(string-as-multibyte STRING)

Documentation

Return a multibyte string with the same individual bytes as STRING.

If STRING is multibyte, the result is STRING itself. Otherwise it is a newly created string, with no text properties.

If STRING is unibyte and contains an individual 8-bit byte (i.e. not part of a correct utf-8 sequence), it is converted to the corresponding multibyte character of charset eight-bit. See also string-to-multibyte.

Beware, this often doesn't really do what you think it does. It is similar to (decode-coding-string STRING 'utf-8-emacs). If you're not sure, whether to use string-as-multibyte or string-to-multibyte, use string-to-multibyte.

View in manual

Probably introduced at or before Emacs version 20.3.

Source Code

// Defined in /usr/src/emacs/src/fns.c
{
  CHECK_STRING (string);

  if (! STRING_MULTIBYTE (string))
    {
      Lisp_Object new_string;
      ptrdiff_t nchars, nbytes;

      parse_str_as_multibyte (SDATA (string),
			      SBYTES (string),
			      &nchars, &nbytes);
      new_string = make_uninit_multibyte_string (nchars, nbytes);
      memcpy (SDATA (new_string), SDATA (string), SBYTES (string));
      if (nbytes != SBYTES (string))
	str_as_multibyte (SDATA (new_string), nbytes,
			  SBYTES (string), NULL);
      string = new_string;
      set_string_intervals (string, NULL);
    }
  return string;
}