Browse Source

ft: 一些新功能

master
lijie 1 year ago
parent
commit
bf3cd5a8ae
2 changed files with 79 additions and 2 deletions
  1. +55
    -0
      src/compile/utBeamToSrc.erl
  2. +24
    -2
      src/template/utEtsFold.erl

+ 55
- 0
src/compile/utBeamToSrc.erl View File

@ -5,6 +5,8 @@
, genSrcs/2
, notCan/2
, reToDir/2
, delFile/2
, isHasUnicode/1
]).
%% beam生成erl文件beam编译选项必要带debug_info才能反编译生成代码
@ -46,6 +48,59 @@ genSrcs(BeamDir, SrcDir) ->
end,
filelib:fold_files(BeamDir, "\\.beam$", true, FunDeal, []).
%% -file
delFile(SrcDir, NewDir) ->
FunDeal =
fun(File, ProAcc) ->
ModName = filename:basename(File, ".erl"),
Module = list_to_atom(ModName),
{ok, CodeBin} = file:read_file(File),
SrcBin = doDelFile(CodeBin, <<>>),
file:write_file(lists:concat([NewDir, Module, ".erl"]), SrcBin),
io:format("build beam:~p to erl:~p success.~n", [Module, Module]),
ProAcc
end,
filelib:fold_files(SrcDir, "\\.erl$", true, FunDeal, []).
doDelFile(CodeBin, SrcBin) ->
case binary:split(CodeBin, <<"-file(">>) of
[Part1] ->
<<SrcBin/binary, Part1/binary>>;
[Part1, Part2] ->
[_, LeftPart] = binary:split(Part2, <<").">>),
delFile(LeftPart, <<SrcBin/binary, Part1/binary>>)
end.
%% unicode字符
isHasUnicode(SrcDir) ->
FunDeal =
fun(File, ProAcc) ->
ModName = filename:basename(File, ".erl"),
Module = list_to_atom(ModName),
{ok, CodeBin} = file:read_file(File),
IsHas = checkUnicode(CodeBin),
case IsHas of
true ->
[Module | ProAcc];
_ ->
ProAcc
end
end,
AllMods = filelib:fold_files(SrcDir, "\\.erl$", true, FunDeal, []),
ModStr = <<<<(atom_to_binary(OneMod))/binary, "\n">> || OneMod <- AllMods>>,
file:write_file("hasUnicodeMod.txt", ModStr).
checkUnicode(<<>>) ->
false;
checkUnicode(<<Word/utf8, Left/binary>>) ->
case Word > 256 of
true ->
true;
_ ->
checkUnicode(Left)
end.
%% beam文件复制到指定的目录
notCan(BeamDir, SrcDir) ->
FunDeal =

+ 24
- 2
src/template/utEtsFold.erl View File

@ -4,7 +4,7 @@
-compile([export_all]).
-export([rank/5, fold/2]).
-export([rank/5, fold/2, fold/3]).
rank(TabId, PageInfo, Page, Limit, TotalCnt) ->
%% tab
@ -64,7 +64,6 @@ makeRankData([{CurKey, Uuid} | AllyIds], LastKey, Acc) ->
makeRankData(AllyIds, LastKey, Acc)
end.
fold(TabId, Limit) ->
ets:safe_fixtable(TabId, true),
try
@ -92,6 +91,29 @@ matchDoFun(_ValueList) ->
%% do something
ok.
fold(TabId, Limit, Fun) ->
ets:safe_fixtable(TabId, true),
try
case ets:match_object(TabId, '$1', Limit) of
'$end_of_table' ->
ok;
{ValueList, NextKey} ->
Fun(ValueList),
continueNext(NextKey, Fun)
end
after
ets:safe_fixtable(TabId, false)
end.
continueNext(NextKey, Fun) ->
case ets:match_object(NextKey) of
'$end_of_table' ->
ok;
{ValueList, NewNextKey} ->
Fun(ValueList),
continueNext(NewNextKey, Fun)
end.
new() ->
ets:new(test_replace, [named_table, public]),
[ets:insert(test_replace, {One, #{ One => One}}) || One <- lists:seq(1, 100000)],

Loading…
Cancel
Save