Permalink
Browse files

apply utf16-surrogate-pair.diff from http://code.google.com/p/mochiwe…

  • Loading branch information...
1 parent ad61de8 commit 5b029015b5ffc2d2592399139b38418dd7a3c1ea @etrepum etrepum committed Sep 28, 2009
Showing with 14 additions and 4 deletions.
  1. +14 −4 src/mochijson2.erl
View
@@ -380,11 +380,20 @@ tokenize_string(B, S=#decoder{offset=O}, Acc) ->
tokenize_string(B, ?ADV_COL(S, 2), [$\r | Acc]);
<<_:O/binary, "\\t", _/binary>> ->
tokenize_string(B, ?ADV_COL(S, 2), [$\t | Acc]);
- <<_:O/binary, "\\u", C3, C2, C1, C0, _/binary>> ->
- %% coalesce UTF-16 surrogate pair?
+ <<_:O/binary, "\\u", C3, C2, C1, C0, Rest/binary>> ->
C = erlang:list_to_integer([C3, C2, C1, C0], 16),
- Acc1 = lists:reverse(xmerl_ucs:to_utf8(C), Acc),
- tokenize_string(B, ?ADV_COL(S, 6), Acc1);
+ if C > 16#D7FF, C < 16#DC00 ->
+ %% coalesce UTF-16 surrogate pair
+ <<"\\u", D3, D2, D1, D0, _/binary>> = Rest,
+ D = erlang:list_to_integer([D3,D2,D1,D0], 16),
+ [CodePoint] = xmerl_ucs:from_utf16be(<<C:16/big-unsigned-integer,
+ D:16/big-unsigned-integer>>),
+ Acc1 = lists:reverse(xmerl_ucs:to_utf8(CodePoint), Acc),
+ tokenize_string(B, ?ADV_COL(S, 12), Acc1);
+ true ->
+ Acc1 = lists:reverse(xmerl_ucs:to_utf8(C), Acc),
+ tokenize_string(B, ?ADV_COL(S, 6), Acc1)
+ end;
<<_:O/binary, C, _/binary>> ->
tokenize_string(B, ?INC_CHAR(S, C), [C | Acc])
end.
@@ -550,6 +559,7 @@ equiv_list([V1 | L1], [V2 | L2]) ->
test_all() ->
[1199344435545.0, 1] = decode(<<"[1199344435545.0,1]">>),
+ <<16#F0,16#9D,16#9C,16#95>> = decode([34,"\\ud835","\\udf15",34]),
test_encoder_utf8(),
test_one(e2j_test_vec(utf8), 1).

0 comments on commit 5b02901

Please sign in to comment.