00001
00002
00003
00004
00005
00006
00007 #ifndef __STRINGPIECE_H__
00008 #define __STRINGPIECE_H__
00009
00015 #include "unicode/utypes.h"
00016 #include "unicode/uobject.h"
00017 #include "unicode/std_string.h"
00018
00019
00020
00021 U_NAMESPACE_BEGIN
00022
00039 class U_COMMON_API StringPiece : public UMemory {
00040 private:
00041 const char* ptr_;
00042 int32_t length_;
00043
00044 public:
00049 StringPiece() : ptr_(NULL), length_(0) { }
00055 StringPiece(const char* str);
00056 #if U_HAVE_STD_STRING
00057
00061 StringPiece(const U_STD_NSQ string& str)
00062 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
00063 #endif
00064
00070 StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
00077 StringPiece(const StringPiece& x, int32_t pos);
00086 StringPiece(const StringPiece& x, int32_t pos, int32_t len);
00087
00098 const char* data() const { return ptr_; }
00104 int32_t size() const { return length_; }
00110 int32_t length() const { return length_; }
00116 UBool empty() const { return length_ == 0; }
00117
00122 void clear() { ptr_ = NULL; length_ = 0; }
00123
00129 void remove_prefix(int32_t n) {
00130 if (n >= 0) {
00131 if (n > length_) {
00132 n = length_;
00133 }
00134 ptr_ += n;
00135 length_ -= n;
00136 }
00137 }
00138
00144 void remove_suffix(int32_t n) {
00145 if (n >= 0) {
00146 if (n <= length_) {
00147 length_ -= n;
00148 } else {
00149 length_ = 0;
00150 }
00151 }
00152 }
00153
00158 static const int32_t npos = 0x7fffffff;
00159
00168 StringPiece substr(int32_t pos, int32_t len = npos) const {
00169 return StringPiece(*this, pos, len);
00170 }
00171 };
00172
00173 U_NAMESPACE_END
00174
00175 #endif // __STRINGPIECE_H__