Function: make-string

make-string is a function defined in alloc.c.

Signature

(make-string LENGTH INIT &optional MULTIBYTE)

Documentation

Return a newly created string of length LENGTH, with INIT in each element.

LENGTH must be an integer. INIT must be an integer that represents a character. If optional argument MULTIBYTE is non-nil, the result will be a multibyte string even if INIT is an ASCII character.

Other relevant functions are documented in the string group.

View in manual

Probably introduced at or before Emacs version 1.12.

Shortdoc

;; string
(make-string 5 ?x)
    => "xxxxx"

Source Code

// Defined in /usr/src/emacs/src/alloc.c
{
  Lisp_Object val;
  EMACS_INT nbytes;

  CHECK_FIXNAT (length);
  CHECK_CHARACTER (init);

  int c = XFIXNAT (init);
  bool clearit = !c;

  if (ASCII_CHAR_P (c) && NILP (multibyte))
    {
      nbytes = XFIXNUM (length);
      val = make_clear_string (nbytes, clearit);
      if (nbytes && !clearit)
	{
	  memset (SDATA (val), c, nbytes);
	  SDATA (val)[nbytes] = 0;
	}
    }
  else
    {
      unsigned char str[MAX_MULTIBYTE_LENGTH];
      ptrdiff_t len = CHAR_STRING (c, str);
      EMACS_INT string_len = XFIXNUM (length);

      if (ckd_mul (&nbytes, len, string_len))
	string_overflow ();
      val = make_clear_multibyte_string (string_len, nbytes, clearit);
      if (!clearit)
	{
	  unsigned char *beg = SDATA (val), *end = beg + nbytes;
	  for (unsigned char *p = beg; p < end; p += len)
	    {
	      /* First time we just copy STR to the data of VAL.  */
	      if (p == beg)
		memcpy (p, str, len);
	      else
		{
		  /* Next time we copy largest possible chunk from
		     initialized to uninitialized part of VAL.  */
		  len = min (p - beg, end - p);
		  memcpy (p, beg, len);
		}
	    }
	}
    }

  return val;
}