MCPcopy Create free account
hub / github.com/CodingGay/BlackDex / StringPiece

Class StringPiece

Bcore/src/main/cpp/base/stringpiece.h:34–219  ·  view source on GitHub ↗

A string-like object that points to a sized piece of memory. Functions or methods may use const StringPiece& parameters to accept either a "const char*" or a "string" value that will be implicitly converted to a StringPiece. The implicit conversion means that it is often appropriate to include this .h file in other files rather than forward-declaring StringPiece as would be appropriate for most

Source from the content-addressed store, hash-verified

32// to include this .h file in other files rather than forward-declaring
33// StringPiece as would be appropriate for most other Google classes.
34class StringPiece {
35 public:
36 // standard STL container boilerplate
37 typedef char value_type;
38 typedef const char* pointer;
39 typedef const char& reference;
40 typedef const char& const_reference;
41 typedef size_t size_type;
42 typedef ptrdiff_t difference_type;
43 static constexpr size_type npos = size_type(-1);
44 typedef const char* const_iterator;
45 typedef const char* iterator;
46 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
47 typedef std::reverse_iterator<iterator> reverse_iterator;
48
49 // We provide non-explicit singleton constructors so users can pass
50 // in a "const char*" or a "string" wherever a "StringPiece" is
51 // expected.
52 StringPiece() : ptr_(nullptr), length_(0) { }
53 StringPiece(const char* str) // NOLINT implicit constructor desired
54 : ptr_(str), length_((str == nullptr) ? 0 : strlen(str)) { }
55 StringPiece(const std::string& str) // NOLINT implicit constructor desired
56 : ptr_(str.data()), length_(str.size()) { }
57 StringPiece(const char* offset, size_t len) : ptr_(offset), length_(len) { }
58
59 // data() may return a pointer to a buffer with embedded NULs, and the
60 // returned buffer may or may not be null terminated. Therefore it is
61 // typically a mistake to pass data() to a routine that expects a NUL
62 // terminated string.
63 const char* data() const { return ptr_; }
64 size_type size() const { return length_; }
65 size_type length() const { return length_; }
66 bool empty() const { return length_ == 0; }
67
68 void clear() {
69 ptr_ = nullptr;
70 length_ = 0;
71 }
72 void set(const char* data_in, size_type len) {
73 ptr_ = data_in;
74 length_ = len;
75 }
76 void set(const char* str) {
77 ptr_ = str;
78 if (str != nullptr) {
79 length_ = strlen(str);
80 } else {
81 length_ = 0;
82 }
83 }
84 void set(const void* data_in, size_type len) {
85 ptr_ = reinterpret_cast<const char*>(data_in);
86 length_ = len;
87 }
88
89 char operator[](size_type i) const {
90 DCHECK_LT(i, length_);
91 return ptr_[i];

Callers 1

substrMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected