24
1
mirror of https://github.com/processone/ejabberd.git synced 2024-06-14 22:00:16 +02:00

Replace calls from 'regexp' to the OTP R12 new module 're' (EJAB-921)

SVN Revision: 2582
This commit is contained in:
Badlop 2009-09-02 14:26:01 +00:00
parent 330a4c9452
commit 29b2da42f5
11 changed files with 67 additions and 60 deletions

View File

@ -281,14 +281,14 @@ build_additional_translators(List) ->
List). List).
print_translation(File, Line, Str, StrT) -> print_translation(File, Line, Str, StrT) ->
{ok, StrQ, _} = regexp:gsub(Str, "\"", "\\\""), StrQ = re:replace(Str, "\"", "\\\"", [global, {return, list}]),
{ok, StrTQ, _} = regexp:gsub(StrT, "\"", "\\\""), StrTQ = re:replace(StrT, "\"", "\\\"", [global, {return, list}]),
io:format("#: ~s:~p~nmsgid \"~s\"~nmsgstr \"~s\"~n~n", [File, Line, StrQ, StrTQ]). io:format("#: ~s:~p~nmsgid \"~s\"~nmsgstr \"~s\"~n~n", [File, Line, StrQ, StrTQ]).
print_translation_obsolete(Str, StrT) -> print_translation_obsolete(Str, StrT) ->
File = "unknown.erl", File = "unknown.erl",
Line = 1, Line = 1,
{ok, StrQ, _} = regexp:gsub(Str, "\"", "\\\""), StrQ = re:replace(Str, "\"", "\\\"", [global, {return, list}]),
{ok, StrTQ, _} = regexp:gsub(StrT, "\"", "\\\""), StrTQ = re:replace(StrT, "\"", "\\\"", [global, {return, list}]),
io:format("#: ~s:~p~n#~~ msgid \"~s\"~n#~~ msgstr \"~s\"~n~n", [File, Line, StrQ, StrTQ]). io:format("#: ~s:~p~n#~~ msgid \"~s\"~n#~~ msgstr \"~s\"~n~n", [File, Line, StrQ, StrTQ]).

View File

@ -301,18 +301,19 @@ match_acl(ACLName, JID, Host) ->
%% String = string() | undefined %% String = string() | undefined
%% RegExp = string() %% RegExp = string()
is_regexp_match(undefined, RegExp) -> is_regexp_match(undefined, _RegExp) ->
false; false;
is_regexp_match(String, RegExp) -> is_regexp_match(String, RegExp) ->
case regexp:first_match(String, RegExp) of try re:run(String, RegExp, [{capture, none}]) of
nomatch -> nomatch ->
false; false;
{match, _, _} -> match ->
true; true
{error, ErrDesc} -> catch
_:ErrDesc ->
?ERROR_MSG( ?ERROR_MSG(
"Wrong regexp ~p in ACL: ~p", "Wrong regexp ~p in ACL:~n~p",
[RegExp, lists:flatten(regexp:format_error(ErrDesc))]), [RegExp, ErrDesc]),
false false
end. end.
@ -321,6 +322,6 @@ is_regexp_match(String, RegExp) ->
%% Glob = string() %% Glob = string()
is_glob_match(String, Glob) -> is_glob_match(String, Glob) ->
is_regexp_match(String, regexp:sh_to_awk(Glob)). is_regexp_match(String, xmerl_regexp:sh_to_awk(Glob)).

View File

@ -277,7 +277,7 @@ try_call_command(Args, Auth, AccessCommands) ->
%% @spec (Args::[string()], Auth, AccessCommands) -> string() | integer() | {string(), integer()} | {error, ErrorType} %% @spec (Args::[string()], Auth, AccessCommands) -> string() | integer() | {string(), integer()} | {error, ErrorType}
call_command([CmdString | Args], Auth, AccessCommands) -> call_command([CmdString | Args], Auth, AccessCommands) ->
{ok, CmdStringU, _} = regexp:gsub(CmdString, "-", "_"), CmdStringU = re:replace(CmdString, "-", "_", [global,{return,list}]),
Command = list_to_atom(CmdStringU), Command = list_to_atom(CmdStringU),
case ejabberd_commands:get_command_format(Command) of case ejabberd_commands:get_command_format(Command) of
{error, command_unknown} -> {error, command_unknown} ->
@ -673,13 +673,13 @@ filter_commands(All, SubString) ->
end. end.
filter_commands_regexp(All, Glob) -> filter_commands_regexp(All, Glob) ->
RegExp = regexp:sh_to_awk(Glob), RegExp = xmerl_regexp:sh_to_awk(Glob),
lists:filter( lists:filter(
fun(Command) -> fun(Command) ->
case regexp:first_match(Command, RegExp) of case re:run(Command, RegExp, [{capture, none}]) of
{match, _, _} -> match ->
true; true;
_ -> nomatch ->
false false
end end
end, end,

View File

@ -735,11 +735,11 @@ match_labels([DL | DLabels], [PL | PLabels]) ->
orelse (C == $-) orelse (C == $*) orelse (C == $-) orelse (C == $*)
end, PL) of end, PL) of
true -> true ->
Regexp = regexp:sh_to_awk(PL), Regexp = xmerl_regexp:sh_to_awk(PL),
case regexp:match(DL, Regexp) of case re:run(DL, Regexp, [{capture, none}]) of
{match, _, _} -> match ->
match_labels(DLabels, PLabels); match_labels(DLabels, PLabels);
_ -> nomatch ->
false false
end; end;
false -> false ->

