summaryrefslogtreecommitdiff
path: root/src/ejabberd_regexp.erl
diff options
context:
space:
mode:
authorPaweł Chmielowski <pchmielowski@process-one.net>2018-01-15 10:31:06 +0100
committerPaweł Chmielowski <pchmielowski@process-one.net>2018-01-15 10:31:26 +0100
commit3df78d3a8f423fa8788a23780fff829e740a2d3b (patch)
treea8e81571ad0ef287531b7249e083adfcb99312a0 /src/ejabberd_regexp.erl
parentMerge pull request #2221 from licaon-kter/patch-1 (diff)
Copy sh_to_ask function from xmerl_regexp.erl
This way we don't need to include xmerl application in our docker container
Diffstat (limited to 'src/ejabberd_regexp.erl')
-rw-r--r--src/ejabberd_regexp.erl66
1 files changed, 60 insertions, 6 deletions
diff --git a/src/ejabberd_regexp.erl b/src/ejabberd_regexp.erl
index fdbf08f5..12564e71 100644
--- a/src/ejabberd_regexp.erl
+++ b/src/ejabberd_regexp.erl
@@ -85,11 +85,65 @@ greplace(String, Regexp, New) ->
A -> A
end.
+
+%% This code was copied and adapted from xmerl_regexp.erl
+
-spec sh_to_awk(binary()) -> binary().
+sh_to_awk(Sh) ->
+ iolist_to_binary([<<"^(">>, sh_to_awk_1(Sh)]). %Fix the beginning
+
+sh_to_awk_1(<<"*", Sh/binary>>) -> %This matches any string
+ [<<".*">>, sh_to_awk_1(Sh)];
+sh_to_awk_1(<<"?", Sh/binary>>) -> %This matches any character
+ [$., sh_to_awk_1(Sh)];
+sh_to_awk_1(<<"[^]", Sh/binary>>) -> %This takes careful handling
+ [<<"\\^">>, sh_to_awk_1(Sh)];
+%% Must move '^' to end.
+sh_to_awk_1(<<"[^", Sh/binary>>) ->
+ [$[, sh_to_awk_2(Sh, true)];
+sh_to_awk_1(<<"[!", Sh/binary>>) ->
+ [<<"[^">>, sh_to_awk_2(Sh, false)];
+sh_to_awk_1(<<"[", Sh/binary>>) ->
+ [$[, sh_to_awk_2(Sh, false)];
+sh_to_awk_1(<<C:8, Sh/binary>>) -> %% Unspecialise everything else which is not an escape character.
+ case sh_special_char(C) of
+ true -> [$\\,C|sh_to_awk_1(Sh)];
+ false -> [C|sh_to_awk_1(Sh)]
+ end;
+sh_to_awk_1(<<>>) ->
+ <<")$">>. %Fix the end
+
+sh_to_awk_2(<<"]", Sh/binary>>, UpArrow) ->
+ [$]|sh_to_awk_3(Sh, UpArrow)];
+sh_to_awk_2(Sh, UpArrow) ->
+ sh_to_awk_3(Sh, UpArrow).
+
+sh_to_awk_3(<<"]", Sh/binary>>, true) ->
+ [<<"^]">>, sh_to_awk_1(Sh)];
+sh_to_awk_3(<<"]", Sh/binary>>, false) ->
+ [$]|sh_to_awk_1(Sh)];
+sh_to_awk_3(<<C:8, Sh/binary>>, UpArrow) ->
+ [C|sh_to_awk_3(Sh, UpArrow)];
+sh_to_awk_3(<<>>, true) ->
+ [$^|sh_to_awk_1([])];
+sh_to_awk_3(<<>>, false) ->
+ sh_to_awk_1([]).
+
+%% -type sh_special_char(char()) -> bool().
+%% Test if a character is a special character.
+
+sh_special_char($|) -> true;
+sh_special_char($*) -> true;
+sh_special_char($+) -> true;
+sh_special_char($?) -> true;
+sh_special_char($() -> true;
+sh_special_char($)) -> true;
+sh_special_char($\\) -> true;
+sh_special_char($^) -> true;
+sh_special_char($$) -> true;
+sh_special_char($.) -> true;
+sh_special_char($[) -> true;
+sh_special_char($]) -> true;
+sh_special_char($") -> true;
+sh_special_char(_C) -> false.
-sh_to_awk(ShRegExp) ->
- case exec({xmerl_regexp, sh_to_awk, [binary_to_list(ShRegExp)]},
- {regexp, sh_to_awk, [binary_to_list(ShRegExp)]})
- of
- A -> iolist_to_binary(A)
- end.