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, ...
随机推荐
- 安卓两个build.gradle的区别
作为安卓入门选手,在导入第三方的时候才发现居然有两个build.gradle,我说咋不对啊,原来是导错了(可能是因为我没有看安卓培训的视频吧). 那么就说一下这两个的作用(一个Project的,一个M ...
- System.Globalization.CultureInfo.InvariantCulture 解决不同地域字符串格式不同问题
同样的DateTime.ToShortDateString() 在不同的地域输出格式不同 如在美国的 日期格式为 : 月-日-年 如在中国的 日期格式为 : 年-月-日 一些时候,这个格式就会 ...
- JOptionPane弹框常用实例
最近在做swing程序中遇到使用消息提示框的,JOptionPane类其中封装了很多的方法. 很方便的,于是就简单的整理了一下. 1.1 showMessageDialog 显示一个带有OK 按钮的模 ...
- ★RFC标准库_目录链接
RFC(Request For Comments)是一个国际标准化的数据库,记录了从计算机到互联网的海量标准协议.它是一个免费公开的IT标准文件分享平台,其内容也在不断增长,与时俱进.它与ISO等组织 ...
- jQuery高级Ajax
.load();加载远程的HTML文件代码,并插入到指定的DOM节点中.可以只传入一个参数,表示加载一个静态的HTML代码片段. $("#div1").load("loa ...
- Js函数初学者练习(一)switch-case结构实现计算器。
前 言 JRedu 给大家介绍一点JS函数的练习题希望初学者多做一些练习能够更好的掌握JS的函数,以及能够提升大家的逻辑思维.(我也是个渣渣希望路过的大神多提建议或意见) 希望能够对大家有所帮助 ...
- 201521123112《Java程序设计》第7周学习总结
1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 下面先贴出contains的源代码: public boolean cont ...
- 201521123016 《Java程序设计》第2周学习总结
1. 本周学习总结 JAVA中string对象创建后不可修改. 使用StringBuilder编写代码,减少内存空间的占用. 字符串使用"+"拼接,拼接后其他类型会被转化为字符串. ...
- 关闭Sublime Text的自动更新的方法
每次打开Sublime text 软件都会提示我让我更新软件,如图: 经过仔细的研究发现可以通过以下途径关闭软件的自动更新 打开Submine Text,找到Preferences -> Set ...
- 201521123076 《Java程序设计》 第十四周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...