View File

@ -182,7 +182,7 @@ parse_attr(less, {Name, Value}, ListOfSubValues) ->
eldap:lessOrEqual(Name, NewValue); eldap:lessOrEqual(Name, NewValue);
parse_attr(equal, {Name, Value}, ListOfSubValues) -> parse_attr(equal, {Name, Value}, ListOfSubValues) ->
{ok, RegSList} = regexp:split(remove_extra_asterisks(Value), "[*]"), RegSList = re:split(remove_extra_asterisks(Value), "[*]", [{return, list}]),
Pattern = case [do_sub(X, ListOfSubValues) || X <- RegSList] of Pattern = case [do_sub(X, ListOfSubValues) || X <- RegSList] of
[Head | Tail] when Tail /= [] -> [Head | Tail] when Tail /= [] ->
{Head, lists:sublist(Tail, length(Tail)-1), lists:last(Tail)}; {Head, lists:sublist(Tail, length(Tail)-1), lists:last(Tail)};
@ -228,14 +228,15 @@ do_sub(S, [{RegExp, New, Times} | T]) ->
do_sub(Result, T). do_sub(Result, T).
do_sub(S, {RegExp, New}, Iter) -> do_sub(S, {RegExp, New}, Iter) ->
case regexp:sub(S, RegExp, New) of try re:replace(S, RegExp, New, [{return, list}]) of
{ok, NewS, 0} -> NewS when NewS == S ->
NewS; NewS;
{ok, NewS, _} when Iter =< ?MAX_RECURSION -> NewS when Iter =< ?MAX_RECURSION ->
do_sub(NewS, {RegExp, New}, Iter+1); do_sub(NewS, {RegExp, New}, Iter+1);
{ok, _, _} when Iter > ?MAX_RECURSION -> _ when Iter > ?MAX_RECURSION ->
throw({regexp, max_substitute_recursion}); throw({regexp, max_substitute_recursion})
_ -> catch
_:_ ->
throw({regexp, bad_regexp}) throw({regexp, bad_regexp})
end; end;
@ -243,14 +244,15 @@ do_sub(S, {_, _, N}, _) when N<1 ->
S; S;
do_sub(S, {RegExp, New, Times}, Iter) -> do_sub(S, {RegExp, New, Times}, Iter) ->
case regexp:sub(S, RegExp, New) of try re:replace(S, RegExp, New, [{return, list}]) of
{ok, NewS, 0} -> NewS when NewS == S ->
NewS; NewS;
{ok, NewS, _} when Iter < Times -> NewS when Iter < Times ->
do_sub(NewS, {RegExp, New, Times}, Iter+1); do_sub(NewS, {RegExp, New, Times}, Iter+1);
{ok, NewS, _} -> NewS ->
NewS; NewS
_ -> catch
_:_ ->
throw({regexp, bad_regexp}) throw({regexp, bad_regexp})
end. end.

View File

@ -89,8 +89,8 @@ get_user_part(String, Pattern) ->
{'EXIT', _} -> {'EXIT', _} ->
{error, badmatch}; {error, badmatch};
Result -> Result ->
case regexp:sub(Pattern, "%u", Result) of case re:replace(Pattern, "%u", Result, [{return, list}]) of
{ok, String, _} -> {ok, Result}; String -> {ok, Result};
_ -> {error, badmatch} _ -> {error, badmatch}
end end
end. end.

View File

