| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #pragma once | 18 #pragma once |
| 19 | 19 |
| 20 #include <algorithm> | 20 #include <algorithm> |
| 21 #include <cstddef> | 21 #include <cstddef> |
| 22 #include <cstring> | 22 #include <cstring> |
| 23 #include <type_traits> | 23 #include <type_traits> |
| 24 #ifdef INSIDE_TESTS |
| 25 #include <iostream> |
| 26 #include <codecvt> |
| 27 #endif |
| 24 | 28 |
| 25 #include "base.h" | 29 #include "base.h" |
| 26 #include "debug.h" | 30 #include "debug.h" |
| 27 #include "library.h" | 31 #include "library.h" |
| 28 | 32 |
| 29 ABP_NS_BEGIN | 33 ABP_NS_BEGIN |
| 30 | 34 |
| 31 inline void String_assert_writable(bool isWritable); | 35 inline void String_assert_writable(bool isWritable); |
| 32 | 36 |
| 33 class String | 37 class String |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 if (currChar >= u'A' && currChar <= u'Z') | 193 if (currChar >= u'A' && currChar <= u'Z') |
| 190 mBuf[i] = currChar + u'a' - u'A'; | 194 mBuf[i] = currChar + u'a' - u'A'; |
| 191 else if (currChar >= 128) | 195 else if (currChar >= 128) |
| 192 { | 196 { |
| 193 mBuf[i] = CharToLower(currChar); | 197 mBuf[i] = CharToLower(currChar); |
| 194 } | 198 } |
| 195 } | 199 } |
| 196 } | 200 } |
| 197 }; | 201 }; |
| 198 | 202 |
| 203 #ifdef INSIDE_TESTS |
| 204 inline std::ostream& operator<<(std::ostream& os, const String& str) |
| 205 { |
| 206 std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter; |
| 207 os << converter.to_bytes(str.data(), str.data() + str.length()); |
| 208 return os; |
| 209 } |
| 210 #endif |
| 211 |
| 199 class DependentString : public String | 212 class DependentString : public String |
| 200 { | 213 { |
| 201 public: | 214 public: |
| 202 explicit DependentString() | 215 explicit DependentString() |
| 203 : String(nullptr, 0, INVALID) | 216 : String(nullptr, 0, INVALID) |
| 204 { | 217 { |
| 205 } | 218 } |
| 206 | 219 |
| 207 explicit DependentString(value_type* buf, size_type len) | 220 explicit DependentString(value_type* buf, size_type len) |
| 208 : String(buf, len, READ_WRITE) | 221 : String(buf, len, READ_WRITE) |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 *this = DependentString(str, pos, len); | 266 *this = DependentString(str, pos, len); |
| 254 } | 267 } |
| 255 | 268 |
| 256 void erase() | 269 void erase() |
| 257 { | 270 { |
| 258 *this = DependentString(); | 271 *this = DependentString(); |
| 259 mLen = DELETED; | 272 mLen = DELETED; |
| 260 } | 273 } |
| 261 }; | 274 }; |
| 262 | 275 |
| 276 #ifdef INSIDE_TESTS |
| 277 inline std::ostream& operator<<(std::ostream& os, const DependentString& str) |
| 278 { |
| 279 return os << static_cast<const String&>(str); |
| 280 } |
| 281 #endif |
| 282 |
| 263 inline DependentString operator "" _str(const String::value_type* str, | 283 inline DependentString operator "" _str(const String::value_type* str, |
| 264 String::size_type len) | 284 String::size_type len) |
| 265 { | 285 { |
| 266 return DependentString(str, len); | 286 return DependentString(str, len); |
| 267 } | 287 } |
| 268 | 288 |
| 269 inline void String_assert_writable(bool isWritable) | 289 inline void String_assert_writable(bool isWritable) |
| 270 { | 290 { |
| 271 assert2(isWritable, u"Writing access to a read-only string"_str); | 291 assert2(isWritable, u"Writing access to a read-only string"_str); |
| 272 } | 292 } |
| 273 | 293 |
| 274 class OwnedString : public String | 294 class OwnedString : public String |
| 275 { | 295 { |
| 276 private: | 296 private: |
| 277 void grow(size_type additionalSize) | 297 void grow(size_type additionalSize) |
| 278 { | 298 { |
| 279 OwnedString newValue(length() + additionalSize); | 299 OwnedString newValue(length() + additionalSize); |
| 280 if (length() > 0) | 300 if (length() > 0) |
| 281 std::memcpy(newValue.mBuf, mBuf, sizeof(value_type) * length()); | 301 std::memcpy(newValue.mBuf, mBuf, sizeof(value_type) * length()); |
| 282 *this = std::move(newValue); | 302 *this = std::move(newValue); |
| 283 } | 303 } |
| 284 | 304 |
| 285 public: | 305 public: |
| 286 explicit OwnedString(size_type len = 0) | 306 explicit OwnedString(size_type len = 0) |
| 287 : String(nullptr, len, READ_WRITE) | 307 : String(nullptr, len, len ? READ_WRITE : INVALID) |
| 288 { | 308 { |
| 289 if (len) | 309 if (len) |
| 290 { | 310 { |
| 291 mBuf = new value_type[length()]; | 311 mBuf = new value_type[length()]; |
| 292 annotate_address(mBuf, "String"); | 312 annotate_address(mBuf, "String"); |
| 293 } | 313 } |
| 294 else | 314 else |
| 295 mBuf = nullptr; | 315 mBuf = nullptr; |
| 296 } | 316 } |
| 297 | 317 |
| 298 explicit OwnedString(const String& str) | 318 explicit OwnedString(const String& str) |
| 299 : OwnedString(str.length()) | 319 : OwnedString(str.length()) |
| 300 { | 320 { |
| 301 if (length()) | 321 if (!str.is_invalid() && !length()) |
| 322 mLen = length() | READ_WRITE; |
| 323 else if (length()) |
| 302 std::memcpy(mBuf, str.mBuf, sizeof(value_type) * length()); | 324 std::memcpy(mBuf, str.mBuf, sizeof(value_type) * length()); |
| 303 } | 325 } |
| 304 | 326 |
| 305 OwnedString(const OwnedString& str) | 327 OwnedString(const OwnedString& str) |
| 306 : OwnedString(static_cast<const String&>(str)) | 328 : OwnedString(static_cast<const String&>(str)) |
| 307 { | 329 { |
| 308 } | 330 } |
| 309 | 331 |
| 310 explicit OwnedString(const value_type* str, size_type len) | 332 explicit OwnedString(const value_type* str, size_type len) |
| 311 : OwnedString(DependentString(str, len)) | 333 : OwnedString(DependentString(str, len)) |
| 312 { | 334 { |
| 313 } | 335 } |
| 314 | 336 |
| 315 explicit OwnedString(OwnedString&& str) | 337 explicit OwnedString(OwnedString&& str) |
| 316 : OwnedString(0) | 338 : OwnedString(0) |
| 317 { | 339 { |
| 318 mBuf = str.mBuf; | 340 mBuf = str.mBuf; |
| 319 mLen = str.mLen; | 341 mLen = str.mLen; |
| 320 str.mBuf = nullptr; | 342 str.mBuf = nullptr; |
| 321 str.mLen = READ_WRITE | 0; | 343 str.mLen = READ_WRITE | 0; |
| 322 } | 344 } |
| 323 | 345 |
| 324 ~OwnedString() | 346 ~OwnedString() |
| 325 { | 347 { |
| 326 if (mBuf) | 348 if (mBuf) |
| 327 delete[] mBuf; | 349 delete[] mBuf; |
| 350 } |
| 351 |
| 352 void reset(const String& str) |
| 353 { |
| 354 *this = str; |
| 355 } |
| 356 |
| 357 void erase() |
| 358 { |
| 359 if (mBuf) |
| 360 delete[] mBuf; |
| 361 mBuf = nullptr; |
| 362 mLen = DELETED; |
| 328 } | 363 } |
| 329 | 364 |
| 330 OwnedString& operator=(const String& str) | 365 OwnedString& operator=(const String& str) |
| 331 { | 366 { |
| 332 *this = OwnedString(str); | 367 *this = OwnedString(str); |
| 333 return *this; | 368 return *this; |
| 334 } | 369 } |
| 335 | 370 |
| 336 OwnedString& operator=(const OwnedString& str) | 371 OwnedString& operator=(const OwnedString& str) |
| 337 { | 372 { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 mBuf[pos++] = '-'; | 437 mBuf[pos++] = '-'; |
| 403 | 438 |
| 404 for (int i = size - 1; i >= 0; i--) | 439 for (int i = size - 1; i >= 0; i--) |
| 405 { | 440 { |
| 406 mBuf[pos + i] = '0' + (num % 10); | 441 mBuf[pos + i] = '0' + (num % 10); |
| 407 num /= 10; | 442 num /= 10; |
| 408 } | 443 } |
| 409 } | 444 } |
| 410 }; | 445 }; |
| 411 | 446 |
| 412 ABP_NS_END | 447 #ifdef INSIDE_TESTS |
| 448 inline std::ostream& operator<<(std::ostream& os, const OwnedString& str) |
| 449 { |
| 450 return os << static_cast<const String&>(str); |
| 451 } |
| 452 #endif |
| 453 ABP_NS_END |
| LEFT | RIGHT |