2010-09-20 19:11:31 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
|
|
|
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
|
|
|
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
|
|
|
|
|
2011-03-22 18:53:02 -07:00
|
|
|
-module(jsx_utils).
|
2010-09-20 19:11:31 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-export([parse_config/1]).
|
|
|
|
-export([extract_config/1, valid_flags/0]).
|
2012-03-31 20:31:28 -07:00
|
|
|
-export([json_escape_sequence/1]).
|
2013-02-05 00:03:58 -08:00
|
|
|
-export([clean_string/2]).
|
2011-09-14 06:44:52 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-include("jsx_config.hrl").
|
2010-09-20 19:11:31 -07:00
|
|
|
|
2010-09-22 23:22:55 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
%% parsing of jsx config
|
|
|
|
parse_config(Config) ->
|
|
|
|
parse_config(Config, #config{}).
|
2011-10-19 06:51:36 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([], Config) ->
|
|
|
|
Config;
|
|
|
|
parse_config([replaced_bad_utf8|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{replaced_bad_utf8=true});
|
|
|
|
parse_config([escaped_forward_slashes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{escaped_forward_slashes=true});
|
|
|
|
parse_config([explicit_end|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{explicit_end=true});
|
|
|
|
parse_config([single_quoted_strings|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{single_quoted_strings=true});
|
|
|
|
parse_config([unescaped_jsonp|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{unescaped_jsonp=true});
|
|
|
|
parse_config([comments|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{comments=true});
|
|
|
|
parse_config([escaped_strings|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{escaped_strings=true});
|
|
|
|
parse_config([dirty_strings|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{dirty_strings=true});
|
|
|
|
parse_config([ignored_bad_escapes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{ignored_bad_escapes=true});
|
|
|
|
parse_config([relax|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{
|
2012-03-31 21:58:18 -07:00
|
|
|
replaced_bad_utf8 = true,
|
|
|
|
single_quoted_strings = true,
|
2012-03-29 00:24:31 -07:00
|
|
|
comments = true,
|
2012-03-31 21:58:18 -07:00
|
|
|
ignored_bad_escapes = true
|
2012-03-29 00:24:31 -07:00
|
|
|
});
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([{pre_encode, Encoder}|Rest] = Options, Config) when is_function(Encoder, 1) ->
|
|
|
|
case Config#config.pre_encode of
|
|
|
|
false -> parse_config(Rest, Config#config{pre_encode=Encoder})
|
|
|
|
; _ -> erlang:error(badarg, [Options, Config])
|
2012-04-06 08:09:52 -07:00
|
|
|
end;
|
2012-03-31 21:58:18 -07:00
|
|
|
%% deprecated flags
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([{pre_encoder, Encoder}|Rest] = Options, Config) when is_function(Encoder, 1) ->
|
|
|
|
case Config#config.pre_encode of
|
|
|
|
false -> parse_config(Rest, Config#config{pre_encode=Encoder})
|
|
|
|
; _ -> erlang:error(badarg, [Options, Config])
|
2012-04-06 08:09:52 -07:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([loose_unicode|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{replaced_bad_utf8=true});
|
|
|
|
parse_config([escape_forward_slash|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{escaped_forward_slashes=true});
|
|
|
|
parse_config([single_quotes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{single_quoted_strings=true});
|
|
|
|
parse_config([no_jsonp_escapes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{unescaped_jsonp=true});
|
|
|
|
parse_config([json_escape|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{escaped_strings=true});
|
|
|
|
parse_config([ignore_bad_escapes|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{ignored_bad_escapes=true});
|
|
|
|
parse_config(Options, Config) ->
|
|
|
|
erlang:error(badarg, [Options, Config]).
|
2011-10-19 06:51:36 -07:00
|
|
|
|
|
|
|
|
2012-03-15 23:06:19 -07:00
|
|
|
valid_flags() ->
|
|
|
|
[
|
2012-03-31 21:58:18 -07:00
|
|
|
replaced_bad_utf8,
|
|
|
|
escaped_forward_slashes,
|
|
|
|
single_quoted_strings,
|
|
|
|
unescaped_jsonp,
|
2012-03-24 19:42:00 -07:00
|
|
|
comments,
|
2012-03-31 21:58:18 -07:00
|
|
|
escaped_strings,
|
2012-03-29 00:18:53 -07:00
|
|
|
dirty_strings,
|
2012-03-31 21:58:18 -07:00
|
|
|
ignored_bad_escapes,
|
|
|
|
explicit_end,
|
|
|
|
relax,
|
2012-04-06 08:09:52 -07:00
|
|
|
pre_encode,
|
2012-03-31 21:58:18 -07:00
|
|
|
%% deprecated flags
|
2012-04-06 08:09:52 -07:00
|
|
|
pre_encoder, %% pre_encode
|
2012-03-31 21:58:18 -07:00
|
|
|
loose_unicode, %% replaced_bad_utf8
|
|
|
|
escape_forward_slash, %% escaped_forward_slashes
|
2013-02-05 20:25:41 -08:00
|
|
|
single_quotes, %% single_quoted_strings
|
2012-03-31 21:58:18 -07:00
|
|
|
no_jsonp_escapes, %% unescaped_jsonp
|
|
|
|
json_escape, %% escaped_strings
|
|
|
|
ignore_bad_escapes %% ignored_bad_escapes
|
2012-03-15 23:06:19 -07:00
|
|
|
].
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2012-03-15 23:06:19 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
extract_config(Config) ->
|
|
|
|
extract_parser_config(Config, []).
|
2012-03-04 18:40:00 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
extract_parser_config([], Acc) -> Acc;
|
|
|
|
extract_parser_config([{K,V}|Rest], Acc) ->
|
2012-03-15 23:06:19 -07:00
|
|
|
case lists:member(K, valid_flags()) of
|
2013-02-12 11:54:42 -08:00
|
|
|
true -> extract_parser_config(Rest, [{K,V}] ++ Acc)
|
|
|
|
; false -> extract_parser_config(Rest, Acc)
|
2012-03-04 18:40:00 -08:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
extract_parser_config([K|Rest], Acc) ->
|
2012-03-15 23:06:19 -07:00
|
|
|
case lists:member(K, valid_flags()) of
|
2013-02-12 11:54:42 -08:00
|
|
|
true -> extract_parser_config(Rest, [K] ++ Acc)
|
|
|
|
; false -> extract_parser_config(Rest, Acc)
|
2012-03-04 18:40:00 -08:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-09-14 06:44:52 -07:00
|
|
|
%% convert a codepoint to it's \uXXXX equiv.
|
|
|
|
json_escape_sequence(X) ->
|
|
|
|
<<A:4, B:4, C:4, D:4>> = <<X:16>>,
|
2012-03-31 01:24:06 -07:00
|
|
|
[$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))].
|
2011-09-14 06:44:52 -07:00
|
|
|
|
|
|
|
|
|
|
|
to_hex(10) -> $a;
|
|
|
|
to_hex(11) -> $b;
|
|
|
|
to_hex(12) -> $c;
|
|
|
|
to_hex(13) -> $d;
|
|
|
|
to_hex(14) -> $e;
|
|
|
|
to_hex(15) -> $f;
|
|
|
|
to_hex(X) -> X + 48. %% ascii "1" is [49], "2" is [50], etc...
|
2010-09-20 19:11:31 -07:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(Bin, #config{dirty_strings=true}) -> Bin;
|
|
|
|
clean_string(Bin, Config) ->
|
|
|
|
case Config#config.replaced_bad_utf8 orelse Config#config.escaped_strings of
|
|
|
|
true -> clean(Bin, [], Config)
|
2013-02-05 00:03:58 -08:00
|
|
|
; false -> ensure_clean(Bin), Bin
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
%% fast path for no escaping and no correcting, throws error if string is 'bad'
|
|
|
|
ensure_clean(<<>>) -> ok;
|
|
|
|
ensure_clean(<<0, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<1, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<2, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<3, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<4, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<5, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<6, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<7, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<8, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<9, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<10, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<11, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<12, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<13, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<14, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<15, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<16, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<17, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<18, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<19, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<20, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<21, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<22, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<23, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<24, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<25, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<26, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<27, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<28, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<29, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<30, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<31, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<32, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<33, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<34, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<35, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<36, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<37, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<38, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<39, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<40, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<41, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<42, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<43, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<44, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<45, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<46, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<47, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<48, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<49, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<50, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<51, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<52, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<53, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<54, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<55, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<56, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<57, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<58, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<59, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<60, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<61, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<62, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<63, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<64, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<65, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<66, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<67, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<68, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<69, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<70, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<71, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<72, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<73, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<74, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<75, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<76, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<77, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<78, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<79, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<80, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<81, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<82, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<83, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<84, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<85, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<86, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<87, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<88, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<89, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<90, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<91, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<92, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<93, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<94, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<95, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<96, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<97, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<98, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<99, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<100, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<101, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<102, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<103, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<104, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<105, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<106, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<107, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<108, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<109, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<110, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<111, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<112, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<113, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<114, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<115, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<116, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<117, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<118, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<119, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<120, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<121, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<122, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<123, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<124, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<125, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<126, Rest/binary>>) -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<127, Rest/binary>>) -> ensure_clean(Rest);
|
2013-02-05 16:40:15 -08:00
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X < 16#d800 -> ensure_clean(Rest);
|
2013-02-05 00:03:58 -08:00
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X > 16#dfff, X < 16#fdd0 -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X > 16#fdef, X < 16#fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#10000, X < 16#1fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#20000, X < 16#2fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#30000, X < 16#3fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#40000, X < 16#4fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#50000, X < 16#5fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#60000, X < 16#6fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#70000, X < 16#7fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#80000, X < 16#8fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#90000, X < 16#9fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#a0000, X < 16#afffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#b0000, X < 16#bfffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#c0000, X < 16#cfffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#d0000, X < 16#dfffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#e0000, X < 16#efffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#f0000, X < 16#ffffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(<<X/utf8, Rest/binary>>) when X >= 16#100000, X < 16#10fffe -> ensure_clean(Rest);
|
|
|
|
ensure_clean(Bin) -> erlang:error(badarg, [Bin]).
|
|
|
|
|
|
|
|
|
|
|
|
%% escape and/or replace bad codepoints if requested
|
2013-02-12 11:54:42 -08:00
|
|
|
clean(<<>>, Acc, _Config) -> unicode:characters_to_binary(lists:reverse(Acc));
|
|
|
|
clean(<<0, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(0, Config) ++ Acc, Config);
|
|
|
|
clean(<<1, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(1, Config) ++ Acc, Config);
|
|
|
|
clean(<<2, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(2, Config) ++ Acc, Config);
|
|
|
|
clean(<<3, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(3, Config) ++ Acc, Config);
|
|
|
|
clean(<<4, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(4, Config) ++ Acc, Config);
|
|
|
|
clean(<<5, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(5, Config) ++ Acc, Config);
|
|
|
|
clean(<<6, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(6, Config) ++ Acc, Config);
|
|
|
|
clean(<<7, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(7, Config) ++ Acc, Config);
|
|
|
|
clean(<<8, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(8, Config) ++ Acc, Config);
|
|
|
|
clean(<<9, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(9, Config) ++ Acc, Config);
|
|
|
|
clean(<<10, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(10, Config) ++ Acc, Config);
|
|
|
|
clean(<<11, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(11, Config) ++ Acc, Config);
|
|
|
|
clean(<<12, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(12, Config) ++ Acc, Config);
|
|
|
|
clean(<<13, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(13, Config) ++ Acc, Config);
|
|
|
|
clean(<<14, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(14, Config) ++ Acc, Config);
|
|
|
|
clean(<<15, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(15, Config) ++ Acc, Config);
|
|
|
|
clean(<<16, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(16, Config) ++ Acc, Config);
|
|
|
|
clean(<<17, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(17, Config) ++ Acc, Config);
|
|
|
|
clean(<<18, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(18, Config) ++ Acc, Config);
|
|
|
|
clean(<<19, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(19, Config) ++ Acc, Config);
|
|
|
|
clean(<<20, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(20, Config) ++ Acc, Config);
|
|
|
|
clean(<<21, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(21, Config) ++ Acc, Config);
|
|
|
|
clean(<<22, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(22, Config) ++ Acc, Config);
|
|
|
|
clean(<<23, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(23, Config) ++ Acc, Config);
|
|
|
|
clean(<<24, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(24, Config) ++ Acc, Config);
|
|
|
|
clean(<<25, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(25, Config) ++ Acc, Config);
|
|
|
|
clean(<<26, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(26, Config) ++ Acc, Config);
|
|
|
|
clean(<<27, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(27, Config) ++ Acc, Config);
|
|
|
|
clean(<<28, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(28, Config) ++ Acc, Config);
|
|
|
|
clean(<<29, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(29, Config) ++ Acc, Config);
|
|
|
|
clean(<<30, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(30, Config) ++ Acc, Config);
|
|
|
|
clean(<<31, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(31, Config) ++ Acc, Config);
|
|
|
|
clean(<<32, Rest/binary>>, Acc, Config) -> clean(Rest, [32] ++ Acc, Config);
|
|
|
|
clean(<<33, Rest/binary>>, Acc, Config) -> clean(Rest, [33] ++ Acc, Config);
|
|
|
|
clean(<<34, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(34, Config) ++ Acc, Config);
|
|
|
|
clean(<<35, Rest/binary>>, Acc, Config) -> clean(Rest, [35] ++ Acc, Config);
|
|
|
|
clean(<<36, Rest/binary>>, Acc, Config) -> clean(Rest, [36] ++ Acc, Config);
|
|
|
|
clean(<<37, Rest/binary>>, Acc, Config) -> clean(Rest, [37] ++ Acc, Config);
|
|
|
|
clean(<<38, Rest/binary>>, Acc, Config) -> clean(Rest, [38] ++ Acc, Config);
|
|
|
|
clean(<<39, Rest/binary>>, Acc, Config) -> clean(Rest, [39] ++ Acc, Config);
|
|
|
|
clean(<<40, Rest/binary>>, Acc, Config) -> clean(Rest, [40] ++ Acc, Config);
|
|
|
|
clean(<<41, Rest/binary>>, Acc, Config) -> clean(Rest, [41] ++ Acc, Config);
|
|
|
|
clean(<<42, Rest/binary>>, Acc, Config) -> clean(Rest, [42] ++ Acc, Config);
|
|
|
|
clean(<<43, Rest/binary>>, Acc, Config) -> clean(Rest, [43] ++ Acc, Config);
|
|
|
|
clean(<<44, Rest/binary>>, Acc, Config) -> clean(Rest, [44] ++ Acc, Config);
|
|
|
|
clean(<<45, Rest/binary>>, Acc, Config) -> clean(Rest, [45] ++ Acc, Config);
|
|
|
|
clean(<<46, Rest/binary>>, Acc, Config) -> clean(Rest, [46] ++ Acc, Config);
|
|
|
|
clean(<<47, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(47, Config) ++ Acc, Config);
|
|
|
|
clean(<<48, Rest/binary>>, Acc, Config) -> clean(Rest, [48] ++ Acc, Config);
|
|
|
|
clean(<<49, Rest/binary>>, Acc, Config) -> clean(Rest, [49] ++ Acc, Config);
|
|
|
|
clean(<<50, Rest/binary>>, Acc, Config) -> clean(Rest, [50] ++ Acc, Config);
|
|
|
|
clean(<<51, Rest/binary>>, Acc, Config) -> clean(Rest, [51] ++ Acc, Config);
|
|
|
|
clean(<<52, Rest/binary>>, Acc, Config) -> clean(Rest, [52] ++ Acc, Config);
|
|
|
|
clean(<<53, Rest/binary>>, Acc, Config) -> clean(Rest, [53] ++ Acc, Config);
|
|
|
|
clean(<<54, Rest/binary>>, Acc, Config) -> clean(Rest, [54] ++ Acc, Config);
|
|
|
|
clean(<<55, Rest/binary>>, Acc, Config) -> clean(Rest, [55] ++ Acc, Config);
|
|
|
|
clean(<<56, Rest/binary>>, Acc, Config) -> clean(Rest, [56] ++ Acc, Config);
|
|
|
|
clean(<<57, Rest/binary>>, Acc, Config) -> clean(Rest, [57] ++ Acc, Config);
|
|
|
|
clean(<<58, Rest/binary>>, Acc, Config) -> clean(Rest, [58] ++ Acc, Config);
|
|
|
|
clean(<<59, Rest/binary>>, Acc, Config) -> clean(Rest, [59] ++ Acc, Config);
|
|
|
|
clean(<<60, Rest/binary>>, Acc, Config) -> clean(Rest, [60] ++ Acc, Config);
|
|
|
|
clean(<<61, Rest/binary>>, Acc, Config) -> clean(Rest, [61] ++ Acc, Config);
|
|
|
|
clean(<<62, Rest/binary>>, Acc, Config) -> clean(Rest, [62] ++ Acc, Config);
|
|
|
|
clean(<<63, Rest/binary>>, Acc, Config) -> clean(Rest, [63] ++ Acc, Config);
|
|
|
|
clean(<<64, Rest/binary>>, Acc, Config) -> clean(Rest, [64] ++ Acc, Config);
|
|
|
|
clean(<<65, Rest/binary>>, Acc, Config) -> clean(Rest, [65] ++ Acc, Config);
|
|
|
|
clean(<<66, Rest/binary>>, Acc, Config) -> clean(Rest, [66] ++ Acc, Config);
|
|
|
|
clean(<<67, Rest/binary>>, Acc, Config) -> clean(Rest, [67] ++ Acc, Config);
|
|
|
|
clean(<<68, Rest/binary>>, Acc, Config) -> clean(Rest, [68] ++ Acc, Config);
|
|
|
|
clean(<<69, Rest/binary>>, Acc, Config) -> clean(Rest, [69] ++ Acc, Config);
|
|
|
|
clean(<<70, Rest/binary>>, Acc, Config) -> clean(Rest, [70] ++ Acc, Config);
|
|
|
|
clean(<<71, Rest/binary>>, Acc, Config) -> clean(Rest, [71] ++ Acc, Config);
|
|
|
|
clean(<<72, Rest/binary>>, Acc, Config) -> clean(Rest, [72] ++ Acc, Config);
|
|
|
|
clean(<<73, Rest/binary>>, Acc, Config) -> clean(Rest, [73] ++ Acc, Config);
|
|
|
|
clean(<<74, Rest/binary>>, Acc, Config) -> clean(Rest, [74] ++ Acc, Config);
|
|
|
|
clean(<<75, Rest/binary>>, Acc, Config) -> clean(Rest, [75] ++ Acc, Config);
|
|
|
|
clean(<<76, Rest/binary>>, Acc, Config) -> clean(Rest, [76] ++ Acc, Config);
|
|
|
|
clean(<<77, Rest/binary>>, Acc, Config) -> clean(Rest, [77] ++ Acc, Config);
|
|
|
|
clean(<<78, Rest/binary>>, Acc, Config) -> clean(Rest, [78] ++ Acc, Config);
|
|
|
|
clean(<<79, Rest/binary>>, Acc, Config) -> clean(Rest, [79] ++ Acc, Config);
|
|
|
|
clean(<<80, Rest/binary>>, Acc, Config) -> clean(Rest, [80] ++ Acc, Config);
|
|
|
|
clean(<<81, Rest/binary>>, Acc, Config) -> clean(Rest, [81] ++ Acc, Config);
|
|
|
|
clean(<<82, Rest/binary>>, Acc, Config) -> clean(Rest, [82] ++ Acc, Config);
|
|
|
|
clean(<<83, Rest/binary>>, Acc, Config) -> clean(Rest, [83] ++ Acc, Config);
|
|
|
|
clean(<<84, Rest/binary>>, Acc, Config) -> clean(Rest, [84] ++ Acc, Config);
|
|
|
|
clean(<<85, Rest/binary>>, Acc, Config) -> clean(Rest, [85] ++ Acc, Config);
|
|
|
|
clean(<<86, Rest/binary>>, Acc, Config) -> clean(Rest, [86] ++ Acc, Config);
|
|
|
|
clean(<<87, Rest/binary>>, Acc, Config) -> clean(Rest, [87] ++ Acc, Config);
|
|
|
|
clean(<<88, Rest/binary>>, Acc, Config) -> clean(Rest, [88] ++ Acc, Config);
|
|
|
|
clean(<<89, Rest/binary>>, Acc, Config) -> clean(Rest, [89] ++ Acc, Config);
|
|
|
|
clean(<<90, Rest/binary>>, Acc, Config) -> clean(Rest, [90] ++ Acc, Config);
|
|
|
|
clean(<<91, Rest/binary>>, Acc, Config) -> clean(Rest, [91] ++ Acc, Config);
|
|
|
|
clean(<<92, Rest/binary>>, Acc, Config) -> clean(Rest, maybe_replace(92, Config) ++ Acc, Config);
|
|
|
|
clean(<<93, Rest/binary>>, Acc, Config) -> clean(Rest, [93] ++ Acc, Config);
|
|
|
|
clean(<<94, Rest/binary>>, Acc, Config) -> clean(Rest, [94] ++ Acc, Config);
|
|
|
|
clean(<<95, Rest/binary>>, Acc, Config) -> clean(Rest, [95] ++ Acc, Config);
|
|
|
|
clean(<<96, Rest/binary>>, Acc, Config) -> clean(Rest, [96] ++ Acc, Config);
|
|
|
|
clean(<<97, Rest/binary>>, Acc, Config) -> clean(Rest, [97] ++ Acc, Config);
|
|
|
|
clean(<<98, Rest/binary>>, Acc, Config) -> clean(Rest, [98] ++ Acc, Config);
|
|
|
|
clean(<<99, Rest/binary>>, Acc, Config) -> clean(Rest, [99] ++ Acc, Config);
|
|
|
|
clean(<<100, Rest/binary>>, Acc, Config) -> clean(Rest, [100] ++ Acc, Config);
|
|
|
|
clean(<<101, Rest/binary>>, Acc, Config) -> clean(Rest, [101] ++ Acc, Config);
|
|
|
|
clean(<<102, Rest/binary>>, Acc, Config) -> clean(Rest, [102] ++ Acc, Config);
|
|
|
|
clean(<<103, Rest/binary>>, Acc, Config) -> clean(Rest, [103] ++ Acc, Config);
|
|
|
|
clean(<<104, Rest/binary>>, Acc, Config) -> clean(Rest, [104] ++ Acc, Config);
|
|
|
|
clean(<<105, Rest/binary>>, Acc, Config) -> clean(Rest, [105] ++ Acc, Config);
|
|
|
|
clean(<<106, Rest/binary>>, Acc, Config) -> clean(Rest, [106] ++ Acc, Config);
|
|
|
|
clean(<<107, Rest/binary>>, Acc, Config) -> clean(Rest, [107] ++ Acc, Config);
|
|
|
|
clean(<<108, Rest/binary>>, Acc, Config) -> clean(Rest, [108] ++ Acc, Config);
|
|
|
|
clean(<<109, Rest/binary>>, Acc, Config) -> clean(Rest, [109] ++ Acc, Config);
|
|
|
|
clean(<<110, Rest/binary>>, Acc, Config) -> clean(Rest, [110] ++ Acc, Config);
|
|
|
|
clean(<<111, Rest/binary>>, Acc, Config) -> clean(Rest, [111] ++ Acc, Config);
|
|
|
|
clean(<<112, Rest/binary>>, Acc, Config) -> clean(Rest, [112] ++ Acc, Config);
|
|
|
|
clean(<<113, Rest/binary>>, Acc, Config) -> clean(Rest, [113] ++ Acc, Config);
|
|
|
|
clean(<<114, Rest/binary>>, Acc, Config) -> clean(Rest, [114] ++ Acc, Config);
|
|
|
|
clean(<<115, Rest/binary>>, Acc, Config) -> clean(Rest, [115] ++ Acc, Config);
|
|
|
|
clean(<<116, Rest/binary>>, Acc, Config) -> clean(Rest, [116] ++ Acc, Config);
|
|
|
|
clean(<<117, Rest/binary>>, Acc, Config) -> clean(Rest, [117] ++ Acc, Config);
|
|
|
|
clean(<<118, Rest/binary>>, Acc, Config) -> clean(Rest, [118] ++ Acc, Config);
|
|
|
|
clean(<<119, Rest/binary>>, Acc, Config) -> clean(Rest, [119] ++ Acc, Config);
|
|
|
|
clean(<<120, Rest/binary>>, Acc, Config) -> clean(Rest, [120] ++ Acc, Config);
|
|
|
|
clean(<<121, Rest/binary>>, Acc, Config) -> clean(Rest, [121] ++ Acc, Config);
|
|
|
|
clean(<<122, Rest/binary>>, Acc, Config) -> clean(Rest, [122] ++ Acc, Config);
|
|
|
|
clean(<<123, Rest/binary>>, Acc, Config) -> clean(Rest, [123] ++ Acc, Config);
|
|
|
|
clean(<<124, Rest/binary>>, Acc, Config) -> clean(Rest, [124] ++ Acc, Config);
|
|
|
|
clean(<<125, Rest/binary>>, Acc, Config) -> clean(Rest, [125] ++ Acc, Config);
|
|
|
|
clean(<<126, Rest/binary>>, Acc, Config) -> clean(Rest, [126] ++ Acc, Config);
|
|
|
|
clean(<<127, Rest/binary>>, Acc, Config) -> clean(Rest, [127] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X < 16#800 ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X == 16#2028; X == 16#2029 ->
|
|
|
|
clean(Rest, maybe_replace(X, Config) ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X < 16#d800 ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X > 16#dfff, X < 16#fdd0 ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X > 16#fdef, X < 16#fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#10000, X < 16#1fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#20000, X < 16#2fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#30000, X < 16#3fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#40000, X < 16#4fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#50000, X < 16#5fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#60000, X < 16#6fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#70000, X < 16#7fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#80000, X < 16#8fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#90000, X < 16#9fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#a0000, X < 16#afffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#b0000, X < 16#bfffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#c0000, X < 16#cfffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#d0000, X < 16#dfffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#e0000, X < 16#efffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#f0000, X < 16#ffffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
|
|
|
clean(<<X/utf8, Rest/binary>>, Acc, Config) when X >= 16#100000, X < 16#10fffe ->
|
|
|
|
clean(Rest, [X] ++ Acc, Config);
|
2013-02-05 00:03:58 -08:00
|
|
|
%% noncharacters
|
2013-02-12 11:54:42 -08:00
|
|
|
clean(<<_/utf8, Rest/binary>>, Acc, Config) ->
|
|
|
|
clean(Rest, maybe_replace(noncharacter, Config) ++ Acc, Config);
|
2013-02-05 00:03:58 -08:00
|
|
|
%% surrogates
|
2013-02-12 11:54:42 -08:00
|
|
|
clean(<<237, X, _, Rest/binary>>, Acc, Config) when X >= 160 ->
|
|
|
|
clean(Rest, maybe_replace(surrogate, Config) ++ Acc, Config);
|
2013-02-05 00:03:58 -08:00
|
|
|
%% u+fffe and u+ffff for R14BXX
|
2013-02-12 11:54:42 -08:00
|
|
|
clean(<<239, 191, X, Rest/binary>>, Acc, Config) when X == 190; X == 191 ->
|
|
|
|
clean(Rest, maybe_replace(noncharacter, Config) ++ Acc, Config);
|
2013-02-05 00:03:58 -08:00
|
|
|
%% overlong encodings and missing continuations of a 2 byte sequence
|
2013-02-12 11:54:42 -08:00
|
|
|
clean(<<X, Rest/binary>>, Acc, Config) when X >= 192, X =< 223 ->
|
|
|
|
clean(strip_continuations(Rest, 1), maybe_replace(badutf, Config) ++ Acc, Config);
|
2013-02-05 00:03:58 -08:00
|
|
|
%% overlong encodings and missing continuations of a 3 byte sequence
|
2013-02-12 11:54:42 -08:00
|
|
|
clean(<<X, Rest/binary>>, Acc, Config) when X >= 224, X =< 239 ->
|
|
|
|
clean(strip_continuations(Rest, 2), maybe_replace(badutf, Config) ++ Acc, Config);
|
2013-02-05 00:03:58 -08:00
|
|
|
%% overlong encodings and missing continuations of a 4 byte sequence
|
2013-02-12 11:54:42 -08:00
|
|
|
clean(<<X, Rest/binary>>, Acc, Config) when X >= 240, X =< 247 ->
|
|
|
|
clean(strip_continuations(Rest, 3), maybe_replace(badutf, Config) ++ Acc, Config);
|
|
|
|
clean(<<_, Rest/binary>>, Acc, Config) ->
|
|
|
|
clean(Rest, maybe_replace(badutf, Config) ++ Acc, Config).
|
2013-02-05 00:03:58 -08:00
|
|
|
|
|
|
|
|
|
|
|
strip_continuations(Bin, 0) -> Bin;
|
|
|
|
strip_continuations(<<X, Rest/binary>>, N) when X >= 128, X =< 191 ->
|
|
|
|
strip_continuations(Rest, N - 1);
|
|
|
|
%% not a continuation byte
|
|
|
|
strip_continuations(Bin, _) -> Bin.
|
|
|
|
|
|
|
|
|
2013-02-19 15:09:28 -08:00
|
|
|
maybe_escape(Escaped, #config{dirty_strings=true}) -> <<Escaped/utf8>>;
|
|
|
|
maybe_escape(Escaped, #config{escaped_strings=true} = Config) -> escape(Escaped, Config);
|
|
|
|
maybe_escape(Escaped, _Config) -> <<Escaped/utf8>>.
|
2013-02-19 01:00:35 -08:00
|
|
|
|
|
|
|
escape($\b, _) -> <<"\\b">>;
|
|
|
|
escape($\t, _) -> <<"\\t">>;
|
|
|
|
escape($\n, _) -> <<"\\n">>;
|
|
|
|
escape($\f, _) -> <<"\\f">>;
|
|
|
|
escape($\r, _) -> <<"\\r">>;
|
|
|
|
escape($\", _) -> <<"\\\"">>;
|
|
|
|
escape($\\, _) -> <<"\\\\">>;
|
|
|
|
escape($/, #config{escaped_forward_slashes=true}) -> <<"\\/">>;
|
|
|
|
escape($/, _) -> <<"/">>;
|
|
|
|
escape(16#2028, #config{unescaped_jsonp=true}) -> <<16#2028/utf8>>;
|
|
|
|
escape(16#2028, _) -> <<"\\u2028">>;
|
|
|
|
escape(16#2029, #config{unescaped_jsonp=true}) -> <<16#2029/utf8>>;
|
|
|
|
escape(16#2029, _) -> <<"\\u2029">>;
|
|
|
|
escape(X, _) when X < 32 ->
|
|
|
|
<<A:4, B:4, C:4, D:4>> = <<X:16>>,
|
|
|
|
<<"\\u", (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))>>.
|
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_replace($\b, #config{escaped_strings=true}) -> [$b, $\\];
|
|
|
|
maybe_replace($\t, #config{escaped_strings=true}) -> [$t, $\\];
|
|
|
|
maybe_replace($\n, #config{escaped_strings=true}) -> [$n, $\\];
|
|
|
|
maybe_replace($\f, #config{escaped_strings=true}) -> [$f, $\\];
|
|
|
|
maybe_replace($\r, #config{escaped_strings=true}) -> [$r, $\\];
|
|
|
|
maybe_replace($\", #config{escaped_strings=true}) -> [$\", $\\];
|
|
|
|
maybe_replace($\\, #config{escaped_strings=true}) -> [$\\, $\\];
|
|
|
|
maybe_replace($/, Config) ->
|
|
|
|
case Config#config.escaped_forward_slashes of
|
2013-02-05 00:03:58 -08:00
|
|
|
true -> [$/, $\\]
|
|
|
|
; false -> [$/]
|
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_replace(X, Config=#config{escaped_strings=true}) when X == 16#2028; X == 16#2029 ->
|
|
|
|
case Config#config.unescaped_jsonp of
|
2013-02-05 00:03:58 -08:00
|
|
|
true -> [X]
|
|
|
|
; false -> lists:reverse(jsx_utils:json_escape_sequence(X))
|
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_replace(X, #config{escaped_strings=true}) when X < 32 ->
|
2013-02-05 00:03:58 -08:00
|
|
|
lists:reverse(jsx_utils:json_escape_sequence(X));
|
2013-02-12 11:54:42 -08:00
|
|
|
maybe_replace(noncharacter, #config{replaced_bad_utf8=true}) -> [16#fffd];
|
|
|
|
maybe_replace(surrogate, #config{replaced_bad_utf8=true}) -> [16#fffd];
|
|
|
|
maybe_replace(badutf, #config{replaced_bad_utf8=true}) -> [16#fffd];
|
2013-02-05 00:03:58 -08:00
|
|
|
maybe_replace(_, _) -> erlang:error(badarg).
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-09-20 19:11:31 -07:00
|
|
|
%% eunit tests
|
|
|
|
-ifdef(TEST).
|
2011-08-10 01:44:38 -07:00
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
2010-09-20 19:11:31 -07:00
|
|
|
|
2011-09-14 06:44:52 -07:00
|
|
|
|
2012-03-31 20:31:28 -07:00
|
|
|
json_escape_sequence_test_() ->
|
2011-09-14 06:44:52 -07:00
|
|
|
[
|
2012-03-31 20:31:28 -07:00
|
|
|
{"json escape sequence test - 16#0000", ?_assertEqual(json_escape_sequence(16#0000), "\\u0000")},
|
|
|
|
{"json escape sequence test - 16#abc", ?_assertEqual(json_escape_sequence(16#abc), "\\u0abc")},
|
|
|
|
{"json escape sequence test - 16#def", ?_assertEqual(json_escape_sequence(16#def), "\\u0def")}
|
2011-09-14 06:44:52 -07:00
|
|
|
].
|
|
|
|
|
2013-02-05 16:40:15 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
config_test_() ->
|
2012-03-29 06:03:14 -07:00
|
|
|
[
|
|
|
|
{"all flags",
|
|
|
|
?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{
|
2012-03-31 21:58:18 -07:00
|
|
|
replaced_bad_utf8=true,
|
|
|
|
escaped_forward_slashes=true,
|
2012-03-29 06:03:14 -07:00
|
|
|
explicit_end=true,
|
2012-03-31 21:58:18 -07:00
|
|
|
single_quoted_strings=true,
|
|
|
|
unescaped_jsonp=true,
|
2012-03-29 06:03:14 -07:00
|
|
|
comments=true,
|
|
|
|
dirty_strings=true,
|
2012-03-31 21:58:18 -07:00
|
|
|
ignored_bad_escapes=true
|
2013-02-05 20:29:38 -08:00
|
|
|
},
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([
|
2013-02-05 20:29:38 -08:00
|
|
|
replaced_bad_utf8,
|
|
|
|
escaped_forward_slashes,
|
|
|
|
explicit_end,
|
|
|
|
single_quoted_strings,
|
|
|
|
unescaped_jsonp,
|
|
|
|
comments,
|
|
|
|
dirty_strings,
|
|
|
|
ignored_bad_escapes
|
|
|
|
])
|
2012-03-29 06:03:14 -07:00
|
|
|
)
|
|
|
|
},
|
|
|
|
{"relax flag",
|
|
|
|
?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{
|
2012-03-31 21:58:18 -07:00
|
|
|
replaced_bad_utf8=true,
|
|
|
|
single_quoted_strings=true,
|
2012-03-29 06:03:14 -07:00
|
|
|
comments=true,
|
2012-03-31 21:58:18 -07:00
|
|
|
ignored_bad_escapes=true
|
2013-02-05 20:29:38 -08:00
|
|
|
},
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([relax])
|
2012-03-29 06:03:14 -07:00
|
|
|
)
|
2013-02-05 20:12:29 -08:00
|
|
|
},
|
2013-02-05 20:25:41 -08:00
|
|
|
{"deprecated flags", ?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{
|
2013-02-05 20:25:41 -08:00
|
|
|
pre_encode=fun lists:length/1,
|
|
|
|
replaced_bad_utf8=true,
|
|
|
|
escaped_forward_slashes=true,
|
|
|
|
single_quoted_strings=true,
|
|
|
|
unescaped_jsonp=true,
|
|
|
|
escaped_strings=true,
|
|
|
|
ignored_bad_escapes=true
|
2013-02-05 20:29:38 -08:00
|
|
|
},
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([
|
2013-02-05 20:29:38 -08:00
|
|
|
{pre_encoder, fun lists:length/1},
|
|
|
|
loose_unicode,
|
|
|
|
escape_forward_slash,
|
|
|
|
single_quotes,
|
|
|
|
no_jsonp_escapes,
|
|
|
|
json_escape,
|
|
|
|
ignore_bad_escapes
|
|
|
|
])
|
2013-02-05 20:25:41 -08:00
|
|
|
)},
|
|
|
|
{"pre_encode flag", ?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{pre_encode=fun lists:length/1},
|
|
|
|
parse_config([{pre_encode, fun lists:length/1}])
|
2013-02-05 20:25:41 -08:00
|
|
|
)},
|
|
|
|
{"two pre_encoders defined", ?_assertError(
|
2013-02-05 20:12:29 -08:00
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([
|
2013-02-05 20:12:29 -08:00
|
|
|
{pre_encode, fun(_) -> true end},
|
|
|
|
{pre_encode, fun(_) -> false end}
|
|
|
|
])
|
2013-02-05 20:29:38 -08:00
|
|
|
)},
|
2013-02-12 11:54:42 -08:00
|
|
|
{"bad option flag", ?_assertError(badarg, parse_config([error]))}
|
2012-03-29 06:03:14 -07:00
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-05 16:40:15 -08:00
|
|
|
%% erlang refuses to encode certain codepoints, so fake them
|
|
|
|
to_fake_utf8(N) when N < 16#0080 -> <<N:8>>;
|
|
|
|
to_fake_utf8(N) when N < 16#0800 ->
|
|
|
|
<<0:5, Y:5, X:6>> = <<N:16>>,
|
|
|
|
<<2#110:3, Y:5, 2#10:2, X:6>>;
|
|
|
|
to_fake_utf8(N) 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_utf8(N) ->
|
|
|
|
<<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>>.
|
|
|
|
|
|
|
|
|
|
|
|
codepoints() ->
|
|
|
|
unicode:characters_to_binary(
|
|
|
|
[32, 33]
|
|
|
|
++ lists:seq(35, 91)
|
|
|
|
++ lists:seq(93, 16#2027)
|
|
|
|
++ lists:seq(16#202a, 16#d7ff)
|
|
|
|
++ lists:seq(16#e000, 16#fdcf)
|
|
|
|
++ lists:seq(16#fdf0, 16#fffd)
|
|
|
|
).
|
|
|
|
|
|
|
|
escapables() ->
|
|
|
|
[ to_fake_utf8(N) || N <-
|
|
|
|
lists:seq(0, 31) ++ [34, 92, 16#2028, 16#2029]
|
|
|
|
].
|
|
|
|
|
|
|
|
extended_codepoints() ->
|
|
|
|
unicode:characters_to_binary(
|
|
|
|
lists:seq(16#10000, 16#1fffd) ++ [
|
|
|
|
16#20000, 16#30000, 16#40000, 16#50000, 16#60000,
|
|
|
|
16#70000, 16#80000, 16#90000, 16#a0000, 16#b0000,
|
2013-02-06 22:27:52 -08:00
|
|
|
16#c0000, 16#d0000, 16#e0000, 16#f0000, 16#100000
|
|
|
|
]
|
2013-02-05 16:40:15 -08:00
|
|
|
).
|
|
|
|
|
|
|
|
noncharacters() -> [ to_fake_utf8(N) || N <- lists:seq(16#fffe, 16#ffff) ].
|
|
|
|
|
|
|
|
extended_noncharacters() ->
|
|
|
|
[ to_fake_utf8(N) || N <- [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() -> [ to_fake_utf8(N) || N <- lists:seq(16#d800, 16#dfff) ].
|
|
|
|
|
|
|
|
reserved_space() -> [ to_fake_utf8(N) || N <- lists:seq(16#fdd0, 16#fdef) ].
|
|
|
|
|
|
|
|
|
2013-02-05 18:08:19 -08:00
|
|
|
fail_ensure(Codepoints, Title) ->
|
2013-02-05 16:40:15 -08:00
|
|
|
{generator,
|
|
|
|
fun() -> case Codepoints of
|
|
|
|
[N|Rest] ->
|
2013-02-05 18:08:19 -08:00
|
|
|
[ {Title, ?_assertError(badarg, ensure_clean(N))}
|
|
|
|
| fail_ensure(Rest, Title)
|
|
|
|
]
|
2013-02-05 16:40:15 -08:00
|
|
|
; [] -> []
|
|
|
|
end end
|
|
|
|
}.
|
|
|
|
|
2013-02-05 18:08:19 -08:00
|
|
|
fail_clean(Codepoints, Title) ->
|
2013-02-05 16:40:15 -08:00
|
|
|
{generator,
|
|
|
|
fun() -> case Codepoints of
|
|
|
|
[N|Rest] ->
|
2013-02-12 11:54:42 -08:00
|
|
|
[ {Title, ?_assertError(badarg, clean(N, [], #config{}))}
|
2013-02-05 18:08:19 -08:00
|
|
|
| fail_clean(Rest, Title)
|
|
|
|
]
|
2013-02-05 16:40:15 -08:00
|
|
|
; [] -> []
|
|
|
|
end end
|
|
|
|
}.
|
|
|
|
|
2013-02-05 18:08:19 -08:00
|
|
|
fail_bad(Codepoints, Title) ->
|
2013-02-05 16:40:15 -08:00
|
|
|
{generator,
|
|
|
|
fun() -> case Codepoints of
|
|
|
|
[N|Rest] ->
|
2013-02-12 11:54:42 -08:00
|
|
|
[ {Title, ?_assertError(badarg, clean(N, [], #config{}))}
|
2013-02-05 18:08:19 -08:00
|
|
|
| fail_bad(Rest, Title)
|
|
|
|
]
|
2013-02-05 16:40:15 -08:00
|
|
|
; [] -> []
|
|
|
|
end end
|
|
|
|
}.
|
|
|
|
|
2013-02-05 18:08:19 -08:00
|
|
|
replace_bad(Codepoints, Title) ->
|
2013-02-05 16:40:15 -08:00
|
|
|
{generator,
|
|
|
|
fun() -> case Codepoints of
|
|
|
|
[N|Rest] ->
|
2013-02-12 11:54:42 -08:00
|
|
|
[ {Title, ?_assertEqual(<<16#fffd/utf8>>, clean(N, [], #config{replaced_bad_utf8=true}))}
|
2013-02-05 18:08:19 -08:00
|
|
|
| replace_bad(Rest, Title)
|
2013-02-05 16:40:15 -08:00
|
|
|
]
|
|
|
|
; [] -> []
|
|
|
|
end end
|
|
|
|
}.
|
|
|
|
|
|
|
|
|
|
|
|
ensure_clean_test_() ->
|
|
|
|
[
|
|
|
|
{"basic codepoints", ?_assertEqual(ok, ensure_clean(codepoints()))},
|
|
|
|
{"escapables", ?_assertEqual(ok, ensure_clean(unicode:characters_to_binary(escapables())))},
|
|
|
|
{"extended codepoints", ?_assertEqual(ok, ensure_clean(extended_codepoints()))},
|
2013-02-05 18:08:19 -08:00
|
|
|
fail_ensure(noncharacters(), "noncharacters"),
|
|
|
|
fail_ensure(extended_noncharacters(), "extended noncharacters"),
|
|
|
|
fail_ensure(surrogates(), "surrogates"),
|
|
|
|
fail_ensure(reserved_space(), "reserved space")
|
2013-02-05 16:40:15 -08:00
|
|
|
].
|
|
|
|
|
|
|
|
|
|
|
|
clean_test_() ->
|
|
|
|
[
|
|
|
|
{"basic codepoints", ?_assertEqual(
|
|
|
|
codepoints(),
|
2013-02-12 11:54:42 -08:00
|
|
|
clean(codepoints(), [], #config{})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-05 18:08:19 -08:00
|
|
|
fail_clean(escapables(), "escapables"),
|
2013-02-05 16:40:15 -08:00
|
|
|
{"extended codepoints", ?_assertEqual(
|
|
|
|
extended_codepoints(),
|
2013-02-12 11:54:42 -08:00
|
|
|
clean(extended_codepoints(), [], #config{})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-05 18:08:19 -08:00
|
|
|
fail_clean(noncharacters(), "noncharacters"),
|
|
|
|
fail_clean(extended_noncharacters(), "extended noncharacters"),
|
|
|
|
fail_clean(surrogates(), "surrogates"),
|
|
|
|
fail_clean(reserved_space(), "reserved space")
|
2013-02-05 16:40:15 -08:00
|
|
|
].
|
|
|
|
|
|
|
|
|
|
|
|
escape_test_() ->
|
|
|
|
[
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape backspace", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\b">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0008, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 15:09:28 -08:00
|
|
|
{"don't escape backspace", ?_assertEqual(
|
|
|
|
<<"\b">>,
|
|
|
|
maybe_escape(16#0008, #config{})
|
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape tab", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\t">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0009, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape newline", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\n">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#000a, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape formfeed", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\f">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#000c, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape carriage return", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\r">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#000d, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape quote", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\\"">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0022, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape forward slash", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\/">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#002f, #config{escaped_strings=true, escaped_forward_slashes=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"do not maybe_escape forward slash", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"/">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#002f, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape backslash", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\\\">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#005c, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape jsonp (u2028)", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\u2028">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#2028, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"do not maybe_escape jsonp (u2028)", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<16#2028/utf8>>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#2028, #config{escaped_strings=true, unescaped_jsonp=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape jsonp (u2029)", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<"\\u2029">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#2029, #config{escaped_strings=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"do not maybe_escape jsonp (u2029)", ?_assertEqual(
|
2013-02-05 16:52:28 -08:00
|
|
|
<<16#2029/utf8>>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#2029, #config{escaped_strings=true, unescaped_jsonp=true})
|
2013-02-05 16:52:28 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0000", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0000">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0000, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0001", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0001">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0001, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0002", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0002">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0002, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0003", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0003">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0003, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0004", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0004">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0004, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0005", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0005">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0005, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0006", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0006">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0006, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0007", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0007">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0007, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u000b", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u000b">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#000b, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u000e", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u000e">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#000e, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u000f", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u000f">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#000f, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0010", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0010">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0010, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0011", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0011">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0011, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0012", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0012">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0012, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0013", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0013">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0013, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0014", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0014">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0014, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0015", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0015">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0015, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0016", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0016">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0016, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0017", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0017">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0017, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0018", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0018">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0018, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u0019", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u0019">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#0019, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u001a", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u001a">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#001a, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u001b", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u001b">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#001b, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u001c", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u001c">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#001c, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u001d", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u001d">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#001d, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u001e", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u001e">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#001e, #config{escaped_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-19 01:00:35 -08:00
|
|
|
{"maybe_escape u001f", ?_assertEqual(
|
2013-02-05 16:40:15 -08:00
|
|
|
<<"\\u001f">>,
|
2013-02-19 01:00:35 -08:00
|
|
|
maybe_escape(16#001f, #config{escaped_strings=true})
|
|
|
|
)},
|
|
|
|
{"dirty strings", ?_assertEqual(
|
|
|
|
<<0>>,
|
2013-02-19 15:09:28 -08:00
|
|
|
maybe_escape(16#0000, #config{escaped_strings=true, dirty_strings=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-05 00:03:58 -08:00
|
|
|
bad_utf8_test_() ->
|
|
|
|
[
|
2013-02-12 11:54:42 -08:00
|
|
|
{"noncharacter u+fffe", ?_assertError(badarg, clean_string(to_fake_utf8(16#fffe), #config{}))},
|
2013-02-05 16:40:15 -08:00
|
|
|
{"noncharacter u+fffe replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(to_fake_utf8(16#fffe), #config{replaced_bad_utf8=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-12 11:54:42 -08:00
|
|
|
{"noncharacter u+ffff", ?_assertError(badarg, clean_string(to_fake_utf8(16#ffff), #config{}))},
|
2013-02-05 16:40:15 -08:00
|
|
|
{"noncharacter u+ffff replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(to_fake_utf8(16#ffff), #config{replaced_bad_utf8=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-05 18:08:19 -08:00
|
|
|
fail_bad(extended_noncharacters(), "extended noncharacters"),
|
|
|
|
replace_bad(extended_noncharacters(), "extended noncharacters replaced"),
|
|
|
|
fail_bad(surrogates(), "surrogates"),
|
|
|
|
replace_bad(surrogates(), "surrogates replaced"),
|
|
|
|
fail_bad(reserved_space(), "reserved space"),
|
|
|
|
replace_bad(reserved_space(), "reserved space replaced"),
|
2013-02-05 16:40:15 -08:00
|
|
|
{"orphan continuation byte u+0080", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#0080>>, #config{})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
|
|
|
{"orphan continuation byte u+0080 replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#0080>>, #config{replaced_bad_utf8=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
|
|
|
{"orphan continuation byte u+00bf", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#00bf>>, #config{})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
|
|
|
{"orphan continuation byte u+00bf replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#00bf>>, #config{replaced_bad_utf8=true})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
|
|
|
{"2 continuation bytes", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<(binary:copy(<<16#0080>>, 2))/binary>>, #config{})
|
2013-02-05 16:40:15 -08:00
|
|
|
)},
|
2013-02-05 12:24:56 -08:00
|
|
|
{"2 continuation bytes replaced", ?_assertEqual(
|
|
|
|
binary:copy(<<16#fffd/utf8>>, 2),
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<(binary:copy(<<16#0080>>, 2))/binary>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
2013-02-05 00:03:58 -08:00
|
|
|
{"3 continuation bytes",
|
2013-02-12 11:54:42 -08:00
|
|
|
?_assertError(badarg, clean_string(<<(binary:copy(<<16#0080>>, 3))/binary>>, #config{}))
|
2013-02-05 00:03:58 -08:00
|
|
|
},
|
2013-02-05 12:24:56 -08:00
|
|
|
{"3 continuation bytes replaced", ?_assertEqual(
|
|
|
|
binary:copy(<<16#fffd/utf8>>, 3),
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<(binary:copy(<<16#0080>>, 3))/binary>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
2013-02-05 00:03:58 -08:00
|
|
|
{"4 continuation bytes",
|
2013-02-12 11:54:42 -08:00
|
|
|
?_assertError(badarg, clean_string(<<(binary:copy(<<16#0080>>, 4))/binary>>, #config{}))
|
2013-02-05 00:03:58 -08:00
|
|
|
},
|
2013-02-05 12:24:56 -08:00
|
|
|
{"4 continuation bytes replaced", ?_assertEqual(
|
|
|
|
binary:copy(<<16#fffd/utf8>>, 4),
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<(binary:copy(<<16#0080>>, 4))/binary>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
2013-02-05 00:03:58 -08:00
|
|
|
{"5 continuation bytes",
|
2013-02-12 11:54:42 -08:00
|
|
|
?_assertError(badarg, clean_string(<<(binary:copy(<<16#0080>>, 5))/binary>>, #config{}))
|
2013-02-05 00:03:58 -08:00
|
|
|
},
|
2013-02-05 12:24:56 -08:00
|
|
|
{"5 continuation bytes replaced", ?_assertEqual(
|
|
|
|
binary:copy(<<16#fffd/utf8>>, 5),
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<(binary:copy(<<16#0080>>, 5))/binary>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
2013-02-05 00:03:58 -08:00
|
|
|
{"6 continuation bytes",
|
2013-02-12 11:54:42 -08:00
|
|
|
?_assertError(badarg, clean_string(<<(binary:copy(<<16#0080>>, 6))/binary>>, #config{}))
|
2013-02-05 12:24:56 -08:00
|
|
|
},
|
|
|
|
{"6 continuation bytes replaced", ?_assertEqual(
|
|
|
|
binary:copy(<<16#fffd/utf8>>, 6),
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<(binary:copy(<<16#0080>>, 6))/binary>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"all continuation bytes", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<(list_to_binary(lists:seq(16#0080, 16#00bf)))/binary>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"all continuation bytes replaced", ?_assertEqual(
|
|
|
|
binary:copy(<<16#fffd/utf8>>, length(lists:seq(16#0080, 16#00bf))),
|
|
|
|
clean_string(
|
|
|
|
<<(list_to_binary(lists:seq(16#0080, 16#00bf)))/binary>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{replaced_bad_utf8=true}
|
2013-02-05 00:03:58 -08:00
|
|
|
)
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
2013-02-12 11:54:42 -08:00
|
|
|
{"lonely start byte", ?_assertError(badarg, clean_string(<<16#00c0>>, #config{}))},
|
2013-02-05 12:24:56 -08:00
|
|
|
{"lonely start byte replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#00c0>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"lonely start bytes (2 byte)", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#00c0, 32, 16#00df>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"lonely start bytes (2 byte) replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32, 16#fffd/utf8>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#00c0, 32, 16#00df>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"lonely start bytes (3 byte)", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#00e0, 32, 16#00ef>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"lonely start bytes (3 byte) replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32, 16#fffd/utf8>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#00e0, 32, 16#00ef>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"lonely start bytes (4 byte)", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#00f0, 32, 16#00f7>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"lonely start bytes (4 byte) replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32, 16#fffd/utf8>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#00f0, 32, 16#00f7>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"missing continuation byte (3 byte)", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<224, 160, 32>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"missing continuation byte (3 byte) replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<224, 160, 32>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"missing continuation byte (4 byte missing one)", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<240, 144, 128, 32>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"missing continuation byte (4 byte missing one) replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<240, 144, 128, 32>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"missing continuation byte (4 byte missing two)", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<240, 144, 32>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"missing continuation byte (4 byte missing two) replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<240, 144, 32>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"overlong encoding of u+002f (2 byte)", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#c0, 16#af, 32>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"overlong encoding of u+002f (2 byte) replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#c0, 16#af, 32>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"overlong encoding of u+002f (3 byte)", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#e0, 16#80, 16#af, 32>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"overlong encoding of u+002f (3 byte) replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#e0, 16#80, 16#af, 32>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"overlong encoding of u+002f (4 byte)", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#f0, 16#80, 16#80, 16#af, 32>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"overlong encoding of u+002f (4 byte) replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#f0, 16#80, 16#80, 16#af, 32>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"highest overlong 2 byte sequence", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#c1, 16#bf, 32>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"highest overlong 2 byte sequence replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#c1, 16#bf, 32>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"highest overlong 3 byte sequence", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#e0, 16#9f, 16#bf, 32>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"highest overlong 3 byte sequence replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#e0, 16#9f, 16#bf, 32>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"highest overlong 4 byte sequence", ?_assertError(
|
|
|
|
badarg,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#f0, 16#8f, 16#bf, 16#bf, 32>>, #config{})
|
2013-02-05 12:24:56 -08:00
|
|
|
)},
|
|
|
|
{"highest overlong 4 byte sequence replaced", ?_assertEqual(
|
|
|
|
<<16#fffd/utf8, 32>>,
|
2013-02-12 11:54:42 -08:00
|
|
|
clean_string(<<16#f0, 16#8f, 16#bf, 16#bf, 32>>, #config{replaced_bad_utf8=true})
|
2013-02-05 12:24:56 -08:00
|
|
|
)}
|
2013-02-05 00:03:58 -08:00
|
|
|
].
|
|
|
|
|
|
|
|
|
2010-09-20 19:11:31 -07:00
|
|
|
-endif.
|