Permalink
Browse files

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
  • Loading branch information...
davisp committed Aug 28, 2011
1 parent 666091a commit 79bd4c5a520c12a46e583cf2e86aae8ce5cfc17e
Showing 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
View
@@ -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;
}
View
@@ -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">>,
View
@@ -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(

0 comments on commit 79bd4c5

Please sign in to comment.