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, ...
随机推荐
- MySQL的简单使用-(一)
MySQL的简单使用 使用MySQL命令行工具 Windows 用户使用: MySQL Client, 输入密码 Linux: mysql -u用户名 -p密码 mysql -uroot -p 显示数 ...
- epoll全面讲解:从实现到应用
多路复用的适用场合 • 当客户处理多个描述符时(例如同时处理交互式输入和网络套接口),必须使用I/O复用. • 如果一个TCP服务器既要处理监听套接口,又要处理已连接套接口,一般也要用 ...
- UIButton和UIimageView
1.按钮控件使用的类是UIButton 点击按钮会触发某个事件 2.按钮控件的初始化 UIButton *button = [UIButton buttonWithType:UIButtonTypeC ...
- 性能测试中vmstat命令的常见使用。
vmstat是Linux中常用的一个命令,尤其是在进行性能测试时,使用该命令,往往能辅助我们进行性能问题定位. 我们先看一下这个命令执行得到的数据. [root@xxx ~]# vmstat 1 10 ...
- js实现换肤效果
一,js换肤的基本原理 基本原理很简单,就是使用 JS 切换对应的 CSS 样式表文件.例如导航网站 Hao123 的右上方就有网页换肤功能.除了切换 CSS 样式表文件之外,通常的网页换肤还需要通过 ...
- Httprequest 获取url 常用方法
HttpServletRequest常用获取URL的方法 1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路 ...
- Java Object中的equals和hashCode
Java的Object对象中有两个方法比较有意思,一个是equals(),一个是hashCode(),那么这两个的作用有些同学可能还不是很清楚,那么同学们现在就进一步了解一下吧. 下面咱们写一个简单的 ...
- js 你所不知道的一面
你真的知道JavaScript吗 JavaScript是一门奇怪的语言,要真正掌握并不容易.废话不多说,来一个快速测试,5道题目,看看你对JavaScript是否真正掌握.准备好了吗?开始咯
- kickstart部署及使用
Linux运维:kickstart : 矮哥linux运维群:93324526 1.环境检查 [root@m01 ~]# cat /etc/redhat-release CentOS release ...
- 1st 四则运算题目生成程序
程序代码见此 程序展示 需求分析 需要程序能根据用户指定生成四则运算的题目,并且能让用户做题,并且最后打分统计正确率 功能设计 主要实现的功能就是: 接受用户输入以便知道要出多少道题目(-n x) 能 ...