blob: 37795508d6863884f1bedf325218c9961ecb6738 [file] [log] [blame]
Carl Shapirod4e48fd2011-06-30 18:53:29 -07001// 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 Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "stringpiece.h"
Carl Shapirod4e48fd2011-06-30 18:53:29 -070015
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016#include <iostream>
17#include <utility>
Carl Shapirod4e48fd2011-06-30 18:53:29 -070018
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070019namespace art {
Carl Shapirod4e48fd2011-06-30 18:53:29 -070020
21bool 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
27void StringPiece::CopyToString(std::string* target) const {
28 target->assign(ptr_, length_);
29}
30
31int 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 Carlstrom6cc18452011-07-18 15:10:33 -070037StringPiece::size_type StringPiece::find(const StringPiece& s, size_type pos) const {
Carl Shapirod4e48fd2011-06-30 18:53:29 -070038 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
47int 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 Carlstrom6cc18452011-07-18 15:10:33 -070056StringPiece::size_type StringPiece::find(char c, size_type pos) const {
Carl Shapirod4e48fd2011-06-30 18:53:29 -070057 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 Carlstrom6cc18452011-07-18 15:10:33 -070064StringPiece::size_type StringPiece::rfind(const StringPiece& s, size_type pos) const {
Carl Shapirod4e48fd2011-06-30 18:53:29 -070065 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 Carlstrom6cc18452011-07-18 15:10:33 -070074StringPiece::size_type StringPiece::rfind(char c, size_type pos) const {
Carl Shapirod4e48fd2011-06-30 18:53:29 -070075 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
85StringPiece 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
91const StringPiece::size_type StringPiece::npos = size_type(-1);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070092
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070093std::ostream& operator<<(std::ostream& o, const art::StringPiece& piece) {
94 o.write(piece.data(), piece.size());
95 return o;
96}
Elliott Hughes1f359b02011-07-17 14:27:17 -070097
98} // namespace art