From 68d53c01b0b8e9a007a6a30158c19e34b2d2a34e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= This module contains functions for manipulating byte-oriented
- binaries. Although the majority of functions could be implemented
+ binaries. Although the majority of functions could be provided
using bit-syntax, the functions in this library are highly
optimized and are expected to either execute faster or consume
- less memory (or both) than a counterpart written in pure Erlang.
The module is implemented according to the EEP (Erlang Enhancement Proposal) 31.
+The module is provided according to Erlang Enhancement Proposal + (EEP) 31.
- The library handles byte-oriented data. Bitstrings that are not
- binaries (does not contain whole octets of bits) will result in a
The library handles byte-oriented data. For bitstrings that are not
+ binaries (does not contain whole octets of bits) a
Opaque data-type representing a compiled - search-pattern. Guaranteed to be a tuple() to allow programs to - distinguish it from non precompiled search patterns.
+Opaque data type representing a compiled
+ search pattern. Guaranteed to be a
A representaion of a part (or range) in a binary. Start is a
- zero-based offset into a binary() and Length is the length of
- that part. As input to functions in this module, a reverse
+ A representaion of a part (or range) in a binary.
Returns the byte at position
Returns the byte at position
The same as
Same as
Converts
Converts
Example:
-1> binary:bin_to_list(<<"erlang">>,{1,3}).
+1> binary:bin_to_list(<<"erlang">>, {1,3}).
"rla"
-%% or [114,108,97] in list notation.
-
- If
If
The same as
Same as
Builds an internal structure representing a compilation of a
- search-pattern, later to be used in the
When a list of binaries is given, it denotes a set of
- alternative binaries to search for. I.e if
+ search pattern, later to be used in functions
+
When a list of binaries is specified, it denotes a set of
+ alternative binaries to search for. For example, if
The list of binaries used for search alternatives shall be flat and proper.
+ alternatives; when only a single binary is specified, the set has + only one element. The order of alternatives in a pattern is + not significant. -If
The list of binaries used for search alternatives must be flat and + proper.
+If
The same as
Same as
Creates a binary with the content of
Creates a binary with the content of
This function will always create a new binary, even if
This function always creates a new binary, even if
By deliberately copying a single binary to avoid referencing - a larger binary, one might, instead of freeing up the larger + a larger binary, one can, instead of freeing up the larger binary for later garbage collection, create much more binary data than needed. Sharing binary data is usually good. Only in special cases, when small parts reference large binaries and the large binaries are no longer used in any process, deliberate - copying might be a good idea.
If
If
The same as
Same as
Converts the binary digit representation, in big endian or little
+ endian, of a positive integer in
Converts the binary digit representation, in big or little
- endian, of a positive integer in
Example:
+Example:
1> binary:decode_unsigned(<<169,138,199>>,big).
-11111111
-
+11111111
The same as
Same as
Converts a positive integer to the smallest possible - representation in a binary digit representation, either big + representation in a binary digit representation, either big endian or little endian.
-Example:
+Example:
-1> binary:encode_unsigned(11111111,big).
-<<169,138,199>>
-
+1> binary:encode_unsigned(11111111, big).
+<<169,138,199>>
Returns the first byte of the binary
Returns the first byte of binary
Returns the last byte of the binary
Returns the last byte of binary
Works exactly as
Works exactly as
+
Returns the length of the longest common prefix of the
- binaries in the list
Example:
-1> binary:longest_common_prefix([<<"erlang">>,<<"ergonomy">>]).
+1> binary:longest_common_prefix([<<"erlang">>, <<"ergonomy">>]).
2
-2> binary:longest_common_prefix([<<"erlang">>,<<"perl">>]).
-0
-
+2> binary:longest_common_prefix([<<"erlang">>, <<"perl">>]).
+0
- If
If
Returns the length of the longest common suffix of the
- binaries in the list
Example:
-1> binary:longest_common_suffix([<<"erlang">>,<<"fang">>]).
+1> binary:longest_common_suffix([<<"erlang">>, <<"fang">>]).
3
-2> binary:longest_common_suffix([<<"erlang">>,<<"perl">>]).
-0
-
-
- If
If
The same as
Same as
Searches for the first occurrence of
Searches for the first occurrence of
The function returns
The function will return
Example:
-1> binary:match(<<"abcde">>, [<<"bcde">>,<<"cd">>],[]).
-{1,4}
-
+1> binary:match(<<"abcde">>, [<<"bcde">>, <<"cd">>],[]).
+{1,4}
Even though Only the given part is searched. Return values still have
- offsets from the beginning of Only the specified part is searched. Return values still have
+ offsets from the beginning of
If none of the strings in
-
If none of the strings in
For a description of
For a description of
If
If
The same as
Same as
Works like
As
The first and longest match is preferred to a shorter, which is illustrated by the following example:
@@ -367,76 +396,84 @@
1> binary:matches(<<"abcde">>,
[<<"bcde">>,<<"bc">>,<<"de">>],[]).
-[{1,4}]
-
-
- The result shows that <<"bcde">> is selected instead of the - shorter match <<"bc">> (which would have given raise to one - more match,<<"de">>). This corresponds to the behavior of posix - regular expressions (and programs like awk), but is not - consistent with alternative matches in re (and Perl), where +[{1,4}] + +
The result shows that <<"bcde">> is selected instead of
+ the shorter match <<"bc">> (which would have given raise to
+ one more match, <<"de">>).
+ This corresponds to the behavior of
+ POSIX regular expressions (and programs like awk), but is not
+ consistent with alternative matches in
If none of the strings in pattern is found, an empty list is returned.
- -For a description of
If none of the strings in a pattern is found, an empty list is + returned.
-If
For a description of
If
Extracts the part of binary
Extracts the part of the binary
Negative length can be used to extract bytes at the end of a binary:
+A 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>>
-
+2> binary:part(Bin, {byte_size(Bin), -5}).
+<<6,7,8,9,10>>
If
If
The same as
Same as
If a binary references a larger binary (often described as
+ being a subbinary), it can be useful to get the size of the
+ referenced binary. This function can be used in a program to trigger the
+ use of
If a binary references a larger binary (often described as
- being a sub-binary), it can be useful to get the size of the
- actual referenced binary. This function can be used in a program
- to trigger the use of
Example:
+Example:
store(Binary, GBSet) ->
@@ -447,26 +484,24 @@ store(Binary, GBSet) ->
_ ->
Binary
end,
- gb_sets:insert(NewBin,GBSet).
-
+ gb_sets:insert(NewBin,GBSet).
In this example, we chose to copy the binary content before
- inserting it in the
Binary sharing will occur whenever binaries are taken apart, - this is the fundamental reason why binaries are fast, +
Binary sharing occurs whenever binaries are taken apart.
+ This is the fundamental reason why binaries are fast,
decomposition can always be done with O(1) complexity. In rare
circumstances this data sharing is however undesirable, why this
- function together with
Example of binary sharing:
-1> A = binary:copy(<<1>>,100).
+1> A = binary:copy(<<1>>, 100).
<<1,1,1,1,1 ...
2> byte_size(A).
100
@@ -477,141 +512,138 @@ store(Binary, GBSet) ->
5> byte_size(B).
10
6> binary:referenced_byte_size(B)
-100
-
+100
Binary data is shared among processes. If another process still references the larger binary, copying the part this - process uses only consumes more memory and will not free up the + process uses only consumes more memory and does not free up the larger binary for garbage collection. Use this kind of intrusive - functions with extreme care, and only if a real problem is - detected.
+ functions with extreme care and only if a real problem is detected.The same as
Same as
Constructs a new binary by replacing the parts in
-
If the matching subpart of
If the matching sub-part of
Example:
-1> binary:replace(<<"abcde">>,<<"b">>,<<"[]">>,[{insert_replaced,1}]).
+1> binary:replace(<<"abcde">>,<<"b">>,<<"[]">>, [{insert_replaced,1}]).
<<"a[b]cde">>
-2> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[]">>,
- [global,{insert_replaced,1}]).
+2> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[]">>,[global,{insert_replaced,1}]).
<<"a[b]c[d]e">>
-3> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[]">>,
- [global,{insert_replaced,[1,1]}]).
+3> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[]">>,[global,{insert_replaced,[1,1]}]).
<<"a[bb]c[dd]e">>
-4> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[-]">>,
- [global,{insert_replaced,[1,2]}]).
-<<"a[b-b]c[d-d]e">>
-
+4> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[-]">>,[global,{insert_replaced,[1,2]}]).
+<<"a[b-b]c[d-d]e">>
- If any position given in
If any position specified in
The options
Options
For a description of
For a description of
The same as
Same as
Splits
Splits
The parts of
The parts of
Example:
+Example:
1> binary:split(<<1,255,4,0,0,0,2,3>>, [<<0,0,0>>,<<2>>],[]).
[<<1,255,4>>, <<2,3>>]
2> binary:split(<<0,1,0,0,4,255,255,9>>, [<<0,0>>, <<255,255>>],[global]).
-[<<0,1>>,<<4>>,<<9>>]
-
+[<<0,1>>,<<4>>,<<9>>]
Summary of options:
-Works as in Works as in
Removes trailing empty parts of the result (as does trim in
Removes trailing empty parts of the result (as does
Removes all empty parts of the result.
Repeats the split until the
Repeats the split until
Example of the difference between a scope and taking the binary apart before splitting:
-1> binary:split(<<"banana">>,[<<"a">>],[{scope,{2,3}}]).
+1> binary:split(<<"banana">>, [<<"a">>],[{scope,{2,3}}]).
[<<"ban">>,<<"na">>]
-2> binary:split(binary:part(<<"banana">>,{2,3}),[<<"a">>],[]).
-[<<"n">>,<<"n">>]
-
+2> binary:split(binary:part(<<"banana">>,{2,3}), [<<"a">>],[]).
+[<<"n">>,<<"n">>]
The return type is always a list of binaries that are all
- referencing
For a description of
For a description of