00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <iostream>
00015 #include <utility>
00016 #include "base/stringpiece.h"
00017
00018 using operations_research::StringPiece;
00019
00020 std::ostream& operator<<(std::ostream& o, const StringPiece& piece) {
00021 o.write(piece.data(), piece.size());
00022 return o;
00023 }
00024
00025 bool operator==(const StringPiece& x, const StringPiece& y) {
00026 int len = x.size();
00027 if (len != y.size()) {
00028 return false;
00029 }
00030 const char* p = x.data();
00031 const char* p2 = y.data();
00032
00033 if ((len > 0) && (p[len-1] != p2[len-1])) return false;
00034 const char* p_limit = p + len;
00035 for (; p < p_limit; p++, p2++) {
00036 if (*p != *p2)
00037 return false;
00038 }
00039 return true;
00040 }
00041
00042 bool operator<(const operations_research::StringPiece& x,
00043 const operations_research::StringPiece& y) {
00044 const int r = memcmp(x.data(), y.data(),
00045 std::min(x.size(), y.size()));
00046 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
00047 }
00048
00049 void StringPiece::CopyToString(std::string* target) const {
00050 target->assign(ptr_, length_);
00051 }
00052
00053 int StringPiece::copy(char* buf, size_type n, size_type pos) const {
00054 int ret = std::min(length_ - pos, n);
00055 memcpy(buf, ptr_ + pos, ret);
00056 return ret;
00057 }
00058
00059 int StringPiece::find(const StringPiece& s, size_type pos) const {
00060 if (length_ < 0 || pos > static_cast<size_type>(length_))
00061 return npos;
00062
00063 const char* result = std::search(ptr_ + pos, ptr_ + length_,
00064 s.ptr_, s.ptr_ + s.length_);
00065 const size_type xpos = result - ptr_;
00066 return xpos + s.length_ <= length_ ? xpos : npos;
00067 }
00068
00069 int StringPiece::compare(const StringPiece& x) const {
00070 int r = memcmp(ptr_, x.ptr_, std::min(length_, x.length_));
00071 if (r == 0) {
00072 if (length_ < x.length_) r = -1;
00073 else if (length_ > x.length_) r = +1;
00074 }
00075 return r;
00076 }
00077
00078 int StringPiece::find(char c, size_type pos) const {
00079 if (length_ <= 0 || pos >= static_cast<size_type>(length_)) {
00080 return npos;
00081 }
00082 const char* result = std::find(ptr_ + pos, ptr_ + length_, c);
00083 return result != ptr_ + length_ ? result - ptr_ : npos;
00084 }
00085
00086 int StringPiece::rfind(const StringPiece& s, size_type pos) const {
00087 if (length_ < s.length_) return npos;
00088 const size_t ulen = length_;
00089 if (s.length_ == 0) return std::min(ulen, pos);
00090
00091 const char* last = ptr_ + std::min(ulen - s.length_, pos) + s.length_;
00092 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
00093 return result != last ? result - ptr_ : npos;
00094 }
00095
00096 int StringPiece::rfind(char c, size_type pos) const {
00097 if (length_ <= 0) return npos;
00098 for (int i = std::min(pos, static_cast<size_type>(length_ - 1));
00099 i >= 0; --i) {
00100 if (ptr_[i] == c) {
00101 return i;
00102 }
00103 }
00104 return npos;
00105 }
00106
00107 StringPiece StringPiece::substr(size_type pos, size_type n) const {
00108 if (pos > length_) pos = length_;
00109 if (n > length_ - pos) n = length_ - pos;
00110 return StringPiece(ptr_ + pos, n);
00111 }
00112
00113 const StringPiece::size_type StringPiece::npos = size_type(-1);