@ -173,11 +173,11 @@ get_module_opt(Host, Module, Opt, Default) ->
get_module_opt_host(Host, Module, Default) -> get_module_opt_host(Host, Module, Default) ->
Val = get_module_opt(Host, Module, host, Default), Val = get_module_opt(Host, Module, host, Default),
element(2, regexp:gsub(Val, "@HOST@", Host)). re:replace(Val, "@HOST@", Host, [global,{return,list}]).
get_opt_host(Host, Opts, Default) -> get_opt_host(Host, Opts, Default) ->
Val = get_opt(host, Opts, Default), Val = get_opt(host, Opts, Default),
element(2, regexp:gsub(Val, "@HOST@", Host)). re:replace(Val, "@HOST@", Host, [global,{return,list}]).
loaded_modules(Host) -> loaded_modules(Host) ->
ets:select(ejabberd_modules, ets:select(ejabberd_modules,

View File

@ -524,7 +524,7 @@ get_local_items({_, Host}, ["all users", [$@ | Diap]], _Server, _Lang) ->
Users -> Users ->
SUsers = lists:sort([{S, U} || {U, S} <- Users]), SUsers = lists:sort([{S, U} || {U, S} <- Users]),
case catch begin case catch begin
{ok, [S1, S2]} = regexp:split(Diap, "-"), [S1, S2] = re:split(Diap, "-", [{return, list}]),
N1 = list_to_integer(S1), N1 = list_to_integer(S1),
N2 = list_to_integer(S2), N2 = list_to_integer(S2),
Sub = lists:sublist(SUsers, N1, N2 - N1 + 1), Sub = lists:sublist(SUsers, N1, N2 - N1 + 1),

View File

@ -415,11 +415,11 @@ add_message_to_log(Nick1, Message, RoomJID, Opts, State) ->
io_lib:format("<font class=\"msc\">~s~s~s</font><br/>", io_lib:format("<font class=\"msc\">~s~s~s</font><br/>",
[Nick, ?T(" has set the subject to: "), htmlize(T,NoFollow,FileFormat)]); [Nick, ?T(" has set the subject to: "), htmlize(T,NoFollow,FileFormat)]);
{body, T} -> {body, T} ->
case {regexp:first_match(T, "^/me\s"), Nick} of case {re:run(T, "^/me\s", [{capture, none}]), Nick} of
{_, ""} -> {_, ""} ->
io_lib:format("<font class=\"msm\">~s</font><br/>", io_lib:format("<font class=\"msm\">~s</font><br/>",
[htmlize(T,NoFollow,FileFormat)]); [htmlize(T,NoFollow,FileFormat)]);
{{match, _, _}, _} -> {match, _} ->
io_lib:format("<font class=\"mne\">~s ~s</font><br/>", io_lib:format("<font class=\"mne\">~s ~s</font><br/>",
[Nick, string:substr(htmlize(T,FileFormat), 5)]); [Nick, string:substr(htmlize(T,FileFormat), 5)]);
{nomatch, _} -> {nomatch, _} ->
@ -664,8 +664,7 @@ fw(F, S, O, FileFormat) ->
html -> html ->
S1; S1;
plaintext -> plaintext ->
{ok, Res, _} = regexp:gsub(S1, "<[^>]*>", ""), re:replace(S1, "<[^>]*>", "", [global,{return,list}])
Res
end, end,
io:format(F, S2, []). io:format(F, S2, []).
@ -794,15 +793,20 @@ htmlize(S1, NoFollow, _FileFormat) ->
S2_list). S2_list).
htmlize2(S1, NoFollow) -> htmlize2(S1, NoFollow) ->
S2 = element(2, regexp:gsub(S1, "\\&", "\\&amp;")), ReplacementRules =
S3 = element(2, regexp:gsub(S2, "<", "\\&lt;")), [{"\\&", "\\&amp;"},
S4 = element(2, regexp:gsub(S3, ">", "\\&gt;")), {"<", "\\&lt;"},
S5 = element(2, regexp:gsub(S4, "((http|https|ftp)://|(mailto|xmpp):)[^] )\'\"}]+", {">", "\\&gt;"},
link_regexp(NoFollow))), {"((http|https|ftp)://|(mailto|xmpp):)[^] )\'\"}]+", link_regexp(NoFollow)},
%% Remove 'right-to-left override' unicode character 0x202e {" ", "\\&nbsp;\\&nbsp;"},
S6 = element(2, regexp:gsub(S5, " ", "\\&nbsp;\\&nbsp;")), {"\\t", "\\&nbsp;\\&nbsp;\\&nbsp;\\&nbsp;"},
S7 = element(2, regexp:gsub(S6, "\\t", "\\&nbsp;\\&nbsp;\\&nbsp;\\&nbsp;")), {[226,128,174], "[RLO]"}], %% Remove 'right-to-left override' unicode character 0x202e
element(2, regexp:gsub(S7, [226,128,174], "[RLO]")). lists:foldl(
fun({RegExp, Replace}, Acc) ->
re:replace(Acc, RegExp, Replace)
end,
S1,
ReplacementRules).
%% Regexp link %% Regexp link
%% Add the nofollow rel attribute when required %% Add the nofollow rel attribute when required

