懒得发首页了,有时候因为贴的代码太多会被下,而且这东西本来也只是对自己学习的记录,阅读体验极差,所以就本地自娱自乐的写着吧!
由于是解析字符串,所以在开始之前介绍一下词法结构体中关于管理字符串类的属性。之前在TokenDesc中,有两个属性,如下。
/**
* 词法结构体
* 每一个TokenDesc代表单独一段词法*/
structTokenDesc {/**
* 字符串词法相关*/LiteralBuffer literal_chars;
LiteralBuffer raw_literal_chars;//...
}
当时没有详细讲,主要也是比较麻烦,在这里介绍一下该类。
classLiteralBuffer final {public:/**
* 根据字符Unicode数值判断是单字节还是双字节字符*/
voidAddChar(uc32 code_unit) {if(is_one_byte()) {if (code_unit <= static_cast(unibrow::Latin1::kMaxChar)) {
AddOneByteChar(static_cast(code_unit));return;
}
ConvertToTwoByte();
}
AddTwoByteChar(code_unit);
}private:/**
* 配置
* constexpr int MB = KB * KB; constexpr int KB = 1024;*/
static const int kInitialCapacity = 16;static const int kGrowthFactor = 4;static const int kMaxGrowth = 1 *MB;/**
* 向容器加字符*/
void AddOneByteChar(byteone_byte_char) {if (position_ >=backing_store_.length()) ExpandBuffer();
backing_store_[position_]=one_byte_char;
position_+=kOneByteSize;
}/**
* 容器扩容
* 初始至少有64的容量 根据需要扩容
* 会生成一个新容量的vector 把数据复制过去并摧毁老的容器*/
voidLiteralBuffer::ExpandBuffer() {int min_capacity =Max(kInitialCapacity, backing_store_.length());
Vector new_store = Vector::New(NewCapacity(min_capacity));if (position_ &