merge new_tests branch
This commit is contained in:
commit
2e5bb4fd3b
94 changed files with 2922 additions and 4822 deletions
|
@ -25,20 +25,20 @@
|
|||
|
||||
-export([encoder/3]).
|
||||
|
||||
-spec encoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:encoder().
|
||||
-spec encoder(Handler::module(), State::any(), Config::jsx:config()) -> jsx:encoder().
|
||||
|
||||
encoder(Handler, State, Opts) ->
|
||||
encoder(Handler, State, Config) ->
|
||||
fun(JSON) ->
|
||||
start(
|
||||
JSON,
|
||||
{Handler, Handler:init(State)},
|
||||
jsx_utils:parse_opts(Opts)
|
||||
jsx_utils:parse_config(Config)
|
||||
)
|
||||
end.
|
||||
|
||||
|
||||
|
||||
-include("jsx_opts.hrl").
|
||||
-include("jsx_config.hrl").
|
||||
|
||||
|
||||
-ifndef(error).
|
||||
|
@ -48,86 +48,86 @@ encoder(Handler, State, Opts) ->
|
|||
-endif.
|
||||
|
||||
|
||||
start(Term, {Handler, State}, Opts) ->
|
||||
Handler:handle_event(end_json, value(pre_encode(Term, Opts), {Handler, State}, Opts)).
|
||||
start(Term, {Handler, State}, Config) ->
|
||||
Handler:handle_event(end_json, value(pre_encode(Term, Config), {Handler, State}, Config)).
|
||||
|
||||
|
||||
value(String, {Handler, State}, Opts) when is_binary(String) ->
|
||||
Handler:handle_event({string, clean_string(String, Opts)}, State);
|
||||
value(Float, {Handler, State}, _Opts) when is_float(Float) ->
|
||||
value(String, {Handler, State}, Config) when is_binary(String) ->
|
||||
Handler:handle_event({string, clean_string(String, Config)}, State);
|
||||
value(Float, {Handler, State}, _Config) when is_float(Float) ->
|
||||
Handler:handle_event({float, Float}, State);
|
||||
value(Int, {Handler, State}, _Opts) when is_integer(Int) ->
|
||||
value(Int, {Handler, State}, _Config) when is_integer(Int) ->
|
||||
Handler:handle_event({integer, Int}, State);
|
||||
value(Literal, {Handler, State}, _Opts)
|
||||
value(Literal, {Handler, State}, _Config)
|
||||
when Literal == true; Literal == false; Literal == null ->
|
||||
Handler:handle_event({literal, Literal}, State);
|
||||
value([{}], {Handler, State}, _Opts) ->
|
||||
value([{}], {Handler, State}, _Config) ->
|
||||
Handler:handle_event(end_object, Handler:handle_event(start_object, State));
|
||||
value([], {Handler, State}, _Opts) ->
|
||||
value([], {Handler, State}, _Config) ->
|
||||
Handler:handle_event(end_array, Handler:handle_event(start_array, State));
|
||||
value([Tuple|_] = List, Handler, Opts) when is_tuple(Tuple) ->
|
||||
list_or_object(List, Handler, Opts);
|
||||
value(List, Handler, Opts) when is_list(List) ->
|
||||
list_or_object(List, Handler, Opts);
|
||||
value(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
|
||||
value([Tuple|_] = List, Handler, Config) when is_tuple(Tuple) ->
|
||||
list_or_object(List, Handler, Config);
|
||||
value(List, Handler, Config) when is_list(List) ->
|
||||
list_or_object(List, Handler, Config);
|
||||
value(Term, Handler, Config) -> ?error([Term, Handler, Config]).
|
||||
|
||||
|
||||
list_or_object([Term|Rest], {Handler, State}, Opts) ->
|
||||
case pre_encode(Term, Opts) of
|
||||
list_or_object([Term|Rest], {Handler, State}, Config) ->
|
||||
case pre_encode(Term, Config) of
|
||||
{K, V} ->
|
||||
object([{K, V}|Rest], {Handler, Handler:handle_event(start_object, State)}, Opts)
|
||||
object([{K, V}|Rest], {Handler, Handler:handle_event(start_object, State)}, Config)
|
||||
; T ->
|
||||
list([T|Rest], {Handler, Handler:handle_event(start_array, State)}, Opts)
|
||||
list([T|Rest], {Handler, Handler:handle_event(start_array, State)}, Config)
|
||||
end.
|
||||
|
||||
|
||||
object([{Key, Value}, Next|Rest], {Handler, State}, Opts) when is_atom(Key); is_binary(Key) ->
|
||||
V = pre_encode(Value, Opts),
|
||||
object([{Key, Value}, Next|Rest], {Handler, State}, Config) when is_atom(Key); is_binary(Key) ->
|
||||
V = pre_encode(Value, Config),
|
||||
object(
|
||||
[pre_encode(Next, Opts)|Rest],
|
||||
[pre_encode(Next, Config)|Rest],
|
||||
{
|
||||
Handler,
|
||||
value(
|
||||
V,
|
||||
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), Opts)}, State)},
|
||||
Opts
|
||||
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), Config)}, State)},
|
||||
Config
|
||||
)
|
||||
},
|
||||
Opts
|
||||
Config
|
||||
);
|
||||
object([{Key, Value}], {Handler, State}, Opts) when is_atom(Key); is_binary(Key) ->
|
||||
object([{Key, Value}], {Handler, State}, Config) when is_atom(Key); is_binary(Key) ->
|
||||
object(
|
||||
[],
|
||||
{
|
||||
Handler,
|
||||
value(
|
||||
pre_encode(Value, Opts),
|
||||
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), Opts)}, State)},
|
||||
Opts
|
||||
pre_encode(Value, Config),
|
||||
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), Config)}, State)},
|
||||
Config
|
||||
)
|
||||
},
|
||||
Opts
|
||||
Config
|
||||
);
|
||||
object([], {Handler, State}, _Opts) -> Handler:handle_event(end_object, State);
|
||||
object(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
|
||||
object([], {Handler, State}, _Config) -> Handler:handle_event(end_object, State);
|
||||
object(Term, Handler, Config) -> ?error([Term, Handler, Config]).
|
||||
|
||||
|
||||
list([Value, Next|Rest], {Handler, State}, Opts) ->
|
||||
list([pre_encode(Next, Opts)|Rest], {Handler, value(Value, {Handler, State}, Opts)}, Opts);
|
||||
list([Value], {Handler, State}, Opts) ->
|
||||
list([], {Handler, value(Value, {Handler, State}, Opts)}, Opts);
|
||||
list([], {Handler, State}, _Opts) -> Handler:handle_event(end_array, State);
|
||||
list(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
|
||||
list([Value, Next|Rest], {Handler, State}, Config) ->
|
||||
list([pre_encode(Next, Config)|Rest], {Handler, value(Value, {Handler, State}, Config)}, Config);
|
||||
list([Value], {Handler, State}, Config) ->
|
||||
list([], {Handler, value(Value, {Handler, State}, Config)}, Config);
|
||||
list([], {Handler, State}, _Config) -> Handler:handle_event(end_array, State);
|
||||
list(Term, Handler, Config) -> ?error([Term, Handler, Config]).
|
||||
|
||||
pre_encode(Value, #opts{pre_encode=false}) -> Value; pre_encode(Value, Opts) ->
|
||||
(Opts#opts.pre_encode)(Value).
|
||||
pre_encode(Value, #config{pre_encode=false}) -> Value;
|
||||
pre_encode(Value, Config) -> (Config#config.pre_encode)(Value).
|
||||
|
||||
|
||||
fix_key(Key) when is_atom(Key) -> fix_key(atom_to_binary(Key, utf8));
|
||||
fix_key(Key) when is_binary(Key) -> Key.
|
||||
|
||||
|
||||
clean_string(Bin, Opts) -> jsx_utils:clean_string(Bin, Opts).
|
||||
clean_string(Bin, Config) -> jsx_utils:clean_string(Bin, Config).
|
||||
|
||||
|
||||
|
||||
|
@ -135,87 +135,20 @@ clean_string(Bin, Opts) -> jsx_utils:clean_string(Bin, Opts).
|
|||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
|
||||
encode(Term) -> encode(Term, []).
|
||||
|
||||
encode(Term, Opts) ->
|
||||
try (encoder(jsx, [], Opts))(Term)
|
||||
catch _:_ -> {error, badarg}
|
||||
end.
|
||||
|
||||
|
||||
encode_test_() ->
|
||||
Data = jsx:test_cases(),
|
||||
[
|
||||
{"naked string", ?_assertEqual(encode(<<"a string\n">>), [{string, <<"a string\n">>}, end_json])},
|
||||
{"escaped naked string", ?_assertEqual(encode(<<"a string\n">>, [escaped_strings]), [{string, <<"a string\\n">>}, end_json])},
|
||||
{"naked integer", ?_assertEqual(encode(123), [{integer, 123}, end_json])},
|
||||
{"naked float", ?_assertEqual(encode(1.23), [{float, 1.23}, end_json])},
|
||||
{"naked literal", ?_assertEqual(encode(null), [{literal, null}, end_json])},
|
||||
{"empty object", ?_assertEqual(encode([{}]), [start_object, end_object, end_json])},
|
||||
{"empty list", ?_assertEqual(encode([]), [start_array, end_array, end_json])},
|
||||
{"simple list", ?_assertEqual(
|
||||
encode([1,2,3,true,false]),
|
||||
[
|
||||
start_array,
|
||||
{integer, 1},
|
||||
{integer, 2},
|
||||
{integer, 3},
|
||||
{literal, true},
|
||||
{literal, false},
|
||||
end_array,
|
||||
end_json
|
||||
]
|
||||
{
|
||||
Title, ?_assertEqual(
|
||||
Events ++ [end_json],
|
||||
start(Term, {jsx, []}, #config{})
|
||||
)
|
||||
},
|
||||
{"simple object", ?_assertEqual(
|
||||
encode([{<<"a">>, true}, {<<"b">>, false}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"a">>},
|
||||
{literal, true},
|
||||
{key, <<"b">>},
|
||||
{literal, false},
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)
|
||||
},
|
||||
{"complex term", ?_assertEqual(
|
||||
encode([
|
||||
{<<"a">>, true},
|
||||
{<<"b">>, false},
|
||||
{<<"c">>, [1,2,3]},
|
||||
{<<"d">>, [{<<"key">>, <<"value">>}]}
|
||||
]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"a">>},
|
||||
{literal, true},
|
||||
{key, <<"b">>},
|
||||
{literal, false},
|
||||
{key, <<"c">>},
|
||||
start_array,
|
||||
{integer, 1},
|
||||
{integer, 2},
|
||||
{integer, 3},
|
||||
end_array,
|
||||
{key, <<"d">>},
|
||||
start_object,
|
||||
{key, <<"key">>},
|
||||
{string, <<"value">>},
|
||||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)
|
||||
},
|
||||
{"atom keys", ?_assertEqual(
|
||||
encode([{key, <<"value">>}]),
|
||||
[start_object, {key, <<"key">>}, {string, <<"value">>}, end_object, end_json]
|
||||
)
|
||||
}
|
||||
} || {Title, _, Term, Events} <- Data
|
||||
].
|
||||
|
||||
|
||||
encode(Term, Config) -> start(Term, {jsx, []}, jsx_utils:parse_config(Config)).
|
||||
|
||||
pre_encoders_test_() ->
|
||||
Term = [
|
||||
{<<"object">>, [
|
||||
|
@ -226,7 +159,6 @@ pre_encoders_test_() ->
|
|||
],
|
||||
[
|
||||
{"no pre encode", ?_assertEqual(
|
||||
encode(Term, []),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"object">>}, start_object,
|
||||
|
@ -242,10 +174,10 @@ pre_encoders_test_() ->
|
|||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
],
|
||||
encode(Term, [])
|
||||
)},
|
||||
{"replace lists with empty lists", ?_assertEqual(
|
||||
encode(Term, [{pre_encode, fun(V) -> case V of [{_,_}|_] -> V; [{}] -> V; V when is_list(V) -> []; _ -> V end end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"object">>}, start_object,
|
||||
|
@ -255,18 +187,18 @@ pre_encoders_test_() ->
|
|||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
],
|
||||
encode(Term, [{pre_encode, fun(V) -> case V of [{_,_}|_] -> V; [{}] -> V; V when is_list(V) -> []; _ -> V end end}])
|
||||
)},
|
||||
{"replace objects with empty objects", ?_assertEqual(
|
||||
encode(Term, [{pre_encode, fun(V) -> case V of [{_,_}|_] -> [{}]; _ -> V end end}]),
|
||||
[
|
||||
start_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
],
|
||||
encode(Term, [{pre_encode, fun(V) -> case V of [{_,_}|_] -> [{}]; _ -> V end end}])
|
||||
)},
|
||||
{"replace all non-list and non_tuple values with false", ?_assertEqual(
|
||||
encode(Term, [{pre_encode, fun(V) when is_list(V); is_tuple(V) -> V; (_) -> false end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"object">>}, start_object,
|
||||
|
@ -282,10 +214,10 @@ pre_encoders_test_() ->
|
|||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
],
|
||||
encode(Term, [{pre_encode, fun(V) when is_list(V); is_tuple(V) -> V; (_) -> false end}])
|
||||
)},
|
||||
{"replace all atoms with atom_to_list", ?_assertEqual(
|
||||
encode(Term, [{pre_encode, fun(V) when is_atom(V) -> unicode:characters_to_binary(atom_to_list(V)); (V) -> V end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"object">>}, start_object,
|
||||
|
@ -301,206 +233,46 @@ pre_encoders_test_() ->
|
|||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
],
|
||||
encode(Term, [{pre_encode, fun(V) when is_atom(V) -> unicode:characters_to_binary(atom_to_list(V)); (V) -> V end}])
|
||||
)},
|
||||
{"pre_encode tuple", ?_assertEqual(
|
||||
encode({1, 2, 3}, [{pre_encode, fun(Tuple) when is_tuple(Tuple) -> tuple_to_list(Tuple); (V) -> V end}]),
|
||||
[
|
||||
start_array,
|
||||
{integer, 1}, {integer, 2}, {integer, 3},
|
||||
end_array,
|
||||
end_json
|
||||
]
|
||||
],
|
||||
encode({1, 2, 3}, [{pre_encode, fun(Tuple) when is_tuple(Tuple) -> tuple_to_list(Tuple); (V) -> V end}])
|
||||
)},
|
||||
{"pre_encode 2-tuples", ?_assertEqual(
|
||||
encode([{two, 1}, {three, 2}], [{pre_encode, fun({K, V}) -> {K, V + 1}; (V) -> V end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"two">>}, {integer, 2}, {key, <<"three">>}, {integer, 3},
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
],
|
||||
encode([{two, 1}, {three, 2}], [{pre_encode, fun({K, V}) -> {K, V + 1}; (V) -> V end}])
|
||||
)},
|
||||
{"pre_encode one field record", ?_assertEqual(
|
||||
encode([{foo, bar}], [{pre_encode, fun({foo, V}) -> {V, undefined}; (undefined) -> false; (V) -> V end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"bar">>}, {literal, false},
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
],
|
||||
encode([{foo, bar}], [{pre_encode, fun({foo, V}) -> {V, undefined}; (undefined) -> false; (V) -> V end}])
|
||||
)},
|
||||
{"pre_encode list", ?_assertEqual(
|
||||
encode([1,2,3], [{pre_encode, fun(X) when is_integer(X) -> X + 1; (V) -> V end}]),
|
||||
[
|
||||
start_array,
|
||||
{integer, 2}, {integer, 3}, {integer, 4},
|
||||
end_array,
|
||||
end_json
|
||||
]
|
||||
],
|
||||
encode([1,2,3], [{pre_encode, fun(X) when is_integer(X) -> X + 1; (V) -> V end}])
|
||||
)}
|
||||
].
|
||||
|
||||
|
||||
escapes_test_() ->
|
||||
[
|
||||
{"backspace escape", ?_assertEqual(encode(<<"\b">>, [escaped_strings]), [{string, <<"\\b">>}, end_json])},
|
||||
{"formfeed escape", ?_assertEqual(encode(<<"\f">>, [escaped_strings]), [{string, <<"\\f">>}, end_json])},
|
||||
{"newline escape", ?_assertEqual(encode(<<"\n">>, [escaped_strings]), [{string, <<"\\n">>}, end_json])},
|
||||
{"carriage return escape", ?_assertEqual(encode(<<"\r">>, [escaped_strings]), [{string, <<"\\r">>}, end_json])},
|
||||
{"tab escape", ?_assertEqual(encode(<<"\t">>, [escaped_strings]), [{string, <<"\\t">>}, end_json])},
|
||||
{"quote escape", ?_assertEqual(encode(<<"\"">>, [escaped_strings]), [{string, <<"\\\"">>}, end_json])},
|
||||
{"single quote escape", ?_assertEqual(encode(<<"'">>, [escaped_strings, single_quoted_strings]), [{string, <<"\\'">>}, end_json])},
|
||||
{"no single quote escape", ?_assertEqual(encode(<<"'">>, [escaped_strings]), [{string, <<"'">>}, end_json])},
|
||||
{"forward slash escape", ?_assertEqual(encode(<<"/">>, [escaped_strings, escaped_forward_slashes]), [{string, <<"\\/">>}, end_json])},
|
||||
{"no forward slash escape", ?_assertEqual(encode(<<"/">>, [escaped_strings]), [{string, <<"/">>}, end_json])},
|
||||
{"back slash escape", ?_assertEqual(encode(<<"\\">>, [escaped_strings]), [{string, <<"\\\\">>}, end_json])},
|
||||
{"jsonp escape", ?_assertEqual(
|
||||
encode(<<16#2028/utf8, 16#2029/utf8>>, [escaped_strings]),
|
||||
[{string, <<"\\u2028\\u2029">>}, end_json]
|
||||
)},
|
||||
{"no jsonp escape", ?_assertEqual(
|
||||
encode(<<16#2028/utf8, 16#2029/utf8>>, [escaped_strings, unescaped_jsonp]),
|
||||
[{string, <<16#2028/utf8, 16#2029/utf8>>}, end_json]
|
||||
)},
|
||||
{"control escape", ?_assertEqual(encode(<<0>>, [escaped_strings]), [{string, <<"\\u0000">>}, end_json])},
|
||||
{"dirty strings", ?_assertEqual(encode(<<"\n">>, [escaped_strings, dirty_strings]), [{string, <<"\n">>}, end_json])},
|
||||
{"ignore bad escapes", ?_assertEqual(encode(<<"\\x25">>, [escaped_strings, ignored_bad_escapes]), [{string, <<"\\\\x25">>}, end_json])}
|
||||
].
|
||||
|
||||
|
||||
surrogates_test_() ->
|
||||
[
|
||||
{"surrogates - badarg",
|
||||
?_assert(check_bad(surrogates()))
|
||||
},
|
||||
{"surrogates - replaced",
|
||||
?_assert(check_replaced(surrogates()))
|
||||
}
|
||||
].
|
||||
|
||||
|
||||
good_characters_test_() ->
|
||||
[
|
||||
{"acceptable codepoints",
|
||||
?_assert(check_good(good()))
|
||||
},
|
||||
{"acceptable codepoints - escaped_strings",
|
||||
?_assert(check_good(good(), [escaped_strings]))
|
||||
},
|
||||
{"acceptable codepoints - replaced_bad_utf8",
|
||||
?_assert(check_good(good(), [escaped_strings]))
|
||||
},
|
||||
{"acceptable codepoints - escaped_strings + replaced_bad_utf8",
|
||||
?_assert(check_good(good(), [escaped_strings, replaced_bad_utf8]))
|
||||
},
|
||||
{"acceptable extended",
|
||||
?_assert(check_good(good_extended()))
|
||||
},
|
||||
{"acceptable extended - escaped_strings",
|
||||
?_assert(check_good(good_extended(), [escaped_strings]))
|
||||
},
|
||||
{"acceptable extended - escaped_strings",
|
||||
?_assert(check_good(good_extended(), [replaced_bad_utf8]))
|
||||
}
|
||||
].
|
||||
|
||||
|
||||
reserved_test_() ->
|
||||
[
|
||||
{"reserved noncharacters - badarg",
|
||||
?_assert(check_bad(reserved_space()))
|
||||
},
|
||||
{"reserved noncharacters - replaced",
|
||||
?_assert(check_replaced(reserved_space()))
|
||||
}
|
||||
].
|
||||
|
||||
|
||||
noncharacters_test_() ->
|
||||
[
|
||||
{"noncharacters - badarg",
|
||||
?_assert(check_bad(noncharacters()))
|
||||
},
|
||||
{"noncharacters - replaced",
|
||||
?_assert(check_replaced(noncharacters()))
|
||||
}
|
||||
].
|
||||
|
||||
|
||||
extended_noncharacters_test_() ->
|
||||
[
|
||||
{"extended noncharacters - badarg",
|
||||
?_assert(check_bad(extended_noncharacters()))
|
||||
},
|
||||
{"extended noncharacters - replaced",
|
||||
?_assert(check_replaced(extended_noncharacters()))
|
||||
}
|
||||
].
|
||||
|
||||
|
||||
check_bad(List) ->
|
||||
[] == lists:dropwhile(fun({_, {error, badarg}}) -> true ; (_) -> false end,
|
||||
check(List, [], [])
|
||||
).
|
||||
|
||||
|
||||
check_replaced(List) ->
|
||||
[] == lists:dropwhile(fun({_, [{string, <<16#fffd/utf8>>}|_]}) -> true ; (_) -> false
|
||||
end,
|
||||
check(List, [replaced_bad_utf8], [])
|
||||
).
|
||||
|
||||
|
||||
check_good(List) -> check_good(List, []).
|
||||
|
||||
check_good(List, Opts) ->
|
||||
[] == lists:dropwhile(fun({_, [{string, _}|_]}) -> true ; (_) -> false end,
|
||||
check(List, Opts, [])
|
||||
).
|
||||
|
||||
|
||||
check([], _Opts, Acc) -> Acc;
|
||||
check([H|T], Opts, Acc) ->
|
||||
R = encode(to_fake_utf(H, utf8), Opts),
|
||||
check(T, Opts, [{H, R}] ++ Acc).
|
||||
|
||||
|
||||
noncharacters() -> lists:seq(16#fffe, 16#ffff).
|
||||
|
||||
extended_noncharacters() ->
|
||||
[16#1fffe, 16#1ffff, 16#2fffe, 16#2ffff]
|
||||
++ [16#3fffe, 16#3ffff, 16#4fffe, 16#4ffff]
|
||||
++ [16#5fffe, 16#5ffff, 16#6fffe, 16#6ffff]
|
||||
++ [16#7fffe, 16#7ffff, 16#8fffe, 16#8ffff]
|
||||
++ [16#9fffe, 16#9ffff, 16#afffe, 16#affff]
|
||||
++ [16#bfffe, 16#bffff, 16#cfffe, 16#cffff]
|
||||
++ [16#dfffe, 16#dffff, 16#efffe, 16#effff]
|
||||
++ [16#ffffe, 16#fffff, 16#10fffe, 16#10ffff].
|
||||
|
||||
surrogates() -> lists:seq(16#d800, 16#dfff).
|
||||
|
||||
reserved_space() -> lists:seq(16#fdd0, 16#fdef).
|
||||
|
||||
good() -> lists:seq(16#0000, 16#d7ff) ++ lists:seq(16#e000, 16#fdcf) ++ lists:seq(16#fdf0, 16#fffd).
|
||||
|
||||
good_extended() -> [16#10000, 16#20000, 16#30000, 16#40000, 16#50000,
|
||||
16#60000, 16#70000, 16#80000, 16#90000, 16#a0000,
|
||||
16#b0000, 16#c0000, 16#d0000, 16#e0000, 16#f0000
|
||||
] ++ lists:seq(16#100000, 16#10fffd).
|
||||
|
||||
|
||||
%% erlang refuses to encode certain codepoints, so fake them all
|
||||
to_fake_utf(N, utf8) when N < 16#0080 -> <<N:8>>;
|
||||
to_fake_utf(N, utf8) when N < 16#0800 ->
|
||||
<<0:5, Y:5, X:6>> = <<N:16>>,
|
||||
<<2#110:3, Y:5, 2#10:2, X:6>>;
|
||||
to_fake_utf(N, utf8) when N < 16#10000 ->
|
||||
<<Z:4, Y:6, X:6>> = <<N:16>>,
|
||||
<<2#1110:4, Z:4, 2#10:2, Y:6, 2#10:2, X:6>>;
|
||||
to_fake_utf(N, utf8) ->
|
||||
<<0:3, W:3, Z:6, Y:6, X:6>> = <<N:24>>,
|
||||
<<2#11110:5, W:3, 2#10:2, Z:6, 2#10:2, Y:6, 2#10:2, X:6>>.
|
||||
|
||||
|
||||
-endif.
|
Loading…
Add table
Add a link
Reference in a new issue