]> git.eshelyaron.com Git - emacs.git/commitdiff
* src/fns.c (Freverse): Allow vectors, bool vectors and strings.
authorDmitry Antipov <dmantipov@yandex.ru>
Thu, 15 May 2014 06:01:46 +0000 (10:01 +0400)
committerDmitry Antipov <dmantipov@yandex.ru>
Thu, 15 May 2014 06:01:46 +0000 (10:01 +0400)
* doc/lispref/lists.texi (Building Cons Cells and Lists): Remove
description of `reverse' and generalize it...
* doc/lispref/sequences.texi (Sequences): ...for sequences here.

doc/lispref/ChangeLog
doc/lispref/lists.texi
doc/lispref/sequences.texi
src/ChangeLog
src/fns.c

index b62a4daa051e7ff243cfe87ea895837adfa3b548..d5fe02d2398c8a3af5d36ae5f27cf1e62d3d6c3e 100644 (file)
@@ -1,3 +1,9 @@
+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.
index cde7d9ce44ce2f53055b50927ba1e590f68e6c27..882dd440491549434e3d04bcde16ea77cf0350c1 100644 (file)
@@ -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
index 01de4ccb0cd8cad12eef96114891100963771b07..c96f1222f3f74bce84c93515bf5611771d69af68 100644 (file)
@@ -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
index e78a32c26ca171160bd3f694cd396cf44d1aa880..ce51b26cf43933be4750b46dd33d2c0538826722 100644 (file)
@@ -1,3 +1,7 @@
+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.
index 53819ed23aab31ca80b9997048f61e3c6b72db81..8f9734cd7cc87a7d9ae040dc46d0ad16cae41c59 100644 (file)
--- a/src/fns.c
+++ b/src/fns.c
@@ -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;
 }
 \f