2011年3月10日木曜日

Erlang binary_part

binary_part(Subject, PosLen) -> binary()
Types:
Subject = binary()
PosLen = {Start,Length}
Start = int()
Length = int()


Extracts the part of the binary described by PosLen.
(PosLenでバイナリの一部を抜き出す)

Negative length can be used to extract bytes at the end of a binary:
(負の値であれば、バイナリの最後から抜き出すこともできる)

If PosLen in any way references outside the binary, a badarg exception is raised.
(もし、PosLenがバイナリの外を参照しようとすると、引数エラーでエラーを発する)

Start is zero-based
(1番左のバイナリは0である)

結果:

2> Bin = <<1,2,3,4,5,6,7,8,9,10>>.
<<1,2,3,4,5,6,7,8,9,10>>
3> binary_part(Bin,{byte_size(Bin), -5)).
* 1: syntax error before: ')'
3> binary_part(Bin,{byte_size(Bin), -5}).
<<6,7,8,9,10>>
4> binary_part(Bin,{1,8}).
<<2,3,4,5,6,7,8,9>>
5> binary_part(Bin,{0,byte_size(Bin)}).
<<1,2,3,4,5,6,7,8,9,10>>
6> binary_part(Bin,{0,byte_size(Bin)+1}).
** exception error: bad argument
     in function  binary_part/2
        called as binary_part(<<1,2,3,4,5,6,7,8,9,10>>,{0,11})
7>