leveldb学习笔记一 SLICE
来源:互联网 发布:ps淘宝详情页模板 编辑:程序博客网 时间:2024/05/22 17:16
Slice是leveldb项目中重写的string,很多谷歌项目都没有使用自带的string,而是重写,Slice功能简单,只有正常的赋值比较,去前缀.
#include <assert.h>#include <stddef.h>#include <string.h>#include <string>namespace leveldb {class Slice { public: // Create an empty slice. Slice() : data_(""), size_(0) { } // Create a slice that refers to d[0,n-1]. Slice(const char* d, size_t n) : data_(d), size_(n) { } // Create a slice that refers to the contents of "s" Slice(const std::string& s) : data_(s.data()), size_(s.size()) { } // Create a slice that refers to s[0,strlen(s)-1] Slice(const char* s) : data_(s), size_(strlen(s)) { } // Return a pointer to the beginning of the referenced data const char* data() const { return data_; } // Return the length (in bytes) of the referenced data size_t size() const { return size_; } // Return true iff the length of the referenced data is zero bool empty() const { return size_ == 0; } // Return the ith byte in the referenced data. // REQUIRES: n < size() char operator[](size_t n) const { assert(n < size()); return data_[n]; } // Change this slice to refer to an empty array void clear() { data_ = ""; size_ = 0; } // Drop the first "n" bytes from this slice. void remove_prefix(size_t n) { assert(n <= size()); data_ += n; size_ -= n; } // Return a string that contains the copy of the referenced data. std::string ToString() const { return std::string(data_, size_); } // Three-way comparison. Returns value: // < 0 iff "*this" < "b", // == 0 iff "*this" == "b", // > 0 iff "*this" > "b" int compare(const Slice& b) const; // Return true iff "x" is a prefix of "*this" bool starts_with(const Slice& x) const { return ((size_ >= x.size_) && (memcmp(data_, x.data_, x.size_) == 0)); } private: const char* data_; size_t size_; // Intentionally copyable};inline bool operator==(const Slice& x, const Slice& y) { return ((x.size() == y.size()) && (memcmp(x.data(), y.data(), x.size()) == 0));}inline bool operator!=(const Slice& x, const Slice& y) { return !(x == y);}inline int Slice::compare(const Slice& b) const { const int min_len = (size_ < b.size_) ? size_ : b.size_; int r = memcmp(data_, b.data_, min_len); if (r == 0) { if (size_ < b.size_) r = -1; else if (size_ > b.size_) r = +1; } return r;}} // namespace leveldb
以上是源代码.
1.4个构造函数实现了基本赋值操作,和string向slice的转换.
2.data(),size()函数用于取出字符串
3.remove_prefix用于去掉前缀,通过地址向后移动需要去除的字符长度来去除前缀.
4.重载比较符号
5.memcmp函数,前两个参数是字符指针,第三个参数为比较长度,用于判定一个字符串是否是另一个的前缀.
0 0
- leveldb学习笔记一 SLICE
- leveldb slice 学习
- leveldb源码学习 1 - Slice
- 一,levelDB源码分析(slice)
- Python学习笔记(一):Python中的slice
- LevelDB学习笔记
- LevelDB学习笔记
- levelDB源码分析-Slice
- Slice-levelDB源码解析
- leveldb阅读-Slice字符串
- leveldb(三):Slice
- Leveldb源码分析之Slice
- leveldb学习一(资源篇)
- LevelDB 学习笔记 —— utils
- levelDB学习笔记 —— table
- levelDB学习笔记——Version
- ICE学习笔记(01):Slice规范
- ICE学习笔记(01):Slice规范
- 问题总结一
- LeetCode 53. Maximum Subarray 题解
- scrapy框架拓展:为download_middleware添加用户代理池
- 协议 protocol
- win10使用genymotion出现Unable to start the virtual device
- leveldb学习笔记一 SLICE
- swift中字符的长度
- springboot war包部署
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串
- C#入门小程序 猜数字
- Python基础语法——函数(四)
- 2017-03-05 CentOS中结合Nginx部署dotnet core Web应用程序
- PAT甲级1007. Maximum Subsequence Sum (25)
- 关于Linux的几个问题