Commit c269148b authored by Dmitry Antipov's avatar Dmitry Antipov
Browse files

* src/fns.c (Freverse): Allow vectors, bool vectors and strings.

* doc/lispref/lists.texi (Building Cons Cells and Lists): Remove
description of `reverse' and generalize it...
* doc/lispref/sequences.texi (Sequences): ...for sequences here.
parent b8e11d41
2014-05-15 Dmitry Antipov <dmantipov@yandex.ru>
* lists.texi (Building Cons Cells and Lists): Remove
description of `reverse' and generalize it...
* sequences.texi (Sequences): ...for sequences here.
2014-05-14 Glenn Morris <rgm@gnu.org>
* files.texi (Changing Files): Mention with-file-modes.
......
......@@ -601,25 +601,6 @@ not a list, the sequence's elements do not become elements of the
resulting list. Instead, the sequence becomes the final @sc{cdr}, like
any other non-list final argument.
@defun reverse list
This function creates a new list whose elements are the elements of
@var{list}, but in reverse order. The original argument @var{list} is
@emph{not} altered.
@example
@group
(setq x '(1 2 3 4))
@result{} (1 2 3 4)
@end group
@group
(reverse x)
@result{} (4 3 2 1)
x
@result{} (1 2 3 4)
@end group
@end example
@end defun
@defun copy-tree tree &optional vecp
This function returns a copy of the tree @code{tree}. If @var{tree} is a
cons cell, this makes a new cons cell with the same @sc{car} and
......
......@@ -217,6 +217,49 @@ y @result{} [foo (69 2)]
@end example
@end defun
@defun reverse seq
@cindex string reverse
@cindex list reverse
@cindex vector reverse
@cindex sequence reverse
This function creates a new sequence whose elements are the elements
of @var{seq}, but in reverse order. The original argument @var{seq}
is @emph{not} altered. Note that char-table cannot be reversed.
@example
@group
(setq x '(1 2 3 4))
@result{} (1 2 3 4)
@end group
@group
(reverse x)
@result{} (4 3 2 1)
x
@result{} (1 2 3 4)
@end group
@group
(setq x [1 2 3 4])
@result{} [1 2 3 4]
@end group
@group
(reverse x)
@result{} [4 3 2 1]
x
@result{} [1 2 3 4]
@end group
@group
(setq x "xyzzy")
@result{} "xyzzy"
@end group
@group
(reverse x)
@result{} "yzzyx"
x
@result{} "xyzzy"
@end group
@end example
@end defun
@node Arrays
@section Arrays
@cindex array
......
2014-05-15 Dmitry Antipov <dmantipov@yandex.ru>
* fns.c (Freverse): Allow vectors, bool vectors and strings.
2014-05-14 Dmitry Antipov <dmantipov@yandex.ru>
Minor cleanup for terminal setup.
......
......@@ -1719,18 +1719,70 @@ Return the reversed list. Expects a properly nil-terminated list. */)
}
DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0,
doc: /* Reverse LIST, copying. Return the reversed list.
doc: /* Return the reversed copy of list, vector, or string SEQ.
See also the function `nreverse', which is used more often. */)
(Lisp_Object list)
(Lisp_Object seq)
{
Lisp_Object new;
for (new = Qnil; CONSP (list); list = XCDR (list))
if (NILP (seq))
return Qnil;
else if (CONSP (seq))
{
QUIT;
new = Fcons (XCAR (list), new);
for (new = Qnil; CONSP (seq); seq = XCDR (seq))
{
QUIT;
new = Fcons (XCAR (seq), new);
}
CHECK_LIST_END (seq, seq);
}
else if (VECTORP (seq))
{
ptrdiff_t i, size = ASIZE (seq);
new = make_uninit_vector (size);
for (i = 0; i < size; i++)
ASET (new, i, AREF (seq, size - i - 1));
}
CHECK_LIST_END (list, list);
else if (BOOL_VECTOR_P (seq))
{
ptrdiff_t i;
EMACS_INT nbits = bool_vector_size (seq);
new = make_uninit_bool_vector (nbits);
for (i = 0; i < nbits; i++)
bool_vector_set (new, i, bool_vector_bitref (seq, nbits - i - 1));
}
else if (STRINGP (seq))
{
ptrdiff_t size = SCHARS (seq), bytes = SBYTES (seq);
if (size == bytes)
{
ptrdiff_t i;
new = make_uninit_string (size);
for (i = 0; i < size; i++)
SSET (new, i, SREF (seq, size - i - 1));
}
else
{
unsigned char *p, *q;
new = make_uninit_multibyte_string (size, bytes);
p = SDATA (seq), q = SDATA (new) + bytes;
while (q > SDATA (new))
{
int ch, len;
ch = STRING_CHAR_AND_LENGTH (p, len);
p += len, q -= len;
CHAR_STRING (ch, q);
}
}
}
else
wrong_type_argument (Qsequencep, seq);
return new;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment