1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker // Copyright (C) 2009-2013, International Business Machines 4*0e209d39SAndroid Build Coastguard Worker // Corporation and others. All Rights Reserved. 5*0e209d39SAndroid Build Coastguard Worker // 6*0e209d39SAndroid Build Coastguard Worker // Copyright 2001 and onwards Google Inc. 7*0e209d39SAndroid Build Coastguard Worker // Author: Sanjay Ghemawat 8*0e209d39SAndroid Build Coastguard Worker 9*0e209d39SAndroid Build Coastguard Worker // This code is a contribution of Google code, and the style used here is 10*0e209d39SAndroid Build Coastguard Worker // a compromise between the original Google code and the ICU coding guidelines. 11*0e209d39SAndroid Build Coastguard Worker // For example, data types are ICU-ified (size_t,int->int32_t), 12*0e209d39SAndroid Build Coastguard Worker // and API comments doxygen-ified, but function names and behavior are 13*0e209d39SAndroid Build Coastguard Worker // as in the original, if possible. 14*0e209d39SAndroid Build Coastguard Worker // Assertion-style error handling, not available in ICU, was changed to 15*0e209d39SAndroid Build Coastguard Worker // parameter "pinning" similar to UnicodeString. 16*0e209d39SAndroid Build Coastguard Worker // 17*0e209d39SAndroid Build Coastguard Worker // In addition, this is only a partial port of the original Google code, 18*0e209d39SAndroid Build Coastguard Worker // limited to what was needed so far. The (nearly) complete original code 19*0e209d39SAndroid Build Coastguard Worker // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib 20*0e209d39SAndroid Build Coastguard Worker // (see ICU ticket 6765, r25517). 21*0e209d39SAndroid Build Coastguard Worker 22*0e209d39SAndroid Build Coastguard Worker #ifndef __STRINGPIECE_H__ 23*0e209d39SAndroid Build Coastguard Worker #define __STRINGPIECE_H__ 24*0e209d39SAndroid Build Coastguard Worker 25*0e209d39SAndroid Build Coastguard Worker /** 26*0e209d39SAndroid Build Coastguard Worker * \file 27*0e209d39SAndroid Build Coastguard Worker * \brief C++ API: StringPiece: Read-only byte string wrapper class. 28*0e209d39SAndroid Build Coastguard Worker */ 29*0e209d39SAndroid Build Coastguard Worker 30*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 31*0e209d39SAndroid Build Coastguard Worker 32*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API 33*0e209d39SAndroid Build Coastguard Worker 34*0e209d39SAndroid Build Coastguard Worker #include <cstddef> 35*0e209d39SAndroid Build Coastguard Worker #include <type_traits> 36*0e209d39SAndroid Build Coastguard Worker 37*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h" 38*0e209d39SAndroid Build Coastguard Worker #include "unicode/std_string.h" 39*0e209d39SAndroid Build Coastguard Worker 40*0e209d39SAndroid Build Coastguard Worker // Arghh! I wish C++ literals were "string". 41*0e209d39SAndroid Build Coastguard Worker 42*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 43*0e209d39SAndroid Build Coastguard Worker 44*0e209d39SAndroid Build Coastguard Worker /** 45*0e209d39SAndroid Build Coastguard Worker * A string-like object that points to a sized piece of memory. 46*0e209d39SAndroid Build Coastguard Worker * 47*0e209d39SAndroid Build Coastguard Worker * We provide non-explicit singleton constructors so users can pass 48*0e209d39SAndroid Build Coastguard Worker * in a "const char*" or a "string" wherever a "StringPiece" is 49*0e209d39SAndroid Build Coastguard Worker * expected. 50*0e209d39SAndroid Build Coastguard Worker * 51*0e209d39SAndroid Build Coastguard Worker * Functions or methods may use StringPiece parameters to accept either a 52*0e209d39SAndroid Build Coastguard Worker * "const char*" or a "string" value that will be implicitly converted to a 53*0e209d39SAndroid Build Coastguard Worker * StringPiece. 54*0e209d39SAndroid Build Coastguard Worker * 55*0e209d39SAndroid Build Coastguard Worker * Systematic usage of StringPiece is encouraged as it will reduce unnecessary 56*0e209d39SAndroid Build Coastguard Worker * conversions from "const char*" to "string" and back again. 57*0e209d39SAndroid Build Coastguard Worker * 58*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 59*0e209d39SAndroid Build Coastguard Worker */ 60*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API StringPiece : public UMemory { 61*0e209d39SAndroid Build Coastguard Worker private: 62*0e209d39SAndroid Build Coastguard Worker const char* ptr_; 63*0e209d39SAndroid Build Coastguard Worker int32_t length_; 64*0e209d39SAndroid Build Coastguard Worker 65*0e209d39SAndroid Build Coastguard Worker public: 66*0e209d39SAndroid Build Coastguard Worker /** 67*0e209d39SAndroid Build Coastguard Worker * Default constructor, creates an empty StringPiece. 68*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 69*0e209d39SAndroid Build Coastguard Worker */ StringPiece()70*0e209d39SAndroid Build Coastguard Worker StringPiece() : ptr_(nullptr), length_(0) { } 71*0e209d39SAndroid Build Coastguard Worker 72*0e209d39SAndroid Build Coastguard Worker /** 73*0e209d39SAndroid Build Coastguard Worker * Constructs from a NUL-terminated const char * pointer. 74*0e209d39SAndroid Build Coastguard Worker * @param str a NUL-terminated const char * pointer 75*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 76*0e209d39SAndroid Build Coastguard Worker */ 77*0e209d39SAndroid Build Coastguard Worker StringPiece(const char* str); 78*0e209d39SAndroid Build Coastguard Worker #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN) 79*0e209d39SAndroid Build Coastguard Worker /** 80*0e209d39SAndroid Build Coastguard Worker * Constructs from a NUL-terminated const char8_t * pointer. 81*0e209d39SAndroid Build Coastguard Worker * @param str a NUL-terminated const char8_t * pointer 82*0e209d39SAndroid Build Coastguard Worker * @stable ICU 67 83*0e209d39SAndroid Build Coastguard Worker */ StringPiece(const char8_t * str)84*0e209d39SAndroid Build Coastguard Worker StringPiece(const char8_t* str) : StringPiece(reinterpret_cast<const char*>(str)) {} 85*0e209d39SAndroid Build Coastguard Worker #endif 86*0e209d39SAndroid Build Coastguard Worker /** 87*0e209d39SAndroid Build Coastguard Worker * Constructs an empty StringPiece. 88*0e209d39SAndroid Build Coastguard Worker * Needed for type disambiguation from multiple other overloads. 89*0e209d39SAndroid Build Coastguard Worker * @param p nullptr 90*0e209d39SAndroid Build Coastguard Worker * @stable ICU 67 91*0e209d39SAndroid Build Coastguard Worker */ StringPiece(std::nullptr_t p)92*0e209d39SAndroid Build Coastguard Worker StringPiece(std::nullptr_t p) : ptr_(p), length_(0) {} 93*0e209d39SAndroid Build Coastguard Worker 94*0e209d39SAndroid Build Coastguard Worker /** 95*0e209d39SAndroid Build Coastguard Worker * Constructs from a std::string. 96*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 97*0e209d39SAndroid Build Coastguard Worker */ StringPiece(const std::string & str)98*0e209d39SAndroid Build Coastguard Worker StringPiece(const std::string& str) 99*0e209d39SAndroid Build Coastguard Worker : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { } 100*0e209d39SAndroid Build Coastguard Worker #if defined(__cpp_lib_char8_t) || defined(U_IN_DOXYGEN) 101*0e209d39SAndroid Build Coastguard Worker /** 102*0e209d39SAndroid Build Coastguard Worker * Constructs from a std::u8string. 103*0e209d39SAndroid Build Coastguard Worker * @stable ICU 67 104*0e209d39SAndroid Build Coastguard Worker */ StringPiece(const std::u8string & str)105*0e209d39SAndroid Build Coastguard Worker StringPiece(const std::u8string& str) 106*0e209d39SAndroid Build Coastguard Worker : ptr_(reinterpret_cast<const char*>(str.data())), 107*0e209d39SAndroid Build Coastguard Worker length_(static_cast<int32_t>(str.size())) { } 108*0e209d39SAndroid Build Coastguard Worker #endif 109*0e209d39SAndroid Build Coastguard Worker 110*0e209d39SAndroid Build Coastguard Worker /** 111*0e209d39SAndroid Build Coastguard Worker * Constructs from some other implementation of a string piece class, from any 112*0e209d39SAndroid Build Coastguard Worker * C++ record type that has these two methods: 113*0e209d39SAndroid Build Coastguard Worker * 114*0e209d39SAndroid Build Coastguard Worker * \code{.cpp} 115*0e209d39SAndroid Build Coastguard Worker * 116*0e209d39SAndroid Build Coastguard Worker * struct OtherStringPieceClass { 117*0e209d39SAndroid Build Coastguard Worker * const char* data(); // or const char8_t* 118*0e209d39SAndroid Build Coastguard Worker * size_t size(); 119*0e209d39SAndroid Build Coastguard Worker * }; 120*0e209d39SAndroid Build Coastguard Worker * 121*0e209d39SAndroid Build Coastguard Worker * \endcode 122*0e209d39SAndroid Build Coastguard Worker * 123*0e209d39SAndroid Build Coastguard Worker * The other string piece class will typically be std::string_view from C++17 124*0e209d39SAndroid Build Coastguard Worker * or absl::string_view from Abseil. 125*0e209d39SAndroid Build Coastguard Worker * 126*0e209d39SAndroid Build Coastguard Worker * Starting with C++20, data() may also return a const char8_t* pointer, 127*0e209d39SAndroid Build Coastguard Worker * as from std::u8string_view. 128*0e209d39SAndroid Build Coastguard Worker * 129*0e209d39SAndroid Build Coastguard Worker * @param str the other string piece 130*0e209d39SAndroid Build Coastguard Worker * @stable ICU 65 131*0e209d39SAndroid Build Coastguard Worker */ 132*0e209d39SAndroid Build Coastguard Worker template <typename T, 133*0e209d39SAndroid Build Coastguard Worker typename = std::enable_if_t< 134*0e209d39SAndroid Build Coastguard Worker (std::is_same_v<decltype(T().data()), const char*> 135*0e209d39SAndroid Build Coastguard Worker #if defined(__cpp_char8_t) 136*0e209d39SAndroid Build Coastguard Worker || std::is_same_v<decltype(T().data()), const char8_t*> 137*0e209d39SAndroid Build Coastguard Worker #endif 138*0e209d39SAndroid Build Coastguard Worker ) && 139*0e209d39SAndroid Build Coastguard Worker std::is_same_v<decltype(T().size()), size_t>>> StringPiece(T str)140*0e209d39SAndroid Build Coastguard Worker StringPiece(T str) 141*0e209d39SAndroid Build Coastguard Worker : ptr_(reinterpret_cast<const char*>(str.data())), 142*0e209d39SAndroid Build Coastguard Worker length_(static_cast<int32_t>(str.size())) {} 143*0e209d39SAndroid Build Coastguard Worker 144*0e209d39SAndroid Build Coastguard Worker /** 145*0e209d39SAndroid Build Coastguard Worker * Constructs from a const char * pointer and a specified length. 146*0e209d39SAndroid Build Coastguard Worker * @param offset a const char * pointer (need not be terminated) 147*0e209d39SAndroid Build Coastguard Worker * @param len the length of the string; must be non-negative 148*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 149*0e209d39SAndroid Build Coastguard Worker */ StringPiece(const char * offset,int32_t len)150*0e209d39SAndroid Build Coastguard Worker StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { } 151*0e209d39SAndroid Build Coastguard Worker #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN) 152*0e209d39SAndroid Build Coastguard Worker /** 153*0e209d39SAndroid Build Coastguard Worker * Constructs from a const char8_t * pointer and a specified length. 154*0e209d39SAndroid Build Coastguard Worker * @param str a const char8_t * pointer (need not be terminated) 155*0e209d39SAndroid Build Coastguard Worker * @param len the length of the string; must be non-negative 156*0e209d39SAndroid Build Coastguard Worker * @stable ICU 67 157*0e209d39SAndroid Build Coastguard Worker */ StringPiece(const char8_t * str,int32_t len)158*0e209d39SAndroid Build Coastguard Worker StringPiece(const char8_t* str, int32_t len) : 159*0e209d39SAndroid Build Coastguard Worker StringPiece(reinterpret_cast<const char*>(str), len) {} 160*0e209d39SAndroid Build Coastguard Worker #endif 161*0e209d39SAndroid Build Coastguard Worker 162*0e209d39SAndroid Build Coastguard Worker /** 163*0e209d39SAndroid Build Coastguard Worker * Substring of another StringPiece. 164*0e209d39SAndroid Build Coastguard Worker * @param x the other StringPiece 165*0e209d39SAndroid Build Coastguard Worker * @param pos start position in x; must be non-negative and <= x.length(). 166*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 167*0e209d39SAndroid Build Coastguard Worker */ 168*0e209d39SAndroid Build Coastguard Worker StringPiece(const StringPiece& x, int32_t pos); 169*0e209d39SAndroid Build Coastguard Worker /** 170*0e209d39SAndroid Build Coastguard Worker * Substring of another StringPiece. 171*0e209d39SAndroid Build Coastguard Worker * @param x the other StringPiece 172*0e209d39SAndroid Build Coastguard Worker * @param pos start position in x; must be non-negative and <= x.length(). 173*0e209d39SAndroid Build Coastguard Worker * @param len length of the substring; 174*0e209d39SAndroid Build Coastguard Worker * must be non-negative and will be pinned to at most x.length() - pos. 175*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 176*0e209d39SAndroid Build Coastguard Worker */ 177*0e209d39SAndroid Build Coastguard Worker StringPiece(const StringPiece& x, int32_t pos, int32_t len); 178*0e209d39SAndroid Build Coastguard Worker 179*0e209d39SAndroid Build Coastguard Worker /** 180*0e209d39SAndroid Build Coastguard Worker * Returns the string pointer. May be nullptr if it is empty. 181*0e209d39SAndroid Build Coastguard Worker * 182*0e209d39SAndroid Build Coastguard Worker * data() may return a pointer to a buffer with embedded NULs, and the 183*0e209d39SAndroid Build Coastguard Worker * returned buffer may or may not be null terminated. Therefore it is 184*0e209d39SAndroid Build Coastguard Worker * typically a mistake to pass data() to a routine that expects a NUL 185*0e209d39SAndroid Build Coastguard Worker * terminated string. 186*0e209d39SAndroid Build Coastguard Worker * @return the string pointer 187*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 188*0e209d39SAndroid Build Coastguard Worker */ data()189*0e209d39SAndroid Build Coastguard Worker const char* data() const { return ptr_; } 190*0e209d39SAndroid Build Coastguard Worker /** 191*0e209d39SAndroid Build Coastguard Worker * Returns the string length. Same as length(). 192*0e209d39SAndroid Build Coastguard Worker * @return the string length 193*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 194*0e209d39SAndroid Build Coastguard Worker */ size()195*0e209d39SAndroid Build Coastguard Worker int32_t size() const { return length_; } 196*0e209d39SAndroid Build Coastguard Worker /** 197*0e209d39SAndroid Build Coastguard Worker * Returns the string length. Same as size(). 198*0e209d39SAndroid Build Coastguard Worker * @return the string length 199*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 200*0e209d39SAndroid Build Coastguard Worker */ length()201*0e209d39SAndroid Build Coastguard Worker int32_t length() const { return length_; } 202*0e209d39SAndroid Build Coastguard Worker /** 203*0e209d39SAndroid Build Coastguard Worker * Returns whether the string is empty. 204*0e209d39SAndroid Build Coastguard Worker * @return true if the string is empty 205*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 206*0e209d39SAndroid Build Coastguard Worker */ empty()207*0e209d39SAndroid Build Coastguard Worker UBool empty() const { return length_ == 0; } 208*0e209d39SAndroid Build Coastguard Worker 209*0e209d39SAndroid Build Coastguard Worker /** 210*0e209d39SAndroid Build Coastguard Worker * Sets to an empty string. 211*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 212*0e209d39SAndroid Build Coastguard Worker */ clear()213*0e209d39SAndroid Build Coastguard Worker void clear() { ptr_ = nullptr; length_ = 0; } 214*0e209d39SAndroid Build Coastguard Worker 215*0e209d39SAndroid Build Coastguard Worker /** 216*0e209d39SAndroid Build Coastguard Worker * Reset the stringpiece to refer to new data. 217*0e209d39SAndroid Build Coastguard Worker * @param xdata pointer the new string data. Need not be nul terminated. 218*0e209d39SAndroid Build Coastguard Worker * @param len the length of the new data 219*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 220*0e209d39SAndroid Build Coastguard Worker */ set(const char * xdata,int32_t len)221*0e209d39SAndroid Build Coastguard Worker void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; } 222*0e209d39SAndroid Build Coastguard Worker 223*0e209d39SAndroid Build Coastguard Worker /** 224*0e209d39SAndroid Build Coastguard Worker * Reset the stringpiece to refer to new data. 225*0e209d39SAndroid Build Coastguard Worker * @param str a pointer to a NUL-terminated string. 226*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 227*0e209d39SAndroid Build Coastguard Worker */ 228*0e209d39SAndroid Build Coastguard Worker void set(const char* str); 229*0e209d39SAndroid Build Coastguard Worker 230*0e209d39SAndroid Build Coastguard Worker #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN) 231*0e209d39SAndroid Build Coastguard Worker /** 232*0e209d39SAndroid Build Coastguard Worker * Resets the stringpiece to refer to new data. 233*0e209d39SAndroid Build Coastguard Worker * @param xdata pointer the new string data. Need not be NUL-terminated. 234*0e209d39SAndroid Build Coastguard Worker * @param len the length of the new data 235*0e209d39SAndroid Build Coastguard Worker * @stable ICU 67 236*0e209d39SAndroid Build Coastguard Worker */ set(const char8_t * xdata,int32_t len)237*0e209d39SAndroid Build Coastguard Worker inline void set(const char8_t* xdata, int32_t len) { 238*0e209d39SAndroid Build Coastguard Worker set(reinterpret_cast<const char*>(xdata), len); 239*0e209d39SAndroid Build Coastguard Worker } 240*0e209d39SAndroid Build Coastguard Worker 241*0e209d39SAndroid Build Coastguard Worker /** 242*0e209d39SAndroid Build Coastguard Worker * Resets the stringpiece to refer to new data. 243*0e209d39SAndroid Build Coastguard Worker * @param str a pointer to a NUL-terminated string. 244*0e209d39SAndroid Build Coastguard Worker * @stable ICU 67 245*0e209d39SAndroid Build Coastguard Worker */ set(const char8_t * str)246*0e209d39SAndroid Build Coastguard Worker inline void set(const char8_t* str) { 247*0e209d39SAndroid Build Coastguard Worker set(reinterpret_cast<const char*>(str)); 248*0e209d39SAndroid Build Coastguard Worker } 249*0e209d39SAndroid Build Coastguard Worker #endif 250*0e209d39SAndroid Build Coastguard Worker 251*0e209d39SAndroid Build Coastguard Worker /** 252*0e209d39SAndroid Build Coastguard Worker * Removes the first n string units. 253*0e209d39SAndroid Build Coastguard Worker * @param n prefix length, must be non-negative and <=length() 254*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 255*0e209d39SAndroid Build Coastguard Worker */ remove_prefix(int32_t n)256*0e209d39SAndroid Build Coastguard Worker void remove_prefix(int32_t n) { 257*0e209d39SAndroid Build Coastguard Worker if (n >= 0) { 258*0e209d39SAndroid Build Coastguard Worker if (n > length_) { 259*0e209d39SAndroid Build Coastguard Worker n = length_; 260*0e209d39SAndroid Build Coastguard Worker } 261*0e209d39SAndroid Build Coastguard Worker ptr_ += n; 262*0e209d39SAndroid Build Coastguard Worker length_ -= n; 263*0e209d39SAndroid Build Coastguard Worker } 264*0e209d39SAndroid Build Coastguard Worker } 265*0e209d39SAndroid Build Coastguard Worker 266*0e209d39SAndroid Build Coastguard Worker /** 267*0e209d39SAndroid Build Coastguard Worker * Removes the last n string units. 268*0e209d39SAndroid Build Coastguard Worker * @param n suffix length, must be non-negative and <=length() 269*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 270*0e209d39SAndroid Build Coastguard Worker */ remove_suffix(int32_t n)271*0e209d39SAndroid Build Coastguard Worker void remove_suffix(int32_t n) { 272*0e209d39SAndroid Build Coastguard Worker if (n >= 0) { 273*0e209d39SAndroid Build Coastguard Worker if (n <= length_) { 274*0e209d39SAndroid Build Coastguard Worker length_ -= n; 275*0e209d39SAndroid Build Coastguard Worker } else { 276*0e209d39SAndroid Build Coastguard Worker length_ = 0; 277*0e209d39SAndroid Build Coastguard Worker } 278*0e209d39SAndroid Build Coastguard Worker } 279*0e209d39SAndroid Build Coastguard Worker } 280*0e209d39SAndroid Build Coastguard Worker 281*0e209d39SAndroid Build Coastguard Worker /** 282*0e209d39SAndroid Build Coastguard Worker * Searches the StringPiece for the given search string (needle); 283*0e209d39SAndroid Build Coastguard Worker * @param needle The string for which to search. 284*0e209d39SAndroid Build Coastguard Worker * @param offset Where to start searching within this string (haystack). 285*0e209d39SAndroid Build Coastguard Worker * @return The offset of needle in haystack, or -1 if not found. 286*0e209d39SAndroid Build Coastguard Worker * @stable ICU 67 287*0e209d39SAndroid Build Coastguard Worker */ 288*0e209d39SAndroid Build Coastguard Worker int32_t find(StringPiece needle, int32_t offset); 289*0e209d39SAndroid Build Coastguard Worker 290*0e209d39SAndroid Build Coastguard Worker /** 291*0e209d39SAndroid Build Coastguard Worker * Compares this StringPiece with the other StringPiece, with semantics 292*0e209d39SAndroid Build Coastguard Worker * similar to std::string::compare(). 293*0e209d39SAndroid Build Coastguard Worker * @param other The string to compare to. 294*0e209d39SAndroid Build Coastguard Worker * @return below zero if this < other; above zero if this > other; 0 if this == other. 295*0e209d39SAndroid Build Coastguard Worker * @stable ICU 67 296*0e209d39SAndroid Build Coastguard Worker */ 297*0e209d39SAndroid Build Coastguard Worker int32_t compare(StringPiece other); 298*0e209d39SAndroid Build Coastguard Worker 299*0e209d39SAndroid Build Coastguard Worker /** 300*0e209d39SAndroid Build Coastguard Worker * Maximum integer, used as a default value for substring methods. 301*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 302*0e209d39SAndroid Build Coastguard Worker */ 303*0e209d39SAndroid Build Coastguard Worker static const int32_t npos; // = 0x7fffffff; 304*0e209d39SAndroid Build Coastguard Worker 305*0e209d39SAndroid Build Coastguard Worker /** 306*0e209d39SAndroid Build Coastguard Worker * Returns a substring of this StringPiece. 307*0e209d39SAndroid Build Coastguard Worker * @param pos start position; must be non-negative and <= length(). 308*0e209d39SAndroid Build Coastguard Worker * @param len length of the substring; 309*0e209d39SAndroid Build Coastguard Worker * must be non-negative and will be pinned to at most length() - pos. 310*0e209d39SAndroid Build Coastguard Worker * @return the substring StringPiece 311*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.2 312*0e209d39SAndroid Build Coastguard Worker */ 313*0e209d39SAndroid Build Coastguard Worker StringPiece substr(int32_t pos, int32_t len = npos) const { 314*0e209d39SAndroid Build Coastguard Worker return StringPiece(*this, pos, len); 315*0e209d39SAndroid Build Coastguard Worker } 316*0e209d39SAndroid Build Coastguard Worker }; 317*0e209d39SAndroid Build Coastguard Worker 318*0e209d39SAndroid Build Coastguard Worker /** 319*0e209d39SAndroid Build Coastguard Worker * Global operator == for StringPiece 320*0e209d39SAndroid Build Coastguard Worker * @param x The first StringPiece to compare. 321*0e209d39SAndroid Build Coastguard Worker * @param y The second StringPiece to compare. 322*0e209d39SAndroid Build Coastguard Worker * @return true if the string data is equal 323*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 324*0e209d39SAndroid Build Coastguard Worker */ 325*0e209d39SAndroid Build Coastguard Worker U_EXPORT UBool U_EXPORT2 326*0e209d39SAndroid Build Coastguard Worker operator==(const StringPiece& x, const StringPiece& y); 327*0e209d39SAndroid Build Coastguard Worker 328*0e209d39SAndroid Build Coastguard Worker /** 329*0e209d39SAndroid Build Coastguard Worker * Global operator != for StringPiece 330*0e209d39SAndroid Build Coastguard Worker * @param x The first StringPiece to compare. 331*0e209d39SAndroid Build Coastguard Worker * @param y The second StringPiece to compare. 332*0e209d39SAndroid Build Coastguard Worker * @return true if the string data is not equal 333*0e209d39SAndroid Build Coastguard Worker * @stable ICU 4.8 334*0e209d39SAndroid Build Coastguard Worker */ 335*0e209d39SAndroid Build Coastguard Worker inline bool operator!=(const StringPiece& x, const StringPiece& y) { 336*0e209d39SAndroid Build Coastguard Worker return !(x == y); 337*0e209d39SAndroid Build Coastguard Worker } 338*0e209d39SAndroid Build Coastguard Worker 339*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 340*0e209d39SAndroid Build Coastguard Worker 341*0e209d39SAndroid Build Coastguard Worker #endif /* U_SHOW_CPLUSPLUS_API */ 342*0e209d39SAndroid Build Coastguard Worker 343*0e209d39SAndroid Build Coastguard Worker #endif // __STRINGPIECE_H__ 344