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 && value[pos] >= u'0' && value[pos] <= u'9'; ++pos) | |
hub
2018/03/05 19:24:49
If the condition `value[pos] >= u'0' && value[pos]
sergei
2018/03/06 10:36:26
Done, but I'm not sure about "123 ", anyway we can
| |
497 { | |
498 // isDengerous is the optimization because there is no need for some check s | |
hub
2018/03/05 19:24:49
isDangerous (typo)
sergei
2018/03/06 10:36:26
Done.
| |
499 // when the values are far from edge cases. | |
500 // It targets the normal values, when a value is prefixed with several | |
501 // zeros additional checks start to work earlier than the actual value of | |
502 // result reaches an edge case, but it does not affect the result. | |
503 bool isDangerous = pos >= std::numeric_limits<T>::digits10; | |
504 // It also invalidates the parsing of too big numbers in comparison with | |
505 // stopping when it encounters a non numerical character. | |
506 // cast<uint8_t>(u"1230"_str) -> 0 | |
507 // cast<uint8_t>(u"123E"_str) -> 123 | |
508 if (isDangerous && std::numeric_limits<T>::max() / 10 < result) | |
509 { | |
510 return 0; | |
511 } | |
512 result *= 10; | |
513 uint8_t digit = value[pos] - u'0'; | |
514 if (isDangerous && (std::numeric_limits<T>::max() - digit < result - (nega tive ? 1 : 0))) | |
515 { | |
516 return 0; | |
517 } | |
518 result += digit; | |
519 } | |
520 return negative ? -result : result; | |
521 } | |
522 }; | |
523 | |
524 template<> | |
525 inline OwnedString lexical_cast<OwnedString>(const String& value) | |
526 { | |
527 return OwnedString{value}; | |
528 } | |
529 | |
460 DependentString TrimSpaces(const String& value); | 530 DependentString TrimSpaces(const String& value); |
461 | 531 |
462 // Splits the `value` string into two `DependentString`s excluding the character staying at `separatorPos`. | 532 // Splits the `value` string into two `DependentString`s excluding the character staying at `separatorPos`. |
463 // Useful for parsing. | 533 // Useful for parsing. |
464 std::pair<DependentString, DependentString> SplitString(const String& value, Str ing::size_type separatorPos); | 534 std::pair<DependentString, DependentString> SplitString(const String& value, Str ing::size_type separatorPos); |
465 | 535 |
466 ABP_NS_END | 536 ABP_NS_END |
OLD | NEW |