emacs-diffs
[Top][All Lists]
Advanced

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

master 52b2ffd 1/3: Improve how dired-mark-sexp interprets file sizes in


From: Lars Ingebrigtsen
Subject: master 52b2ffd 1/3: Improve how dired-mark-sexp interprets file sizes in non-C locales
Date: Fri, 3 Dec 2021 11:23:06 -0500 (EST)

branch: master
commit 52b2ffd83b95c2bd974f23f71860a5ea123e3b02
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Improve how dired-mark-sexp interprets file sizes in non-C locales
    
    * lisp/dired-x.el (dired-x--string-to-number): Try to understand
    localised numbers (with "." separators or the like) (bug#23373).
---
 lisp/dired-x.el            | 22 +++++++++++++++-------
 test/lisp/dired-x-tests.el |  7 +++++++
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/lisp/dired-x.el b/lisp/dired-x.el
index 855e58e..38d8a95 100644
--- a/lisp/dired-x.el
+++ b/lisp/dired-x.el
@@ -1265,13 +1265,21 @@ sure that a trailing letter in STR is one of 
BKkMGTPEZY."
   (let* ((val (string-to-number str))
          (u (unless (zerop val)
               (aref str (1- (length str))))))
-    (when (and u (> u ?9))
-      (when (= u ?k)
-        (setq u ?K))
-      (let ((units '(?B ?K ?M ?G ?T ?P ?E ?Z ?Y)))
-        (while (and units (/= (pop units) u))
-          (setq val (* 1024.0 val)))))
-    val))
+    ;; If we don't have a unit at the end, but we have some
+    ;; non-numeric strings in the string, then the string may be
+    ;; something like "4.134" or "4,134" meant to represent 4134
+    ;; (seen in some locales).
+    (if (and u
+             (<= ?0 u ?9)
+             (string-match-p "[^0-9]" str))
+        (string-to-number (replace-regexp-in-string "[^0-9]+" "" str))
+      (when (and u (> u ?9))
+        (when (= u ?k)
+          (setq u ?K))
+        (let ((units '(?B ?K ?M ?G ?T ?P ?E ?Z ?Y)))
+          (while (and units (/= (pop units) u))
+            (setq val (* 1024.0 val)))))
+      val)))
 
 (defun dired-mark-sexp (predicate &optional unflag-p)
   "Mark files for which PREDICATE returns non-nil.
diff --git a/test/lisp/dired-x-tests.el b/test/lisp/dired-x-tests.el
index fe4b971..c6ac7c6 100644
--- a/test/lisp/dired-x-tests.el
+++ b/test/lisp/dired-x-tests.el
@@ -60,5 +60,12 @@
     (should (equal (dired-guess-default '("/tmp/foo.png" "/tmp/foo.txt"))
                    nil))))
 
+(ert-deftest dired-x--string-to-number ()
+  (should (= (dired-x--string-to-number "2.4K") 2457.6))
+  (should (= (dired-x--string-to-number "2400") 2400))
+  (should (= (dired-x--string-to-number "123.4M") 129394278.4))
+  (should (= (dired-x--string-to-number "123.40000M") 129394278.4))
+  (should (= (dired-x--string-to-number "4.134") 4134)))
+
 (provide 'dired-x-tests)
 ;;; dired-x-tests.el ends here



reply via email to

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