Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r76933 - sandbox/SOC/2011/checks/boost/checks
From: pierre.talbot.6114_at_[hidden]
Date: 2012-02-07 09:16:50


Author: trademark
Date: 2012-02-07 09:16:49 EST (Tue, 07 Feb 2012)
New Revision: 76933
URL: http://svn.boost.org/trac/boost/changeset/76933

Log:
Change filter_valid_value_with_pos prototype which is now bool require(const value_type &value, std::size_t value_pos)
Text files modified:
   sandbox/SOC/2011/checks/boost/checks/amex.hpp | 10 ++++------
   sandbox/SOC/2011/checks/boost/checks/basic_check_algorithm.hpp | 6 ++++--
   sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp | 21 ++++++++++++++-------
   sandbox/SOC/2011/checks/boost/checks/isbn.hpp | 14 ++++++--------
   sandbox/SOC/2011/checks/boost/checks/mastercard.hpp | 10 ++++------
   sandbox/SOC/2011/checks/boost/checks/modulus11.hpp | 13 +++----------
   sandbox/SOC/2011/checks/boost/checks/visa.hpp | 8 ++++----
   7 files changed, 39 insertions(+), 43 deletions(-)

Modified: sandbox/SOC/2011/checks/boost/checks/amex.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/amex.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/amex.hpp 2012-02-07 09:16:49 EST (Tue, 07 Feb 2012)
@@ -54,14 +54,12 @@
 
     \remarks This function use the macro AMEX_SIZE to find the real position from left to right.
   */
- static void filter_valid_value_with_pos(std::size_t current_valid_value, std::size_t current_value_position)
+ template <typename value_type>
+ static bool require(const value_type &value, std::size_t value_pos)
   {
- std::size_t real_pos_from_left = AMEX_SIZE - current_value_position - checkdigit_size;
+ std::size_t real_pos_from_left = AMEX_SIZE - value_pos - checkdigit_size;
 
- if(real_pos_from_left == 1 && current_valid_value != 3)
- throw std::invalid_argument("The Major Industry Identifier of an American Express should be 3!");
- else if(real_pos_from_left == 2 && current_valid_value != 4 && current_valid_value != 7)
- throw std::invalid_argument("The Issuer Identification Number of an American Express should be 34 or 37!");
+ return (real_pos_from_left != 1 || value == '3') && (real_pos_from_left != 2 || value == '4' || value == '7');
   }
 };
 

Modified: sandbox/SOC/2011/checks/boost/checks/basic_check_algorithm.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/basic_check_algorithm.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/basic_check_algorithm.hpp 2012-02-07 09:16:49 EST (Tue, 07 Feb 2012)
@@ -120,7 +120,7 @@
 
     \remarks This function should be overloaded if you want to calculate the checksum of a sequence.
   */
- static std::size_t operate_on_valid_value(std::size_t, std::size_t, std::size_t)
+ static std::size_t process(std::size_t, std::size_t, std::size_t)
   {
     return std::size_t();
   }
@@ -135,8 +135,10 @@
 
     \remarks This function should be overloaded if you want to filter the values with their positions.
   */
- static void filter_valid_value_with_pos(std::size_t, std::size_t)
+ template <typename value_type>
+ static bool require(const value_type&, std::size_t)
   {
+ return true;
   }
 };
 

Modified: sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp 2012-02-07 09:16:49 EST (Tue, 07 Feb 2012)
@@ -61,22 +61,29 @@
 {
   std::size_t value_counter = 0;
   std::size_t checksum = 0;
- for(; seq_begin != seq_end && !size_contract::reach_one_past_the_end(value_counter); ++seq_begin)
+ bool error = false;
+ for(; seq_begin != seq_end && !error && !size_contract::reach_one_past_the_end(value_counter); ++seq_begin)
   {
     try
     {
       if(!algorithm::skip(*seq_begin))
       {
- std::size_t value = boost::checks::detail::lexical_cast(*seq_begin);
- value = algorithm::translate_to_valid_value(value);
- algorithm::filter_valid_value_with_pos(value, value_counter);
- checksum = algorithm::process(checksum, value, value_counter);
- ++value_counter;
- }
+ if(!algorithm::require(*seq_begin, value_counter))
+ error = true;
+ else
+ {
+ std::size_t value = boost::checks::detail::lexical_cast(*seq_begin);
+ value = algorithm::translate_to_valid_value(value);
+ checksum = algorithm::process(checksum, value, value_counter);
+ ++value_counter;
+ }
+ }
     }
     catch(boost::checks::translation_exception){
     }
   }
