我现在正在写 hg(mercurial) 的客户。对于标签命令,响应如下:
<<"添加一个\na\n提交的变更集 0:44108598f0ec643e7d90e9f18a2b6740401a510a\ntip
1:ce4daf41b6ae\nmy tags
0:44108598f0ec\ntest tag 0:44108598f0ec local\n">>.
python的相关代码如下:
t = []
for line in out.splitlines():
taglocal = line.endswith(' local')
if taglocal:
line = line[:-6]
name, rev = line.rsplit(' ', 1)
rev, node = rev.split(':')
t.append((name.rstrip(), int(rev), node, taglocal))
return t
我必须检查每一行是否有“本地”后缀,但编译器会给出语法错误。如何正确和优雅地写作。错误信息:
src/emercurial_client.erl:763: illegal pattern
代码
process_tags(List)->
process_tags(List,[]).
process_tags([],Result)->
lists:reverse(Result);
process_tags([Line|Rest],Result) ->
B = binary_to_list(Line),
A = process_tags_line(B),
process_tags(Rest,[A|Result]).
process_tags_line(New_list ++"local")-> %%<-----error here
process_tags_line(New_list);
process_tags_line(New_list)->
%% case List of
%% Data ++ " local" -> %%<-----also match error
%% New_list = Data;
%% _ ->
%% New_list = List
%% end,
[Name,Part2] = string:tokens(Data," "),
[Rev,Node] = string:tokens(Part2,":"),
{trim(Name),love_misc:to_integer(Rev),
node,New_list}.
修改后如下:
process_tags(List)->
List_b = binary:split(List,<<$\n>>,[global]),
Result = process_tags(List_b,[]),
%% error_logger:info_report([client_process_tags,Result]),
Result.
process_tags([],Result)->
lists:reverse(Result);
process_tags([<<>>],Result)->
lists:reverse(Result);
process_tags([Line|Rest],Result) ->
B = binary_to_list(Line),
A = process_tags_line(B),
process_tags(Rest,[A|Result]).
process_tags_line(List) ->
%% error_logger:info_report([client_tags_line_1,List]),
case lists:suffix(" local",List) of
true ->
New_list = lists:sublist(List,1,length(List)-7);
_ ->
New_list = List
end,
{Name,Part2} = rsplit(New_list,$\s),
{Rev,Node} =
rsplit(Part2,$:),
Rev_a = string:substr(Rev,1,length(Rev)-1),
{love_misc:trim(Name),love_misc:to_integer(Rev_a),Node,New_list}.
rsplit(A,Char)->
Index = string:rchr(A,Char),
lists:split(Index,A).