| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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 | 24 #ifdef INSIDE_TESTS |
| 25 #include <iostream> | 25 #include <iostream> |
| 26 #include <codecvt> | 26 #include <codecvt> |
| 27 #endif | 27 #endif |
| 28 #include <utility> | 28 #include <utility> |
| 29 #include <limits> | |
| 29 | 30 |
| 30 #include "base.h" | 31 #include "base.h" |
| 31 #include "debug.h" | 32 #include "debug.h" |
| 32 #include "library.h" | 33 #include "library.h" |
| 33 | 34 |
| 34 ABP_NS_BEGIN | 35 ABP_NS_BEGIN |
| 35 | 36 |
| 36 inline void String_assert_writable(bool isWritable); | 37 inline void String_assert_writable(bool isWritable); |
| 37 | 38 |
| 38 class String | 39 class String |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 450 } | 451 } |
| 451 }; | 452 }; |
| 452 | 453 |
| 453 #ifdef INSIDE_TESTS | 454 #ifdef INSIDE_TESTS |
| 454 inline std::ostream& operator<<(std::ostream& os, const OwnedString& str) | 455 inline std::ostream& operator<<(std::ostream& os, const OwnedString& str) |
| 455 { | 456 { |
| 456 return os << static_cast<const String&>(str); | 457 return os << static_cast<const String&>(str); |
| 457 } | 458 } |
| 458 #endif | 459 #endif |
| 459 | 460 |
| 461 template<typename T> | |
| 462 struct LexicalCastImpl; | |
| 463 | |
| 464 /// Performs common conversions of a text represented value. | |
| 465 template<typename T> | |
| 466 inline T lexical_cast(const String& value) | |
| 467 { | |
| 468 return LexicalCastImpl<T>::Convert(value); | |
| 469 } | |
| 470 | |
| 471 template<> | |
| 472 struct LexicalCastImpl<bool> | |
| 473 { | |
| 474 static bool Convert(const String& value) | |
| 475 { | |
| 476 return value == u"true"_str; | |
| 477 } | |
| 478 }; | |
| 479 | |
| 480 template<typename T> | |
| 481 struct LexicalCastImpl | |
| 482 { | |
| 483 static_assert(std::is_integral<T>::value, "T should be a number"); | |
| 484 static T Convert(const String& value) | |
| 485 { | |
| 486 String::size_type len = value.length(); | |
| 487 if (len == 0) | |
| 488 return 0; | |
| 489 String::size_type pos = 0; | |
| 490 bool negative = std::numeric_limits<T>::is_signed && value[0] == u'-'; | |
| 491 if (negative) | |
| 492 { | |
| 493 ++pos; | |
| 494 } | |
| 495 T result = 0; | |
| 496 for (; pos < len; ++pos) | |
| 497 { | |
| 498 auto c = value[pos]; | |
| 499 if (c < u'0' || c > u'9') | |
| 500 return 0; | |
| 501 // isDangerous is the optimization because there is no need for some check s | |
| 502 // when the values are far from edge cases. | |
| 503 // It targets the normal values, when a value is prefixed with several | |
| 504 // zeros additional checks start to work earlier than the actual value of | |
| 505 // result reaches an edge case, but it does not affect the result. | |
| 506 bool isDangerous = pos >= std::numeric_limits<T>::digits10; | |
|
hub
2018/03/06 14:45:53
this check on the number of digits could also be d
sergei
2018/03/06 15:41:32
This condition is satisfied but from a different p
hub
2018/03/06 15:56:45
Acknowledged.
| |
| 507 // It also invalidates the parsing of too big numbers in comparison with | |
| 508 // stopping when it encounters a non numerical character. | |
| 509 // cast<uint8_t>(u"1230"_str) -> 0 | |
| 510 // cast<uint8_t>(u"123E"_str) -> 123 | |
| 511 if (isDangerous && std::numeric_limits<T>::max() / 10 < result) | |
| 512 { | |
| 513 return 0; | |
| 514 } | |
| 515 result *= 10; | |
| 516 uint8_t digit = c - u'0'; | |
| 517 if (isDangerous && (std::numeric_limits<T>::max() - digit < result - (nega tive ? 1 : 0))) | |
| 518 { | |
| 519 return 0; | |
| 520 } | |
| 521 result += digit; | |
| 522 } | |
| 523 return negative ? -result : result; | |
| 524 } | |
| 525 }; | |
| 526 | |
| 527 template<> | |
| 528 inline OwnedString lexical_cast<OwnedString>(const String& value) | |
| 529 { | |
| 530 return OwnedString{value}; | |
| 531 } | |
| 532 | |
| 460 DependentString TrimSpaces(const String& value); | 533 DependentString TrimSpaces(const String& value); |
| 461 | 534 |
| 462 // Splits the `value` string into two `DependentString`s excluding the character staying at `separatorPos`. | 535 // Splits the `value` string into two `DependentString`s excluding the character staying at `separatorPos`. |
| 463 // Useful for parsing. | 536 // Useful for parsing. |
| 464 std::pair<DependentString, DependentString> SplitString(const String& value, Str ing::size_type separatorPos); | 537 std::pair<DependentString, DependentString> SplitString(const String& value, Str ing::size_type separatorPos); |
| 465 | 538 |
| 466 ABP_NS_END | 539 ABP_NS_END |
| OLD | NEW |