1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-26 07:33:47 +00:00

Add some pure and side-effect-free declarations

* lisp/subr.el (string-to-list, string-to-vector, string-or-null-p)
(booleanp, special-form-p, plistp, macrop, compiled-function-p)
(flatten-tree):
* lisp/emacs-lisp/subr-x.el (string-join, string-truncate-left)
(string-blank-p, string-remove-prefix, string-remove-suffix)
(string-pad, string-chop-newline):
Declare functions pure, side-effect-free, and/or error-free.
This commit is contained in:
Mattias Engdegård 2023-02-15 12:01:25 +01:00
parent 51e3f91f50
commit 16ba96ad3d
2 changed files with 16 additions and 0 deletions

View File

@ -102,6 +102,7 @@ threading."
"Join all STRINGS using SEPARATOR. "Join all STRINGS using SEPARATOR.
Optional argument SEPARATOR must be a string, a vector, or a list of Optional argument SEPARATOR must be a string, a vector, or a list of
characters; nil stands for the empty string." characters; nil stands for the empty string."
(declare (pure t) (side-effect-free t))
(mapconcat #'identity strings separator)) (mapconcat #'identity strings separator))
(define-obsolete-function-alias 'string-reverse 'reverse "25.1") (define-obsolete-function-alias 'string-reverse 'reverse "25.1")
@ -112,6 +113,7 @@ characters; nil stands for the empty string."
When truncating, \"...\" is always prepended to the string, so When truncating, \"...\" is always prepended to the string, so
the resulting string may be longer than the original if LENGTH is the resulting string may be longer than the original if LENGTH is
3 or smaller." 3 or smaller."
(declare (pure t) (side-effect-free t))
(let ((strlen (length string))) (let ((strlen (length string)))
(if (<= strlen length) (if (<= strlen length)
string string
@ -124,16 +126,19 @@ the resulting string may be longer than the original if LENGTH is
"Check whether STRING is either empty or only whitespace. "Check whether STRING is either empty or only whitespace.
The following characters count as whitespace here: space, tab, newline and The following characters count as whitespace here: space, tab, newline and
carriage return." carriage return."
(declare (pure t) (side-effect-free t))
(string-match-p "\\`[ \t\n\r]*\\'" string)) (string-match-p "\\`[ \t\n\r]*\\'" string))
(defsubst string-remove-prefix (prefix string) (defsubst string-remove-prefix (prefix string)
"Remove PREFIX from STRING if present." "Remove PREFIX from STRING if present."
(declare (pure t) (side-effect-free t))
(if (string-prefix-p prefix string) (if (string-prefix-p prefix string)
(substring string (length prefix)) (substring string (length prefix))
string)) string))
(defsubst string-remove-suffix (suffix string) (defsubst string-remove-suffix (suffix string)
"Remove SUFFIX from STRING if present." "Remove SUFFIX from STRING if present."
(declare (pure t) (side-effect-free t))
(if (string-suffix-p suffix string) (if (string-suffix-p suffix string)
(substring string 0 (- (length string) (length suffix))) (substring string 0 (- (length string) (length suffix)))
string)) string))
@ -252,6 +257,7 @@ is done.
If START is nil (or not present), the padding is done to the end If START is nil (or not present), the padding is done to the end
of the string, and if non-nil, padding is done to the start of of the string, and if non-nil, padding is done to the start of
the string." the string."
(declare (pure t) (side-effect-free t))
(unless (natnump length) (unless (natnump length)
(signal 'wrong-type-argument (list 'natnump length))) (signal 'wrong-type-argument (list 'natnump length)))
(let ((pad-length (- length (length string)))) (let ((pad-length (- length (length string))))
@ -261,6 +267,7 @@ the string."
(defun string-chop-newline (string) (defun string-chop-newline (string)
"Remove the final newline (if any) from STRING." "Remove the final newline (if any) from STRING."
(declare (pure t) (side-effect-free t))
(string-remove-suffix "\n" string)) (string-remove-suffix "\n" string))
(defun replace-region-contents (beg end replace-fn (defun replace-region-contents (beg end replace-fn

View File

@ -4124,15 +4124,18 @@ system's shell."
(defsubst string-to-list (string) (defsubst string-to-list (string)
"Return a list of characters in STRING." "Return a list of characters in STRING."
(declare (side-effect-free t))
(append string nil)) (append string nil))
(defsubst string-to-vector (string) (defsubst string-to-vector (string)
"Return a vector of characters in STRING." "Return a vector of characters in STRING."
(declare (side-effect-free t))
(vconcat string)) (vconcat string))
(defun string-or-null-p (object) (defun string-or-null-p (object)
"Return t if OBJECT is a string or nil. "Return t if OBJECT is a string or nil.
Otherwise, return nil." Otherwise, return nil."
(declare (pure t) (side-effect-free error-free))
(or (stringp object) (null object))) (or (stringp object) (null object)))
(defun list-of-strings-p (object) (defun list-of-strings-p (object)
@ -4145,21 +4148,25 @@ Otherwise, return nil."
(defun booleanp (object) (defun booleanp (object)
"Return t if OBJECT is one of the two canonical boolean values: t or nil. "Return t if OBJECT is one of the two canonical boolean values: t or nil.
Otherwise, return nil." Otherwise, return nil."
(declare (pure t) (side-effect-free error-free))
(and (memq object '(nil t)) t)) (and (memq object '(nil t)) t))
(defun special-form-p (object) (defun special-form-p (object)
"Non-nil if and only if OBJECT is a special form." "Non-nil if and only if OBJECT is a special form."
(declare (side-effect-free error-free))
(if (and (symbolp object) (fboundp object)) (if (and (symbolp object) (fboundp object))
(setq object (indirect-function object))) (setq object (indirect-function object)))
(and (subrp object) (eq (cdr (subr-arity object)) 'unevalled))) (and (subrp object) (eq (cdr (subr-arity object)) 'unevalled)))
(defun plistp (object) (defun plistp (object)
"Non-nil if and only if OBJECT is a valid plist." "Non-nil if and only if OBJECT is a valid plist."
(declare (pure t) (side-effect-free error-free))
(let ((len (proper-list-p object))) (let ((len (proper-list-p object)))
(and len (zerop (% len 2))))) (and len (zerop (% len 2)))))
(defun macrop (object) (defun macrop (object)
"Non-nil if and only if OBJECT is a macro." "Non-nil if and only if OBJECT is a macro."
(declare (side-effect-free t))
(let ((def (indirect-function object))) (let ((def (indirect-function object)))
(when (consp def) (when (consp def)
(or (eq 'macro (car def)) (or (eq 'macro (car def))
@ -4169,6 +4176,7 @@ Otherwise, return nil."
"Return non-nil if OBJECT is a function that has been compiled. "Return non-nil if OBJECT is a function that has been compiled.
Does not distinguish between functions implemented in machine code Does not distinguish between functions implemented in machine code
or byte-code." or byte-code."
(declare (side-effect-free error-free))
(or (subrp object) (byte-code-function-p object))) (or (subrp object) (byte-code-function-p object)))
(defun field-at-pos (pos) (defun field-at-pos (pos)
@ -6843,6 +6851,7 @@ returned list are in the same order as in TREE.
\(flatten-tree \\='(1 (2 . 3) nil (4 5 (6)) 7)) \(flatten-tree \\='(1 (2 . 3) nil (4 5 (6)) 7))
=> (1 2 3 4 5 6 7)" => (1 2 3 4 5 6 7)"
(declare (side-effect-free error-free))
(let (elems) (let (elems)
(while (consp tree) (while (consp tree)
(let ((elem (pop tree))) (let ((elem (pop tree)))