---
title: "R14A实现了EEP31，添加了binary模块"
author: "mryufeng"
date: "2010-05-21T15:15:28Z"
tags: ["C", "C++", "Erlang", "数据结构", "网络协议"]
categories: ["编程语言"]
slug: "r14a-eep31-binary"
draft: false
lastmod: "2010-05-21T15:15:28Z"
id: 671768
url: "https://mryufeng.iteye.com/blog/671768"
---

Erlang的binary数据结构非常强大，而且偏向底层，在作网络程序的时候，很方便的能够和二进制协议对应起来。但是由于这个数据结构加入erlang语言的时间不是很长，相关的配套模块不是很多。 在binary的匹配，替换，修改就显的非常麻烦。 于是有了EEP31 。 R14A昨天已经实现了这个功能， 在stdlib下添加了个binary模块。 这个模块大部分功能是由BIF实现的，同时充分考虑了CPU使用的公平性，源码大部分在erl_bif_binary.c下。 还添加了个gurad函数： binary_part进一步方便我们写匹配条件。

我们在源码里面发现了以下注释：
/*
* The native implementation functions for the module binary.
* Searching is implemented using aither Boyer-More or Aho-Corasick
* depending on number of searchstrings (BM if one, AC if more than one).
* Native implementation is mostly for efficiency, nothing
* (except binary:referenced_byte_size) really *needs* to be implemented
* in native code.
*/

这个模块兼顾了效率和方便性，使用起来就大大简化了代码的复杂度，有福气了。