00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef OR_TOOLS_BASE_STRINGPIECE_H_
00026 #define OR_TOOLS_BASE_STRINGPIECE_H_
00027
00028 #include <string.h>
00029 #include <algorithm>
00030 #include <iosfwd>
00031 #include <string>
00032 #include <cstddef>
00033
00034 namespace operations_research {
00035
00036 class StringPiece {
00037 private:
00038 const char* ptr_;
00039 int length_;
00040
00041 public:
00042
00043
00044
00045 StringPiece() : ptr_(NULL), length_(0) { }
00046 StringPiece(const char* str)
00047 : ptr_(str), length_((str == NULL) ? 0 : static_cast<int>(strlen(str))) { }
00048 StringPiece(const std::string& str)
00049 : ptr_(str.data()), length_(static_cast<int>(str.size())) { }
00050 StringPiece(const char* offset, int len) : ptr_(offset), length_(len) { }
00051
00052
00053
00054
00055
00056 const char* data() const { return ptr_; }
00057 int size() const { return length_; }
00058 int length() const { return length_; }
00059 bool empty() const { return length_ == 0; }
00060
00061 void clear() {
00062 ptr_ = NULL;
00063 length_ = 0;
00064 }
00065 void set(const char* data, int len) {
00066 ptr_ = data;
00067 length_ = len;
00068 }
00069 void set(const char* str) {
00070 ptr_ = str;
00071 if (str != NULL)
00072 length_ = static_cast<int>(strlen(str));
00073 else
00074 length_ = 0;
00075 }
00076 void set(const void* data, int len) {
00077 ptr_ = reinterpret_cast<const char*>(data);
00078 length_ = len;
00079 }
00080
00081 char operator[](int i) const { return ptr_[i]; }
00082
00083 void remove_prefix(int n) {
00084 ptr_ += n;
00085 length_ -= n;
00086 }
00087
00088 void remove_suffix(int n) {
00089 length_ -= n;
00090 }
00091
00092 int compare(const StringPiece& x) const;
00093
00094 std::string as_string() const {
00095 return std::string(data(), size());
00096 }
00097
00098
00099
00100
00101
00102 std::string ToString() const {
00103 return std::string(data(), size());
00104 }
00105
00106 void CopyToString(std::string* target) const;
00107 void AppendToString(std::string* target) const;
00108
00109
00110 bool starts_with(const StringPiece& x) const {
00111 return ((length_ >= x.length_) &&
00112 (memcmp(ptr_, x.ptr_, x.length_) == 0));
00113 }
00114
00115
00116 bool ends_with(const StringPiece& x) const {
00117 return ((length_ >= x.length_) &&
00118 (memcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0));
00119 }
00120
00121
00122 typedef char value_type;
00123 typedef const char* pointer;
00124 typedef const char& reference;
00125 typedef const char& const_reference;
00126 typedef size_t size_type;
00127 typedef ptrdiff_t difference_type;
00128 static const size_type npos;
00129 typedef const char* const_iterator;
00130 typedef const char* iterator;
00131 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00132 typedef std::reverse_iterator<iterator> reverse_iterator;
00133 iterator begin() const { return ptr_; }
00134 iterator end() const { return ptr_ + length_; }
00135 const_reverse_iterator rbegin() const {
00136 return const_reverse_iterator(ptr_ + length_);
00137 }
00138 const_reverse_iterator rend() const {
00139 return const_reverse_iterator(ptr_);
00140 }
00141
00142 int max_size() const { return length_; }
00143 int capacity() const { return length_; }
00144
00145 int copy(char* buf, size_type n, size_type pos = 0) const;
00146
00147 int find(const StringPiece& s, size_type pos = 0) const;
00148 int find(char c, size_type pos = 0) const;
00149 int rfind(const StringPiece& s, size_type pos = npos) const;
00150 int rfind(char c, size_type pos = npos) const;
00151
00152 StringPiece substr(size_type pos, size_type n = npos) const;
00153 };
00154
00155 }
00156
00157 bool operator==(const operations_research::StringPiece& x,
00158 const operations_research::StringPiece& y);
00159
00160 inline bool operator!=(const operations_research::StringPiece& x,
00161 const operations_research::StringPiece& y) {
00162 return !(x == y);
00163 }
00164
00165 bool operator<(const operations_research::StringPiece& x,
00166 const operations_research::StringPiece& y);
00167
00168 inline bool operator>(const operations_research::StringPiece& x,
00169 const operations_research::StringPiece& y) {
00170 return y < x;
00171 }
00172
00173 inline bool operator<=(const operations_research::StringPiece& x,
00174 const operations_research::StringPiece& y) {
00175 return !(x > y);
00176 }
00177
00178 inline bool operator>=(const operations_research::StringPiece& x,
00179 const operations_research::StringPiece& y) {
00180 return !(x < y);
00181 }
00182
00183
00184 extern std::ostream& operator<<(std::ostream& o,
00185 const operations_research::StringPiece& piece);
00186
00187 #endif // OR_TOOLS_BASE_STRINGPIECE_H_