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