/root/doris/be/src/gutil/strings/stringpiece.cc
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | // Copyright 2004 and onwards Google Inc. | 
| 2 |  | // | 
| 3 |  | // | 
| 4 |  |  | 
| 5 |  | #include "gutil/strings/stringpiece.h" | 
| 6 |  |  | 
| 7 |  | // IWYU pragma: no_include <pstl/glue_algorithm_defs.h> | 
| 8 |  |  | 
| 9 |  | #include "common/logging.h" | 
| 10 |  | #include <string.h> | 
| 11 |  | #include <algorithm> | 
| 12 |  | #include <climits> | 
| 13 |  | #include <string> | 
| 14 |  | #include <deque> | 
| 15 |  | #include <ostream> | 
| 16 |  |  | 
| 17 |  | #include "gutil/stl_util.h" | 
| 18 |  | #include "gutil/strings/memutil.h" | 
| 19 |  |  | 
| 20 |  | using std::copy; | 
| 21 |  | using std::max; | 
| 22 |  | using std::min; | 
| 23 |  | using std::reverse; | 
| 24 |  | using std::sort; | 
| 25 |  | using std::swap; | 
| 26 |  | using std::string; | 
| 27 |  |  | 
| 28 | 0 | std::ostream& operator<<(std::ostream& o, StringPiece piece) { | 
| 29 | 0 |     o.write(piece.data(), piece.size()); | 
| 30 | 0 |     return o; | 
| 31 | 0 | } | 
| 32 |  |  | 
| 33 | 0 | StringPiece::StringPiece(StringPiece x, int pos) : ptr_(x.ptr_ + pos), length_(x.length_ - pos) { | 
| 34 | 0 |     DCHECK_LE(0, pos); | 
| 35 | 0 |     DCHECK_LE(pos, x.length_); | 
| 36 | 0 | } | 
| 37 |  |  | 
| 38 |  | StringPiece::StringPiece(StringPiece x, int pos, int len) | 
| 39 | 0 |         : ptr_(x.ptr_ + pos), length_(min(len, x.length_ - pos)) { | 
| 40 | 0 |     DCHECK_LE(0, pos); | 
| 41 | 0 |     DCHECK_LE(pos, x.length_); | 
| 42 | 0 |     DCHECK_GE(len, 0); | 
| 43 | 0 | } | 
| 44 |  |  | 
| 45 | 0 | void StringPiece::CopyToString(string* target) const { | 
| 46 | 0 |     STLAssignToString(target, ptr_, length_); | 
| 47 | 0 | } | 
| 48 |  |  | 
| 49 | 0 | void StringPiece::AppendToString(string* target) const { | 
| 50 | 0 |     STLAppendToString(target, ptr_, length_); | 
| 51 | 0 | } | 
| 52 |  |  | 
| 53 | 0 | int StringPiece::copy(char* buf, size_type n, size_type pos) const { | 
| 54 | 0 |     int ret = min(length_ - pos, n); | 
| 55 | 0 |     memcpy(buf, ptr_ + pos, ret); | 
| 56 | 0 |     return ret; | 
| 57 | 0 | } | 
| 58 |  |  | 
| 59 | 0 | bool StringPiece::contains(StringPiece s) const { | 
| 60 | 0 |     return find(s, 0) != npos; | 
| 61 | 0 | } | 
| 62 |  |  | 
| 63 | 3.85k | int StringPiece::find(StringPiece s, size_type pos) const { | 
| 64 | 3.85k |     if (length_ <= 0 || pos > static_cast<size_type>(length_)) {  Branch (64:9): [True: 49, False: 3.81k]
  Branch (64:25): [True: 0, False: 3.81k]
 | 
| 65 | 49 |         if (length_ == 0 && pos == 0 && s.length_ == 0) return 0;   Branch (65:13): [True: 49, False: 0]
  Branch (65:29): [True: 49, False: 0]
  Branch (65:41): [True: 0, False: 49]
 | 
| 66 | 49 |         return npos; | 
| 67 | 49 |     } | 
| 68 | 3.81k |     const char* result = memmatch(ptr_ + pos, length_ - pos, s.ptr_, s.length_); | 
| 69 | 3.81k |     return result ? result - ptr_ : npos;   Branch (69:12): [True: 2.85k, False: 960]
 | 
| 70 | 3.85k | } | 
| 71 |  |  | 
| 72 | 0 | int StringPiece::find(char c, size_type pos) const { | 
| 73 | 0 |     if (length_ <= 0 || pos >= static_cast<size_type>(length_)) {  Branch (73:9): [True: 0, False: 0]
  Branch (73:25): [True: 0, False: 0]
 | 
| 74 | 0 |         return npos; | 
| 75 | 0 |     } | 
| 76 | 0 |     const char* result = static_cast<const char*>(memchr(ptr_ + pos, c, length_ - pos)); | 
| 77 | 0 |     return result != nullptr ? result - ptr_ : npos;   Branch (77:12): [True: 0, False: 0]
 | 
| 78 | 0 | } | 
| 79 |  |  | 
| 80 | 0 | int StringPiece::rfind(StringPiece s, size_type pos) const { | 
| 81 | 0 |     if (length_ < s.length_) return npos;   Branch (81:9): [True: 0, False: 0]
 | 
| 82 | 0 |     const size_t ulen = length_; | 
| 83 | 0 |     if (s.length_ == 0) return min(ulen, pos);   Branch (83:9): [True: 0, False: 0]
 | 
| 84 |  |  | 
| 85 | 0 |     const char* last = ptr_ + min(ulen - s.length_, pos) + s.length_; | 
| 86 | 0 |     const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); | 
| 87 | 0 |     return result != last ? result - ptr_ : npos;   Branch (87:12): [True: 0, False: 0]
 | 
