Function: dired-x--string-to-number

dired-x--string-to-number is a byte-compiled function defined in dired-x.el.gz.

Signature

(dired-x--string-to-number STR)

Documentation

Like string-to-number but recognize a trailing unit prefix.

For example, 2K is expanded to 2048.0. The caller should make sure that a trailing letter in STR is one of BKkMGTPEZY.

Source Code

;; Defined in /usr/src/emacs/lisp/dired-x.el.gz
;;; Miscellaneous internal functions

;; Needed if ls -lh is supported and also for GNU ls -ls.
(defun dired-x--string-to-number (str)
  "Like `string-to-number' but recognize a trailing unit prefix.
For example, 2K is expanded to 2048.0.  The caller should make
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))))))
    ;; 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)))