diff --git a/src/hz_format.erl b/src/hz_format.erl index f27be1d..03bd2d3 100644 --- a/src/hz_format.erl +++ b/src/hz_format.erl @@ -42,8 +42,6 @@ price(Style, Pucks) -> Formatted :: string(). %% @doc %% A simplified format function covering the most common formats desired. -%% -%% ``` price(gaju, us, Pucks) -> western($,, $., 3, all, Pucks); @@ -245,7 +243,8 @@ jp2(puck, Precision, Pucks) -> true -> [$0, puck_mark()]; false -> - PuckingString = lists:flatten(string:pad(lists:reverse(lists:sublist(P, Digits)), 18, leading, $0)), + PucksToGive = lists:sublist(P, Digits), + PuckingString = lists:flatten(string:pad(lists:reverse(PucksToGive), 18, leading, $0)), case lists:all(fun(C) -> C =:= $0 end, PuckingString) of false -> myriad4(puck_mark(), h, PuckingString); true -> [$0, puck_mark()] @@ -339,34 +338,30 @@ one_gaju() -> 1_000_000_000_000_000_000. %% style. When in doubt, always call `read/2' with a style specified. read(Format) -> - case assess_style(Format) of + case assess_style(string:trim(Format)) of us -> read(us, Format); ch -> read(ch, Format); jp -> read(jp, Format); Error -> Error end. +assess_style([H1, H2 | Numbers]) + when H1 =:= $木 orelse H1 =:= $本 orelse H2 =:= $木 orelse H2 =:= $本 -> + case count($., Numbers) > 1 of + false -> us; + true -> ch + end; assess_style(Format) -> - CharIndex = count_chars(Format), - case maps:find($., CharIndex) of - {ok, 1} -> - us; - {ok, N} when N > 1 -> - ch; - error -> - case maps:is_key($木, CharIndex) orelse maps:is_key($本, CharIndex) of - true -> read(jp, Format); - false -> {error, format} - end + case lists:member($木, Format) orelse lists:member($本, Format) of + true -> jp; + false -> {error, format} end. -count_chars(Format) -> - count_chars(Format, #{}). +count(Char, String) -> count(Char, String, 0). -count_chars([H | T], A) -> count_chars(T, maps:update_with(H, fun inc/1, 1, A)); -count_chars([], A) -> A. - -inc(N) -> N + 1. +count(C, [C | T], A) -> count(C, T, A + 1); +count(C, [_ | T], A) -> count(C, T, A); +count(_, [], A) -> A. -spec read(Style, Format) -> Result