Fix byte count parsing
This commit is contained in:
@ -28,18 +28,20 @@
|
|||||||
"Metric power suffixes used in parse-byte-count.")
|
"Metric power suffixes used in parse-byte-count.")
|
||||||
(defparameter *byte-count-pattern*
|
(defparameter *byte-count-pattern*
|
||||||
(cl-ppcre:create-scanner
|
(cl-ppcre:create-scanner
|
||||||
(format nil "^\\s*(?(?=(?:^|[^0-9])\\.[0-9])|([0-9]+))\\.?((?<=\\.)[0-9]+)?~
|
(format nil "^\\s*(?(?=\\.[0-9])\\.|([0-9]+))~
|
||||||
\\s*(?:(?:([kmgtpezyrq])(i)?)?B)?$\\s*")
|
\\.?(?:(?<=\\.)(0*)([0-9]+))?~
|
||||||
|
\\s*(?:(?:([kmgtpezyrq])(i)?)?B)?$\\s*$")
|
||||||
:extended-mode t
|
:extended-mode t
|
||||||
:case-insensitive-mode t)
|
:case-insensitive-mode t)
|
||||||
"Regexp scanner for parse-byte-count.")
|
"Regexp scanner for parse-byte-count.")
|
||||||
|
|
||||||
(defun make-float (int dec)
|
(defun make-float (int leading-zero-count dec)
|
||||||
(if (zerop dec)
|
(if (zerop dec)
|
||||||
(float int)
|
(float int)
|
||||||
(+ (float int)
|
(+ (float int)
|
||||||
(* (if (minusp int) -1 1)
|
(* (if (minusp int) -1 1)
|
||||||
(/ (float dec)
|
(/ (float dec)
|
||||||
|
(expt 10 leading-zero-count)
|
||||||
(expt 10 (1+ (floor (log dec 10)))))))))
|
(expt 10 (1+ (floor (log dec 10)))))))))
|
||||||
|
|
||||||
(defun find-suffix-expt (suffix)
|
(defun find-suffix-expt (suffix)
|
||||||
@ -51,10 +53,14 @@
|
|||||||
(defun parse-byte-count (string)
|
(defun parse-byte-count (string)
|
||||||
"Parse a byte count from STRING."
|
"Parse a byte count from STRING."
|
||||||
(or (ppcre:register-groups-bind
|
(or (ppcre:register-groups-bind
|
||||||
((#'parse-integer int dec)
|
((#'parse-integer int)
|
||||||
|
leading-zeros
|
||||||
|
(#'parse-integer dec)
|
||||||
(#'find-suffix-expt power) base-two)
|
(#'find-suffix-expt power) base-two)
|
||||||
(*byte-count-pattern* string :sharedp t)
|
(*byte-count-pattern* string :sharedp t)
|
||||||
(let ((count (* (make-float (or int 0) (or dec 0))
|
(let ((count (* (make-float (or int 0)
|
||||||
|
(length leading-zeros)
|
||||||
|
(or dec 0))
|
||||||
(expt (if base-two 1024 1000) (or power 0)))))
|
(expt (if base-two 1024 1000) (or power 0)))))
|
||||||
(if (and (not power) (plusp (mod count 1)))
|
(if (and (not power) (plusp (mod count 1)))
|
||||||
(error "Byte count is not a natural number: ~A" count)
|
(error "Byte count is not a natural number: ~A" count)
|
||||||
|
|||||||
Reference in New Issue
Block a user