Binary matching
Matthias Lang
matthias@REDACTED
Mon Sep 30 08:55:01 CEST 2002
Jay Nelson writes:
> There is no documented way to print a binary as string
> which makes things a bit difficult to review.
A string _is_ a list in Erlang. So binary_to_list will get you a
"string" if the binary concerned is a string.
> Eshell V5.1.2 (abort with ^G)
> 1> Bin = <<"just a test">>.
> <<106,117,115,116,32,97,32,116,101,115,116>>
> 2> <<W1:32/binary, Rest/binary>> = Bin.
> ** exited: {{badmatch,<<106,117,115,116,32,97,32,116,101,115,116>>},
> [{erl_eval,expr,3}]} **
> OK, I can stick a string in a binary but I can't split it in two
> by specifying a binary length and then getting the rest,
The width of 'binary' types in a binary match is measured in bytes,
not bits, you're getting the badmatch above because the binary you're
matching isn't 32 bytes long:
1> Bin = <<"just a test">>.
<<106,117,115,116,32,97,32,116,101,115,116>>
2> <<W1:4/binary, Rest/binary>> = Bin.
<<106,117,115,116,32,97,32,116,101,115,116>>
3> W1.
<<106,117,115,116>>
> I want to write:
>
> breakLines(Binary) -> lines(Binary, []).
> lines(<<>>, Acc) -> lists:reverse(Acc).
> lines(<<Line/binary, 13, 10, Rest/binary>>, Acc) ->
> lines(Rest, [Line | Acc]).
> Seems straightforward and not too difficult for the pattern-matcher,
> but then I'm no compiler writer.
The binary syntax was a fairly radical change when introduced and the
implementation was deliberately conservative, leaving out some of the
features which were discussed earlier, such as your example.
The best way to split socket input into lines is to let the driver do
it by specifing {packet, line} in the socket options.
> Am I too worried about efficiency?
Probably. Converting all string-related code to binary manipulations
probably won't make your code go faster. Judicious use of binaries
probably will.
> Should I forget about binaries?
No.
Binaries are fantastically useful for encoding and decoding
bit-oriented protocols and files. They can provide nice speedups in
many situations, especially when IO is involved.
> I can't even figure out how to determine the length of
> the binary without converting it to a list.
size(Binary)
Matthias
More information about the erlang-questions
mailing list