all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Kevin Rodgers <kevin.d.rodgers@gmail.com>
To: "Edward O'Connor" <hober0@gmail.com>, 679@emacsbugs.donarmstrong.com
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	[thread overview]
Message-ID: <48A39781.6030606@gmail.com> (raw)
In-Reply-To: <3b31caf90808122211p3354755td198047ad90064a4@mail.gmail.com>

Edward O'Connor wrote:
> Hi,
> 
>> 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)
		"\\([0-9]+\\)?\\(\\.[0-9]+\\)?\\([Ee][+-]?[0-9]+\\)?")
	       ((equal base 8) "[0-7]+")
	       ((equal base 16) "[0-9A-Fa-f]+"))))
     (cond ((and (null sign) (char-equal (json-peek) ?-))
	   (json-advance)
	   (- (json-read-number t base)))
	  ((and (null sign) (char-equal (json-peek) ?+))
	   (json-advance)
	   (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)))))))

Thanks,

-- 
Kevin Rodgers
Denver, Colorado, USA






  reply	other threads:[~2008-08-14  2:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-13  3:42 bug#679: 23.0.60; Function json-read-number does not handle complete Javascript spec Chong Yidong
2008-08-13  5:11 ` Edward O'Connor
2008-08-14  2:25   ` Kevin Rodgers [this message]
2008-08-19  5:37     ` Edward O'Connor
2008-08-19  5:37     ` Edward O'Connor
  -- strict thread matches above, loose matches on Subject: below --
2008-08-09  7:05 sand
2008-08-09 10:57 ` OFFICE ZERO
2008-08-09 14:14 ` OFFICE ZERO
2008-08-13  3:11 ` Kevin Rodgers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=48A39781.6030606@gmail.com \
    --to=kevin.d.rodgers@gmail.com \
    --cc=679@emacsbugs.donarmstrong.com \
    --cc=hober0@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.