Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement surrogate pairs in the encoder. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/encoder.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ STREAM (or to *JSON-OUTPUT*)."
(write-char #\" stream)
nil)

(defun encode-json-surrogate-pairs-character (char)
"Write characters in range of surrogate pairs to JSON
escape sequence, char -> string."
(let* ((c (char-code char))
(o (- c #x10000)))
(let ((c1 (logior #xD800 (ash o -10)))
(c2 (logior #xDC00 (logand o #x3FF))))
(format nil "\\u~X\\u~X" c1 c2))))

(defun write-json-chars (s stream)
"Write JSON representations (chars or escape sequences) of
characters in string S to STREAM."
Expand All @@ -383,6 +392,8 @@ characters in string S to STREAM."
with special
if (setq special (car (rassoc ch +json-lisp-escaped-chars+)))
do (write-char #\\ stream) (write-char special stream)
else if (<= #x10000 code #x10FFFF)
do (write-string (encode-json-surrogate-pairs-character ch) stream)
else if (< #x1f code #x7f)
do (write-char ch stream)
else
Expand Down