Browse Source

Fix uescape encoding bug

The encoding length for unicode values was applied to the wrong index
variable.

Thanks to Paul Guyot for the detailed bug report and fix.

Fixes #7
pull/8/merge 0.2.0
Paul J. Davis 13 years ago
parent
commit
79bd4c5a52
3 changed files with 22 additions and 10 deletions
  1. +1
    -1
      c_src/encoder.c
  2. +10
    -1
      test/004-strings.t
  3. +11
    -8
      test/util.erl

+ 1
- 1
c_src/encoder.c View File

@ -242,7 +242,7 @@ enc_string(Encoder* e, ERL_NIF_TERM val)
if(uval < 0) {
return 0;
}
ulen = utf8_esc_len(uval);
esc_extra = utf8_esc_len(uval);
if(ulen < 0) {
return 0;
}

+ 10
- 1
test/004-strings.t View File

@ -6,8 +6,9 @@ main([]) ->
code:add_pathz("ebin"),
code:add_pathz("test"),
etap:plan(76),
etap:plan(78),
util:test_good(good()),
util:test_good(uescaped(), [uescape]),
util:test_errors(errors()),
test_utf8(utf8_cases()),
@ -30,6 +31,14 @@ good() ->
}
].
uescaped() ->
[
{
<<"\"\\u8CA8\\u5481\\u3002\\u0091\\u0091\"">>,
<<232,178,168,229,146,129,227,128,130,194,145,194,145>>
}
].
errors() ->
[
<<"\"foo">>,

+ 11
- 8
test/util.erl View File

@ -1,8 +1,11 @@
-module(util).
-export([test_good/1, test_errors/1]).
-export([test_good/1, test_good/2, test_errors/1]).
test_good(Cases) ->
lists:foreach(fun(Case) -> check_good(Case) end, Cases).
test_good(Cases, []).
test_good(Cases, Options) ->
lists:foreach(fun(Case) -> check_good(Case, Options) end, Cases).
test_errors(Cases) ->
lists:foreach(fun(Case) -> check_error(Case) end, Cases).
@ -13,18 +16,18 @@ ok_dec(J, _E) ->
ok_enc(E, _J) ->
lists:flatten(io_lib:format("Encoded ~p", [E])).
do_encode(E) ->
iolist_to_binary(jiffy:encode(E)).
do_encode(E, Options) ->
iolist_to_binary(jiffy:encode(E, Options)).
error_mesg(J) ->
lists:flatten(io_lib:format("Decoding ~p returns an error.", [J])).
check_good({J, E}) ->
check_good({J, E}, Options) ->
etap:is(jiffy:decode(J), E, ok_dec(J, E)),
etap:is(do_encode(E), J, ok_enc(E, J));
check_good({J, E, J2}) ->
etap:is(do_encode(E, Options), J, ok_enc(E, J));
check_good({J, E, J2}, Options) ->
etap:is(jiffy:decode(J), E, ok_dec(J, E)),
etap:is(do_encode(E), J2, ok_enc(E, J2)).
etap:is(do_encode(E, Options), J2, ok_enc(E, J2)).
check_error({J, E}) ->
etap:fun_is(

Loading…
Cancel
Save