C++ 一个字符串的代理类(String View)
2016-09-22 12:31
459 查看
在学习muduo/base源码的时候看到一个StringPiece类,是个
关于
简单摘抄下来就是
The purpose of any and all kinds of “string reference” and “array reference” proposals is to avoid copying data which is already owned somewhere else and of which only a non-mutating view is required. The string_view in question is one such proposal; there were earlier ones called string_ref and array_ref, too.
The idea is always to store a pair of pointer-to-first-element and size of some existing data array or string.
muduo的
string view,记录下来。
关于
string view,看http://stackoverflow.com/questions/20803826/what-is-string-view。
简单摘抄下来就是
The purpose of any and all kinds of “string reference” and “array reference” proposals is to avoid copying data which is already owned somewhere else and of which only a non-mutating view is required. The string_view in question is one such proposal; there were earlier ones called string_ref and array_ref, too.
The idea is always to store a pair of pointer-to-first-element and size of some existing data array or string.
string view解释过来就是个窗口,你通过这个窗口查看代理的字符串的内容,移动指针和改变长度就可以移动窗口,而且,自身不需要存储字符串,只读,不可修改,生命期应和代理的字符串一致。
muduo的
StringPiece代码如下,个人做了一点修改:
class StringPiece { public: StringPiece() : _ptr(nullptr), _length(0) { } StringPiece(const char* str) : _ptr(str), _length(strlen(_ptr)) { } StringPiece(const unsigned char* str) : _ptr(reinterpret_cast<const char*>(str)), _length(strlen(_ptr)) { } StringPiece(const std::string& str) : _ptr(str.data()), _length(static_cast<size_t>(str.length())) { } StringPiece(const char* offset, size_t len) : _ptr(offset), _length(len) { } const char* data() const { return _ptr; } size_t size() const { return _length; } bool empty() const { return _length == 0; } const char* begin() const { return _ptr; } const char* end() const { return _ptr + _length; } void clear() { _ptr = nullptr; _length = 0; } void set(const char* buffer, size_t len) { _ptr = buffer; _length = len; } void set(const char* str) { _ptr = str; _length = strlen(str); } void set(const void* buffer, size_t len) { _ptr = reinterpret_cast<const char*>(buffer); _length = len; } char operator[](size_t i) const { return _ptr[i]; } void remove_prefix(size_t n) { assert(n <= _length); _ptr += n; _length -= n; } void remove_suffix(size_t n) { assert(n <= _length); _length -= n; } bool operator==(const StringPiece& x) const { return ((_length == x._length) && (memcmp(_ptr, x._ptr, _length) == 0)); } bool operator!=(const StringPiece& sp) const { return !(*this == sp); } std::string asString() const { return std::string(data(), size()); } void copyToStdString(std::string* target) const { assert(target != nullptr); target->assign(_ptr, _length); } bool startWith(const StringPiece& x) const { return ((_length >= x._length) && (memcmp(_ptr, x._ptr, x._length) == 0)); } int compare(const StringPiece& x) const { int r = memcmp(_ptr, x._ptr, _length < x._length ? _length : x._length); if (r == 0) { if (_length < x._length) r = -1; else if (_length > x._length) r = +1; } return r; } private: const char* _ptr; size_t _length; };
相关文章推荐
- c++中怎么判断一个string类型的字符串变量是否为数字?
- leetcode 434. Number of Segments in a String 按照空格分割字符串 + C++的stringstream的一个很好应用示范
- c++中怎么判断一个string类型的字符串变量是否为数字?
- 将一个十六进制字符串转换为十进制数值的问题|| 以及C++中string转换成char*|| c++文件按行输入
- C++之String的find方法,查找一个字符串在另一个字符串的什么位置;leveldb字符串转数字解析办法
- C++如何判断一个string字符串,是否是数字
- C++中,如何执行一个控制台命令并返回结果到字符串string中
- 网络通信中,DELPHI的string类型数据转换为C++字符串发送的一个陷阱
- c++中怎么判断一个string类型的字符串变量是否为数字?
- PHP - Manual手册 - CLXI. String 字符串处理函数 - strpos查找一个字符串第一次出现的位置
- C++字符串完全指南 第二部分-字符串封装类(String Wrapper Classes)
- C/C++字符串处理(3):String ADT - 字符串只是抽象数据类型
- 一个关于c++字符串处理和delete[]与delete差别的问题
- c++ int to string(整型到字符串)
- C/C++字符串处理盘点:Char*/String/StringBuilder/TextPool/Rope
- C++中 替换string类型字符串中汉字的问题
- C/C++字符串处理(2):String - 常字符串
- C/C++字符串处理(2):String - 常字符串
- C++的中英文字符串表示(string,wstring)
- PHP - Manual手册 - 函数参考 - String 字符串处理函数 - explode使用一个字符串分割另一个字符串