Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (C) 2009-2013, International Business Machines |
michael@0 | 2 | // Corporation and others. All Rights Reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Copyright 2001 and onwards Google Inc. |
michael@0 | 5 | // Author: Sanjay Ghemawat |
michael@0 | 6 | |
michael@0 | 7 | // This code is a contribution of Google code, and the style used here is |
michael@0 | 8 | // a compromise between the original Google code and the ICU coding guidelines. |
michael@0 | 9 | // For example, data types are ICU-ified (size_t,int->int32_t), |
michael@0 | 10 | // and API comments doxygen-ified, but function names and behavior are |
michael@0 | 11 | // as in the original, if possible. |
michael@0 | 12 | // Assertion-style error handling, not available in ICU, was changed to |
michael@0 | 13 | // parameter "pinning" similar to UnicodeString. |
michael@0 | 14 | // |
michael@0 | 15 | // In addition, this is only a partial port of the original Google code, |
michael@0 | 16 | // limited to what was needed so far. The (nearly) complete original code |
michael@0 | 17 | // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib |
michael@0 | 18 | // (see ICU ticket 6765, r25517). |
michael@0 | 19 | |
michael@0 | 20 | #ifndef __STRINGPIECE_H__ |
michael@0 | 21 | #define __STRINGPIECE_H__ |
michael@0 | 22 | |
michael@0 | 23 | /** |
michael@0 | 24 | * \file |
michael@0 | 25 | * \brief C++ API: StringPiece: Read-only byte string wrapper class. |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | #include "unicode/utypes.h" |
michael@0 | 29 | #include "unicode/uobject.h" |
michael@0 | 30 | #include "unicode/std_string.h" |
michael@0 | 31 | |
michael@0 | 32 | // Arghh! I wish C++ literals were "string". |
michael@0 | 33 | |
michael@0 | 34 | U_NAMESPACE_BEGIN |
michael@0 | 35 | |
michael@0 | 36 | /** |
michael@0 | 37 | * A string-like object that points to a sized piece of memory. |
michael@0 | 38 | * |
michael@0 | 39 | * We provide non-explicit singleton constructors so users can pass |
michael@0 | 40 | * in a "const char*" or a "string" wherever a "StringPiece" is |
michael@0 | 41 | * expected. |
michael@0 | 42 | * |
michael@0 | 43 | * Functions or methods may use const StringPiece& parameters to accept either |
michael@0 | 44 | * a "const char*" or a "string" value that will be implicitly converted to |
michael@0 | 45 | * a StringPiece. |
michael@0 | 46 | * |
michael@0 | 47 | * Systematic usage of StringPiece is encouraged as it will reduce unnecessary |
michael@0 | 48 | * conversions from "const char*" to "string" and back again. |
michael@0 | 49 | * |
michael@0 | 50 | * @stable ICU 4.2 |
michael@0 | 51 | */ |
michael@0 | 52 | class U_COMMON_API StringPiece : public UMemory { |
michael@0 | 53 | private: |
michael@0 | 54 | const char* ptr_; |
michael@0 | 55 | int32_t length_; |
michael@0 | 56 | |
michael@0 | 57 | public: |
michael@0 | 58 | /** |
michael@0 | 59 | * Default constructor, creates an empty StringPiece. |
michael@0 | 60 | * @stable ICU 4.2 |
michael@0 | 61 | */ |
michael@0 | 62 | StringPiece() : ptr_(NULL), length_(0) { } |
michael@0 | 63 | /** |
michael@0 | 64 | * Constructs from a NUL-terminated const char * pointer. |
michael@0 | 65 | * @param str a NUL-terminated const char * pointer |
michael@0 | 66 | * @stable ICU 4.2 |
michael@0 | 67 | */ |
michael@0 | 68 | StringPiece(const char* str); |
michael@0 | 69 | #if U_HAVE_STD_STRING |
michael@0 | 70 | /** |
michael@0 | 71 | * Constructs from a std::string. |
michael@0 | 72 | * @stable ICU 4.2 |
michael@0 | 73 | */ |
michael@0 | 74 | StringPiece(const std::string& str) |
michael@0 | 75 | : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { } |
michael@0 | 76 | #endif |
michael@0 | 77 | /** |
michael@0 | 78 | * Constructs from a const char * pointer and a specified length. |
michael@0 | 79 | * @param offset a const char * pointer (need not be terminated) |
michael@0 | 80 | * @param len the length of the string; must be non-negative |
michael@0 | 81 | * @stable ICU 4.2 |
michael@0 | 82 | */ |
michael@0 | 83 | StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { } |
michael@0 | 84 | /** |
michael@0 | 85 | * Substring of another StringPiece. |
michael@0 | 86 | * @param x the other StringPiece |
michael@0 | 87 | * @param pos start position in x; must be non-negative and <= x.length(). |
michael@0 | 88 | * @stable ICU 4.2 |
michael@0 | 89 | */ |
michael@0 | 90 | StringPiece(const StringPiece& x, int32_t pos); |
michael@0 | 91 | /** |
michael@0 | 92 | * Substring of another StringPiece. |
michael@0 | 93 | * @param x the other StringPiece |
michael@0 | 94 | * @param pos start position in x; must be non-negative and <= x.length(). |
michael@0 | 95 | * @param len length of the substring; |
michael@0 | 96 | * must be non-negative and will be pinned to at most x.length() - pos. |
michael@0 | 97 | * @stable ICU 4.2 |
michael@0 | 98 | */ |
michael@0 | 99 | StringPiece(const StringPiece& x, int32_t pos, int32_t len); |
michael@0 | 100 | |
michael@0 | 101 | /** |
michael@0 | 102 | * Returns the string pointer. May be NULL if it is empty. |
michael@0 | 103 | * |
michael@0 | 104 | * data() may return a pointer to a buffer with embedded NULs, and the |
michael@0 | 105 | * returned buffer may or may not be null terminated. Therefore it is |
michael@0 | 106 | * typically a mistake to pass data() to a routine that expects a NUL |
michael@0 | 107 | * terminated string. |
michael@0 | 108 | * @return the string pointer |
michael@0 | 109 | * @stable ICU 4.2 |
michael@0 | 110 | */ |
michael@0 | 111 | const char* data() const { return ptr_; } |
michael@0 | 112 | /** |
michael@0 | 113 | * Returns the string length. Same as length(). |
michael@0 | 114 | * @return the string length |
michael@0 | 115 | * @stable ICU 4.2 |
michael@0 | 116 | */ |
michael@0 | 117 | int32_t size() const { return length_; } |
michael@0 | 118 | /** |
michael@0 | 119 | * Returns the string length. Same as size(). |
michael@0 | 120 | * @return the string length |
michael@0 | 121 | * @stable ICU 4.2 |
michael@0 | 122 | */ |
michael@0 | 123 | int32_t length() const { return length_; } |
michael@0 | 124 | /** |
michael@0 | 125 | * Returns whether the string is empty. |
michael@0 | 126 | * @return TRUE if the string is empty |
michael@0 | 127 | * @stable ICU 4.2 |
michael@0 | 128 | */ |
michael@0 | 129 | UBool empty() const { return length_ == 0; } |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * Sets to an empty string. |
michael@0 | 133 | * @stable ICU 4.2 |
michael@0 | 134 | */ |
michael@0 | 135 | void clear() { ptr_ = NULL; length_ = 0; } |
michael@0 | 136 | |
michael@0 | 137 | /** |
michael@0 | 138 | * Reset the stringpiece to refer to new data. |
michael@0 | 139 | * @param xdata pointer the new string data. Need not be nul terminated. |
michael@0 | 140 | * @param len the length of the new data |
michael@0 | 141 | * @stable ICU 4.8 |
michael@0 | 142 | */ |
michael@0 | 143 | void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; } |
michael@0 | 144 | |
michael@0 | 145 | /** |
michael@0 | 146 | * Reset the stringpiece to refer to new data. |
michael@0 | 147 | * @param str a pointer to a NUL-terminated string. |
michael@0 | 148 | * @stable ICU 4.8 |
michael@0 | 149 | */ |
michael@0 | 150 | void set(const char* str); |
michael@0 | 151 | |
michael@0 | 152 | /** |
michael@0 | 153 | * Removes the first n string units. |
michael@0 | 154 | * @param n prefix length, must be non-negative and <=length() |
michael@0 | 155 | * @stable ICU 4.2 |
michael@0 | 156 | */ |
michael@0 | 157 | void remove_prefix(int32_t n) { |
michael@0 | 158 | if (n >= 0) { |
michael@0 | 159 | if (n > length_) { |
michael@0 | 160 | n = length_; |
michael@0 | 161 | } |
michael@0 | 162 | ptr_ += n; |
michael@0 | 163 | length_ -= n; |
michael@0 | 164 | } |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * Removes the last n string units. |
michael@0 | 169 | * @param n suffix length, must be non-negative and <=length() |
michael@0 | 170 | * @stable ICU 4.2 |
michael@0 | 171 | */ |
michael@0 | 172 | void remove_suffix(int32_t n) { |
michael@0 | 173 | if (n >= 0) { |
michael@0 | 174 | if (n <= length_) { |
michael@0 | 175 | length_ -= n; |
michael@0 | 176 | } else { |
michael@0 | 177 | length_ = 0; |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | /** |
michael@0 | 183 | * Maximum integer, used as a default value for substring methods. |
michael@0 | 184 | * @stable ICU 4.2 |
michael@0 | 185 | */ |
michael@0 | 186 | static const int32_t npos; // = 0x7fffffff; |
michael@0 | 187 | |
michael@0 | 188 | /** |
michael@0 | 189 | * Returns a substring of this StringPiece. |
michael@0 | 190 | * @param pos start position; must be non-negative and <= length(). |
michael@0 | 191 | * @param len length of the substring; |
michael@0 | 192 | * must be non-negative and will be pinned to at most length() - pos. |
michael@0 | 193 | * @return the substring StringPiece |
michael@0 | 194 | * @stable ICU 4.2 |
michael@0 | 195 | */ |
michael@0 | 196 | StringPiece substr(int32_t pos, int32_t len = npos) const { |
michael@0 | 197 | return StringPiece(*this, pos, len); |
michael@0 | 198 | } |
michael@0 | 199 | }; |
michael@0 | 200 | |
michael@0 | 201 | /** |
michael@0 | 202 | * Global operator == for StringPiece |
michael@0 | 203 | * @param x The first StringPiece to compare. |
michael@0 | 204 | * @param y The second StringPiece to compare. |
michael@0 | 205 | * @return TRUE if the string data is equal |
michael@0 | 206 | * @stable ICU 4.8 |
michael@0 | 207 | */ |
michael@0 | 208 | U_EXPORT UBool U_EXPORT2 |
michael@0 | 209 | operator==(const StringPiece& x, const StringPiece& y); |
michael@0 | 210 | |
michael@0 | 211 | /** |
michael@0 | 212 | * Global operator != for StringPiece |
michael@0 | 213 | * @param x The first StringPiece to compare. |
michael@0 | 214 | * @param y The second StringPiece to compare. |
michael@0 | 215 | * @return TRUE if the string data is not equal |
michael@0 | 216 | * @stable ICU 4.8 |
michael@0 | 217 | */ |
michael@0 | 218 | inline UBool operator!=(const StringPiece& x, const StringPiece& y) { |
michael@0 | 219 | return !(x == y); |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | U_NAMESPACE_END |
michael@0 | 223 | |
michael@0 | 224 | #endif // __STRINGPIECE_H__ |