Allow all printable unicode characters
Relax config:assert_string/1 such that all printable unicode
characters are allowed and add the appropropriate support
throughout the module.
diff --git a/src/config.erl b/src/config.erl
index 908dee7..f8c41c6 100644
--- a/src/config.erl
+++ b/src/config.erl
@@ -189,7 +189,7 @@
gen_server:call(?MODULE, {delete, Section, Key, Persist, Reason}).
assert_string(Term) ->
- case io_lib:printable_list(Term) of
+ case io_lib:printable_unicode_list(Term) of
true ->
ok;
false ->
@@ -313,12 +313,12 @@
throw({startup_error, Msg})
end,
- Lines = re:split(IniBin, "\r\n|\n|\r|\032", [{return, list}]),
+ Lines = re:split(IniBin, "\r\n|\n|\r|\032", [{return, list}, unicode]),
{_, ParsedIniValues} =
lists:foldl(fun(Line, {AccSectionName, AccValues}) ->
case string:strip(Line) of
"[" ++ Rest ->
- case re:split(Rest, "\\]", [{return, list}]) of
+ case re:split(Rest, "\\]", [{return, list}, unicode]) of
[NewSectionName, ""] ->
{NewSectionName, AccValues};
_Else -> % end bracket not at end, ignore this line
@@ -327,7 +327,7 @@
";" ++ _Comment ->
{AccSectionName, AccValues};
Line2 ->
- case re:split(Line2, "\s?=\s?", [{return, list}]) of
+ case re:split(Line2, "\s?=\s?", [{return, list}, unicode]) of
[Value] ->
MultiLineValuePart = case re:run(Line, "^ \\S", []) of
{match, _} ->
@@ -338,7 +338,7 @@
case {MultiLineValuePart, AccValues} of
{true, [{{_, ValueName}, PrevValue} | AccValuesRest]} ->
% remove comment
- case re:split(Value, " ;|\t;", [{return, list}]) of
+ case re:split(Value, " ;|\t;", [{return, list}, unicode]) of
[[]] ->
% empty line
{AccSectionName, AccValues};
@@ -355,7 +355,7 @@
[ValueName|LineValues] -> % yeehaw, got a line!
RemainingLine = config_util:implode(LineValues, "="),
% removes comments
- case re:split(RemainingLine, " ;|\t;", [{return, list}]) of
+ case re:split(RemainingLine, " ;|\t;", [{return, list}, unicode]) of
[[]] ->
% empty line means delete this key
ets:delete(?MODULE, {AccSectionName, ValueName}),
diff --git a/src/config_writer.erl b/src/config_writer.erl
index 2812686..56f1d54 100644
--- a/src/config_writer.erl
+++ b/src/config_writer.erl
@@ -28,14 +28,17 @@
%% @doc Saves a Section/Key/Value triple to the ini file File::filename()
save_to_file({{Section, Key}, Value}, File) ->
{ok, OldFileContents} = file:read_file(File),
- Lines = re:split(OldFileContents, "\r\n|\n|\r|\032", [{return, list}]),
+ Lines = re:split(OldFileContents, "\r\n|\n|\r|\032", [{return, list}, unicode]),
SectionLine = "[" ++ Section ++ "]",
- {ok, Pattern} = re:compile(["^(", Key, "\\s*=)|\\[[a-zA-Z0-9\.\_-]*\\]"]),
+ {ok, Pattern} = re:compile(
+ ["^(", Key, "\\s*=)|\\[[a-zA-Z0-9\.\_-]*\\]"],
+ [unicode]
+ ),
NewLines = process_file_lines(Lines, [], SectionLine, Pattern, Key, Value),
NewFileContents = reverse_and_add_newline(strip_empty_lines(NewLines), []),
- ok = file:write_file(File, NewFileContents).
+ ok = file:write_file(File, unicode:characters_to_binary(NewFileContents)).
process_file_lines([Section|Rest], SeenLines, Section, Pattern, Key, Value) ->
diff --git a/test/config_tests.erl b/test/config_tests.erl
index 778e383..c6c5fec 100644
--- a/test/config_tests.erl
+++ b/test/config_tests.erl
@@ -98,7 +98,8 @@
config_override_tests(),
config_persistent_changes_tests(),
config_no_files_tests(),
- config_listener_behaviour_tests()
+ config_listener_behaviour_tests(),
+ config_unicode_tests()
]
}.
@@ -218,6 +219,23 @@
}
}.
+config_unicode_tests() ->
+ {
+ "Config unicode tests",
+ {
+ foreachx,
+ fun setup/1, fun teardown/2,
+ [
+ {{temporary, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_2]},
+ fun should_get_non_latin1_value/2},
+ {{persistent, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_2]},
+ fun should_set_non_latin1_value/2},
+ {{persistent, [?CONFIG_DEFAULT, ?CONFIG_FIXTURE_2]},
+ fun should_delete_non_latin1_value/2}
+ ]
+ }
+ }.
+
should_load_all_configs() ->
?_assert(length(config:all()) > 0).
@@ -250,6 +268,12 @@
?_assertException(error, badarg,
config:get([f, o, o], [b, a, r], [b, a, z])).
+should_get_non_latin1_value(_, _) ->
+ ?_test(begin
+ ?assertEqual("true",
+ config:get("fizbang", [1090, 1077, 1089, 1090]))
+ end).
+
should_return_any_supported_default() ->
Values = [undefined, "list", true, false, 0.1, 1],
Tests = [{lists:flatten(io_lib:format("for type(~p)", [V])), V}
@@ -278,6 +302,14 @@
?_assertException(error, badarg,
config:set([f, o, o], [b, a, r], [b, a, z], false)).
+should_set_non_latin1_value(_, _) ->
+ ?_test(begin
+ ?assertEqual(ok,
+ config:set("fizbang", [1090, 1077, 1089, 1090], "false")),
+ ?assertEqual("false",
+ config:get("fizbang", [1090, 1077, 1089, 1090]))
+ end).
+
should_return_undefined_atom_after_option_deletion() ->
?_assertEqual(undefined,
begin
@@ -294,6 +326,12 @@
?_assertException(error, badarg,
config:delete([f, o, o], [b, a, r], false)).
+should_delete_non_latin1_value(_, _) ->
+ ?_test(begin
+ ?assertEqual(ok,
+ config:delete("fizbang", [1090, 1077, 1089, 1090]))
+ end).
+
should_ensure_in_defaults(_, _) ->
?_test(begin
?assertEqual("500",
diff --git a/test/fixtures/config_tests_2.ini b/test/fixtures/config_tests_2.ini
index 5f46357..86e61ce 100644
--- a/test/fixtures/config_tests_2.ini
+++ b/test/fixtures/config_tests_2.ini
@@ -20,3 +20,4 @@
[fizbang]
unicode = normalized
+ัะตัั = true