Permalink
Browse files

simple binary key encoding util functions

  • Loading branch information...
1 parent 0483d90 commit 6eeb59a5a319107267d2ca49a80d6bc8b791ab65 @japerk committed May 23, 2010
Showing with 8 additions and 2 deletions.
  1. +8 −2 src/erldis_binaries.erl
View
@@ -1,6 +1,6 @@
-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_atom(X) -> list_to_binary(atom_to_list(X));
@@ -11,6 +11,8 @@ to_binary(X) -> term_to_binary(X).
join([], _)->
<<>>;
+join(Array, Sep) when not is_binary(Sep) ->
+ join(Array, to_binary(Sep));
join(Array, Sep)->
F = fun(Elem, Acc) ->
E2 = to_binary(Elem),
@@ -19,4 +21,8 @@ join(Array, Sep)->
Sz = size(Sep),
<<_: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.