diff options
| author | Dmitry Antipov | 2014-05-15 10:01:46 +0400 |
|---|---|---|
| committer | Dmitry Antipov | 2014-05-15 10:01:46 +0400 |
| commit | c269148bf9a039edff92b1dd7a415d30c21d3087 (patch) | |
| tree | 00a599d3a6b51ddceb51faddce5ef704c6a74e3e | |
| parent | b8e11d4102870e70dc50796454fcdae658caadbb (diff) | |
| download | emacs-c269148bf9a039edff92b1dd7a415d30c21d3087.tar.gz emacs-c269148bf9a039edff92b1dd7a415d30c21d3087.zip | |
* 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.
| -rw-r--r-- | doc/lispref/ChangeLog | 6 | ||||
| -rw-r--r-- | doc/lispref/lists.texi | 19 | ||||
| -rw-r--r-- | doc/lispref/sequences.texi | 43 | ||||
| -rw-r--r-- | src/ChangeLog | 4 | ||||
| -rw-r--r-- | src/fns.c | 64 |
5 files changed, 111 insertions, 25 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index b62a4daa051..d5fe02d2398 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | 2014-05-15 Dmitry Antipov <dmantipov@yandex.ru> | ||
| 2 | |||
| 3 | * lists.texi (Building Cons Cells and Lists): Remove | ||
| 4 | description of `reverse' and generalize it... | ||
| 5 | * sequences.texi (Sequences): ...for sequences here. | ||
| 6 | |||
| 1 | 2014-05-14 Glenn Morris <rgm@gnu.org> | 7 | 2014-05-14 Glenn Morris <rgm@gnu.org> |
| 2 | 8 | ||
| 3 | * files.texi (Changing Files): Mention with-file-modes. | 9 | * files.texi (Changing Files): Mention with-file-modes. |
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi index cde7d9ce44c..882dd440491 100644 --- a/doc/lispref/lists.texi +++ b/doc/lispref/lists.texi | |||
| @@ -601,25 +601,6 @@ not a list, the sequence's elements do not become elements of the | |||
| 601 | resulting list. Instead, the sequence becomes the final @sc{cdr}, like | 601 | resulting list. Instead, the sequence becomes the final @sc{cdr}, like |
| 602 | any other non-list final argument. | 602 | any other non-list final argument. |
| 603 | 603 | ||
| 604 | @defun reverse list | ||
| 605 | This function creates a new list whose elements are the elements of | ||
| 606 | @var{list}, but in reverse order. The original argument @var{list} is | ||
| 607 | @emph{not} altered. | ||
| 608 | |||
| 609 | @example | ||
| 610 | @group | ||
| 611 | (setq x '(1 2 3 4)) | ||
| 612 | @result{} (1 2 3 4) | ||
| 613 | @end group | ||
| 614 | @group | ||
| 615 | (reverse x) | ||
| 616 | @result{} (4 3 2 1) | ||
| 617 | x | ||
| 618 | @result{} (1 2 3 4) | ||
| 619 | @end group | ||
| 620 | @end example | ||
| 621 | @end defun | ||
| 622 | |||
| 623 | @defun copy-tree tree &optional vecp | 604 | @defun copy-tree tree &optional vecp |
| 624 | This function returns a copy of the tree @code{tree}. If @var{tree} is a | 605 | This function returns a copy of the tree @code{tree}. If @var{tree} is a |
| 625 | cons cell, this makes a new cons cell with the same @sc{car} and | 606 | cons cell, this makes a new cons cell with the same @sc{car} and |
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 01de4ccb0cd..c96f1222f3f 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi | |||
| @@ -217,6 +217,49 @@ y @result{} [foo (69 2)] | |||
| 217 | @end example | 217 | @end example |
| 218 | @end defun | 218 | @end defun |
| 219 | 219 | ||
| 220 | @defun reverse seq | ||
| 221 | @cindex string reverse | ||
| 222 | @cindex list reverse | ||
| 223 | @cindex vector reverse | ||
| 224 | @cindex sequence reverse | ||
| 225 | This function creates a new sequence whose elements are the elements | ||
| 226 | of @var{seq}, but in reverse order. The original argument @var{seq} | ||
| 227 | is @emph{not} altered. Note that char-table cannot be reversed. | ||
| 228 | |||
| 229 | @example | ||
| 230 | @group | ||
| 231 | (setq x '(1 2 3 4)) | ||
| 232 | @result{} (1 2 3 4) | ||
| 233 | @end group | ||
| 234 | @group | ||
| 235 | (reverse x) | ||
| 236 | @result{} (4 3 2 1) | ||
| 237 | x | ||
| 238 | @result{} (1 2 3 4) | ||
| 239 | @end group | ||
| 240 | @group | ||
| 241 | (setq x [1 2 3 4]) | ||
| 242 | @result{} [1 2 3 4] | ||
| 243 | @end group | ||
| 244 | @group | ||
| 245 | (reverse x) | ||
| 246 | @result{} [4 3 2 1] | ||
| 247 | x | ||
| 248 | @result{} [1 2 3 4] | ||
| 249 | @end group | ||
| 250 | @group | ||
| 251 | (setq x "xyzzy") | ||
| 252 | @result{} "xyzzy" | ||
| 253 | @end group | ||
| 254 | @group | ||
| 255 | (reverse x) | ||
| 256 | @result{} "yzzyx" | ||
| 257 | x | ||
| 258 | @result{} "xyzzy" | ||
| 259 | @end group | ||
| 260 | @end example | ||
| 261 | @end defun | ||
| 262 | |||
| 220 | @node Arrays | 263 | @node Arrays |
| 221 | @section Arrays | 264 | @section Arrays |
| 222 | @cindex array | 265 | @cindex array |
diff --git a/src/ChangeLog b/src/ChangeLog index e78a32c26ca..ce51b26cf43 100644 --- a/src/ChangeLog +++ b/src/ChangeLog | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | 2014-05-15 Dmitry Antipov <dmantipov@yandex.ru> | ||
| 2 | |||
| 3 | * fns.c (Freverse): Allow vectors, bool vectors and strings. | ||
| 4 | |||
| 1 | 2014-05-14 Dmitry Antipov <dmantipov@yandex.ru> | 5 | 2014-05-14 Dmitry Antipov <dmantipov@yandex.ru> |
| 2 | 6 | ||
| 3 | Minor cleanup for terminal setup. | 7 | Minor cleanup for terminal setup. |
| @@ -1719,18 +1719,70 @@ Return the reversed list. Expects a properly nil-terminated list. */) | |||
| 1719 | } | 1719 | } |
| 1720 | 1720 | ||
| 1721 | DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0, | 1721 | DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0, |
| 1722 | doc: /* Reverse LIST, copying. Return the reversed list. | 1722 | doc: /* Return the reversed copy of list, vector, or string SEQ. |
| 1723 | See also the function `nreverse', which is used more often. */) | 1723 | See also the function `nreverse', which is used more often. */) |
| 1724 | (Lisp_Object list) | 1724 | (Lisp_Object seq) |
| 1725 | { | 1725 | { |
| 1726 | Lisp_Object new; | 1726 | Lisp_Object new; |
| 1727 | 1727 | ||
| 1728 | for (new = Qnil; CONSP (list); list = XCDR (list)) | 1728 | if (NILP (seq)) |
| 1729 | return Qnil; | ||
| 1730 | else if (CONSP (seq)) | ||
| 1729 | { | 1731 | { |
| 1730 | QUIT; | 1732 | for (new = Qnil; CONSP (seq); seq = XCDR (seq)) |
| 1731 | new = Fcons (XCAR (list), new); | 1733 | { |
| 1734 | QUIT; | ||
| 1735 | new = Fcons (XCAR (seq), new); | ||
| 1736 | } | ||
| 1737 | CHECK_LIST_END (seq, seq); | ||
| 1738 | } | ||
| 1739 | else if (VECTORP (seq)) | ||
| 1740 | { | ||
| 1741 | ptrdiff_t i, size = ASIZE (seq); | ||
| 1742 | |||
| 1743 | new = make_uninit_vector (size); | ||
| 1744 | for (i = 0; i < size; i++) | ||
| 1745 | ASET (new, i, AREF (seq, size - i - 1)); | ||
| 1732 | } | 1746 | } |
| 1733 | CHECK_LIST_END (list, list); | 1747 | else if (BOOL_VECTOR_P (seq)) |
| 1748 | { | ||
| 1749 | ptrdiff_t i; | ||
| 1750 | EMACS_INT nbits = bool_vector_size (seq); | ||
| 1751 | |||
| 1752 | new = make_uninit_bool_vector (nbits); | ||
| 1753 | for (i = 0; i < nbits; i++) | ||
| 1754 | bool_vector_set (new, i, bool_vector_bitref (seq, nbits - i - 1)); | ||
| 1755 | } | ||
| 1756 | else if (STRINGP (seq)) | ||
| 1757 | { | ||
| 1758 | ptrdiff_t size = SCHARS (seq), bytes = SBYTES (seq); | ||
| 1759 | |||
| 1760 | if (size == bytes) | ||
| 1761 | { | ||
| 1762 | ptrdiff_t i; | ||
| 1763 | |||
| 1764 | new = make_uninit_string (size); | ||
| 1765 | for (i = 0; i < size; i++) | ||
| 1766 | SSET (new, i, SREF (seq, size - i - 1)); | ||
| 1767 | } | ||
| 1768 | else | ||
| 1769 | { | ||
| 1770 | unsigned char *p, *q; | ||
| 1771 | |||
| 1772 | new = make_uninit_multibyte_string (size, bytes); | ||
| 1773 | p = SDATA (seq), q = SDATA (new) + bytes; | ||
| 1774 | while (q > SDATA (new)) | ||
| 1775 | { | ||
| 1776 | int ch, len; | ||
| 1777 | |||
| 1778 | ch = STRING_CHAR_AND_LENGTH (p, len); | ||
| 1779 | p += len, q -= len; | ||
| 1780 | CHAR_STRING (ch, q); | ||
| 1781 | } | ||
| 1782 | } | ||
| 1783 | } | ||
| 1784 | else | ||
| 1785 | wrong_type_argument (Qsequencep, seq); | ||
| 1734 | return new; | 1786 | return new; |
| 1735 | } | 1787 | } |
| 1736 | 1788 | ||