Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 1 | // Copyright 2010 Google |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 14 | #include "stringpiece.h" |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 15 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | #include <iostream> |
| 17 | #include <utility> |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 18 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 19 | namespace art { |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 20 | |
| 21 | bool operator<(const art::StringPiece& x, const art::StringPiece& y) { |
| 22 | const int r = memcmp(x.data(), y.data(), |
| 23 | std::min(x.size(), y.size())); |
| 24 | return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
| 25 | } |
| 26 | |
| 27 | void StringPiece::CopyToString(std::string* target) const { |
| 28 | target->assign(ptr_, length_); |
| 29 | } |
| 30 | |
| 31 | int StringPiece::copy(char* buf, size_type n, size_type pos) const { |
| 32 | int ret = std::min(length_ - pos, n); |
| 33 | memcpy(buf, ptr_ + pos, ret); |
| 34 | return ret; |
| 35 | } |
| 36 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 37 | StringPiece::size_type StringPiece::find(const StringPiece& s, size_type pos) const { |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 38 | if (length_ < 0 || pos > static_cast<size_type>(length_)) |
| 39 | return npos; |
| 40 | |
| 41 | const char* result = std::search(ptr_ + pos, ptr_ + length_, |
| 42 | s.ptr_, s.ptr_ + s.length_); |
| 43 | const size_type xpos = result - ptr_; |
| 44 | return xpos + s.length_ <= static_cast<size_type>(length_) ? xpos : npos; |
| 45 | } |
| 46 | |
| 47 | int StringPiece::compare(const StringPiece& x) const { |
| 48 | int r = memcmp(ptr_, x.ptr_, std::min(length_, x.length_)); |
| 49 | if (r == 0) { |
| 50 | if (length_ < x.length_) r = -1; |
| 51 | else if (length_ > x.length_) r = +1; |
| 52 | } |
| 53 | return r; |
| 54 | } |
| 55 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 56 | StringPiece::size_type StringPiece::find(char c, size_type pos) const { |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 57 | if (length_ <= 0 || pos >= static_cast<size_type>(length_)) { |
| 58 | return npos; |
| 59 | } |
| 60 | const char* result = std::find(ptr_ + pos, ptr_ + length_, c); |
| 61 | return result != ptr_ + length_ ? result - ptr_ : npos; |
| 62 | } |
| 63 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 64 | StringPiece::size_type StringPiece::rfind(const StringPiece& s, size_type pos) const { |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 65 | if (length_ < s.length_) return npos; |
| 66 | const size_t ulen = length_; |
| 67 | if (s.length_ == 0) return std::min(ulen, pos); |
| 68 | |
| 69 | const char* last = ptr_ + std::min(ulen - s.length_, pos) + s.length_; |
| 70 | const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); |
| 71 | return result != last ? result - ptr_ : npos; |
| 72 | } |
| 73 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 74 | StringPiece::size_type StringPiece::rfind(char c, size_type pos) const { |
Carl Shapiro | d4e48fd | 2011-06-30 18:53:29 -0700 | [diff] [blame] | 75 | if (length_ <= 0) return npos; |
| 76 | for (int i = std::min(pos, static_cast<size_type>(length_ - 1)); |
| 77 | i >= 0; --i) { |
| 78 | if (ptr_[i] == c) { |
| 79 | return i; |
| 80 | } |
| 81 | } |
| 82 | return npos; |
| 83 | } |
| 84 | |
| 85 | StringPiece StringPiece::substr(size_type pos, size_type n) const { |
| 86 | if (pos > static_cast<size_type>(length_)) pos = length_; |
| 87 | if (n > length_ - pos) n = length_ - pos; |
| 88 | return StringPiece(ptr_ + pos, n); |
| 89 | } |
| 90 | |
| 91 | const StringPiece::size_type StringPiece::npos = size_type(-1); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 92 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 93 | std::ostream& operator<<(std::ostream& o, const art::StringPiece& piece) { |
| 94 | o.write(piece.data(), piece.size()); |
| 95 | return o; |
| 96 | } |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 97 | |
| 98 | } // namespace art |