Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: compiled/String.h

Issue 29714586: Noissue - implement a couple of common lexical_cast cases. (Closed) Base URL: github.com:adblockplus/adblockpluscore
Left Patch Set: Created March 5, 2018, 3:38 p.m.
Right Patch Set: address comment Created March 6, 2018, 10:35 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/compiled/String.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 String::size_type len = value.length(); 486 String::size_type len = value.length();
487 if (len == 0) 487 if (len == 0)
488 return 0; 488 return 0;
489 String::size_type pos = 0; 489 String::size_type pos = 0;
490 bool negative = std::numeric_limits<T>::is_signed && value[0] == u'-'; 490 bool negative = std::numeric_limits<T>::is_signed && value[0] == u'-';
491 if (negative) 491 if (negative)
492 { 492 {
493 ++pos; 493 ++pos;
494 } 494 }
495 T result = 0; 495 T result = 0;
496 for (; pos < len && value[pos] >= u'0' && value[pos] <= u'9'; ++pos) 496 for (; pos < len; ++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 { 497 {
498 // isDengerous is the optimization because there is no need for some check s 498 auto c = value[pos];
hub 2018/03/05 19:24:49 isDangerous (typo)
sergei 2018/03/06 10:36:26 Done.
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
499 // when the values are far from edge cases. 502 // when the values are far from edge cases.
500 // It targets the normal values, when a value is prefixed with several 503 // 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 504 // 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. 505 // result reaches an edge case, but it does not affect the result.
503 bool isDangerous = pos >= std::numeric_limits<T>::digits10; 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.
504 // It also invalidates the parsing of too big numbers in comparison with 507 // It also invalidates the parsing of too big numbers in comparison with
505 // stopping when it encounters a non numerical character. 508 // stopping when it encounters a non numerical character.
506 // cast<uint8_t>(u"1230"_str) -> 0 509 // cast<uint8_t>(u"1230"_str) -> 0
507 // cast<uint8_t>(u"123E"_str) -> 123 510 // cast<uint8_t>(u"123E"_str) -> 123
508 if (isDangerous && std::numeric_limits<T>::max() / 10 < result) 511 if (isDangerous && std::numeric_limits<T>::max() / 10 < result)
509 { 512 {
510 return 0; 513 return 0;
511 } 514 }
512 result *= 10; 515 result *= 10;
513 uint8_t digit = value[pos] - u'0'; 516 uint8_t digit = c - u'0';
514 if (isDangerous && (std::numeric_limits<T>::max() - digit < result - (nega tive ? 1 : 0))) 517 if (isDangerous && (std::numeric_limits<T>::max() - digit < result - (nega tive ? 1 : 0)))
515 { 518 {
516 return 0; 519 return 0;
517 } 520 }
518 result += digit; 521 result += digit;
519 } 522 }
520 return negative ? -result : result; 523 return negative ? -result : result;
521 } 524 }
522 }; 525 };
523 526
524 template<> 527 template<>
525 inline OwnedString lexical_cast<OwnedString>(const String& value) 528 inline OwnedString lexical_cast<OwnedString>(const String& value)
526 { 529 {
527 return OwnedString{value}; 530 return OwnedString{value};
528 } 531 }
529 532
530 DependentString TrimSpaces(const String& value); 533 DependentString TrimSpaces(const String& value);
531 534
532 // 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`.
533 // Useful for parsing. 536 // Useful for parsing.
534 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);
535 538
536 ABP_NS_END 539 ABP_NS_END
LEFTRIGHT

Powered by Google App Engine
This is Rietveld