Skip to content

Commit

Permalink
simple binary key encoding util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
japerk committed May 23, 2010
1 parent 0483d90 commit 6eeb59a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/erldis_binaries.erl
@@ -1,6 +1,6 @@
-module(erldis_binaries). -module(erldis_binaries).


-export([to_binary/1, join/2]). -export([to_binary/1, join/2, encode_key/1, encode_key_parts/1]).


to_binary(X) when is_list(X) -> list_to_binary(X); to_binary(X) when is_list(X) -> list_to_binary(X);
to_binary(X) when is_atom(X) -> list_to_binary(atom_to_list(X)); to_binary(X) when is_atom(X) -> list_to_binary(atom_to_list(X));
Expand All @@ -11,6 +11,8 @@ to_binary(X) -> term_to_binary(X).


join([], _)-> join([], _)->
<<>>; <<>>;
join(Array, Sep) when not is_binary(Sep) ->
join(Array, to_binary(Sep));
join(Array, Sep)-> join(Array, Sep)->
F = fun(Elem, Acc) -> F = fun(Elem, Acc) ->
E2 = to_binary(Elem), E2 = to_binary(Elem),
Expand All @@ -19,4 +21,8 @@ join(Array, Sep)->


Sz = size(Sep), Sz = size(Sep),
<<_:Sz/bytes, Result/binary>> = lists:foldl(F, <<>>, Array), <<_:Sz/bytes, Result/binary>> = lists:foldl(F, <<>>, Array),
Result. Result.

encode_key(Key) -> re:replace(Key, <<" ">>, <<"_">>, [{return, binary}]).

encode_key_parts(Parts) -> encode_key(join(Parts, <<":">>)).

0 comments on commit 6eeb59a

Please sign in to comment.