]> git.eshelyaron.com Git - emacs.git/commitdiff
Change the string-limit parameter semantics
authorLars Ingebrigtsen <larsi@gnus.org>
Tue, 22 Dec 2020 05:54:32 +0000 (06:54 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Tue, 22 Dec 2020 05:54:32 +0000 (06:54 +0100)
* lisp/emacs-lisp/subr-x.el (string-limit): Alter the calling
convention.

doc/lispref/strings.texi
lisp/emacs-lisp/shortdoc.el
lisp/emacs-lisp/subr-x.el
test/lisp/emacs-lisp/subr-x-tests.el

index 17cc1a471248d5d04f0f4a12fbb4a773d00da424..80e936e97433669afdf95686a7865933d933088a 100644 (file)
@@ -405,13 +405,12 @@ there are individual words that are longer than @var{length}, these
 will not be shortened.
 @end defun
 
-@defun string-limit string length
-Return a string that's shorter than @var{length}.  If @var{string} is
-shorter than @var{length}, @var{string} is returned as is.  If
-@var{length} is positive, return a substring of @var{string}
-consisting of the first @var{length} characters.  If @var{length} is
-negative, return a string of the @var{-length} last characters
-instead.
+@defun string-limit string length &optional end
+If @var{string} is shorter than @var{length}, @var{string} is returned
+as is.  Otherwise, return a substring of @var{string} consisting of
+the first @var{length} characters.  If the optional @var{end}
+parameter is given, return a string of the @var{length} last
+characters instead.
 @end defun
 
 @defun string-lines string &optional omit-nulls
index 7bb7d233b477df68941ea50a233050240a892b9b..eb57e706608fa78872a045fcb5e2d6ebd0371bfc 100644 (file)
@@ -145,7 +145,7 @@ There can be any number of :example/:result elements."
    :eval (substring "foobar" 3))
   (string-limit
    :eval (string-limit "foobar" 3)
-   :eval (string-limit "foobar" -3)
+   :eval (string-limit "foobar" 3 t)
    :eval (string-limit "foobar" 10))
   (split-string
    :eval (split-string "foo bar")
index 6f4f7ed5dce3397554c9575cf4c07c8bf5e0ef04..b79482fd4b3d6b04cf60aaa9be694edf113c6bfe 100644 (file)
@@ -286,17 +286,20 @@ result will have lines that are longer than LENGTH."
       (fill-region (point-min) (point-max)))
     (buffer-string)))
 
-(defun string-limit (string length)
+(defun string-limit (string length &optional end)
   "Return (up to) a LENGTH substring of STRING.
 If STRING is shorter than or equal to LENGTH, the entire string
-is returned unchanged.  If STRING is longer than LENGTH, and
-LENGTH is a positive number, return a substring consisting of the
-first LENGTH characters of STRING.  If LENGTH is negative, return
-a substring consisting of the last LENGTH characters of STRING."
+is returned unchanged.
+
+If STRING is longer than LENGTH, return a substring consisting of
+the first LENGTH characters of STRING.  If END is non-nil, return
+the last LENTGH characters instead."
+  (unless (natnump length)
+    (signal 'wrong-type-argument (list 'natnump length)))
   (cond
-   ((<= (length string) (abs length)) string)
-   ((>= length 0) (substring string 0 length))
-   ((substring string length))))
+   ((<= (length string) length) string)
+   (end (substring string (- (length string) length)))
+   (t (substring string 0 length))))
 
 (defun string-lines (string &optional omit-nulls)
   "Split STRING into a list of lines.
index 2e16cd0f30b4ba082dee58b72c4f1b8e450c0700..52b480951499ac40e6d96afd1bc31ab4b96dd767 100644 (file)
 (ert-deftest subr-string-limit ()
   (should (equal (string-limit "foo" 10) "foo"))
   (should (equal (string-limit "foo" 2) "fo"))
-  (should (equal (string-limit "foo" -2) "oo"))
-  (should (equal (string-limit "abc" -10) "abc"))
-  (should (equal (string-limit "foo" 0) "")))
+  (should (equal (string-limit "foo" 2 t) "oo"))
+  (should (equal (string-limit "abc" 10 t) "abc"))
+  (should (equal (string-limit "foo" 0) ""))
+  (should-error (string-limit "foo" -1)))
 
 (ert-deftest subr-string-lines ()
   (should (equal (string-lines "foo") '("foo")))