OLD | NEW |
(Empty) | |
| 1 #ifndef ADBLOCK_PLUS_STRING_H |
| 2 #define ADBLOCK_PLUS_STRING_H |
| 3 |
| 4 #include <cstddef> |
| 5 #include <cstring> |
| 6 #include <algorithm> |
| 7 |
| 8 #include "debug.h" |
| 9 |
| 10 inline void String_assert_readonly(bool readOnly); |
| 11 |
| 12 class String |
| 13 { |
| 14 friend class DependentString; |
| 15 friend class OwnedString; |
| 16 |
| 17 public: |
| 18 typedef char16_t value_type; |
| 19 typedef size_t size_type; |
| 20 |
| 21 // Type flags, stored in the top 2 bits of the mLen member |
| 22 static constexpr size_type INVALID = 0xC0000000; |
| 23 static constexpr size_type DELETED = 0x80000000; |
| 24 static constexpr size_type READ_ONLY = 0x40000000; |
| 25 static constexpr size_type READ_WRITE = 0x00000000; |
| 26 |
| 27 static constexpr size_type FLAGS_MASK = 0xC0000000; |
| 28 static constexpr size_type LENGTH_MASK = 0x3FFFFFFF; |
| 29 |
| 30 static constexpr size_type npos = -1; |
| 31 |
| 32 protected: |
| 33 value_type* mBuf; |
| 34 size_type mLen; |
| 35 |
| 36 String(value_type* buf, size_type len, size_type flags) |
| 37 : mBuf(buf), mLen((len & LENGTH_MASK) | flags) |
| 38 { |
| 39 } |
| 40 |
| 41 void reset(value_type* buf, size_type len, size_type flags) |
| 42 { |
| 43 mBuf = buf; |
| 44 mLen = (len & LENGTH_MASK) | flags; |
| 45 } |
| 46 |
| 47 public: |
| 48 size_type length() const |
| 49 { |
| 50 return mLen & LENGTH_MASK; |
| 51 } |
| 52 |
| 53 bool empty() const |
| 54 { |
| 55 return !(mLen & LENGTH_MASK); |
| 56 } |
| 57 |
| 58 const value_type* data() const |
| 59 { |
| 60 return mBuf; |
| 61 } |
| 62 |
| 63 value_type* data() |
| 64 { |
| 65 String_assert_readonly(is_readOnly()); |
| 66 return mBuf; |
| 67 } |
| 68 |
| 69 const value_type& operator[](size_type pos) const |
| 70 { |
| 71 return mBuf[pos]; |
| 72 } |
| 73 |
| 74 value_type& operator[](size_type pos) |
| 75 { |
| 76 String_assert_readonly(is_readOnly()); |
| 77 return mBuf[pos]; |
| 78 } |
| 79 |
| 80 bool is_readOnly() const |
| 81 { |
| 82 return (mLen & FLAGS_MASK) != READ_WRITE; |
| 83 } |
| 84 |
| 85 bool equals(const String& other) const |
| 86 { |
| 87 if (length() != other.length()) |
| 88 return false; |
| 89 |
| 90 return memcmp(mBuf, other.mBuf, sizeof(value_type) * length()) == 0; |
| 91 } |
| 92 |
| 93 size_type find(value_type c, size_type pos = 0) const |
| 94 { |
| 95 for (size_type i = pos; i < length(); ++i) |
| 96 if (mBuf[i] == c) |
| 97 return i; |
| 98 return npos; |
| 99 } |
| 100 |
| 101 size_type find(const String& str, size_type pos = 0) const |
| 102 { |
| 103 if (!str.length()) |
| 104 return pos; |
| 105 |
| 106 if (length() - pos < str.length()) |
| 107 return npos; |
| 108 |
| 109 for (; pos < length() - str.length(); ++pos) |
| 110 { |
| 111 if (mBuf[pos] == str[0] && |
| 112 memcmp(mBuf + pos, str.mBuf, sizeof(value_type) * str.length()) == 0) |
| 113 { |
| 114 return pos; |
| 115 } |
| 116 } |
| 117 |
| 118 return npos; |
| 119 } |
| 120 |
| 121 size_type rfind(value_type c, size_type pos = npos) const |
| 122 { |
| 123 if (length() == 0) |
| 124 return npos; |
| 125 |
| 126 if (pos == npos) |
| 127 pos = length() - 1; |
| 128 |
| 129 for (int i = pos; i >= 0; --i) |
| 130 if (mBuf[i] == c) |
| 131 return i; |
| 132 return npos; |
| 133 } |
| 134 }; |
| 135 |
| 136 class DependentString : public String |
| 137 { |
| 138 public: |
| 139 DependentString() |
| 140 : String(nullptr, 0, INVALID) |
| 141 { |
| 142 } |
| 143 |
| 144 DependentString(value_type* buf, size_type len) |
| 145 : String(buf, len, READ_WRITE) |
| 146 { |
| 147 } |
| 148 |
| 149 DependentString(const value_type* buf, size_type len) |
| 150 : String(const_cast<value_type*>(buf), len, READ_ONLY) |
| 151 { |
| 152 } |
| 153 |
| 154 DependentString(String& str, size_type pos = 0, size_type len = npos) |
| 155 : String( |
| 156 str.mBuf + std::min(pos, str.length()), |
| 157 std::min(len, str.length() - std::min(pos, str.length())), |
| 158 READ_WRITE |
| 159 ) |
| 160 { |
| 161 } |
| 162 |
| 163 DependentString(const String& str, size_type pos = 0, size_type len = npos) |
| 164 : String( |
| 165 str.mBuf + std::min(pos, str.length()), |
| 166 std::min(len, str.length() - std::min(pos, str.length())), |
| 167 READ_ONLY |
| 168 ) |
| 169 { |
| 170 } |
| 171 |
| 172 void reset(value_type* buf, size_type len) |
| 173 { |
| 174 *this = DependentString(buf, len); |
| 175 } |
| 176 |
| 177 void reset(const value_type* buf, size_type len) |
| 178 { |
| 179 *this = DependentString(buf, len); |
| 180 } |
| 181 |
| 182 void reset(String& str, size_type pos = 0, size_type len = npos) |
| 183 { |
| 184 *this = DependentString(str, pos, len); |
| 185 } |
| 186 |
| 187 void reset(const String& str, size_type pos = 0, size_type len = npos) |
| 188 { |
| 189 *this = DependentString(str, pos, len); |
| 190 } |
| 191 |
| 192 bool is_invalid() const |
| 193 { |
| 194 return (mLen & FLAGS_MASK) == INVALID; |
| 195 } |
| 196 |
| 197 bool is_deleted() const |
| 198 { |
| 199 return (mLen & FLAGS_MASK) == DELETED; |
| 200 } |
| 201 }; |
| 202 |
| 203 class OwnedString : public String |
| 204 { |
| 205 private: |
| 206 value_type* allocate(size_type len) |
| 207 { |
| 208 if (len) |
| 209 return new value_type[len]; |
| 210 else |
| 211 return nullptr; |
| 212 } |
| 213 |
| 214 void resize(size_type newLength, bool copy) |
| 215 { |
| 216 size_type oldLength = length(); |
| 217 value_type* oldBuffer = mBuf; |
| 218 |
| 219 reset(nullptr, newLength, READ_WRITE); |
| 220 newLength = length(); |
| 221 mBuf = allocate(newLength); |
| 222 annotate_address(mBuf, "String"); |
| 223 |
| 224 if (copy && oldLength) |
| 225 memcpy(mBuf, oldBuffer, sizeof(value_type) * std::min(oldLength, newLength
)); |
| 226 if (oldBuffer) |
| 227 delete[] oldBuffer; |
| 228 } |
| 229 |
| 230 public: |
| 231 OwnedString(size_type len = 0) |
| 232 : String(nullptr, len, READ_WRITE) |
| 233 { |
| 234 mBuf = allocate(length()); |
| 235 annotate_address(mBuf, "String"); |
| 236 } |
| 237 |
| 238 OwnedString(const String& str) |
| 239 : OwnedString(str.length()) |
| 240 { |
| 241 memcpy(mBuf, str.mBuf, sizeof(value_type) * length()); |
| 242 } |
| 243 |
| 244 OwnedString(const OwnedString& str) |
| 245 : OwnedString(static_cast<const String&>(str)) |
| 246 { |
| 247 } |
| 248 |
| 249 OwnedString(const value_type* str, size_type len) |
| 250 : OwnedString(DependentString(str, len)) |
| 251 { |
| 252 } |
| 253 |
| 254 OwnedString(OwnedString&& str) |
| 255 : OwnedString(str.length()) |
| 256 { |
| 257 mBuf = str.mBuf; |
| 258 str.mBuf = nullptr; |
| 259 str.mLen = READ_WRITE | 0; |
| 260 } |
| 261 |
| 262 OwnedString(const char* source, size_type len) |
| 263 : OwnedString(len) |
| 264 { |
| 265 for (size_type i = 0; i < len; i++) |
| 266 mBuf[i] = source[i]; |
| 267 } |
| 268 |
| 269 ~OwnedString() |
| 270 { |
| 271 if (mBuf) |
| 272 delete[] mBuf; |
| 273 } |
| 274 |
| 275 OwnedString& operator=(const String& str) |
| 276 { |
| 277 *this = std::move(OwnedString(str)); |
| 278 return *this; |
| 279 } |
| 280 |
| 281 OwnedString& operator=(const OwnedString& str) |
| 282 { |
| 283 *this = std::move(OwnedString(str)); |
| 284 return *this; |
| 285 } |
| 286 |
| 287 OwnedString& operator=(OwnedString&& str) |
| 288 { |
| 289 mBuf = str.mBuf; |
| 290 mLen = str.mLen; |
| 291 str.mBuf = nullptr; |
| 292 str.mLen = READ_WRITE | 0; |
| 293 return *this; |
| 294 } |
| 295 |
| 296 void append(const value_type* source, size_type sourceLen) |
| 297 { |
| 298 if (!sourceLen) |
| 299 return; |
| 300 |
| 301 size_t oldLength = length(); |
| 302 resize(oldLength + sourceLen, true); |
| 303 memcpy(mBuf + oldLength, source, sizeof(value_type) * sourceLen); |
| 304 } |
| 305 |
| 306 void append(const String& str) |
| 307 { |
| 308 append(str.mBuf, str.length()); |
| 309 } |
| 310 |
| 311 void append(value_type c) |
| 312 { |
| 313 append(&c, 1); |
| 314 } |
| 315 }; |
| 316 |
| 317 inline DependentString operator "" _str(const String::value_type* str, |
| 318 String::size_type len) |
| 319 { |
| 320 return DependentString(str, len); |
| 321 } |
| 322 |
| 323 inline void String_assert_readonly(bool readOnly) |
| 324 { |
| 325 assert(!readOnly, u"Writing access to a read-only string"_str); |
| 326 } |
| 327 |
| 328 #endif |
OLD | NEW |