| 88 | 0 | } | 
| 89 |  |  | 
| 90 |  | // Search range is [0..pos] inclusive.  If pos == npos, search everything. | 
| 91 | 0 | int StringPiece::rfind(char c, size_type pos) const { | 
| 92 |  |     // Note: memrchr() is not available on Windows. | 
| 93 | 0 |     if (length_ <= 0) return npos;   Branch (93:9): [True: 0, False: 0]
 | 
| 94 | 0 |     for (int i = min(pos, static_cast<size_type>(length_ - 1)); i >= 0; --i) {  Branch (94:65): [True: 0, False: 0]
 | 
| 95 | 0 |         if (ptr_[i] == c) {  Branch (95:13): [True: 0, False: 0]
 | 
| 96 | 0 |             return i; | 
| 97 | 0 |         } | 
| 98 | 0 |     } | 
| 99 | 0 |     return npos; | 
| 100 | 0 | } | 
| 101 |  |  | 
| 102 |  | // For each character in characters_wanted, sets the index corresponding | 
| 103 |  | // to the ASCII code of that character to 1 in table.  This is used by | 
| 104 |  | // the find_.*_of methods below to tell whether or not a character is in | 
| 105 |  | // the lookup table in constant time. | 
| 106 |  | // The argument `table' must be an array that is large enough to hold all | 
| 107 |  | // the possible values of an unsigned char.  Thus it should be be declared | 
| 108 |  | // as follows: | 
| 109 |  | //   bool table[UCHAR_MAX + 1] | 
| 110 | 0 | static inline void BuildLookupTable(StringPiece characters_wanted, bool* table) { | 
| 111 | 0 |     const int length = characters_wanted.length(); | 
| 112 | 0 |     const char* const data = characters_wanted.data(); | 
| 113 | 0 |     for (int i = 0; i < length; ++i) {  Branch (113:21): [True: 0, False: 0]
 | 
| 114 | 0 |         table[static_cast<unsigned char>(data[i])] = true; | 
| 115 | 0 |     } | 
| 116 | 0 | } | 
| 117 |  |  | 
| 118 | 0 | int StringPiece::find_first_of(StringPiece s, size_type pos) const { | 
| 119 | 0 |     if (length_ <= 0 || s.length_ <= 0) {  Branch (119:9): [True: 0, False: 0]
  Branch (119:25): [True: 0, False: 0]
 | 
| 120 | 0 |         return npos; | 
| 121 | 0 |     } | 
| 122 |  |     // Avoid the cost of BuildLookupTable() for a single-character search. | 
| 123 | 0 |     if (s.length_ == 1) return find_first_of(s.ptr_[0], pos);   Branch (123:9): [True: 0, False: 0]
 | 
| 124 |  |  | 
| 125 | 0 |     bool lookup[UCHAR_MAX + 1] = {false}; | 
| 126 | 0 |     BuildLookupTable(s, lookup); | 
| 127 | 0 |     for (int i = pos; i < length_; ++i) {  Branch (127:23): [True: 0, False: 0]
 | 
| 128 | 0 |         if (lookup[static_cast<unsigned char>(ptr_[i])]) {  Branch (128:13): [True: 0, False: 0]
 | 
| 129 | 0 |             return i; | 
| 130 | 0 |         } | 
| 131 | 0 |     } | 
| 132 | 0 |     return npos; | 
| 133 | 0 | } | 
| 134 |  |  | 
| 135 | 0 | int StringPiece::find_first_not_of(StringPiece s, size_type pos) const { | 
| 136 | 0 |     if (length_ <= 0) return npos;   Branch (136:9): [True: 0, False: 0]
 | 
| 137 | 0 |     if (s.length_ <= 0) return 0;   Branch (137:9): [True: 0, False: 0]
 | 
| 138 |  |     // Avoid the cost of BuildLookupTable() for a single-character search. | 
| 139 | 0 |     if (s.length_ == 1) return find_first_not_of(s.ptr_[0], pos);   Branch (139:9): [True: 0, False: 0]
 | 
| 140 |  |  | 
| 141 | 0 |     bool lookup[UCHAR_MAX + 1] = {false}; | 
| 142 | 0 |     BuildLookupTable(s, lookup); | 
| 143 | 0 |     for (int i = pos; i < length_; ++i) {  Branch (143:23): [True: 0, False: 0]
 | 
| 144 | 0 |         if (!lookup[static_cast<unsigned char>(ptr_[i])]) {  Branch (144:13): [True: 0, False: 0]
 | 
| 145 | 0 |             return i; | 
| 146 | 0 |         } | 
| 147 | 0 |     } | 
| 148 | 0 |     return npos; | 
| 149 | 0 | } | 
| 150 |  |  | 
| 151 | 0 | int StringPiece::find_first_not_of(char c, size_type pos) const { | 
| 152 | 0 |     if (length_ <= 0) return npos;   Branch (152:9): [True: 0, False: 0]
 | 
| 153 |  |  | 
| 154 | 0 |     for (; pos < static_cast<size_type>(length_); ++pos) {  Branch (154:12): [True: 0, False: 0]
 | 
| 155 | 0 |         if (ptr_[pos] != c) {  Branch (155:13): [True: 0, False: 0]
 | 
| 156 | 0 |             return pos; | 
| 157 | 0 |         } | 
| 158 | 0 |     } | 
| 159 | 0 |     return npos; | 
| 160 | 0 | } | 
| 161 |  |  | 
| 162 | 0 | int StringPiece::find_last_of(StringPiece s, size_type pos) const { | 
| 163 | 0 |     if (length_ <= 0 || s.length_ <= 0) return npos;   Branch (163:9): [True: 0, False: 0]
  Branch (163:25): [True: 0, False: 0]
 | 
| 164 |  |     // Avoid the cost of BuildLookupTable() for a single-character search. | 
| 165 | 0 |     if (s.length_ == 1) return find_last_of(s.ptr_[0], pos);   Branch (165:9): [True: 0, False: 0]
 | 
| 166 |  |  | 
| 167 | 0 |     bool lookup[UCHAR_MAX + 1] = {false}; | 
| 168 | 0 |     BuildLookupTable(s, lookup); | 
| 169 | 0 |     for (int i = min(pos, static_cast<size_type>(length_ - 1)); i >= 0; --i) {  Branch (169:65): [True: 0, False: 0]
 | 
| 170 | 0 |         if (lookup[static_cast<unsigned char>(ptr_[i])]) {  Branch (170:13): [True: 0, False: 0]
 | 
| 171 | 0 |             return i; | 
| 172 | 0 |         } | 
| 173 | 0 |     } | 
| 174 | 0 |     return npos; | 
| 175 | 0 | } | 
| 176 |  |  | 
| 177 | 0 | int StringPiece::find_last_not_of(StringPiece s, size_type pos) const { | 
| 178 | 0 |     if (length_ <= 0) return npos;   Branch (178:9): [True: 0, False: 0]
 | 
| 179 |  |  | 
| 180 | 0 |     int i = min(pos, static_cast<size_type>(length_ - 1)); | 
| 181 | 0 |     if (s.length_ <= 0) return i;   Branch (181:9): [True: 0, False: 0]
 | 
| 182 |  |  | 
| 183 |  |     // Avoid the cost of BuildLookupTable() for a single-character search. | 
| 184 | 0 |     if (s.length_ == 1) return find_last_not_of(s.ptr_[0], pos);   Branch (184:9): [True: 0, False: 0]
 | 
| 185 |  |  | 
| 186 | 0 |     bool lookup[UCHAR_MAX + 1] = {false}; | 
| 187 | 0 |     BuildLookupTable(s, lookup); | 
| 188 | 0 |     for (; i >= 0; --i) {  Branch (188:12): [True: 0, False: 0]
 | 
| 189 | 0 |         if (!lookup[static_cast<unsigned char>(ptr_[i])]) {  Branch (189:13): [True: 0, False: 0]
 | 
| 190 | 0 |             return i; | 
| 191 | 0 |         } | 
| 192 | 0 |     } | 
| 193 | 0 |     return npos; | 
| 194 | 0 | } | 
| 195 |  |  | 
| 196 | 0 | int StringPiece::find_last_not_of(char c, size_type pos) const { | 
| 197 | 0 |     if (length_ <= 0) return npos;   Branch (197:9): [True: 0, False: 0]
 | 
| 198 |  |  | 
| 199 | 0 |     for (int i = min(pos, static_cast<size_type>(length_ - 1)); i >= 0; --i) {  Branch (199:65): [True: 0, False: 0]
 | 
| 200 | 0 |         if (ptr_[i] != c) {  Branch (200:13): [True: 0, False: 0]
 | 
| 201 | 0 |             return i; | 
| 202 | 0 |         } | 
| 203 | 0 |     } | 
| 204 | 0 |     return npos; | 
| 205 | 0 | } | 
| 206 |  |  | 
| 207 | 0 | StringPiece StringPiece::substr(size_type pos, size_type n) const { | 
| 208 | 0 |     if (pos > length_) pos = length_;   Branch (208:9): [True: 0, False: 0]
 | 
| 209 | 0 |     if (n > length_ - pos) n = length_ - pos;   Branch (209:9): [True: 0, False: 0]
 | 
| 210 | 0 |     return StringPiece(ptr_ + pos, n); | 
| 211 | 0 | } | 
| 212 |  |  | 
| 213 |  | const StringPiece::size_type StringPiece::npos = size_type(-1); |