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 U_STD_NSQ 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
00142 void remove_prefix(int32_t n) {
00143 if (n >= 0) {
00144 if (n > length_) {
00145 n = length_;
00146 }
00147 ptr_ += n;
00148 length_ -= n;
00149 }
00150 }
00151
00157 void remove_suffix(int32_t n) {
00158 if (n >= 0) {
00159 if (n <= length_) {
00160 length_ -= n;
00161 } else {
00162 length_ = 0;
00163 }
00164 }
00165 }
00166
00171 static const int32_t npos = 0x7fffffff;
00172
00181 StringPiece substr(int32_t pos, int32_t len = npos) const {
00182 return StringPiece(*this, pos, len);
00183 }
00184 };
00185
00186 U_NAMESPACE_END
00187
00188 #endif // __STRINGPIECE_H__