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, ...
随机推荐
- 编写JsonResult封装JSON返回值(模板参阅)
编写JsonResult封装JSON返回值 package cn.tedu.note.util; import java.io.Serializable; import cn.tedu.note.se ...
- Spring+mybatis 实现aop数据库读写分离,多数据库源配置
在数据库层面大都采用读写分离技术,就是一个Master数据库,多个Slave数据库.Master库负责数据更新和实时数据查询,Slave库当然负责非实时数据查询.因为在实际的应用中,数据库都是读多写少 ...
- JavaScript--我发现,原来你是这样的JS(基础概念--灵魂篇,一起来学js吧)
介绍 这是红宝书(JavaScript高级程序设计 3版)的读书笔记第三篇(灵魂篇介绍),有着剩下的第三章的知识内容,当然其中还有我个人的理解.红宝书这本书可以说是难啃的,要看完不容易,挺厚的,要看懂 ...
- 闭锁——CountDownLatch
一.概念 闭锁是一个同步工具类,主要用于等待其他线程活动结束后,再执行后续的操作.例如:在王者荣耀游戏中,需要10名玩家都准备就绪后,游戏才能开始. CountDownLatch是concurrent ...
- hdu 2503 1713 1108 最小公倍数&最大公约数
gcd模板: __int64 gcd(__int64 a,__int64 b) { retur b==0?a:gcd(b,a%b); } 1108: #include<iostream> ...
- poj 3522 Kruskal
题意:求图的一个生成树使其最大边权与最小边权的差值最小,求其最小值 思路:利用贪心思想,先对边进行排序,然后从最小边开始枚举,每次进行kruskal向右加入边,若加入边刚好能遍历所有点,记录最后加入的 ...
- ★RFC标准库_目录链接
RFC(Request For Comments)是一个国际标准化的数据库,记录了从计算机到互联网的海量标准协议.它是一个免费公开的IT标准文件分享平台,其内容也在不断增长,与时俱进.它与ISO等组织 ...
- Linux设置全局代理与yum代理
设置全局代理,方法如下: 修改 /etc/profile 文件,添加下面内容: http_proxy=http://username:password@yourproxy:8080/ ftp_prox ...
- dnsmasq一次成功的配置
第一次用这个小软件,感觉还不错,因为没有像bind那样配置起来繁琐,并且我们也不需要去配置很多文件,内外网访问互不干涉. 我是在centos6.5下进行配置的: 先说说自己的理解: dnsmasq先去 ...
- yyt
红颜迤逦隔云梯, 相思萦系解花语. 我有相思千般意, 百磨不灭铭肝肠.