Skip to content

Commit

Permalink
Implement surrogate pair handling using Bufs
Browse files Browse the repository at this point in the history
Tested on Rakudo 2013.08-3-g7cb364b (Parrot & JVM)
  • Loading branch information
Anthony Parsons committed Sep 8, 2013
1 parent 215b406 commit 60b3d80
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/JSON/Tiny.pm
Expand Up @@ -26,7 +26,9 @@ multi to-json(Str:D $d) {
'"'
~ $d.trans(['"', '\\', "\b", "\f", "\n", "\r", "\t"]
=> ['\"', '\\\\', '\b', '\f', '\n', '\r', '\t'])\
.subst(/<-[\c32..\c126]>/, { ord(~$_).fmt('\u%04x') }, :g)
.subst(/<-[\c32..\c126]>/, {
$_.Str.encode('utf-16').values».fmt('\u%04x').join
}, :g)
~ '"'
}
multi to-json(Positional:D $d) {
Expand Down
5 changes: 2 additions & 3 deletions lib/JSON/Tiny/Actions.pm
Expand Up @@ -39,9 +39,8 @@ method value:sym<array>($/) { make $<array>.ast }
method str($/) { make ~$/ }

method str_escape($/) {
if $<xdigit> {
# make chr(:16($<xdigit>.join)); # preferred version of next line, but it doesn't work on Niecza yet
make chr(eval "0x" ~ $<xdigit>.join);
if $<utf16_codepoint> {
make utf16.new( $<utf16_codepoint>.map('0x'~*.Int ).decode();
} else {
my %h = '\\' => "\\",
'/' => "/",
Expand Down
6 changes: 5 additions & 1 deletion lib/JSON/Tiny/Grammar.pm
Expand Up @@ -31,7 +31,11 @@ token str {
}

token str_escape {
<["\\/bfnrt]> | u <xdigit>**4
<["\\/bfnrt]> | 'u' <utf16_codepoint>+ % '\u'
}

token utf16_codepoint {
<.xdigit>**4
}

# vim: ft=perl6

0 comments on commit 60b3d80

Please sign in to comment.