00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __STRINGPIECE_H__
00023 #define __STRINGPIECE_H__
00024
00030 #include "unicode/utypes.h"
00031
00032 #if U_SHOW_CPLUSPLUS_API
00033
00034 #include <cstddef>
00035 #include <type_traits>
00036
00037 #include "unicode/uobject.h"
00038 #include "unicode/std_string.h"
00039
00040
00041
00042 U_NAMESPACE_BEGIN
00043
00060 class U_COMMON_API StringPiece : public UMemory {
00061 private:
00062 const char* ptr_;
00063 int32_t length_;
00064
00065 public:
00070 StringPiece() : ptr_(NULL), length_(0) { }
00076 StringPiece(const char* str);
00081 StringPiece(const std::string& str)
00082 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
00083 #ifndef U_HIDE_DRAFT_API
00084
00103 template <typename T,
00104 typename = typename std::enable_if<
00105 std::is_same<decltype(T().data()), const char*>::value &&
00106 std::is_same<decltype(T().size()), size_t>::value>::type>
00107 StringPiece(T str)
00108 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) {}
00109 #endif // U_HIDE_DRAFT_API
00110
00116 StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
00123 StringPiece(const StringPiece& x, int32_t pos);
00132 StringPiece(const StringPiece& x, int32_t pos, int32_t len);
00133
00144 const char* data() const { return ptr_; }
00150 int32_t size() const { return length_; }
00156 int32_t length() const { return length_; }
00162 UBool empty() const { return length_ == 0; }
00163
00168 void clear() { ptr_ = NULL; length_ = 0; }
00169
00176 void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
00177
00183 void set(const char* str);
00184
00190 void remove_prefix(int32_t n) {
00191 if (n >= 0) {
00192 if (n > length_) {
00193 n = length_;
00194 }
00195 ptr_ += n;
00196 length_ -= n;
00197 }
00198 }
00199
00205 void remove_suffix(int32_t n) {
00206 if (n >= 0) {
00207 if (n <= length_) {
00208 length_ -= n;
00209 } else {
00210 length_ = 0;
00211 }
00212 }
00213 }
00214
00219 static const int32_t npos;
00220
00229 StringPiece substr(int32_t pos, int32_t len = npos) const {
00230 return StringPiece(*this, pos, len);
00231 }
00232 };
00233
00241 U_EXPORT UBool U_EXPORT2
00242 operator==(const StringPiece& x, const StringPiece& y);
00243
00251 inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
00252 return !(x == y);
00253 }
00254
00255 U_NAMESPACE_END
00256
00257 #endif
00258
00259 #endif // __STRINGPIECE_H__