Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/stringpiece.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 // Copyright (C) 2009-2013, International Business Machines
0004 // Corporation and others. All Rights Reserved.
0005 //
0006 // Copyright 2001 and onwards Google Inc.
0007 // Author: Sanjay Ghemawat
0008 
0009 // This code is a contribution of Google code, and the style used here is
0010 // a compromise between the original Google code and the ICU coding guidelines.
0011 // For example, data types are ICU-ified (size_t,int->int32_t),
0012 // and API comments doxygen-ified, but function names and behavior are
0013 // as in the original, if possible.
0014 // Assertion-style error handling, not available in ICU, was changed to
0015 // parameter "pinning" similar to UnicodeString.
0016 //
0017 // In addition, this is only a partial port of the original Google code,
0018 // limited to what was needed so far. The (nearly) complete original code
0019 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
0020 // (see ICU ticket 6765, r25517).
0021 
0022 #ifndef __STRINGPIECE_H__
0023 #define __STRINGPIECE_H__
0024 
0025 /**
0026  * \file 
0027  * \brief C++ API: StringPiece: Read-only byte string wrapper class.
0028  */
0029 
0030 #include "unicode/utypes.h"
0031 
0032 #if U_SHOW_CPLUSPLUS_API
0033 
0034 #include <cstddef>
0035 #include <type_traits>
0036 
0037 #include "unicode/uobject.h"
0038 #include "unicode/std_string.h"
0039 
0040 // Arghh!  I wish C++ literals were "string".
0041 
0042 U_NAMESPACE_BEGIN
0043 
0044 /**
0045  * A string-like object that points to a sized piece of memory.
0046  *
0047  * We provide non-explicit singleton constructors so users can pass
0048  * in a "const char*" or a "string" wherever a "StringPiece" is
0049  * expected.
0050  *
0051  * Functions or methods may use StringPiece parameters to accept either a
0052  * "const char*" or a "string" value that will be implicitly converted to a
0053  * StringPiece.
0054  *
0055  * Systematic usage of StringPiece is encouraged as it will reduce unnecessary
0056  * conversions from "const char*" to "string" and back again.
0057  *
0058  * @stable ICU 4.2
0059  */
0060 class U_COMMON_API StringPiece : public UMemory {
0061  private:
0062   const char*   ptr_;
0063   int32_t       length_;
0064 
0065  public:
0066   /**
0067    * Default constructor, creates an empty StringPiece.
0068    * @stable ICU 4.2
0069    */
0070   StringPiece() : ptr_(nullptr), length_(0) { }
0071 
0072   /**
0073    * Constructs from a NUL-terminated const char * pointer.
0074    * @param str a NUL-terminated const char * pointer
0075    * @stable ICU 4.2
0076    */
0077   StringPiece(const char* str);
0078 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
0079   /**
0080    * Constructs from a NUL-terminated const char8_t * pointer.
0081    * @param str a NUL-terminated const char8_t * pointer
0082    * @stable ICU 67
0083    */
0084   StringPiece(const char8_t* str) : StringPiece(reinterpret_cast<const char*>(str)) {}
0085 #endif
0086   /**
0087    * Constructs an empty StringPiece.
0088    * Needed for type disambiguation from multiple other overloads.
0089    * @param p nullptr
0090    * @stable ICU 67
0091    */
0092   StringPiece(std::nullptr_t p) : ptr_(p), length_(0) {}
0093 
0094   /**
0095    * Constructs from a std::string.
0096    * @stable ICU 4.2
0097    */
0098   StringPiece(const std::string& str)
0099     : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
0100 #if defined(__cpp_lib_char8_t) || defined(U_IN_DOXYGEN)
0101   /**
0102    * Constructs from a std::u8string.
0103    * @stable ICU 67
0104    */
0105   StringPiece(const std::u8string& str)
0106     : ptr_(reinterpret_cast<const char*>(str.data())),
0107       length_(static_cast<int32_t>(str.size())) { }
0108 #endif
0109 
0110   /**
0111    * Constructs from some other implementation of a string piece class, from any
0112    * C++ record type that has these two methods:
0113    *
0114    * \code{.cpp}
0115    *
0116    *   struct OtherStringPieceClass {
0117    *     const char* data();  // or const char8_t*
0118    *     size_t size();
0119    *   };
0120    *
0121    * \endcode
0122    *
0123    * The other string piece class will typically be std::string_view from C++17
0124    * or absl::string_view from Abseil.
0125    *
0126    * Starting with C++20, data() may also return a const char8_t* pointer,
0127    * as from std::u8string_view.
0128    *
0129    * @param str the other string piece
0130    * @stable ICU 65
0131    */
0132   template <typename T,
0133             typename = typename std::enable_if<
0134                 (std::is_same<decltype(T().data()), const char*>::value
0135 #if defined(__cpp_char8_t)
0136                     || std::is_same<decltype(T().data()), const char8_t*>::value
0137 #endif
0138                 ) &&
0139                 std::is_same<decltype(T().size()), size_t>::value>::type>
0140   StringPiece(T str)
0141       : ptr_(reinterpret_cast<const char*>(str.data())),
0142         length_(static_cast<int32_t>(str.size())) {}
0143 
0144   /**
0145    * Constructs from a const char * pointer and a specified length.
0146    * @param offset a const char * pointer (need not be terminated)
0147    * @param len the length of the string; must be non-negative
0148    * @stable ICU 4.2
0149    */
0150   StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
0151 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
0152   /**
0153    * Constructs from a const char8_t * pointer and a specified length.
0154    * @param str a const char8_t * pointer (need not be terminated)
0155    * @param len the length of the string; must be non-negative
0156    * @stable ICU 67
0157    */
0158   StringPiece(const char8_t* str, int32_t len) :
0159       StringPiece(reinterpret_cast<const char*>(str), len) {}
0160 #endif
0161 
0162   /**
0163    * Substring of another StringPiece.
0164    * @param x the other StringPiece
0165    * @param pos start position in x; must be non-negative and <= x.length().
0166    * @stable ICU 4.2
0167    */
0168   StringPiece(const StringPiece& x, int32_t pos);
0169   /**
0170    * Substring of another StringPiece.
0171    * @param x the other StringPiece
0172    * @param pos start position in x; must be non-negative and <= x.length().
0173    * @param len length of the substring;
0174    *            must be non-negative and will be pinned to at most x.length() - pos.
0175    * @stable ICU 4.2
0176    */
0177   StringPiece(const StringPiece& x, int32_t pos, int32_t len);
0178 
0179   /**
0180    * Returns the string pointer. May be nullptr if it is empty.
0181    *
0182    * data() may return a pointer to a buffer with embedded NULs, and the
0183    * returned buffer may or may not be null terminated.  Therefore it is
0184    * typically a mistake to pass data() to a routine that expects a NUL
0185    * terminated string.
0186    * @return the string pointer
0187    * @stable ICU 4.2
0188    */
0189   const char* data() const { return ptr_; }
0190   /**
0191    * Returns the string length. Same as length().
0192    * @return the string length
0193    * @stable ICU 4.2
0194    */
0195   int32_t size() const { return length_; }
0196   /**
0197    * Returns the string length. Same as size().
0198    * @return the string length
0199    * @stable ICU 4.2
0200    */
0201   int32_t length() const { return length_; }
0202   /**
0203    * Returns whether the string is empty.
0204    * @return true if the string is empty
0205    * @stable ICU 4.2
0206    */
0207   UBool empty() const { return length_ == 0; }
0208 
0209   /**
0210    * Sets to an empty string.
0211    * @stable ICU 4.2
0212    */
0213   void clear() { ptr_ = nullptr; length_ = 0; }
0214 
0215   /**
0216    * Reset the stringpiece to refer to new data.
0217    * @param xdata pointer the new string data.  Need not be nul terminated.
0218    * @param len the length of the new data
0219    * @stable ICU 4.8
0220    */
0221   void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
0222 
0223   /**
0224    * Reset the stringpiece to refer to new data.
0225    * @param str a pointer to a NUL-terminated string. 
0226    * @stable ICU 4.8
0227    */
0228   void set(const char* str);
0229 
0230 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
0231   /**
0232    * Resets the stringpiece to refer to new data.
0233    * @param xdata pointer the new string data. Need not be NUL-terminated.
0234    * @param len the length of the new data
0235    * @stable ICU 67
0236    */
0237   inline void set(const char8_t* xdata, int32_t len) {
0238       set(reinterpret_cast<const char*>(xdata), len);
0239   }
0240 
0241   /**
0242    * Resets the stringpiece to refer to new data.
0243    * @param str a pointer to a NUL-terminated string.
0244    * @stable ICU 67
0245    */
0246   inline void set(const char8_t* str) {
0247       set(reinterpret_cast<const char*>(str));
0248   }
0249 #endif
0250 
0251   /**
0252    * Removes the first n string units.
0253    * @param n prefix length, must be non-negative and <=length()
0254    * @stable ICU 4.2
0255    */
0256   void remove_prefix(int32_t n) {
0257     if (n >= 0) {
0258       if (n > length_) {
0259         n = length_;
0260       }
0261       ptr_ += n;
0262       length_ -= n;
0263     }
0264   }
0265 
0266   /**
0267    * Removes the last n string units.
0268    * @param n suffix length, must be non-negative and <=length()
0269    * @stable ICU 4.2
0270    */
0271   void remove_suffix(int32_t n) {
0272     if (n >= 0) {
0273       if (n <= length_) {
0274         length_ -= n;
0275       } else {
0276         length_ = 0;
0277       }
0278     }
0279   }
0280 
0281   /**
0282    * Searches the StringPiece for the given search string (needle);
0283    * @param needle The string for which to search.
0284    * @param offset Where to start searching within this string (haystack).
0285    * @return The offset of needle in haystack, or -1 if not found.
0286    * @stable ICU 67
0287    */
0288   int32_t find(StringPiece needle, int32_t offset);
0289 
0290   /**
0291    * Compares this StringPiece with the other StringPiece, with semantics
0292    * similar to std::string::compare().
0293    * @param other The string to compare to.
0294    * @return below zero if this < other; above zero if this > other; 0 if this == other.
0295    * @stable ICU 67
0296    */
0297   int32_t compare(StringPiece other);
0298 
0299   /**
0300    * Maximum integer, used as a default value for substring methods.
0301    * @stable ICU 4.2
0302    */
0303   static const int32_t npos; // = 0x7fffffff;
0304 
0305   /**
0306    * Returns a substring of this StringPiece.
0307    * @param pos start position; must be non-negative and <= length().
0308    * @param len length of the substring;
0309    *            must be non-negative and will be pinned to at most length() - pos.
0310    * @return the substring StringPiece
0311    * @stable ICU 4.2
0312    */
0313   StringPiece substr(int32_t pos, int32_t len = npos) const {
0314     return StringPiece(*this, pos, len);
0315   }
0316 };
0317 
0318 /**
0319  * Global operator == for StringPiece
0320  * @param x The first StringPiece to compare.
0321  * @param y The second StringPiece to compare.
0322  * @return true if the string data is equal
0323  * @stable ICU 4.8
0324  */
0325 U_EXPORT UBool U_EXPORT2 
0326 operator==(const StringPiece& x, const StringPiece& y);
0327 
0328 /**
0329  * Global operator != for StringPiece
0330  * @param x The first StringPiece to compare.
0331  * @param y The second StringPiece to compare.
0332  * @return true if the string data is not equal
0333  * @stable ICU 4.8
0334  */
0335 inline bool operator!=(const StringPiece& x, const StringPiece& y) {
0336   return !(x == y);
0337 }
0338 
0339 U_NAMESPACE_END
0340 
0341 #endif /* U_SHOW_CPLUSPLUS_API */
0342 
0343 #endif  // __STRINGPIECE_H__