Ian Hodson | 2ee91b4 | 2012-05-14 12:29:36 +0100 | [diff] [blame] | 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 | int StringPiece::copy(char* buf, size_type n, size_type pos) const { |
| 37 | int ret = min(length_ - pos, n); |
| 38 | memcpy(buf, ptr_ + pos, ret); |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | int StringPiece::find(const StringPiece& s, size_type pos) const { |
| 43 | if (length_ < 0 || pos > static_cast<size_type>(length_)) |
| 44 | return npos; |
| 45 | |
| 46 | const char* result = std::search(ptr_ + pos, ptr_ + length_, |
| 47 | s.ptr_, s.ptr_ + s.length_); |
| 48 | const size_type xpos = result - ptr_; |
| 49 | return xpos + s.length_ <= length_ ? xpos : npos; |
| 50 | } |
| 51 | |
| 52 | int StringPiece::find(char c, size_type pos) const { |
| 53 | if (length_ <= 0 || pos >= static_cast<size_type>(length_)) { |
| 54 | return npos; |
| 55 | } |
| 56 | const char* result = std::find(ptr_ + pos, ptr_ + length_, c); |
| 57 | return result != ptr_ + length_ ? result - ptr_ : npos; |
| 58 | } |
| 59 | |
| 60 | int StringPiece::rfind(const StringPiece& s, size_type pos) const { |
| 61 | if (length_ < s.length_) return npos; |
| 62 | const size_t ulen = length_; |
| 63 | if (s.length_ == 0) return min(ulen, pos); |
| 64 | |
| 65 | const char* last = ptr_ + min(ulen - s.length_, pos) + s.length_; |
| 66 | const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); |
| 67 | return result != last ? result - ptr_ : npos; |
| 68 | } |
| 69 | |
| 70 | int StringPiece::rfind(char c, size_type pos) const { |
| 71 | if (length_ <= 0) return npos; |
| 72 | for (int i = min(pos, static_cast<size_type>(length_ - 1)); |
| 73 | i >= 0; --i) { |
| 74 | if (ptr_[i] == c) { |
| 75 | return i; |
| 76 | } |
| 77 | } |
| 78 | return npos; |
| 79 | } |
| 80 | |
| 81 | StringPiece StringPiece::substr(size_type pos, size_type n) const { |
| 82 | if (pos > length_) pos = length_; |
| 83 | if (n > length_ - pos) n = length_ - pos; |
| 84 | return StringPiece(ptr_ + pos, n); |
| 85 | } |
| 86 | |
| 87 | const StringPiece::size_type StringPiece::npos = size_type(-1); |