diff --git a/src/hz_format.erl b/src/hz_format.erl index 87d3b16..08a83f6 100644 --- a/src/hz_format.erl +++ b/src/hz_format.erl @@ -20,7 +20,7 @@ amount(Pucks) -> -spec amount(Style, Pucks) -> Formatted - when Style :: us | jp | metric | heresy | {Separator, Span}, + when Style :: us | jp | metric | legacy | {Separator, Span}, Separator :: $, | $_, Span :: 3 | 4, Pucks :: integer(), @@ -35,7 +35,7 @@ amount(Style, Pucks) -> -spec amount(Unit, Style, Pucks) -> Formatted when Unit :: gaju | puck, - Style :: us | jp | metric | heresy | {Separator, Span}, + Style :: us | jp | metric | legacy | {Separator, Span}, Separator :: $, | $_, Span :: 3 | 4, Pucks :: integer(), @@ -51,7 +51,7 @@ amount(Unit, jp, Pucks) -> jp(Unit, all, Pucks); amount(Unit, metric, Pucks) -> bestern(Unit, ranks(metric), all, Pucks); -amount(Unit, heresy, Pucks) -> +amount(Unit, legacy, Pucks) -> bestern(Unit, ranks(heresy), all, Pucks); amount(gaju, {Separator, Span}, Pucks) -> western(Separator, $., Span, all, Pucks); @@ -61,7 +61,7 @@ amount(puck, {Separator, Span}, Pucks) -> -spec amount(Unit, Style, Precision, Pucks) -> Serialized when Unit :: gaju | puck, - Style :: us | jp | metric | heresy | {Separator, Span}, + Style :: us | jp | metric | legacy | {Separator, Span}, Precision :: all | 0..18, Separator :: $, | $_, Span :: 3 | 4, @@ -134,7 +134,7 @@ western2(Separator, Span, Pucks) -> western(Separator, Break, Span, Precision, Pucks) when Pucks >= 0 -> western2(Separator, Break, Span, Precision, Pucks); western(Separator, Break, Span, Precision, Pucks) when Pucks < 0 -> - [$- | western2(Separator, Break, Span, Precision, Pucks)]. + [$- | western2(Separator, Break, Span, Precision, Pucks * -1)]. western2(Separator, _, Span, 0, Pucks) -> @@ -432,6 +432,11 @@ read([C | Rest]) C =:= $\r orelse C =:= $\n -> read(Rest); +read([C | Rest]) when $0 =< C andalso C =< $9 -> + read(Rest, [C], []); +read([C | Rest]) when $0 =< C andalso C =< $9 -> + NumC = C - $0 + $0, + read(Rest, [NumC], []); read(_) -> io:format("Barfing~n"), {error, format}. @@ -457,7 +462,15 @@ read_w_gajus([], A) -> G = list_to_integer(lists:reverse(A)) * one_gaju(), {ok, G}; read_w_gajus([C, 32 | Rest], A) -> - read_b_gajus(Rest, [{C, A}]); + read(Rest, [], [{C, A}]); +read_w_gajus([32, $G | Rest], A) -> + case read(Rest, [], [{$G, A}]) of + {ok, P} -> + G = list_to_integer(lists:reverse(A)) * one_gaju(), + {ok, G + P}; + Error -> + Error + end; read_w_gajus(_, _) -> io:format("Derping~n"), {error, format}. @@ -475,77 +488,89 @@ read_w_pucks([], A) -> Padded = lists:flatten(string:pad(lists:reverse(A), 18, trailing, $0)), {ok, list_to_integer(Padded)}. -read_b_gajus(_, _) -> - {error, nyi}. + +read([C | Rest], A, S) when $0 =< C andalso C =< $9 -> + read(Rest, [C | A], S); +read([C | Rest], A, S) when $0 =< C andalso C =< $9 -> + NumC = C - $0 + $0, + read(Rest, [NumC | A], S); +read([$木], A, S) -> + calc([{$木, A} | S]); +read([$木, 32 | Rest], A, S) -> + read(Rest, [], [{$木, A} | S]); +read([$本], A, S) -> + calc([{$本, A} | S]); +read([32, $G], A, S) -> + calc([{$G, A} | S]); +read([32, $G, 32 | Rest], A, S) -> + read(Rest, [], [{$G, A} | S]); +read([32, $P], A, S) -> + calc([{$P, A} | S]); +read([C, 32 | Rest], A, S) -> + read(Rest, [], [{C, A} | S]); +read([C | Rest], A, S) -> + read(Rest, [], [{C, A} | S]); +read(_, _, _) -> + {error, format}. + +calc(S) -> + calc(S, 0). + +calc([{_, []} | S], A) -> + calc(S, A); +calc([{M, Cs} | S], A) -> + case magnitude(M) of + {ok, J} -> + N = list_to_integer(lists:reverse(Cs)) * J, + calc(S, A + N); + Error -> + Error + end; +calc([], A) -> + {ok, A}. + +%magnitude_j("万") -> 1_0000; +%magnitude_j("億") -> 1_0000_0000; +%magnitude_j("兆") -> 1_0000_0000_0000; +%magnitude_j("京") -> 1_0000_0000_0000_0000; +%magnitude_j("垓") -> 1_0000_0000_0000_0000_0000; +%magnitude_j("秭") -> 1_0000_0000_0000_0000_0000_0000; +%magnitude_j("穣") -> 1_0000_0000_0000_0000_0000_0000_0000; +%magnitude_j("溝") -> 1_0000_0000_0000_0000_0000_0000_0000_0000; +%magnitude_j("澗") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000; +%magnitude_j("正") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000; +%magnitude_j("載") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000; +%magnitude_j("極") -> 1_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000; +%magnitude_j("木") -> one_gaju(); +%magnitude_j("本") -> 1. -%is_numchar(C) -> $0 =< C andalso C =< $9. -% -%read_jp([$-, Format]) -> -% read_jp_neg(Format); -%read_jp([$-, Format]) -> -% read_jp_neg(Format); -%read_jp(Format) -> -% read_jp2(Format). -% -%read_jp_neg(Format) -> -% case read_jp2(Format) of -% {ok, Pucks} -> {ok, Pucks * -1}; -% Error -> Error -% end. -% -%read_jp2(Format) -> -% case segment_jp(Format) of -% {ok, Segments} -> assemble_jp(Segments); -% Error -> Error -% end. -% -%segment_jp(Format) -> -% case string:split(Format, [gaju_mark()], all) of -% [Gajus, Pucks] -> -% case read_segment(Gajus) of -% {ok, GajuSegments} -> -% case read_segment(Pucks) of -% {ok, PuckSegments} -> {ok, GajuSegments, PuckSegments}; -% Error -> Error -% end; -% Error -> -% Error -% end; -% [Gajus] -> -% case read_segment(Gajus) of -% {ok, GajuSegments} -> {ok, GajuSegments, ["0"]}; -% Error -> Error -% end; -% [] -> -% {ok, ["0"], ["0"]}; -% _ -> -% {error, format} -% end. -% -%read_segment([C | T], R, A) when $0 =< C andalso C =< $9 -> -% N = C - $0, -% read_segment(T, R, [NA); -%read_segment([C | T], R, A) when $0 =< C andalso C =< $9 -> -% -% -% -%assemble_jp({GajuSegments, PuckSegments}) -> -% GajuString = lists:flatten(lists:map(fun expand_jp_myriad/1, GajuSegments)), -% PuckString = lists:flatten(lists:map(fun expand_jp_myriad/1, PuckDegments)), -% {ok, integer_to_list(lists:append(GajuString, PuckString))}. -%assemble_jp(PuckSegments) -> -% PuckString = lists:flatten(lists:map(fun expand_jp_myriad/1, PuckDegments)), -% {ok, integer_to_list(PuckString)}. -% -%expand_jp_myriad(String) -> -% string:pad(String, 4, leading, $0). -% -% -%hw_jp_numchar(C) when $0 =< C andalso C =< $9 -> -% C - $0; -%hw_jp_numchar(C) -> -% C. +magnitude("木") -> + {ok, one_gaju()}; +magnitude("本") -> + {ok, 1}; +magnitude("G") -> + {ok, one_gaju()}; +magnitude("P") -> + {ok, 1}; +magnitude(Mark) -> + case rank(Mark, ranks(jp), 1_0000, 1) of + {ok, J} -> + {ok, J}; + error -> + case rank(Mark, ranks(metric), 1_000, 1) of + {ok, N} -> {ok, N}; + error -> rank(Mark, ranks(heresy), 1_000, 1) + end + end. + + +rank(Mark, [Mark | _], Magnitude, Sum) -> + {ok, Sum * Magnitude}; +rank(Mark, [_ | Rest], Magnitude, Sum) -> + rank(Mark, Rest, Magnitude, Sum * Magnitude); +rank(_, [], _, _) -> + error. -spec price_to_string(Pucks) -> Gajus