|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r73884 - sandbox/SOC/2011/checks/boost/checks
From: pierre.talbot.6114_at_[hidden]
Date: 2011-08-18 08:26:34
Author: trademark
Date: 2011-08-18 08:26:34 EDT (Thu, 18 Aug 2011)
New Revision: 73884
URL: http://svn.boost.org/trac/boost/changeset/73884
Log:
Add check and compute function of the mastercard check digit verification.
Text files modified:
sandbox/SOC/2011/checks/boost/checks/mastercard.hpp | 44 ++++++++++++++++++++++++++++++++++-----
1 files changed, 38 insertions(+), 6 deletions(-)
Modified: sandbox/SOC/2011/checks/boost/checks/mastercard.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/mastercard.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/mastercard.hpp 2011-08-18 08:26:34 EDT (Thu, 18 Aug 2011)
@@ -5,16 +5,48 @@
// http://www.boost.org/LICENSE_1_0.txt
// See http://www.boost.org for updates, documentation, and revision history.
-#ifndef BOOST_MASTERCARD_INCLUDED
-#define BOOST_MASTERCARD_INCLUDED
+#ifndef BOOST_CHECKS_MASTERCARD_HPP
+#define BOOST_CHECKS_MASTERCARD_HPP
+#include <boost/checks/luhn.hpp>
+
+#define MASTERCARD_SIZE 16
+#define MASTERCARD_SIZE_WITHOUT_CHECKDIGIT 15
namespace boost {
namespace checks{
-/* Mastercard : use luhn algorithm */
-} // namespace checks
-} // namespace boost
+template <unsigned int number_of_virtual_value_skipped = 0>
+struct mastercard_algorithm : boost::checks::luhn_algorithm < number_of_virtual_value_skipped >
+{
+ static void filter_valid_value_with_pos(const unsigned int current_valid_value, const unsigned int current_value_position )
+ {
+ const unsigned int real_pos_from_left = MASTERCARD_SIZE - current_value_position - number_of_virtual_value_skipped ;
+
+ if( real_pos_from_left == 1 && current_valid_value != 5)
+ throw std::invalid_argument("The Major Industry Identifier of a Mastercard should be 5.") ;
+ else if( real_pos_from_left == 2 && (current_valid_value == 0 || current_valid_value > 5) )
+ throw std::invalid_argument("The Issuer Identification Number of an American Express should be between 51 and 55." ) ;
+ }
+};
+
+typedef mastercard_algorithm<0> mastercard_check_algorithm ;
+typedef mastercard_algorithm<1> mastercard_compute_algorithm ;
+
+template <typename check_range>
+bool check_mastercard (const check_range& check_seq)
+{
+ return boost::checks::check_sequence<mastercard_check_algorithm, MASTERCARD_SIZE> ( check_seq ) ;
+}
+
+template <typename check_range>
+typename boost::checks::mastercard_compute_algorithm::checkdigit<check_range>::type compute_mastercard (const check_range& check_seq)
+{
+ return boost::checks::compute_checkdigit<mastercard_compute_algorithm, MASTERCARD_SIZE_WITHOUT_CHECKDIGIT> ( check_seq ) ;
+}
+
+
+}}
+#endif // BOOST_CHECKS_MASTERCARD_HPP
-#endif
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