首页 > 代码库 > binary
binary
模块说明
- 函数经过优化,比等价Erlang实现(比如位语法)更快,更节省内存
- 大部分可用位语法等价实现
- 模块的实现根据EEP(Erlang Enhancement Proposal)31
- 模块只处理面向字节的数据binary,位串Bitstring不是binary,使用会badarg异常
- 操作基于0索引
- 所有的选项列表,后面的选项覆盖前面的选项
- 超出范围的操作会导致badarg异常
数据类型
cp() 编译过的搜索模式
part() ={Pos,Len}部分,{基于0的开始,长度} ,长度可为负
导出函数
- at(Bin,Pos) : 返回Pos处的字节
- bin_to_list(Bin) :等同 bin_to_list(Bin,PosLen={0,byte_size(Bin)})
- bin_to_list(Bin,PosLen): 返回Bin从Pos起Len长的字节数组
- bin_to_list(Bin,Pos,Len) : 参上,参数分开而已
- compile_pattern(Bin | [Bin]) : 返回编译好的搜索模式cp()
- copy(Bin) : 返回Bin的一份拷贝; Bin可能引用着更大的binary,继续使用Bin就继续引用着,使用拷贝,则大的binary可能回收
- copy(Bin,N) : 返回N倍的Bin拷贝
- decode_unsigned(Bin) : 把Bin表示的整数解码成无符号整数,默认big 大端记法
- decode_unsigned(Bin,Endianess) :同上,只是以Endianess 解释, Endianess=big | little
- encode_unsigned(Unsigned) : 参见decode_unsigned(Bin)
- encode_unsigned(Unsigned,Endianess) : 参见 decode_unsigned(Bin,Endianess)
- first(Bin) : 获取第1个字节,超出范围badarg 异常
- last(Bin) : 最后1个字节,超出范围badarg异常
- list_to_bin(ByteList) : 和erlang:list_to_binary 一样,只是为了完整性。 字节列表转成Bin,字节列表时iodata() ,即可嵌套
- longest_common_preifx([Bin]) : Bin 列表最长前缀长度
- longest_common_suffix([Bin]) : Bin 列表最长后缀长度
- match(Bin, Pattern) : 只返回第1处匹配;没有返回nomatch;Pattern=Bin | [Bin] | cp() ; 重叠的匹配首先以起始位置优先,然后以长度最大优先
- match(Bin,Pattern,[Option]) : 同上, 增加了选项列表。 Option = {scope, {Pos, Len}} ,后面的覆盖前面的
- matches(Bin, Pattern) : 等同 matches(Bin, Pattern,[]) ,返回全部非重叠的匹配 {Pos,Len} 列表
- matches(Bin, Pattern, [Option]) : 同上, 可通过Option指定匹配范围 Option={scope,{Pos,Len}}
- part(Bin, PosLen) : 返回 Bin 中 {Pos,Len} 的部分
- part(Bin, Pos, Len) : 同上,参数分开
- referenced_byte_size(Bin) : 返回 Bin 引用的更大binary的字节长度
- replace(Bin, Pattern, Replacement) : 根据Pattern把匹配的替换成Replacement = binary() , 只替换一次
- replace(Bin, Patter, Replacement, [Option]) : 同上,增加了Option = global | {scope,PosLen} | {insert_replaced, [Pos]} , scope 会取消global的作用, scope后面的覆盖前面的,insert_replaced 也是这样
- split(Bin,Pattern) : 根据模式拆分Bin,返回拆分SubBin列表,等同split(Bin,Pattern,[])
- split(Bin,Pattern,[Option]) : 同上。增加了选项。Option = {scope, PosLen} | trim | global ;同上,scope会取消global;trim会移除结果列表末尾空<<>>
例子
6> binary:replace(<<"abcdabc">>,<<"abc">>,<<"ef">>).
<<"efdabc">>
7> binary:replace(<<"abcdabc">>,[<<"abc">>,<<"bc">>],<<"ef">>).
<<"efdabc">>
8> binary:replace(<<"abcdefg">>,[<<"abc">>,<<"efg">>],<<"ef">>).
<<"efdefg">>
9> binary:replace(<<"abcde">>,<<"b">>,<<"[{]">>,[{insert_replaced,1}]).
<<"a[b{]cde">>
10> binary:replace(<<"abcde">>,<<"b">>,<<"[{]">>,[{insert_replaced,2}]).
<<"a[{b]cde">>
11> binary:replace(<<"abcde">>,<<"b">>,<<"[{]">>,[{insert_replaced,1},{insert_replaced,2}]).
<<"a[{b]cde">>
12> binary:replace(<<"abcde">>,<<"b">>,<<"[{]">>,[{insert_replaced,3},{insert_replaced,2}]).
<<"a[{b]cde">>
13> binary:replace(<<"abcde">>,<<"b">>,<<"[{]">>,[{insert_replaced,3},{insert_replaced,3}]).
<<"a[{]bcde">>
14> binary:replace(<<"abcde">>,<<"b">>,<<"[{]">>,[{insert_replaced,4},{insert_replaced,3}]).
<<"a[{]bcde">>
15> binary:replace(<<"abcde">>,<<"b">>,<<"[{]">>,[{insert_replaced,4},{insert_replaced,4}]).
** exception error: bad argument
in function binary:replace/4 (binary.erl, line 317)
17> binary:replace(<<"abcde">>,<<"b">>,<<"[{]">>,[{scope,{0,5}},{insert_replaced,0}]).
<<"ab[{]cde">>
18> binary:replace(<<"abcde">>,<<"b">>,<<"[{]">>,[{scope,{0,6}},{insert_replaced,0}]).
** exception error: bad argument
in function binary:replace/4 (binary.erl, line 317)
19> binary:split(<<0,100,4,255,255,9>>,[<<0,0>>,<<255,255>>],[global]).
[<<0,100,4>>,<<"\t">>]
20> binary:split(<<0,1,0,0,4,255,255,9>>,[<<0,0>>,<<255,255>>],[global]).
[<<0,1>>,<<4>>,<<"\t">>]
21> binary:split(<<0,1,0,0,4,255,255,0,9>>,[<<0,0>>,<<255,255>>],[global]).
[<<0,1>>,<<4>>,<<0,9>>]
22> binary:split(<<0,1,0,0,4,255,255,0,9>>,[<<0,0>>,<<255,255>>],[global,trim]).
[<<0,1>>,<<4>>,<<0,9>>]
23> binary:split(<<0,1,0,0,255,255,0,9>>,[<<0,0>>,<<255,255>>],[global,trim]).
[<<0,1>>,<<>>,<<0,9>>]
24> binary:split(<<0,1,0,0,255,255,0,9>>,[<<0,0>>,<<255,255>>],[global]).
[<<0,1>>,<<>>,<<0,9>>]
25> binary:split(<<0,1,0,0,0,9,255,255>>,[<<0,0>>,<<255,255>>],[global]).
[<<0,1>>,<<0,9>>,<<>>]
26> binary:split(<<0,1,0,0,0,9,255,255>>,[<<0,0>>,<<255,255>>],[global,trim]).
[<<0,1>>,<<0,9>>]
27> binary:split(<<0,0,1,1,0,9,255,255>>,[<<0,0>>,<<255,255>>],[global,trim]).
[<<>>,<<1,1,0,9>>]
28> binary:split(<<0,0,1,1,0,9,255,255>>,[<<0,0>>,<<255,255>>],[global]).
[<<>>,<<1,1,0,9>>,<<>>]
30> binary:split(<<"banana">>,<<"a">>,[{scope,{2,3}}]).
[<<"ban">>,<<"na">>]
31> binary:split(<<"banana">>,<<"a">>,[{scope,{2,3}},global]).
[<<"ban">>,<<"na">>]
32> binary:split(<<"banana">>,<<"a">>,[global]).
[<<"b">>,<<"n">>,<<"n">>,<<>>]
33> binary:split(<<"banana">>,<<"a">>,[{scope,{2,3}},global]).
[<<"ban">>,<<"na">>]
34> binary:split(<<"banana,banana">>,<<"a">>,[{scope,{2,3}},global]).
[<<"ban">>,<<"na,banana">>]
35> binary:split(<<"banana,banana">>,<<"a">>,[{scope,{2,3}},{scope,{9,3}}]).
[<<"banana,ban">>,<<"na">>]
36> binary:split(<<"banana,banana">>,<<"a">>,[{scope,{2,3}},{scope,{9,3}},gloabl]).
** exception error: bad argument
in function binary:split/3 (binary.erl, line 242)
37> binary:split(<<"banana,banana">>,<<"a">>,[{scope,{2,3}},{scope,{9,3}},global]).
[<<"banana,ban">>,<<"na">>]
38> binary:split(<<"banana,banana">>,<<"a">>,[{scope,{2,3}},global]).
[<<"ban">>,<<"na,banana">>]
39> binary:replace(<<"abcdefabc">>,
39> <<"abc">>,
39> [{scope,{0,4}},
39> {cope,{5,8}}]).
** exception error: bad argument
in function binary:replace/4 (binary.erl, line 317)
49> binary:replace(<<"abcdefok">>,[<<"abc">>,<<"ok">>],<<"HH">>,[global,{scope,{0,4}}]).
<<"HHdefok">>
50> binary:replace(<<"abcdefok">>,[<<"abc">>,<<"ok">>],<<"HH">>,[global,{scope,{0,4}},{scope,{5,3}}]).
<<"abcdefHH">>
51> binary:replace(<<"abcdefok">>,[<<"abc">>,<<"ok">>],<<"HH">>,[{scope,{0,4}},global]).
<<"HHdefok">>
52> binary:replace(<<"abcdefok">>,[<<"abc">>,<<"ok">>],<<"HH">>,[{scope,{0,4}},{scope,{5,3}},global]).
<<"abcdefHH">>
53> binary:replace(<<"abcdefok">>,[<<"abc">>,<<"ok">>],<<"HH">>,[{global,{5,3}},{scope,{0,4}},global]).
** exception error: bad argument
in function binary:replace/4 (binary.erl, line 317)
54> binary:replace(<<"abcdefok">>,[<<"abc">>,<<"ok">>],<<"HH">>,[{scope,{5,3}},{scope,{0,4}},global]).
<<"HHdefok">>
55> binary:referenced_byte_size(<<"yao">>).
256
56> binary:referenced_byte_size(<<"yaos">>).
256
binary