Function: aset

aset is a function defined in data.c.

Signature

(aset ARRAY IDX NEWELT)

Documentation

Store into the element of ARRAY at index IDX the value NEWELT.

Return NEWELT. ARRAY may be a vector, a string, a char-table or a bool-vector. IDX starts at 0. If ARRAY is a unibyte string, NEWELT must be a single byte (0-255). If ARRAY is a multibyte string, NEWELT and the previous character at index IDX must both be ASCII (0-127).

View in manual

Probably introduced at or before Emacs version 1.12.

Source Code

// Defined in /usr/src/emacs/src/data.c
{
  register EMACS_INT idxval;

  CHECK_FIXNUM (idx);
  idxval = XFIXNUM (idx);
  if (! RECORDP (array))
    CHECK_ARRAY (array, Qarrayp);

  if (VECTORP (array))
    {
      if (idxval < 0 || idxval >= ASIZE (array))
	args_out_of_range (array, idx);
      ASET (array, idxval, newelt);
    }
  else if (BOOL_VECTOR_P (array))
    {
      if (idxval < 0 || idxval >= bool_vector_size (array))
	args_out_of_range (array, idx);
      bool_vector_set (array, idxval, !NILP (newelt));
    }
  else if (CHAR_TABLE_P (array))
    {
      CHECK_CHARACTER (idx);
      CHAR_TABLE_SET (array, idxval, newelt);
    }
  else if (RECORDP (array))
    {
      if (idxval < 0 || idxval >= PVSIZE (array))
	args_out_of_range (array, idx);
      ASET (array, idxval, newelt);
    }
  else /* STRINGP */
    {
      if (idxval < 0 || idxval >= SCHARS (array))
	args_out_of_range (array, idx);
      CHECK_CHARACTER (newelt);
      int c = XFIXNAT (newelt);

      if (STRING_MULTIBYTE (array))
	{
	  if (c > 0x7f)
	    error ("Attempt to store non-ASCII char into multibyte string");
	  ptrdiff_t idxval_byte = string_char_to_byte (array, idxval);
	  unsigned char *p = SDATA (array) + idxval_byte;
	  if (*p > 0x7f)
	    error ("Attempt to replace non-ASCII char in multibyte string");
	  *p = c;
	}
      else
	{
	  if (c > 0xff)
	    error ("Attempt to store non-byte value into unibyte string");
	  SSET (array, idxval, c);
	}
    }
  return newelt;
}