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_(nullptr), length_(0) { }
00071
00077 StringPiece(const char* str);
00078 #ifndef U_HIDE_DRAFT_API
00079 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
00080
00085 StringPiece(const char8_t* str) : StringPiece(reinterpret_cast<const char*>(str)) {}
00086 #endif
00087
00093 StringPiece(std::nullptr_t p) : ptr_(p), length_(0) {}
00094 #endif // U_HIDE_DRAFT_API
00095
00100 StringPiece(const std::string& str)
00101 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
00102 #ifndef U_HIDE_DRAFT_API
00103 #if defined(__cpp_lib_char8_t) || defined(U_IN_DOXYGEN)
00104
00108 StringPiece(const std::u8string& str)
00109 : ptr_(reinterpret_cast<const char*>(str.data())),
00110 length_(static_cast<int32_t>(str.size())) { }
00111 #endif
00112 #endif // U_HIDE_DRAFT_API
00113
00114 #ifndef U_HIDE_DRAFT_API
00115
00137 template <typename T,
00138 typename = typename std::enable_if<
00139 (std::is_same<decltype(T().data()), const char*>::value
00140 #if defined(__cpp_char8_t)
00141 || std::is_same<decltype(T().data()), const char8_t*>::value
00142 #endif
00143 ) &&
00144 std::is_same<decltype(T().size()), size_t>::value>::type>
00145 StringPiece(T str)
00146 : ptr_(reinterpret_cast<const char*>(str.data())),
00147 length_(static_cast<int32_t>(str.size())) {}
00148 #endif // U_HIDE_DRAFT_API
00149
00156 StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
00157 #ifndef U_HIDE_DRAFT_API
00158 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
00159
00165 StringPiece(const char8_t* str, int32_t len) :
00166 StringPiece(reinterpret_cast<const char*>(str), len) {}
00167 #endif
00168 #endif // U_HIDE_DRAFT_API
00169
00176 StringPiece(const StringPiece& x, int32_t pos);
00185 StringPiece(const StringPiece& x, int32_t pos, int32_t len);
00186
00197 const char* data() const { return ptr_; }
00203 int32_t size() const { return length_; }
00209 int32_t length() const { return length_; }
00215 UBool empty() const { return length_ == 0; }
00216
00221 void clear() { ptr_ = nullptr; length_ = 0; }
00222
00229 void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
00230
00236 void set(const char* str);
00237
00238 #ifndef U_HIDE_DRAFT_API
00239 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
00240
00246 inline void set(const char8_t* xdata, int32_t len) {
00247 set(reinterpret_cast<const char*>(xdata), len);
00248 }
00249
00255 inline void set(const char8_t* str) {
00256 set(reinterpret_cast<const char*>(str));
00257 }
00258 #endif
00259 #endif // U_HIDE_DRAFT_API
00260
00266 void remove_prefix(int32_t n) {
00267 if (n >= 0) {
00268 if (n > length_) {
00269 n = length_;
00270 }
00271 ptr_ += n;
00272 length_ -= n;
00273 }
00274 }
00275
00281 void remove_suffix(int32_t n) {
00282 if (n >= 0) {
00283 if (n <= length_) {
00284 length_ -= n;
00285 } else {
00286 length_ = 0;
00287 }
00288 }
00289 }
00290
00291 #ifndef U_HIDE_DRAFT_API
00292
00299 int32_t find(StringPiece needle, int32_t offset);
00300
00308 int32_t compare(StringPiece other);
00309 #endif // U_HIDE_DRAFT_API
00310
00315 static const int32_t npos;
00316
00325 StringPiece substr(int32_t pos, int32_t len = npos) const {
00326 return StringPiece(*this, pos, len);
00327 }
00328 };
00329
00337 U_EXPORT UBool U_EXPORT2
00338 operator==(const StringPiece& x, const StringPiece& y);
00339
00347 inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
00348 return !(x == y);
00349 }
00350
00351 U_NAMESPACE_END
00352
00353 #endif
00354
00355 #endif // __STRINGPIECE_H__