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的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Binary Watch 二进制表
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
随机推荐
- 插入排序与希尔排序Java实现
public class TestMain { public static void main(String[] args) { Integer[] a = new Integer[5000]; fo ...
- WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据(转)
WebForm.aspx 页面通过 AJAX 访问WebForm.aspx.cs类中的方法,获取数据 WebForm1.aspx 页面 (原生AJAX请求,写法一) <%@ Page Langu ...
- 201521123013 《Java程序设计》第7周学习总结
1. 本章学习总结 2. 书面作业 Q1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 public boolean contains(Object o) { r ...
- 201521123069 《Java程序设计》 第4周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. (1)文档注释(类注释.方法注释.属性注释.通用注释.) (2)多态性.instanceof ...
- 201521123064 《Java程序设计》第10周学习总结
1. 本章学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. ① 定义Thread类的子类,覆盖Thread类的run()方法,然后创建该子类的实例(一般不用该方法,开销 ...
- Activiti-05-.Deployment and MN 2.0 Introduction
Business archives (业务归档) 为了部署流程,业务档案必须被封装,业务档案是activiti 引擎部署的单元,它相当于zip文件,它包含BPMN 2.0 processes, tas ...
- hadoop超租约报错:
解决方法:修改linux打开文件最大限制Java代码 echo "fs.file-max = 65535" >> /etc/sysctl.conf echo " ...
- CyclicBarrier的使用之王者荣耀打大龙
最近一直整并发这块东西,顺便写点Java并发的例子,给大家做个分享,也强化下自己记忆,如果有什么错误或者不当的地方,欢迎大家斧正. LOL和王者荣耀的玩家很多,许多人应该都有打大龙的经历,话说前期大家 ...
- OC——关于KVC
我们知道在C#中可以通过反射读写一个对象的属性,有时候这种方式特别方便,因为你可以利用字符串的方式去动态控制一个对象.其实由于ObjC的语言特性,你根部不必进行任何操作就可以进行属性的动态读写,这种方 ...
- Spring框架(一)
Spring: Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由 Rod Johnson在其著作 Expert One-On-One J2EE Deve ...