summaryrefslogtreecommitdiff
path: root/src/fns.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@Penguin.CS.UCLA.EDU>2018-08-21 02:05:07 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2018-08-21 02:05:31 -0700
commit77fc2725985b4e5ef977ae6930835c7f0771c61c (patch)
treec578f223cba18da66b1a7cb7a38ef7e8c29e1967 /src/fns.c
parenteb83344fc7c08ec08b51e7700f1ac2632afa462c (diff)
downloademacs-77fc2725985b4e5ef977ae6930835c7f0771c61c.tar.gz
emacs-77fc2725985b4e5ef977ae6930835c7f0771c61c.tar.bz2
emacs-77fc2725985b4e5ef977ae6930835c7f0771c61c.zip
Fix glitches introduced by nthcdr changes
* src/fns.c (Fnthcdr): Fix recently-introduced bug when nthcdr is supposed to yield a non-nil non-cons. Reported by Glenn Morris and by Pip Cet here: https://lists.gnu.org/r/emacs-devel/2018-08/msg00699.html https://lists.gnu.org/r/emacs-devel/2018-08/msg00708.html Speed up nthcdr for small N, as suggested by Pip Cet here: https://lists.gnu.org/r/emacs-devel/2018-08/msg00707.html * test/src/fns-tests.el (test-nthcdr-simple): New test.
Diffstat (limited to 'src/fns.c')
-rw-r--r--src/fns.c35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/fns.c b/src/fns.c
index 8cff6b1b6ca..9d681017c14 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -1402,6 +1402,8 @@ DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
doc: /* Take cdr N times on LIST, return the result. */)
(Lisp_Object n, Lisp_Object list)
{
+ Lisp_Object tail = list;
+
CHECK_INTEGER (n);
/* A huge but in-range EMACS_INT that can be substituted for a
@@ -1412,24 +1414,41 @@ DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0,
EMACS_INT num;
if (FIXNUMP (n))
- num = XFIXNUM (n);
+ {
+ num = XFIXNUM (n);
+
+ /* Speed up small lists by omitting circularity and quit checking. */
+ if (num < 128)
+ {
+ for (; 0 < num; num--, tail = XCDR (tail))
+ if (! CONSP (tail))
+ {
+ CHECK_LIST_END (tail, list);
+ return Qnil;
+ }
+ return tail;
+ }
+ }
else
{
- num = mpz_sgn (XBIGNUM (n)->value);
- if (0 < num)
- num = large_num;
+ if (mpz_sgn (XBIGNUM (n)->value) < 0)
+ return tail;
+ num = large_num;
}
EMACS_INT tortoise_num = num;
- Lisp_Object tail = list, saved_tail = tail;
+ Lisp_Object saved_tail = tail;
FOR_EACH_TAIL_SAFE (tail)
{
- if (num <= 0)
- return tail;
- if (tail == li.tortoise)
+ /* If the tortoise just jumped (which is rare),
+ update TORTOISE_NUM accordingly. */
+ if (EQ (tail, li.tortoise))
tortoise_num = num;
+
saved_tail = XCDR (tail);
num--;
+ if (num == 0)
+ return saved_tail;
rarely_quit (num);
}