Skip to content

Instantly share code, notes, and snippets.

@arcusfelis
Created March 8, 2012 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arcusfelis/2002078 to your computer and use it in GitHub Desktop.
Save arcusfelis/2002078 to your computer and use it in GitHub Desktop.
decode_string.erl
-module(decode_string).
-compile(export_all).
%% decode a single null-terminated string
new_decode_string(Bin) ->
Idx = first(Bin, 0),
<<H:Idx/binary, _, T/binary>> = Bin,
{H, T}.
first(<<X, T/binary>>, Idx) ->
case X of
0 -> Idx;
_ -> first(T, Idx+1)
end.
old_decode_string(Bin) ->
decode_string(Bin, <<>>).
decode_string(<<0, Rest/binary>>, Str) ->
{Str, Rest};
decode_string(<<C, Rest/binary>>, Str) ->
decode_string(Rest, <<Str/binary, C>>).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment