[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 9480169 1/2: Change the string-limit parameter semantics
From: |
Lars Ingebrigtsen |
Subject: |
master 9480169 1/2: Change the string-limit parameter semantics |
Date: |
Tue, 22 Dec 2020 00:59:36 -0500 (EST) |
branch: master
commit 9480169f1b8a27ed61db0913989c9a81339ccd9d
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>
Change the string-limit parameter semantics
* lisp/emacs-lisp/subr-x.el (string-limit): Alter the calling
convention.
---
doc/lispref/strings.texi | 13 ++++++-------
lisp/emacs-lisp/shortdoc.el | 2 +-
lisp/emacs-lisp/subr-x.el | 19 +++++++++++--------
test/lisp/emacs-lisp/subr-x-tests.el | 7 ++++---
4 files changed, 22 insertions(+), 19 deletions(-)
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index 17cc1a4..80e936e 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -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
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el
index 7bb7d23..eb57e70 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -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")
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 6f4f7ed..b79482f 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -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.
diff --git a/test/lisp/emacs-lisp/subr-x-tests.el
b/test/lisp/emacs-lisp/subr-x-tests.el
index 2e16cd0..52b4809 100644
--- a/test/lisp/emacs-lisp/subr-x-tests.el
+++ b/test/lisp/emacs-lisp/subr-x-tests.el
@@ -595,9 +595,10 @@
(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")))