00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __STRINGPIECE_H__
00021 #define __STRINGPIECE_H__
00022
00028 #include "unicode/utypes.h"
00029 #include "unicode/uobject.h"
00030 #include "unicode/std_string.h"
00031
00032
00033
00034 U_NAMESPACE_BEGIN
00035
00052 class U_COMMON_API StringPiece : public UMemory {
00053 private:
00054 const char* ptr_;
00055 int32_t length_;
00056
00057 public:
00062 StringPiece() : ptr_(NULL), length_(0) { }
00068 StringPiece(const char* str);
00069 #if U_HAVE_STD_STRING
00070
00074 StringPiece(const std::string& str)
00075 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
00076 #endif
00077
00083 StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
00090 StringPiece(const StringPiece& x, int32_t pos);
00099 StringPiece(const StringPiece& x, int32_t pos, int32_t len);
00100
00111 const char* data() const { return ptr_; }
00117 int32_t size() const { return length_; }
00123 int32_t length() const { return length_; }
00129 UBool empty() const { return length_ == 0; }
00130
00135 void clear() { ptr_ = NULL; length_ = 0; }
00136
00143 void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
00144
00150 void set(const char* str);
00151
00157 void remove_prefix(int32_t n) {
00158 if (n >= 0) {
00159 if (n > length_) {
00160 n = length_;
00161 }
00162 ptr_ += n;
00163 length_ -= n;
00164 }
00165 }
00166
00172 void remove_suffix(int32_t n) {
00173 if (n >= 0) {
00174 if (n <= length_) {
00175 length_ -= n;
00176 } else {
00177 length_ = 0;
00178 }
00179 }
00180 }
00181
00186 static const int32_t npos = 0x7fffffff;
00187
00196 StringPiece substr(int32_t pos, int32_t len = npos) const {
00197 return StringPiece(*this, pos, len);
00198 }
00199 };
00200
00208 U_EXPORT UBool U_EXPORT2
00209 operator==(const StringPiece& x, const StringPiece& y);
00210
00218 inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
00219 return !(x == y);
00220 }
00221
00222 U_NAMESPACE_END
00223
00224 #endif // __STRINGPIECE_H__