Commit 1d228a31 authored by Jay Belanger's avatar Jay Belanger
Browse files

(math-read-number, math-read-number-simple): Use `save-match-data'.

parent 28a90c44
2009-10-23 Jay Belanger <jay.p.belanger@gmail.com>
* calc/calc.el (math-read-number, math-read-number-simple): Use
`save-match-data'.
2009-10-22 Stefan Monnier <monnier@iro.umontreal.ca> 2009-10-22 Stefan Monnier <monnier@iro.umontreal.ca>
* simple.el (normal-erase-is-backspace-mode): Use input-decode-map * simple.el (normal-erase-is-backspace-mode): Use input-decode-map
......
...@@ -3526,88 +3526,90 @@ largest Emacs integer.") ...@@ -3526,88 +3526,90 @@ largest Emacs integer.")
(defun math-read-number (s &optional decimal) (defun math-read-number (s &optional decimal)
"Convert the string S into a Calc number." "Convert the string S into a Calc number."
(math-normalize (math-normalize
(cond (save-match-data
(cond
;; Integers (most common case)
((string-match "\\` *\\([0-9]+\\) *\\'" s) ;; Integers (most common case)
(let ((digs (math-match-substring s 1))) ((string-match "\\` *\\([0-9]+\\) *\\'" s)
(if (and (memq calc-language calc-lang-c-type-hex) (let ((digs (math-match-substring s 1)))
(> (length digs) 1) (if (and (memq calc-language calc-lang-c-type-hex)
(eq (aref digs 0) ?0) (> (length digs) 1)
(null decimal)) (eq (aref digs 0) ?0)
(math-read-number (concat "8#" digs)) (null decimal))
(if (<= (length digs) (* 2 math-bignum-digit-length)) (math-read-number (concat "8#" digs))
(string-to-number digs) (if (<= (length digs) (* 2 math-bignum-digit-length))
(cons 'bigpos (math-read-bignum digs)))))) (string-to-number digs)
(cons 'bigpos (math-read-bignum digs))))))
;; Clean up the string if necessary
((string-match "\\`\\(.*\\)[ \t\n]+\\([^\001]*\\)\\'" s) ;; Clean up the string if necessary
(math-read-number (concat (math-match-substring s 1) ((string-match "\\`\\(.*\\)[ \t\n]+\\([^\001]*\\)\\'" s)
(math-match-substring s 2)))) (math-read-number (concat (math-match-substring s 1)
(math-match-substring s 2))))
;; Plus and minus signs
((string-match "^[-_+]\\(.*\\)$" s) ;; Plus and minus signs
(let ((val (math-read-number (math-match-substring s 1)))) ((string-match "^[-_+]\\(.*\\)$" s)
(and val (if (eq (aref s 0) ?+) val (math-neg val))))) (let ((val (math-read-number (math-match-substring s 1))))
(and val (if (eq (aref s 0) ?+) val (math-neg val)))))
;; Forms that require extensions module
((string-match "[^-+0-9eE.]" s) ;; Forms that require extensions module
(require 'calc-ext) ((string-match "[^-+0-9eE.]" s)
(math-read-number-fancy s)) (require 'calc-ext)
(math-read-number-fancy s))
;; Decimal point
((string-match "^\\([0-9]*\\)\\.\\([0-9]*\\)$" s) ;; Decimal point
(let ((int (math-match-substring s 1)) ((string-match "^\\([0-9]*\\)\\.\\([0-9]*\\)$" s)
(frac (math-match-substring s 2))) (let ((int (math-match-substring s 1))
(let ((ilen (length int)) (frac (math-match-substring s 2)))
(flen (length frac))) (let ((ilen (length int))
(let ((int (if (> ilen 0) (math-read-number int t) 0)) (flen (length frac)))
(frac (if (> flen 0) (math-read-number frac t) 0))) (let ((int (if (> ilen 0) (math-read-number int t) 0))
(and int frac (or (> ilen 0) (> flen 0)) (frac (if (> flen 0) (math-read-number frac t) 0)))
(list 'float (and int frac (or (> ilen 0) (> flen 0))
(math-add (math-scale-int int flen) frac) (list 'float
(- flen))))))) (math-add (math-scale-int int flen) frac)
(- flen)))))))
;; "e" notation
((string-match "^\\(.*\\)[eE]\\([-+]?[0-9]+\\)$" s) ;; "e" notation
(let ((mant (math-match-substring s 1)) ((string-match "^\\(.*\\)[eE]\\([-+]?[0-9]+\\)$" s)
(exp (math-match-substring s 2))) (let ((mant (math-match-substring s 1))
(let ((mant (if (> (length mant) 0) (math-read-number mant t) 1)) (exp (math-match-substring s 2)))
(exp (if (<= (length exp) (if (memq (aref exp 0) '(?+ ?-)) 8 7)) (let ((mant (if (> (length mant) 0) (math-read-number mant t) 1))
(string-to-number exp)))) (exp (if (<= (length exp) (if (memq (aref exp 0) '(?+ ?-)) 8 7))
(and mant exp (Math-realp mant) (> exp -4000000) (< exp 4000000) (string-to-number exp))))
(let ((mant (math-float mant))) (and mant exp (Math-realp mant) (> exp -4000000) (< exp 4000000)
(list 'float (nth 1 mant) (+ (nth 2 mant) exp))))))) (let ((mant (math-float mant)))
(list 'float (nth 1 mant) (+ (nth 2 mant) exp)))))))
;; Syntax error!
(t nil)))) ;; Syntax error!
(t nil)))))
;;; Parse a very simple number, keeping all digits. ;;; Parse a very simple number, keeping all digits.
(defun math-read-number-simple (s) (defun math-read-number-simple (s)
"Convert the string S into a Calc number. "Convert the string S into a Calc number.
S is assumed to be a simple number (integer or float without an exponent) S is assumed to be a simple number (integer or float without an exponent)
and all digits are kept, regardless of Calc's current precision." and all digits are kept, regardless of Calc's current precision."
(cond (save-match-data
;; Integer (cond
((string-match "^[0-9]+$" s) ;; Integer
(if (string-match "^\\(0+\\)" s) ((string-match "^[0-9]+$" s)
(setq s (substring s (match-end 0)))) (if (string-match "^\\(0+\\)" s)
(if (<= (length s) (* 2 math-bignum-digit-length)) (setq s (substring s (match-end 0))))
(string-to-number s) (if (<= (length s) (* 2 math-bignum-digit-length))
(cons 'bigpos (math-read-bignum s)))) (string-to-number s)
;; Minus sign (cons 'bigpos (math-read-bignum s))))
((string-match "^-[0-9]+$" s) ;; Minus sign
(if (<= (length s) (1+ (* 2 math-bignum-digit-length))) ((string-match "^-[0-9]+$" s)
(string-to-number s) (if (<= (length s) (1+ (* 2 math-bignum-digit-length)))
(cons 'bigneg (math-read-bignum (substring s 1))))) (string-to-number s)
;; Decimal point (cons 'bigneg (math-read-bignum (substring s 1)))))
((string-match "^\\(-?[0-9]*\\)\\.\\([0-9]*\\)$" s) ;; Decimal point
(let ((int (math-match-substring s 1)) ((string-match "^\\(-?[0-9]*\\)\\.\\([0-9]*\\)$" s)
(frac (math-match-substring s 2))) (let ((int (math-match-substring s 1))
(list 'float (math-read-number-simple (concat int frac)) (frac (math-match-substring s 2)))
(- (length frac))))) (list 'float (math-read-number-simple (concat int frac))
;; Syntax error! (- (length frac)))))
(t nil))) ;; Syntax error!
(t nil))))
(defun math-match-substring (s n) (defun math-match-substring (s n)
(if (match-beginning n) (if (match-beginning n)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment