aboutsummaryrefslogtreecommitdiff
path: root/src/str.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/str.erl')
-rw-r--r--src/str.erl40
1 files changed, 34 insertions, 6 deletions
diff --git a/src/str.erl b/src/str.erl
index 27d21075a..16f866972 100644
--- a/src/str.erl
+++ b/src/str.erl
@@ -5,7 +5,7 @@
%%% Created : 23 Feb 2012 by Evgeniy Khramtsov <ekhramtsov@process-one.net>
%%%
%%%
-%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
+%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
@@ -64,7 +64,10 @@
to_float/1,
prefix/2,
suffix/2,
- to_integer/1]).
+ format/2,
+ to_integer/1,
+ sha/1,
+ to_hexlist/1]).
%%%===================================================================
%%% API
@@ -92,7 +95,10 @@ rchr(B, C) ->
-spec str(binary(), binary()) -> non_neg_integer().
str(B1, B2) ->
- string:str(binary_to_list(B1), binary_to_list(B2)).
+ case binary:match(B1, B2) of
+ {R, _Len} -> R+1;
+ _ -> 0
+ end.
-spec rstr(binary(), binary()) -> non_neg_integer().
@@ -112,7 +118,7 @@ cspan(B1, B2) ->
-spec copies(binary(), non_neg_integer()) -> binary().
copies(B, N) ->
- iolist_to_binary(string:copies(binary_to_list(B), N)).
+ binary:copy(B, N).
-spec words(binary()) -> pos_integer().
@@ -200,7 +206,7 @@ join(L, Sep) ->
-spec substr(binary(), pos_integer()) -> binary().
substr(B, N) ->
- iolist_to_binary(string:substr(binary_to_list(B), N)).
+ binary_part(B, N-1, byte_size(B)-N+1).
-spec chr(binary(), char()) -> non_neg_integer().
@@ -220,7 +226,7 @@ chars(C, N) ->
-spec substr(binary(), pos_integer(), non_neg_integer()) -> binary().
substr(B, S, E) ->
- iolist_to_binary(string:substr(binary_to_list(B), S, E)).
+ binary_part(B, S-1, E).
-spec strip(binary(), both | left | right, char()) -> binary().
@@ -277,6 +283,25 @@ prefix(Prefix, B) ->
suffix(B1, B2) ->
lists:suffix(binary_to_list(B1), binary_to_list(B2)).
+-spec format(io:format(), list()) -> binary().
+
+format(Format, Args) ->
+ unicode:characters_to_binary(io_lib:format(Format, Args)).
+
+
+-spec sha(iodata()) -> binary().
+
+sha(Text) ->
+ Bin = crypto:hash(sha, Text),
+ to_hexlist(Bin).
+
+-spec to_hexlist(binary()) -> binary().
+
+to_hexlist(S) when is_list(S) ->
+ to_hexlist(iolist_to_binary(S));
+to_hexlist(Bin) when is_binary(Bin) ->
+ << <<(digit_to_xchar(N div 16)), (digit_to_xchar(N rem 16))>> || <<N>> <= Bin >>.
+
%%%===================================================================
%%% Internal functions
%%%===================================================================
@@ -284,3 +309,6 @@ join_s([], _Sep) ->
[];
join_s([H|T], Sep) ->
[H, [[Sep, X] || X <- T]].
+
+digit_to_xchar(D) when (D >= 0) and (D < 10) -> D + $0;
+digit_to_xchar(D) -> D + $a - 10.