ICU 73.2  73.2
stringpiece.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 // Copyright (C) 2009-2013, International Business Machines
00004 // Corporation and others. All Rights Reserved.
00005 //
00006 // Copyright 2001 and onwards Google Inc.
00007 // Author: Sanjay Ghemawat
00008 
00009 // This code is a contribution of Google code, and the style used here is
00010 // a compromise between the original Google code and the ICU coding guidelines.
00011 // For example, data types are ICU-ified (size_t,int->int32_t),
00012 // and API comments doxygen-ified, but function names and behavior are
00013 // as in the original, if possible.
00014 // Assertion-style error handling, not available in ICU, was changed to
00015 // parameter "pinning" similar to UnicodeString.
00016 //
00017 // In addition, this is only a partial port of the original Google code,
00018 // limited to what was needed so far. The (nearly) complete original code
00019 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
00020 // (see ICU ticket 6765, r25517).
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 // Arghh!  I wish C++ literals were "string".
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 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
00079 
00084   StringPiece(const char8_t* str) : StringPiece(reinterpret_cast<const char*>(str)) {}
00085 #endif
00086 
00092   StringPiece(std::nullptr_t p) : ptr_(p), length_(0) {}
00093 
00098   StringPiece(const std::string& str)
00099     : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
00100 #if defined(__cpp_lib_char8_t) || defined(U_IN_DOXYGEN)
00101 
00105   StringPiece(const std::u8string& str)
00106     : ptr_(reinterpret_cast<const char*>(str.data())),
00107       length_(static_cast<int32_t>(str.size())) { }
00108 #endif
00109 
00132   template <typename T,
00133             typename = typename std::enable_if<
00134                 (std::is_same<decltype(T().data()), const char*>::value
00135 #if defined(__cpp_char8_t)
00136                     || std::is_same<decltype(T().data()), const char8_t*>::value
00137 #endif
00138                 ) &&
00139                 std::is_same<decltype(T().size()), size_t>::value>::type>
00140   StringPiece(T str)
00141       : ptr_(reinterpret_cast<const char*>(str.data())),
00142         length_(static_cast<int32_t>(str.size())) {}
00143 
00150   StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
00151 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
00152 
00158   StringPiece(const char8_t* str, int32_t len) :
00159       StringPiece(reinterpret_cast<const char*>(str), len) {}
00160 #endif
00161 
00168   StringPiece(const StringPiece& x, int32_t pos);
00177   StringPiece(const StringPiece& x, int32_t pos, int32_t len);
00178 
00189   const char* data() const { return ptr_; }
00195   int32_t size() const { return length_; }
00201   int32_t length() const { return length_; }
00207   UBool empty() const { return length_ == 0; }
00208 
00213   void clear() { ptr_ = nullptr; length_ = 0; }
00214 
00221   void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
00222 
00228   void set(const char* str);
00229 
00230 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
00231 
00237   inline void set(const char8_t* xdata, int32_t len) {
00238       set(reinterpret_cast<const char*>(xdata), len);
00239   }
00240 
00246   inline void set(const char8_t* str) {
00247       set(reinterpret_cast<const char*>(str));
00248   }
00249 #endif
00250 
00256   void remove_prefix(int32_t n) {
00257     if (n >= 0) {
00258       if (n > length_) {
00259         n = length_;
00260       }
00261       ptr_ += n;
00262       length_ -= n;
00263     }
00264   }
00265 
00271   void remove_suffix(int32_t n) {
00272     if (n >= 0) {
00273       if (n <= length_) {
00274         length_ -= n;
00275       } else {
00276         length_ = 0;
00277       }
00278     }
00279   }
00280 
00288   int32_t find(StringPiece needle, int32_t offset);
00289 
00297   int32_t compare(StringPiece other);
00298 
00303   static const int32_t npos; // = 0x7fffffff;
00304 
00313   StringPiece substr(int32_t pos, int32_t len = npos) const {
00314     return StringPiece(*this, pos, len);
00315   }
00316 };
00317 
00325 U_EXPORT UBool U_EXPORT2 
00326 operator==(const StringPiece& x, const StringPiece& y);
00327 
00335 inline bool operator!=(const StringPiece& x, const StringPiece& y) {
00336   return !(x == y);
00337 }
00338 
00339 U_NAMESPACE_END
00340 
00341 #endif /* U_SHOW_CPLUSPLUS_API */
00342 
00343 #endif  // __STRINGPIECE_H__