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 */
16
17 #include "stringpiece.h"
18
19 #include <ostream>
20 #include <utility>
21
22 #include "logging.h"
23
24 namespace art {
25
26 #if !defined(NDEBUG)
operator [](size_type i) const27 char StringPiece::operator[](size_type i) const {
28 CHECK_LT(i, length_);
29 return ptr_[i];
30 }
31 #endif
32
CopyToString(std::string * target) const33 void StringPiece::CopyToString(std::string* target) const {
34 target->assign(ptr_, length_);
35 }
36
copy(char * buf,size_type n,size_type pos) const37 StringPiece::size_type StringPiece::copy(char* buf, size_type n, size_type pos) const {
38 size_type ret = std::min(length_ - pos, n);
39 memcpy(buf, ptr_ + pos, ret);
40 return ret;
41 }
42
find(const StringPiece & s,size_type pos) const43 StringPiece::size_type StringPiece::find(const StringPiece& s, size_type pos) const {
44 if (length_ == 0 || pos > static_cast<size_type>(length_)) {
45 return npos;
46 }
47 const char* result = std::search(ptr_ + pos, ptr_ + length_, s.ptr_, s.ptr_ + s.length_);
48 const size_type xpos = result - ptr_;
49 return xpos + s.length_ <= length_ ? xpos : npos;
50 }
51
compare(const StringPiece & x) const52 int StringPiece::compare(const StringPiece& x) const {
53 int r = memcmp(ptr_, x.ptr_, std::min(length_, x.length_));
54 if (r == 0) {
55 if (length_ < x.length_) r = -1;
56 else if (length_ > x.length_) r = +1;
57 }
58 return r;
59 }
60
find(char c,size_type pos) const61 StringPiece::size_type StringPiece::find(char c, size_type pos) const {
62 if (length_ == 0 || pos >= length_) {
63 return npos;
64 }
65 const char* result = std::find(ptr_ + pos, ptr_ + length_, c);
66 return result != ptr_ + length_ ? result - ptr_ : npos;
67 }
68
rfind(const StringPiece & s,size_type pos) const69 StringPiece::size_type StringPiece::rfind(const StringPiece& s, size_type pos) const {
70 if (length_ < s.length_) return npos;
71 const size_t ulen = length_;
72 if (s.length_ == 0) return std::min(ulen, pos);
73
74 const char* last = ptr_ + std::min(ulen - s.length_, pos) + s.length_;
75 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
76 return result != last ? result - ptr_ : npos;
77 }
78
rfind(char c,size_type pos) const79 StringPiece::size_type StringPiece::rfind(char c, size_type pos) const {
80 if (length_ == 0) return npos;
81 for (int i = std::min(pos, static_cast<size_type>(length_ - 1));
82 i >= 0; --i) {
83 if (ptr_[i] == c) {
84 return i;
85 }
86 }
87 return npos;
88 }
89
substr(size_type pos,size_type n) const90 StringPiece StringPiece::substr(size_type pos, size_type n) const {
91 if (pos > static_cast<size_type>(length_)) pos = length_;
92 if (n > length_ - pos) n = length_ - pos;
93 return StringPiece(ptr_ + pos, n);
94 }
95
operator <<(std::ostream & o,const StringPiece & piece)96 std::ostream& operator<<(std::ostream& o, const StringPiece& piece) {
97 o.write(piece.data(), piece.size());
98 return o;
99 }
100
101 } // namespace art
102