+ if(error)
+ throw std::invalid_argument("");
   size_contract::respect_size_contract(value_counter);
   return checksum;
 }

Modified: sandbox/SOC/2011/checks/boost/checks/isbn.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/isbn.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/isbn.hpp 2012-02-07 09:16:49 EST (Tue, 07 Feb 2012)
@@ -55,16 +55,14 @@
 
     \remarks This function use the macro EAN13_SIZE to find the real position from left to right.
   */
- static void filter_valid_value_with_pos(std::size_t value, std::size_t value_position)
+ template <typename value_type>
+ static bool require(const value_type &value, std::size_t value_pos)
   {
- std::size_t real_pos_from_left = EAN13_SIZE - value_position - checkdigit_size;
+ std::size_t real_pos_from_left = EAN13_SIZE - value_pos - checkdigit_size;
 
- if(real_pos_from_left == 1 && value != 9)
- throw std::invalid_argument("The first digit should be 9!");
- else if(real_pos_from_left == 2 && value != 7)
- throw std::invalid_argument("The second digit should be 7!");
- else if(real_pos_from_left == 3 && value != 8 && value != 9)
- throw std::invalid_argument("The third digit should be 8 or 9!");
+ return (real_pos_from_left != 1 || value == '9') &&
+ (real_pos_from_left != 2 || value == '7') &&
+ (real_pos_from_left != 3 || value == '8' || value == '9');
   }
 };
 

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 2012-02-07 09:16:49 EST (Tue, 07 Feb 2012)
@@ -52,14 +52,12 @@
 
     \remarks This function use the macro MASTERCARD_SIZE to find the real position from left to right.
   */
- static void filter_valid_value_with_pos(std::size_t current_valid_value, std::size_t current_value_position)
+ template <typename value_type>
+ static bool require(const value_type &value, std::size_t value_pos)
   {
- std::size_t real_pos_from_left = MASTERCARD_SIZE - current_value_position - checkdigit_size;
+ std::size_t real_pos_from_left = MASTERCARD_SIZE - value_pos - checkdigit_size;
 
- 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 Mastercard should be between 51 and 55!");
+ return (real_pos_from_left != 1 || value == '5') && (real_pos_from_left != 2 || (value >= '1' && value <= '5'));
   }
 };
 

Modified: sandbox/SOC/2011/checks/boost/checks/modulus11.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/modulus11.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/modulus11.hpp 2012-02-07 09:16:49 EST (Tue, 07 Feb 2012)
@@ -70,17 +70,10 @@
   }
 
 /* pre: value must be valid */
- static void filter_valid_value_with_pos(std::size_t value, std::size_t value_position)
+ template <typename value_type>
+ static bool require(const value_type &value, std::size_t value_pos)
   {
- // Must be the first digit if the value == 'X'. (reverse traversal).
- if(value == 'X' || value == 'x')
- {
- if(value_position + checkdigit_size > 0)
- throw std::invalid_argument("The character X must be the last");
- }
- else if(value > 10)
- throw std::invalid_argument("The character X must be the last");
-
+ return value_pos == 0 || (value != 'X' && value != 'x');
   }
 
   /*!

Modified: sandbox/SOC/2011/checks/boost/checks/visa.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/visa.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/visa.hpp 2012-02-07 09:16:49 EST (Tue, 07 Feb 2012)
@@ -52,12 +52,12 @@
 
     \remarks This function use the macro VISA_SIZE to find the real position from left to right.
   */
- static void filter_valid_value_with_pos(std::size_t current_valid_value, std::size_t current_value_position)
+ template <typename value_type>
+ static bool require(const value_type &value, std::size_t value_pos)
   {
- std::size_t real_pos_from_left = VISA_SIZE - current_value_position - checkdigit_size;
+ std::size_t real_pos_from_left = VISA_SIZE - value_pos - checkdigit_size;
 
- if(real_pos_from_left == 1 && current_valid_value != 4)
- throw std::invalid_argument("The Major Industry Identifier of a VISA credit card should be 4!");
+ return real_pos_from_left != 1 || value == '4';
   }
 };
 


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