emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/compat 1bf572df40: compat-29: Use lax-plist-get and lax


From: ELPA Syncer
Subject: [elpa] externals/compat 1bf572df40: compat-29: Use lax-plist-get and lax-plist-put as optimization
Date: Sat, 13 Jan 2024 19:04:10 -0500 (EST)

branch: externals/compat
commit 1bf572df407f3e40fa062810ac2e7342b4eca140
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    compat-29: Use lax-plist-get and lax-plist-put as optimization
---
 compat-29.el    | 50 ++++++++++++++++++++++++++------------------------
 compat-tests.el | 10 +++++++++-
 2 files changed, 35 insertions(+), 25 deletions(-)

diff --git a/compat-29.el b/compat-29.el
index cd012106ff..5b0a4dfbb3 100644
--- a/compat-29.el
+++ b/compat-29.el
@@ -102,38 +102,40 @@ Unibyte strings are converted to multibyte for 
comparison."
 (compat-defun plist-get (plist prop &optional predicate) ;; 
<compat-tests:plist-get>
   "Handle optional argument PREDICATE."
   :extended t
-  (if (or (null predicate) (eq predicate 'eq))
-      (plist-get plist prop)
-    (catch 'found
-      (while (consp plist)
-        (when (funcall predicate prop (car plist))
-          (throw 'found (cadr plist)))
-        (setq plist (cddr plist))))))
+  (pcase predicate
+    ((or `nil `eq) (plist-get plist prop))
+    (`equal (lax-plist-get plist prop))
+    (_ (catch 'found
+         (while (consp plist)
+           (when (funcall predicate prop (car plist))
+             (throw 'found (cadr plist)))
+           (setq plist (cddr plist)))))))
 
 (compat-defun plist-put (plist prop val &optional predicate) ;; 
<compat-tests:plist-get>
   "Handle optional argument PREDICATE."
   :extended t
-  (if (or (null predicate) (eq predicate 'eq))
-      (plist-put plist prop val)
-    (catch 'found
-      (let ((tail plist))
-        (while (consp tail)
-          (when (funcall predicate prop (car tail))
-            (setcar (cdr tail) val)
-            (throw 'found plist))
-          (setq tail (cddr tail))))
-      (nconc plist (list prop val)))))
+  (pcase predicate
+    ((or `nil `eq) (plist-put plist prop val))
+    (`equal (lax-plist-put plist prop val))
+    (_ (catch 'found
+         (let ((tail plist))
+           (while (consp tail)
+             (when (funcall predicate prop (car tail))
+               (setcar (cdr tail) val)
+               (throw 'found plist))
+             (setq tail (cddr tail))))
+         (nconc plist (list prop val))))))
 
 (compat-defun plist-member (plist prop &optional predicate) ;; 
<compat-tests:plist-get>
   "Handle optional argument PREDICATE."
   :extended t
-  (if (or (null predicate) (eq predicate 'eq))
-      (plist-member plist prop)
-    (catch 'found
-      (while (consp plist)
-        (when (funcall predicate prop (car plist))
-          (throw 'found plist))
-        (setq plist (cddr plist))))))
+  (pcase predicate
+    ((or `nil `eq) (plist-member plist prop))
+    (_ (catch 'found
+         (while (consp plist)
+           (when (funcall predicate prop (car plist))
+             (throw 'found plist))
+           (setq plist (cddr plist)))))))
 
 ;;;; Defined in gv.el
 
diff --git a/compat-tests.el b/compat-tests.el
index 4616b8cbf8..44f9228726 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -1054,7 +1054,15 @@
     (should-equal (compat-call plist-get list "first" #'string=) 10)
     (should-equal (compat-call plist-get list "second" #'string=) 2)
     (should (compat-call plist-member list "first" #'string=))
-    (should-not (compat-call plist-member list "third" #'string=))))
+    (should-not (compat-call plist-member list "third" #'string=)))
+  (let (list)
+    (setq list (compat-call plist-put list "first" 1 #'equal))
+    (setq list (compat-call plist-put list "second" 2 #'equal))
+    (setq list (compat-call plist-put list "first" 10 #'equal))
+    (should-equal (compat-call plist-get list "first" #'equal) 10)
+    (should-equal (compat-call plist-get list "second" #'equal) 2)
+    (should (compat-call plist-member list "first" #'equal))
+    (should-not (compat-call plist-member list "third" #'equal))))
 
 (ert-deftest compat-garbage-collect-maybe ()
   (garbage-collect-maybe 10))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]