From 76fc2dcdc88c904e810d84d2da7f9bf1c3d14a81 Mon Sep 17 00:00:00 2001 From: Fred Hebert Date: Wed, 1 Nov 2017 10:21:28 -0400 Subject: [PATCH] Support OTP-21's unicode-only constraints Functions start being available in OTP-20, so to get rid of warnings, a conditional compilation flag is set there. The cspan function has no direct equivalent, but can be worked around. Likely heavier work would be required around its usage where strings are not handled as lists, but only through 'string' functions. However this will still cover most cases and get rid of compile-time warnings for now. --- rebar.config | 4 +++- rebar.lock | 1 + src/getopt.erl | 20 ++++++++++++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 rebar.lock diff --git a/rebar.config b/rebar.config index 7d740be..336be3b 100644 --- a/rebar.config +++ b/rebar.config @@ -11,5 +11,7 @@ warn_export_vars, warn_exported_vars, warn_missing_spec, - warn_untyped_record, debug_info]}. + warn_untyped_record, debug_info, + {platform_define, "^2", unicode_str} +]}. {xref_checks, [undefined_function_calls]}. diff --git a/rebar.lock b/rebar.lock new file mode 100644 index 0000000..57afcca --- /dev/null +++ b/rebar.lock @@ -0,0 +1 @@ +[]. diff --git a/src/getopt.erl b/src/getopt.erl index c161ff6..25c1d0e 100644 --- a/src/getopt.erl +++ b/src/getopt.erl @@ -442,7 +442,7 @@ to_type(integer, Arg) -> to_type(float, Arg) -> list_to_float(Arg); to_type(boolean, Arg) -> - LowerArg = string:to_lower(Arg), + LowerArg = lowercase(Arg), case is_arg_true(LowerArg) of true -> true; @@ -500,7 +500,7 @@ is_implicit_arg(_Type, _Arg) -> -spec is_boolean_arg(string()) -> boolean(). is_boolean_arg(Arg) -> - LowerArg = string:to_lower(Arg), + LowerArg = lowercase(Arg), is_arg_true(LowerArg) orelse is_arg_false(LowerArg). @@ -619,7 +619,7 @@ usage_cmd_line(ProgramName, OptSpecList, CmdLineTail) -> %% already wrapped according to the maximum MaxLineLength. -spec usage_cmd_line_options(MaxLineLength :: non_neg_integer(), [option_spec()], CmdLineTail :: string()) -> iolist(). usage_cmd_line_options(MaxLineLength, OptSpecList, CmdLineTail) -> - usage_cmd_line_options(MaxLineLength, OptSpecList ++ string:tokens(CmdLineTail, " "), [], 0, []). + usage_cmd_line_options(MaxLineLength, OptSpecList ++ lexemes(CmdLineTail, " "), [], 0, []). usage_cmd_line_options(MaxLineLength, [OptSpec | Tail], LineAcc, LineAccLength, Acc) -> Option = [$\s | lists:flatten(usage_cmd_line_option(OptSpec))], @@ -790,7 +790,7 @@ wrap_text_line(Length, [_ | _] = Help, Acc, Count, CurrentLineAcc) -> %% Look for the first whitespace character in the current (reversed) line %% buffer to get a wrapped line. If there is no whitespace just cut the %% line at the position corresponding to the maximum length. - {NextLineAcc, WrappedLine} = case string:cspan(CurrentLineAcc, " \t") of + {NextLineAcc, WrappedLine} = case cspan(CurrentLineAcc, " \t") of WhitespacePos when WhitespacePos < Count -> lists:split(WhitespacePos, CurrentLineAcc); _ -> @@ -933,3 +933,15 @@ to_string(Atom) when is_atom(Atom) -> atom_to_list(Atom); to_string(Value) -> io_lib:format("~p", [Value]). + +%% OTP-20/21 conversion to unicode string module +-ifdef(unicode_str). +lowercase(Str) -> string:lowercase(Str). +lexemes(Str, Separators) -> string:lexemes(Str, Separators). +cspan(Str, Chars) -> length(element(1,string:take(Str, Chars, true))). +-else. +lowercase(Str) -> string:to_lower(Str). +lexemes(Str, Separators) -> string:tokens(Str, Separators). +cspan(Str, Chars) -> string:cspan(Str, Chars). +-endif. +