This commit is contained in:
Craig Everett 2025-12-02 19:40:33 +09:00
parent beed46a38b
commit 975325db14

View File

@ -5,7 +5,7 @@
-module(hz_format). -module(hz_format).
-export([price/1, price/2, price/3, price/4, -export([price/1, price/2, price/3, price/4,
read/1, read/1, read/2,
price_to_string/1, string_to_price/1]). price_to_string/1, string_to_price/1]).
-spec price(Pucks) -> Formatted -spec price(Pucks) -> Formatted
@ -211,12 +211,12 @@ jp2(gaju, 0, Pucks) ->
myriad4(gaju_mark(), h, G); myriad4(gaju_mark(), h, G);
jp2(gaju, all, Pucks) -> jp2(gaju, all, Pucks) ->
H = jp(gaju, 0, Pucks), H = jp(gaju, 0, Pucks),
P = lists:flatten(string:pad(integer_to_list(Pucks rem one_gaju()), 18, leading, $0), P = lists:flatten(string:pad(integer_to_list(Pucks rem one_gaju()), 18, leading, $0)),
T = myriad4("", l, lists:reverse(P)), T = myriad4("", l, lists:reverse(P)),
lists:flatten([H, " ", T]); lists:flatten([H, " ", T]);
jp2(gaju, Precision, Pucks) -> jp2(gaju, Precision, Pucks) ->
H = jp(gaju, 0, Pucks), H = jp(gaju, 0, Pucks),
P = lists:flatten(string:pad(integer_to_list(Pucks rem one_gaju()), 18, leading, $0), P = lists:flatten(string:pad(integer_to_list(Pucks rem one_gaju()), 18, leading, $0)),
Digits = min(Precision, 18), Digits = min(Precision, 18),
T = T =
case length(P) > Digits of case length(P) > Digits of
@ -346,7 +346,30 @@ read(Format) ->
Error -> Error Error -> Error
end. end.
-spec read(Style, Formatted) -> Result 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
end.
count_chars(Format) ->
count_chars(Format, #{}).
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.
-spec read(Style, Format) -> Result
when Style :: us | ch | jp | undefined, when Style :: us | ch | jp | undefined,
Format :: string(), Format :: string(),
Result :: {ok, Pucks} | {error, Reason}, Result :: {ok, Pucks} | {error, Reason},
@ -382,7 +405,7 @@ read2(jp, Format) ->
read2(undefined, Format) -> read2(undefined, Format) ->
read(Format). read(Format).
read_western(Break, [$-, Rest]) -> read_western(Break, [$-, Format]) ->
case read_western2(Break, Format) of case read_western2(Break, Format) of
{ok, Pucks} -> {ok, Pucks * -1}; {ok, Pucks} -> {ok, Pucks * -1};
Error -> Error Error -> Error
@ -448,7 +471,14 @@ segment_jp(Format) ->
case string:split(Format, [gaju_mark()], all) of case string:split(Format, [gaju_mark()], all) of
[Gajus, Pucks] -> [Gajus, Pucks] ->
case read_segment(Gajus) of case read_segment(Gajus) of
{ok, {ok, GajuSegments} ->
case read_segment(Pucks) of
{ok, PuckSegments} -> {ok, GajuSegments, PuckSegments};
Error -> Error
end;
Error ->
Error
end;
[Gajus] -> [Gajus] ->
case read_segment(Gajus) of case read_segment(Gajus) of
{ok, GajuSegments} -> {ok, GajuSegments, ["0"]}; {ok, GajuSegments} -> {ok, GajuSegments, ["0"]};
@ -460,6 +490,8 @@ segment_jp(Format) ->
{error, format} {error, format}
end. end.
read_segment(
assemble_jp({GajuSegments, PuckSegments}) -> assemble_jp({GajuSegments, PuckSegments}) ->
GajuString = lists:flatten(lists:map(fun expand_jp_myriad/1, GajuSegments)), GajuString = lists:flatten(lists:map(fun expand_jp_myriad/1, GajuSegments)),
PuckString = lists:flatten(lists:map(fun expand_jp_myriad/1, PuckDegments)), PuckString = lists:flatten(lists:map(fun expand_jp_myriad/1, PuckDegments)),
@ -472,9 +504,9 @@ expand_jp_myriad(String) ->
string:pad(String, 4, leading, $0). string:pad(String, 4, leading, $0).
lower_jp_numchar(C) when $ =< C andalso C =< $ -> hw_jp_numchar(C) when $ =< C andalso C =< $ ->
C - $; C - $;
lower_jp_numchar(C) -> hw_jp_numchar(C) ->
C. C.