Subject: Re: bug#20154: 25.0.50; json-encode-string is too slow for large strings

From: Eli Zaretskii <[email-masked]>
Date: Sat, 21 Mar 2015 10:07:22 +0200

> Date: Sat, 21 Mar 2015 00:26:03 +0200
> From: Dmitry Gutov <[email-masked]>
> CC: [email-masked]
> 
> As per your comment, this seems to be the best we can do for long 
> strings, without diving into C:
> 
> (defun json-encode-string-1 (string)
>    "Return a JSON representation of STRING."
>    (with-temp-buffer
>      (insert string)
>      (goto-char (point-min))
>      ;; Skip over ASCIIish printable characters.
>      (while (re-search-forward "\\([\"\\/\b\f\n\r\t]\\)\\|[^ -~]" nil t)
>        (replace-match
>         (if (match-beginning 1)
>             ;; Special JSON character (\n, \r, etc.).
>             (format "\\%c" (char-after (match-beginning 0)))

Do you really need 'format' here?  Why not

    (concat "\\" (char-to-string (char-after (match-beginning 0))))

instead?

Or even simply insert these two parts at the match point, after
deleting the match.