comparison Core/Toolbox.cpp @ 1428:0a355eeeb351

optimization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Jun 2015 13:26:34 +0200
parents fe384a9d3b51
children ad94a3583b07
comparison
equal deleted inserted replaced
1427:d710ea64f0fd 1428:0a355eeeb351
683 digest[4]); 683 digest[4]);
684 } 684 }
685 685
686 bool Toolbox::IsSHA1(const std::string& str) 686 bool Toolbox::IsSHA1(const std::string& str)
687 { 687 {
688 if (str.size() != 44) 688 if (str.size() == 0)
689 {
690 return false;
691 }
692
693 const char* start = &str[0];
694 const char* end = start + str.size();
695
696 // Trim the beginning of the string
697 while (start < end)
698 {
699 if (*start == '\0' ||
700 isspace(*start))
701 {
702 start++;
703 }
704 else
705 {
706 break;
707 }
708 }
709
710 // Trim the trailing of the string
711 while (start < end)
712 {
713 if (*(end - 1) == '\0' ||
714 isspace(*(end - 1)))
715 {
716 end--;
717 }
718 else
719 {
720 break;
721 }
722 }
723
724 if (end - start != 44)
689 { 725 {
690 return false; 726 return false;
691 } 727 }
692 728
693 for (unsigned int i = 0; i < 44; i++) 729 for (unsigned int i = 0; i < 44; i++)
695 if (i == 8 || 731 if (i == 8 ||
696 i == 17 || 732 i == 17 ||
697 i == 26 || 733 i == 26 ||
698 i == 35) 734 i == 35)
699 { 735 {
700 if (str[i] != '-') 736 if (start[i] != '-')
701 return false; 737 return false;
702 } 738 }
703 else 739 else
704 { 740 {
705 if (!isalnum(str[i])) 741 if (!isalnum(start[i]))
706 return false; 742 return false;
707 } 743 }
708 } 744 }
709 745
710 return true; 746 return true;