diff options
author | Dmitry Gutov <dgutov@yandex.ru> | 2015-03-23 00:50:58 +0200 |
---|---|---|
committer | Dmitry Gutov <dgutov@yandex.ru> | 2015-03-23 00:50:58 +0200 |
commit | a3b6c249e9757761404c1f7a57de4217dcc2e583 (patch) | |
tree | 5488317660f0969c35858d244a0baa3b07908ed5 /lisp/json.el | |
parent | 2a954e8aa6917572cbf9431f7b1a9ae19be18d7c (diff) | |
download | emacs-a3b6c249e9757761404c1f7a57de4217dcc2e583.tar.gz emacs-a3b6c249e9757761404c1f7a57de4217dcc2e583.tar.bz2 emacs-a3b6c249e9757761404c1f7a57de4217dcc2e583.zip |
Rewrite json-encode-string
Fixes: debbugs:20154
* lisp/json.el (json-decode-char0): Delete this alias.
(json-encode-string): Rewrite to improve performance.
(json-encode-char): Fold into `json-encode-string'.
Diffstat (limited to 'lisp/json.el')
-rw-r--r-- | lisp/json.el | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/lisp/json.el b/lisp/json.el index 98974e67b7e..fb0f62c8777 100644 --- a/lisp/json.el +++ b/lisp/json.el @@ -55,7 +55,6 @@ ;; Compatibility code -(defalias 'json-encode-char0 'encode-char) (defalias 'json-decode-char0 'decode-char) @@ -313,24 +312,28 @@ representation will be parsed correctly." ;; String encoding -(defun json-encode-char (char) - "Encode CHAR as a JSON string." - (setq char (json-encode-char0 char 'ucs)) - (let ((control-char (car (rassoc char json-special-chars)))) - (cond - ;; Special JSON character (\n, \r, etc.). - (control-char - (format "\\%c" control-char)) - ;; ASCIIish printable character. - ((and (> char 31) (< char 127)) - (format "%c" char)) - ;; Fallback: UCS code point in \uNNNN form. - (t - (format "\\u%04x" char))))) - (defun json-encode-string (string) "Return a JSON representation of STRING." - (format "\"%s\"" (mapconcat 'json-encode-char string ""))) + ;; Reimplement the meat of `replace-regexp-in-string', for + ;; performance (bug#20154). + (let ((l (length string)) + (start 0) + res mb) + ;; Skip over ASCIIish printable characters. + (while (setq mb (string-match "[\"\\/\b\f\n\r\t]\\|[^ -~]" string start)) + (let* ((c (aref string mb)) + (special (rassq c json-special-chars))) + (push (substring string start mb) res) + (push (if special + ;; Special JSON character (\n, \r, etc.). + (string ?\\ (car special)) + ;; Fallback: UCS code point in \uNNNN form. + (format "\\u%04x" c)) + res) + (setq start (1+ mb)))) + (push (substring string start l) res) + (push "\"" res) + (apply #'concat "\"" (nreverse res)))) (defun json-encode-key (object) "Return a JSON representation of OBJECT. |