|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r73020 - sandbox/SOC/2011/checks/boost/checks
From: pierre.talbot.6114_at_[hidden]
Date: 2011-07-12 12:38:54
Author: trademark
Date: 2011-07-12 12:38:53 EDT (Tue, 12 Jul 2011)
New Revision: 73020
URL: http://svn.boost.org/trac/boost/changeset/73020
Log:
Update of the syntax (typename In > in , Out > out)
Update of the functions mod11
Add prototype of mod97 functions
Text files modified:
sandbox/SOC/2011/checks/boost/checks/modulus.hpp | 268 ++++++++++++++++++++++++---------------
1 files changed, 162 insertions(+), 106 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-07-12 12:38:53 EDT (Tue, 12 Jul 2011)
@@ -19,15 +19,15 @@
* \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 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).
+ * \returns 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 <typename In>
-inline bool check_luhn(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)
{
// If the number of digits isn't given (equal to 0), we count these.
if(!nbr_digits)
{
- In iter = begin;
+ in iter = begin;
while(iter != end)
{
if(*iter >= '0' && *iter <= '9')
@@ -64,15 +64,15 @@
* \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 calculate 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 is equal to the position of the last digit encountered plus one if the expression provided is correct, otherwise is equal to end.
- * \result 0 is returned if the expression given have not nbr_digits (or no digit if nbr_digits is equal to 0). Otherwise the ASCII character of the check digit is returned.
+ * \returns 0 is returned if the expression given have not nbr_digits (or no digit if nbr_digits is equal to 0). Otherwise the ASCII character of the check digit is returned.
*/
-template <typename Out, typename In>
-inline Out compute_luhn(In &begin, const In &end, unsigned int nbr_digits)
+template <typename out, typename in>
+inline out compute_luhn(in &begin, const in &end, unsigned int nbr_digits)
{
// If the number of digits isn't given (equal to 0), we count these.
if(!nbr_digits)
{
- In iter = begin;
+ in iter = begin;
while(iter != end)
{
if(*iter >= '0' && *iter <= '9')
@@ -100,13 +100,13 @@
return ((10 - sum % 10) % 10) | '0';
}
-template <typename In>
-inline char compute_luhn(In &begin, const In &end, unsigned int nbr_digits)
+template <typename in>
+inline char compute_luhn(in &begin, const in &end, unsigned int nbr_digits)
{
// If the number of digits isn't given (equal to 0), we count these.
if(!nbr_digits)
{
- In iter = begin;
+ in iter = begin;
while(iter != end)
{
if(*iter >= '0' && *iter <= '9')
@@ -133,6 +133,118 @@
}
return (10 - sum % 10) % 10 | '0';
}
+
+/** Validate the check digit of the number provided with the modulus 10 algorithm.
+ * \tparam Iterator with at least the caracteristics of an input iterator. It represents the beginning or the ending of a sequence of character.
+ * \tparam unsigned int[] which represents an array of weight.
+ * \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\[\]] weight Represents the weight pattern of the sequence starting on the left of the expression. If weight is null, the algorithm will apply a weight of 1 on the sequence.
+ * \param [unsigned int] nbr_digits Represents the number of digits on which the modulus 10 algorithm will operate. If the size is < 1, the modulus 10 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 is equal to the position of the check digit plus one if the expression provided is correct, otherwise is equal to end.
+ * \returns 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 <typename in>
+inline bool check_mod10(in &begin, const in &end, unsigned int weight[], unsigned int nbr_digits)
+{
+ // If the weight is null or have a size of zero, we apply a weight of 1 on each digit.
+ if(weight == NULL)
+ {
+ unsigned int weight_tmp[] = {1};
+ weight = weight_tmp;
+ }
+ // If the number of digits isn't given (equal to 0), we count these.
+ if(!nbr_digits)
+ {
+ in iter = begin;
+ while(iter != end)
+ {
+ if(*iter >= '0' && *iter <= '9')
+ ++nbr_digits;
+ ++iter;
+ }
+ // Return false if there is no digit in the expression given.
+ if(!nbr_digits)
+ {
+ begin = end;
+ return false;
+ }
+ }
+ int sum = 0;
+ int weight_size = sizeof(weight) / sizeof(weight[0]) ;
+ /* We start with the leftmost digit and we multiply by the weight indicated.
+ * The sum of all digits is computed. */
+ for(int i=0; nbr_digits > 0 && begin != end; ++i, --nbr_digits)
+ {
+ if(*begin >= '0' && *begin <= '9')
+ sum += (*begin & 15) * weight[i % weight_size] ;
+ ++begin;
+ }
+ // Return true if the number of digit is equal to the number specified and the sum is valid.
+ return !nbr_digits && !(sum % 10) ;
+}
+
+/** Compute the check digit of the number provided with the modulus 10 algorithm.
+ * \tparam Iterator with at least the caracteristics of an input iterator. It represents the beginning or the ending of a sequence of character.
+ * \tparam unsigned int[] which represents an array of weight.
+ * \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\[\]] weight Represents the weight pattern of the sequence starting on the left of the expression. If weight is null, the algorithm will apply a weight of 1 on the sequence.
+ * \param [unsigned int] nbr_digits Represents the number of digits on which the modulus 10 algorithm will operate. If the size is < 1, the modulus 10 algorithm will calculate 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 is equal to the position of the last digit encountered plus one if the expression provided is correct, otherwise is equal to end.
+ * \returns 0 is returned if the expression given have not nbr_digits (or no digit if nbr_digits is equal to 0). Otherwise the ASCII character of the check digit is returned.
+ */
+
+template <typename in>
+inline char compute_mod10(in &begin, const in &end, unsigned int weight[], unsigned int nbr_digits)
+{
+ // If the weight is null we apply a weight of 1 on each digit.
+ if(weight == NULL)
+ {
+ unsigned int weight_tmp[] = {1};
+ weight = weight_tmp;
+ }
+ // If the number of digits isn't given (equal to 0), we count these.
+ if(!nbr_digits)
+ {
+ in iter = begin;
+ while(iter != end)
+ {
+ if(*iter >= '0' && *iter <= '9')
+ ++nbr_digits;
+ ++iter;
+ }
+ // Return false if there is no digit in the expression given.
+ if(!nbr_digits)
+ {
+ begin = end;
+ return 0;
+ }
+ }
+ int sum = 0;
+ int weight_size = sizeof(weight) / sizeof(weight[0]) ;
+ /* We start with the leftmost digit and we multiply by the weight indicated.
+ * The sum of all digits is computed. */
+ for(int i=0; nbr_digits > 0 && begin != end; ++i, --nbr_digits)
+ {
+ if(*begin >= '0' && *begin <= '9')
+ sum += (*begin & 15) * weight[i % weight_size] ;
+ ++begin;
+ }
+ if(nbr_digits > 0)
+ {
+ begin = end;
+ return 0;
+ }
+ return (10 - sum % 10) % 10 | '0' ;
+}
+
+
/** Calculate the check digit of the number provided with the modulus 11 algorithm.
* \tparam Iterator with at least the caracteristics of an input iterator. It represents the beginning or the ending of a sequence of character.
* \tparam unsigned int which represents a size.
@@ -141,15 +253,15 @@
* \param [unsigned int] nbr_digits Represents the number of digits on which the MOD11 algorithm will operate. If the size is < 1, the MOD11 algorithm will calculate 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 is equal to the position of the last digit encountered plus one if the expression provided is correct, otherwise is equal to end.
- * \result 0 is returned if the expression given have not nbr_digits (or no digit if nbr_digits is equal to 0). Otherwise the ASCII character of the check digit is returned.
+ * \returns 0 is returned if the expression given have not nbr_digits (or no digit if nbr_digits is equal to 0). Otherwise the ASCII character of the check digit is returned.
*/
-template <typename In>
-inline bool check_mod11(In &begin, const 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 (equal to 0), we count these.
if(!nbr_digits)
{
- In iter = begin;
while(iter != end)
{
if(*iter >= '0' && *iter <= '9')
@@ -162,6 +274,7 @@
begin = end;
return false;
}
+ iter = begin;
}
int sum = 0;
// Sum the n-1 first digit with a weigth of n. (The weigth is the contribution
@@ -201,15 +314,15 @@
* \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 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).
+ * \returns 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 <typename Out, typename In>
-inline Out compute_mod11(In &begin, const In &end, unsigned int nbr_digits)
+template <typename out, typename in>
+inline out compute_mod11(in &begin, const in &end, unsigned int nbr_digits)
{
+ in iter = begin;
// If the number of digits isn't given (equal to 0), we count these.
if(!nbr_digits)
{
- In iter = begin;
while(iter != end)
{
if(*iter >= '0' && *iter <= '9')
@@ -222,6 +335,7 @@
begin = end;
return 0;
}
+ iter = begin;
}
int sum = 0;
// Sum the digits with a weigth of nbr_digits. (The weigth is the contribution
@@ -240,119 +354,61 @@
int check_digit = (11 - sum % 11) %11;
if(check_digit == 10)
return 'X';
- return check_digit;
+ return check_digit + '0';
}
-/** Validate the check digit of the number provided with the modulus 10 algorithm.
- * \tparam Iterator with at least the caracteristics of an input iterator. It represents the beginning or the ending of a sequence of character.
- * \tparam unsigned int[] which represents an array of weight.
- * \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\[\]] weight Represents the weight pattern of the sequence starting on the left of the expression. If weight is null, the algorithm will apply a weight of 1 on the sequence.
- * \param [unsigned int] nbr_digits Represents the number of digits on which the modulus 10 algorithm will operate. If the size is < 1, the modulus 10 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 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 <typename In>
-inline bool check_mod10(In &begin, const In &end, unsigned int weight[], unsigned int nbr_digits)
-{
- // If the weight is null or have a size of zero, we apply a weight of 1 on each digit.
- if(weight == NULL)
- {
- unsigned int weight_tmp[] = {1};
- weight = weight_tmp;
- }
- int weight_size = sizeof(weight) / sizeof(weight[0]);
- // If the number of digits isn't given (equal to 0), we count these.
- if(!nbr_digits)
- {
- In iter = begin;
- while(iter != end)
- {
- if(*iter >= '0' && *iter <= '9')
- ++nbr_digits;
- ++iter;
- }
- // Return false if there is no digit in the expression given.
- if(!nbr_digits)
- {
- begin = end;
- return false;
- }
- }
- int sum = 0;
- /* We start with the leftmost digit and we multiply by the weight indicated.
- * The sum of all digits is computed. */
- for(int i=0; nbr_digits > 0 && begin != end; ++i)
- {
- if(*begin >= '0' && *begin <= '9')
- sum += (*begin & 15) * weight[i % weight_size] ;
- ++begin;
- }
- // Return true if the number of digit is equal to the number specified and the sum is valid.
- return !nbr_digits && !(sum % 10) ;
-}
-
-/** Compute the check digit of the number provided with the modulus 10 algorithm.
+/** Validate the check digit of the number provided with the modulus 11 algorithm.
* \tparam Iterator with at least the caracteristics of an input iterator. It represents the beginning or the ending of a sequence of character.
- * \tparam unsigned int[] which represents an array of weight.
* \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\[\]] weight Represents the weight pattern of the sequence starting on the left of the expression. If weight is null, the algorithm will apply a weight of 1 on the sequence.
- * \param [unsigned int] nbr_digits Represents the number of digits on which the modulus 10 algorithm will operate. If the size is < 1, the modulus 10 algorithm will calculate 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 is equal to the position of the last digit encountered plus one if the expression provided is correct, otherwise is equal to end.
- * \result 0 is returned if the expression given have not nbr_digits (or no digit if nbr_digits is equal to 0). Otherwise the ASCII character of the check digit is returned.
+ * \post begin is equal to the position of the check digit plus one if the expression provided is correct, otherwise is equal to end.
+ * \returns 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 <typename In>
-inline char compute_mod10(In &begin, const In &end, unsigned int weight[], unsigned int nbr_digits)
+template <typename in>
+inline char compute_mod11(in &begin, const in &end, unsigned int nbr_digits)
{
- // If the weight is null we apply a weight of 1 on each digit.
- if(weight == NULL)
- {
- unsigned int weight_tmp[] = {1};
- weight = weight_tmp;
- }
- int weight_size = sizeof(weight) / sizeof(weight[0]);
+ in iter = begin;
// If the number of digits isn't given (equal to 0), we count these.
if(!nbr_digits)
{
- In iter = begin;
while(iter != end)
{
if(*iter >= '0' && *iter <= '9')
++nbr_digits;
++iter;
}
- // Return false if there is no digit in the expression given.
+ // Return 0 if there is no digit in the expression given.
if(!nbr_digits)
{
begin = end;
return 0;
}
+ iter = begin;
}
int sum = 0;
- /* We start with the leftmost digit and we multiply by the weight indicated.
- * The sum of all digits is computed. */
- for(int i=0; nbr_digits > 0 && begin != end; ++i)
+ // Sum the digits with a weigth of nbr_digits. (The weigth is the contribution
+ // of a digit to the final sum).
+ ++nbr_digits; // Adding the unknown check digit to the size...
+ while(nbr_digits > 1 && iter != end)
{
if(*begin >= '0' && *begin <= '9')
- sum += (*begin & 15) * weight[i % weight_size] ;
+ sum += *begin & 15 * nbr_digits-- ;
++begin;
}
- if(nbr_digits > 0)
- {
- begin = end;
+ if(nbr_digits > 1)
return 0;
- }
- return (10 - sum % 10) % 10 | '0' ;
+
+ // Compute the check digit.
+ char check_digit = (11 - sum % 11) %11;
+ if(check_digit == 10)
+ return 'X';
+ return check_digit + '0';
}
+
/*!!!
The modulus 97 algorithm wait for a sequence of numbers only ! and will not do anything else that sum the digits and calculate the modulus of this sum.
If you need to check an IBAN use the algorithms in iban.hpp */
@@ -365,11 +421,11 @@
* \param [unsigned int] nbr_digits Represents the number of digits on which the modulus 97 algorithm will operate. If the size is < 1, the modulus 97 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 is equal to the position of the check digits 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).
+ * \returns 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 <typename In>
-inline bool check_mod97(In &begin, const In &end, unsigned int nbr_digit)
+template <typename in>
+inline bool check_mod97(in &begin, const in &end, unsigned int nbr_digit)
{
return false;
}
@@ -382,11 +438,11 @@
* \param [unsigned int] nbr_digits Represents the number of digits on which the modulus 97 algorithm will operate. If the size is < 1, the modulus 97 algorithm will calculate 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 is equal to the position of the last digit encountered plus one if the expression provided is correct, otherwise is equal to end.
- * \result 0 is returned if the expression given have not nbr_digits (or no digit if nbr_digits is equal to 0). Otherwise the ASCII characters of the check digits are returned.
+ * \returns 0 is returned if the expression given have not nbr_digits (or no digit if nbr_digits is equal to 0). Otherwise the ASCII characters of the check digits are returned.
*/
-template <typename In>
-inline char compute_mod97(In &begin, const In &end, unsigned int nbr_digit)
+template <typename in>
+inline char compute_mod97(in &begin, const in &end, unsigned int nbr_digit)
{
return false;
}
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