|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r72781 - sandbox/SOC/2011/checks/boost/checks
From: pierre.talbot.6114_at_[hidden]
Date: 2011-06-28 05:40:53
Author: trademark
Date: 2011-06-28 05:40:52 EDT (Tue, 28 Jun 2011)
New Revision: 72781
URL: http://svn.boost.org/trac/boost/changeset/72781
Log:
Modification of the prototype of the 2 first functions
Modification of the usage of the "begin" parameter
Text files modified:
sandbox/SOC/2011/checks/boost/checks/modulus.hpp | 94 +++++++++++++++++++++------------------
1 files changed, 50 insertions(+), 44 deletions(-)
Modified: sandbox/SOC/2011/checks/boost/checks/modulus.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/modulus.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/modulus.hpp 2011-06-28 05:40:52 EDT (Tue, 28 Jun 2011)
@@ -12,98 +12,104 @@
namespace checks{
/** Validate the check digit of the number provided with the Luhn algorithm.
- * \tparam Iterator which represents the beginning or the ending of a sequence of character.<br> unsigned int which represents a size.
+ * \tparam Iterator which represents the beginning or the ending of a sequence of character.
+ * \tparam unsigned int which represents a size.
* \param [in] begin Represents the beginning of the sequence to check.
* \param [in] end Represents one off the limit of the sequence to check.
* \param [unsigned int] nbr_digits Represents the number of digits on which the Luhn algorithm will operate. If the size is < 1, the luhn algorithm will validate the check digit with all the digit encountered.
* \pre begin and end are valid initialized iterators. They represent a sequence of character encoded in big-endian mode in a format compatible with the 7 bits ASCII.
- * \post begin and end are unchanged.
- * \result end is returned if the number of digits found is less or equals to 1 or not equals to nbr_digits.<br> begin is returned if the check digit of the sequence is not valid.<br> An iterator on the check digit is returned if the sequence is valid.
+ * \post begin is equal to the position of the check digit plus one if the expression provided is correct, otherwise is equal to end.
+ * \result true is returned if the expression given have a valid check digit and have nbr_digits (or more than 0 digit if nbr_digits is equal to 0).
*/
-template <class In>
-inline In check_luhn(const In &begin, const In &end, unsigned int nbr_digits)
+template <typename In>
+inline bool check_luhn(In &begin, const In &end, unsigned int nbr_digits)
{
- In iter = begin;
- // If the number of digits isn't given, we compute it.
+ // If the number of digits isn't given (equal to 0), we compute it.
if(!nbr_digits)
{
+ In iter = begin;
while(iter != end)
{
if(*iter >= '0' && *iter <= '9')
++nbr_digits;
++iter;
}
- // Return end if the number of digit is equals to 0.
+ // Return false if there is no digits in the expression given.
if(!nbr_digits)
- return end;
- iter = begin;
+ {
+ begin = end;
+ return false;
+ }
}
int sum = 0,v;
- while(nbr_digits > 0 && iter != end)
+ while(nbr_digits > 0 && begin != end)
{
- if(*iter >= '0' && *iter <= '9')
- sum += (v= (*iter & 15) << (--nbr_digits&1)) - 9 * (v>9);
- ++iter;
+ if(*begin >= '0' && *begin <= '9')
+ sum += (v= (*begin & 15) << (--nbr_digits&1)) - 9 * (v>9);
+ ++begin;
}
- // Return end if the number of digit isn't equal to the number specified.
- if(nbr_digits)
- return end;
- // Return begin if the sum is invalid
- // Return iter if the sum is valid
- return (sum % 10) ? begin : --iter ;
+ // Return true if the number of digit is equal to the number specified and the sum is valid.
+ return !nbr_digits && sum % 10 ;
}
/** Validate the check digit of the number provided with the modulus 11 algorithm.
- * \tparam Iterator which represents the beginning or the ending of a sequence of character.<br> Int which represents a size.
+ * \tparam Iterator which represents the beginning or the ending of a sequence of character.
+ * \tparam unsigned int which represents a size.
* \param [in] begin Represents the beginning of the sequence to check.
* \param [in] end Represents one off the limit of the sequence to check.
- * \param [int] nbr_digits Represents the number of digits on which the modulus 11 algorithm will operate. If the size is < 1, the modulus 11 algorithm will validate the check digit with all the digit encountered.
+ * \param [unsigned int] nbr_digits Represents the number of digits on which the Luhn algorithm will operate. If the size is < 1, the luhn algorithm will validate the check digit with all the digit encountered.
* \pre begin and end are valid initialized iterators. They represent a sequence of character encoded in big-endian mode in a format compatible with the 7 bits ASCII.
- * \post begin and end are unchanged.
- * \result end is returned if the number of digits found is less or equals to 1 or not equals to nbr_digits.<br> begin is returned if the check digit of the sequence is not valid.<br> An iterator on the check digit is returned if the sequence is valid.
+ * \post begin is equal to the position of the check digit plus one if the expression provided is correct, otherwise is equal to end.
+ * \result true is returned if the expression given have a valid check digit and have nbr_digits (or more than 0 digit if nbr_digits is equal to 0).
*/
-template <class In>
-inline In check_mod11(In begin, In end, unsigned int nbr_digits)
+template <typename In>
+inline bool check_mod11(In &begin, const In end, unsigned int nbr_digits)
{
- In iter = begin;
- // If the number of digits isn't given, we compute it.
- if(nbr_digits == 0)
+ // If the number of digits isn't given (equal to 0), we compute it.
+ if(!nbr_digits)
{
+ In iter = begin;
while(iter != end)
- if(*iter++ >= '0' && *iter <= '9')
+ {
+ if(*iter >= '0' && *iter <= '9')
++nbr_digits;
- iter = begin;
+ ++iter;
+ }
+ // Return false if there is no digits in the expression given.
+ if(!nbr_digits)
+ {
+ begin = end;
+ return false;
+ }
}
int sum = 0;
// Sum the n-1 first digit with a weigth of n
while(nbr_digits > 1 && iter != end)
{
- if(*iter >= '0' && *iter <= '9')
- sum += *iter & 15 * nbr_digits--;
- ++iter;
+ if(*begin >= '0' && *begin <= '9')
+ sum += *begin & 15 * nbr_digits--;
+ ++begin;
}
// Add the check digit to the sum (add 10 if the check digit equals 'x' or 'X')
- while(nbr_digits && iter != end)
+ while(begin != end)
{
- if(*iter >= '0' && *iter <= '9')
+ if(*begin >= '0' && *begin <= '9')
{
- sum += *iter & 15;
+ sum += *begin & 15;
--nbr_digits;
+ break;
}
- else if(*iter == 'x' || *iter == 'X')
+ else if(*begin == 'x' || *begin == 'X')
{
sum += 10;
--nbr_digits;
+ break;
}
else
++iter;
}
- // Return end if the number of digit isn't equal to the number specified.
- if(nbr_digits)
- return end;
- // Return begin if the sum is invalid
- // Return iter if the sum is valid
- return (sum % 11) ? begin : --iter ;
+ // Return true if the number of digit is equal to the number specified and the sum is valid.
+ return !nbr_digits && sum % 11;
}
template <class In>
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk