[Top][All Lists]

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

bug#679: 23.0.60; Function json-read-number does not handle complete Jav

From: Kevin Rodgers
Subject: bug#679: 23.0.60; Function json-read-number does not handle complete Javascript spec
Date: Wed, 13 Aug 2008 20:25:05 -0600
User-agent: Thunderbird (Macintosh/20080707)

Edward O'Connor wrote:

Could you take a look at this bug report?

Sure. I'll have a fix to you in the next few days.

Here's an improved version: Besides recognizing the D. and .D number
forms mentioned in the original bug report, and the positive signed
forms mentioned in my first response, it also

(a) recognizes hexadecimal and octal integers per http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Literals#Integers

(b) correctly fails to recognize more than 1 leading sign (e.g. --, ++, +-, -+)

(c) correctly fails to recognize exponent forms without a preceding number.

I hope you'll use it.

(defun json-read-number (&optional sign base)
  "Read the JSON number following point.
The optional SIGN and BASE arguments are for internal use.

N.B.: Only numbers which can fit in Emacs Lisp's native number
representation will be parsed correctly."
  ;; If SIGN is non-nil, the number is explicitly signed.
  ;; If BASE is null, read a decimal integer or floating point;
  ;; if BASE is 16, read a hexadecimal integer;
  ;; if BASE is 8, read an octal integer.
  (let ((number-regexp
         (cond ((null base)
               ((equal base 8) "[0-7]+")
               ((equal base 16) "[0-9A-Fa-f]+"))))
    (cond ((and (null sign) (char-equal (json-peek) ?-))
           (- (json-read-number t base)))
          ((and (null sign) (char-equal (json-peek) ?+))
           (json-read-number t base))
          ((and (null base) (looking-at "0[Xx]"))
           (json-advance 2)
           (json-read-number t 16))
          ((and (null base) (looking-at "0[0-7]"))
           (json-advance 1)
           (json-read-number t 8))
          ((and (looking-at number-regexp)
                (or base
                    (match-beginning 1)
                    (match-beginning 2)))
           (goto-char (match-end 0))
           (string-to-number (match-string 0) base))
          (t (signal 'json-number-format (list (point)))))))


Kevin Rodgers
Denver, Colorado, USA

reply via email to

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