From 97ab480df55cf574ab42a87b6927ef5bba83000e Mon Sep 17 00:00:00 2001 From: Patrik Nyblom Date: Mon, 10 May 2010 16:27:58 +0200 Subject: Add documentation for binary module Correct behaviour of copy/2 witn 0 copies. --- erts/doc/src/erlang.xml | 53 +++++++++++++++++++++++++++++++++++++ erts/emulator/beam/erl_bif_binary.c | 3 ++- 2 files changed, 55 insertions(+), 1 deletion(-) (limited to 'erts') diff --git a/erts/doc/src/erlang.xml b/erts/doc/src/erlang.xml index cd9bb85f5c..e90160dfd7 100644 --- a/erts/doc/src/erlang.xml +++ b/erts/doc/src/erlang.xml @@ -252,6 +252,54 @@ iolist() = [char() | binary() | iolist()] "Erlang" + + binary_part(Subject, PosLen) -> binary() + Extracts a part of a binary + + Subject = binary() + PosLen = {Start,Length} + Start = int() + Length = int() + + +

Extracts the part of the binary described by PosLen.

+ +

Negative length can be used to extract bytes at the end of a binary:

+ + +1> Bin = <<1,2,3,4,5,6,7,8,9,10>>. +2> binary_part(Bin,{byte_size(Bin), -5)). +<<6,7,8,9,10>> + + +

If PosLen in any way references outside the binary, a badarg exception is raised.

+ +

Start is zero-based, i.e:

+ +1> Bin = <<1,2,3>> +2> binary_part(Bin,{0,2}). +<<1,2>> + + +

See the STDLIB module binary for details about the PosLen semantics.

+ +

Allowed in guard tests.

+
+
+ + binary_part(Subject, Start, Length) -> binary() + Extracts a part of a binary + + Subject = binary() + Start = int() + Length = int() + + +

The same as binary_part(Subject, {Pos, Len}).

+ +

Allowed in guard tests.

+
+
binary_to_atom(Binary, Encoding) -> atom() Convert from text representation to an atom @@ -318,6 +366,11 @@ iolist() = [char() | binary() | iolist()] corresponding to the bytes from position Start to position Stop in Binary. Positions in the binary are numbered starting from 1.

+ +

This functions indexing style of using one-based indices for + binaries is deprecated. New code should use the functions in + the STDLIB module binary instead. They consequently + use the same (zero-based) style of indexing.

diff --git a/erts/emulator/beam/erl_bif_binary.c b/erts/emulator/beam/erl_bif_binary.c index 0a40e28474..3e8480324c 100644 --- a/erts/emulator/beam/erl_bif_binary.c +++ b/erts/emulator/beam/erl_bif_binary.c @@ -2369,7 +2369,8 @@ static BIF_RETTYPE do_binary_copy(Process *p, Eterm bin, Eterm en) goto badarg; } if (!n) { - goto badarg; + Eterm res_term = erts_new_heap_binary(p,NULL,0,&bytes); + BIF_RET(res_term); } ERTS_GET_BINARY_BYTES(bin,bytes,bit_offs,bit_size); if (bit_size != 0) { -- cgit v1.2.3