View File

@ -561,8 +561,8 @@ is_user_in_group({_U, S} = US, Group, Host) ->
%% @spec (Host::string(), {User::string(), Server::string()}, Group::string()) -> {atomic, ok} %% @spec (Host::string(), {User::string(), Server::string()}, Group::string()) -> {atomic, ok}
add_user_to_group(Host, US, Group) -> add_user_to_group(Host, US, Group) ->
{LUser, LServer} = US, {LUser, LServer} = US,
case regexp:match(LUser, "^@.+@$") of case re:run(LUser, "^@.+@$", [{capture, none}]) of
{match,_,_} -> match ->
GroupOpts = mod_shared_roster:get_group_opts(Host, Group), GroupOpts = mod_shared_roster:get_group_opts(Host, Group),
AllUsersOpt = AllUsersOpt =
case LUser == "@all@" of case LUser == "@all@" of
@ -593,8 +593,8 @@ push_displayed_to_user(LUser, LServer, Group, Host, Subscription) ->
remove_user_from_group(Host, US, Group) -> remove_user_from_group(Host, US, Group) ->
GroupHost = {Group, Host}, GroupHost = {Group, Host},
{LUser, LServer} = US, {LUser, LServer} = US,
case regexp:match(LUser, "^@.+@$") of case re:run(LUser, "^@.+@$", [{capture, none}]) of
{match,_,_} -> match ->
GroupOpts = mod_shared_roster:get_group_opts(Host, Group), GroupOpts = mod_shared_roster:get_group_opts(Host, Group),
NewGroupOpts = NewGroupOpts =
case LUser of case LUser of
@ -856,7 +856,7 @@ shared_roster_group(Host, Group, Query, Lang) ->
[] []
end ++ [[us_to_list(Member), $\n] || Member <- Members], end ++ [[us_to_list(Member), $\n] || Member <- Members],
FDisplayedGroups = [[DG, $\n] || DG <- DisplayedGroups], FDisplayedGroups = [[DG, $\n] || DG <- DisplayedGroups],
DescNL = length(element(2, regexp:split(Description, "\n"))), DescNL = length(re:split(Description, "\n", [{return, list}])),
FGroup = FGroup =
?XAE("table", [?XMLATTR('class', <<"withtextareas">>)], ?XAE("table", [?XMLATTR('class', <<"withtextareas">>)],
[?XE("tbody", [?XE("tbody",

View File

@ -1232,13 +1232,13 @@ acl_spec_select(ID, Opt) ->
term_to_string(T) -> term_to_string(T) ->
StringParagraph = lists:flatten(io_lib:format("~1000000p", [T])), StringParagraph = lists:flatten(io_lib:format("~1000000p", [T])),
%% Remove from the string all the carriage returns characters %% Remove from the string all the carriage returns characters
{ok, StringLine, _} = regexp:gsub(StringParagraph, "\\n ", ""), StringLine = re:replace(StringParagraph, "\\n ", "", [global,{return,list}]),
StringLine. StringLine.
%% @spec (T::any(), Cols::integer()) -> {NumLines::integer(), Paragraph::string()} %% @spec (T::any(), Cols::integer()) -> {NumLines::integer(), Paragraph::string()}
term_to_paragraph(T, Cols) -> term_to_paragraph(T, Cols) ->
Paragraph = erl_prettypr:format(erl_syntax:abstract(T), [{paper, Cols}]), Paragraph = erl_prettypr:format(erl_syntax:abstract(T), [{paper, Cols}]),
{ok, FieldList} = regexp:split(Paragraph, "\n"), FieldList = re:split(Paragraph, "\n", [{return, list}]),
NumLines = length(FieldList), NumLines = length(FieldList),
{NumLines, Paragraph}. {NumLines, Paragraph}.
@ -1563,7 +1563,7 @@ list_users_parse_query(Query, Host) ->
list_users_in_diapason(Host, Diap, Lang, URLFunc) -> list_users_in_diapason(Host, Diap, Lang, URLFunc) ->
Users = ejabberd_auth:get_vh_registered_users(Host), Users = ejabberd_auth:get_vh_registered_users(Host),
SUsers = lists:sort([{S, U} || {U, S} <- Users]), SUsers = lists:sort([{S, U} || {U, S} <- Users]),
{ok, [S1, S2]} = regexp:split(Diap, "-"), [S1, S2] = re:split(Diap, "-", [{return, list}]),
N1 = list_to_integer(S1), N1 = list_to_integer(S1),
N2 = list_to_integer(S2), N2 = list_to_integer(S2),
Sub = lists:sublist(SUsers, N1, N2 - N1 + 1), Sub = lists:sublist(SUsers, N1, N2 - N1 + 1),