| OLD | NEW |
| (Empty) |
| 1 // Copyright 2004 The RE2 Authors. All Rights Reserved. | |
| 2 // Use of this source code is governed by a BSD-style | |
| 3 // license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "re2/stringpiece.h" | |
| 6 #include "util/util.h" | |
| 7 | |
| 8 using re2::StringPiece; | |
| 9 | |
| 10 std::ostream& operator<<(std::ostream& o, const StringPiece& piece) { | |
| 11 o.write(piece.data(), piece.size()); | |
| 12 return o; | |
| 13 } | |
| 14 | |
| 15 bool StringPiece::_equal(const StringPiece& x, const StringPiece& y) { | |
| 16 int len = x.size(); | |
| 17 if (len != y.size()) { | |
| 18 return false; | |
| 19 } | |
| 20 const char* p = x.data(); | |
| 21 const char* p2 = y.data(); | |
| 22 // Test last byte in case strings share large common prefix | |
| 23 if ((len > 0) && (p[len-1] != p2[len-1])) return false; | |
| 24 const char* p_limit = p + len; | |
| 25 for (; p < p_limit; p++, p2++) { | |
| 26 if (*p != *p2) | |
| 27 return false; | |
| 28 } | |
| 29 return true; | |
| 30 } | |
| 31 | |
| 32 void StringPiece::CopyToString(string* target) const { | |
| 33 target->assign(ptr_, length_); | |
| 34 } | |
| 35 | |
| 36 void StringPiece::AppendToString(string* target) const { | |
| 37 target->append(ptr_, length_); | |
| 38 } | |
| 39 | |
| 40 StringPiece::size_type StringPiece::copy(char* buf, size_type n, | |
| 41 size_type pos) const { | |
| 42 size_type ret = min(length_ - pos, n); | |
| 43 memcpy(buf, ptr_ + pos, ret); | |
| 44 return ret; | |
| 45 } | |
| 46 | |
| 47 bool StringPiece::contains(StringPiece s) const { | |
| 48 return find(s, 0) != npos; | |
| 49 } | |
| 50 | |
| 51 StringPiece::size_type StringPiece::find(const StringPiece& s, | |
| 52 size_type pos) const { | |
| 53 if (length_ < 0 || pos > static_cast<size_type>(length_)) | |
| 54 return npos; | |
| 55 | |
| 56 const char* result = std::search(ptr_ + pos, ptr_ + length_, | |
| 57 s.ptr_, s.ptr_ + s.length_); | |
| 58 const size_type xpos = result - ptr_; | |
| 59 return xpos + s.length_ <= static_cast<size_type>(length_) ? xpos : npos; | |
| 60 } | |
| 61 | |
| 62 StringPiece::size_type StringPiece::find(char c, size_type pos) const { | |
| 63 if (length_ <= 0 || pos >= static_cast<size_type>(length_)) { | |
| 64 return npos; | |
| 65 } | |
| 66 const char* result = std::find(ptr_ + pos, ptr_ + length_, c); | |
| 67 return result != ptr_ + length_ ? result - ptr_ : npos; | |
| 68 } | |
| 69 | |
| 70 StringPiece::size_type StringPiece::rfind(const StringPiece& s, | |
| 71 size_type pos) const { | |
| 72 if (length_ < s.length_) return npos; | |
| 73 const size_type ulen = length_; | |
| 74 if (s.length_ == 0) return min(ulen, pos); | |
| 75 | |
| 76 const char* last = ptr_ + min(ulen - s.length_, pos) + s.length_; | |
| 77 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); | |
| 78 return result != last ? result - ptr_ : npos; | |
| 79 } | |
| 80 | |
| 81 StringPiece::size_type StringPiece::rfind(char c, size_type pos) const { | |
| 82 if (length_ <= 0) return npos; | |
| 83 for (int i = static_cast<int>(min(pos, static_cast<size_type>(length_ - 1))); | |
| 84 i >= 0; --i) { | |
| 85 if (ptr_[i] == c) { | |
| 86 return i; | |
| 87 } | |
| 88 } | |
| 89 return npos; | |
| 90 } | |
| 91 | |
| 92 StringPiece StringPiece::substr(size_type pos, size_type n) const { | |
| 93 if (pos > static_cast<size_type>(length_)) pos = static_cast<size_type>(length
_); | |
| 94 if (n > length_ - pos) n = length_ - pos; | |
| 95 return StringPiece(ptr_ + pos, static_cast<int>(n)); | |
| 96 } | |
| 97 | |
| 98 const StringPiece::size_type StringPiece::npos = size_type(-1); | |
| OLD | NEW |