|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r82653 - in sandbox/SOC/2011/checks: boost/checks libs/checks/doc libs/checks/example
From: pierre.talbot.6114_at_[hidden]
Date: 2013-01-28 03:52:41
Author: trademark
Date: 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
New Revision: 82653
URL: http://svn.boost.org/trac/boost/changeset/82653
Log:
* Update the documentation and examples.
* Think of improvements though the doc.
* The doc is newer than the source file and currently not describing the current source file.
* Update some functionalities in the source file.
Added:
sandbox/SOC/2011/checks/libs/checks/doc/description.qbk (contents, props changed)
sandbox/SOC/2011/checks/libs/checks/doc/extending_library.qbk (contents, props changed)
sandbox/SOC/2011/checks/libs/checks/doc/internationalization.qbk (contents, props changed)
sandbox/SOC/2011/checks/libs/checks/doc/modifying_components.qbk (contents, props changed)
sandbox/SOC/2011/checks/libs/checks/doc/quickstart.qbk (contents, props changed)
sandbox/SOC/2011/checks/libs/checks/doc/reference.qbk (contents, props changed)
sandbox/SOC/2011/checks/libs/checks/example/amex_example.cpp (contents, props changed)
sandbox/SOC/2011/checks/libs/checks/example/filter_example.cpp (contents, props changed)
Text files modified:
sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp | 3
sandbox/SOC/2011/checks/boost/checks/filter.hpp | 4
sandbox/SOC/2011/checks/boost/checks/modulus10.hpp | 35 +-------
sandbox/SOC/2011/checks/boost/checks/modulus11.hpp | 35 +++-----
sandbox/SOC/2011/checks/boost/checks/visa.hpp | 2
sandbox/SOC/2011/checks/libs/checks/doc/checks.qbk | 24 ++++
sandbox/SOC/2011/checks/libs/checks/example/checks_tutorial.cpp | 8 +
sandbox/SOC/2011/checks/libs/checks/example/rtn.hpp | 71 +++++++++++------
sandbox/SOC/2011/checks/libs/checks/example/vin.hpp | 162 ++++++++++++++++++---------------------
9 files changed, 171 insertions(+), 173 deletions(-)
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 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -83,7 +83,8 @@
(features::begin(x), features::end(x), counter);
if(checksum == bad_sequence)
return false;
- return typename features::checksum::validate_checkdigit()(checksum);
+
+ return chec
}
/*!
Modified: sandbox/SOC/2011/checks/boost/checks/filter.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/filter.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/filter.hpp 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -28,7 +28,7 @@
typedef bool result_type;
template <typename value_type>
- bool operator()(const value_type &value)
+ bool operator()(const value_type &value) const
{
return value >= '0' && value <= '9';
}
@@ -39,7 +39,7 @@
typedef bool result_type;
template <typename value_type>
- bool operator()(const value_type &value)
+ bool operator()(const value_type &value) const
{
return (value >= '0' && value <= '9') || std::tolower(value) == 'x';
}
Modified: sandbox/SOC/2011/checks/boost/checks/modulus10.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/modulus10.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/modulus10.hpp 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -17,50 +17,27 @@
#endif
#include <cstddef>
-#include <boost/checks/weighted_sum.hpp>
namespace boost{
namespace checks{
-/*! \class modulus10_algorithm
- \brief This class can be used to compute or validate checksum with a basic modulus 10.
-
- \tparam mod10_weight must meet the weight concept requirements.
- \tparam iteration_sense must meet the iteration_sense concept requirements.
- \tparam checkdigit_size Help functions to provide same behavior on sequence with and without check digits. No "real" value in the sequence will be skipped.
-*/
- /*!
- \brief Validate a checksum with a simple modulus 10.
-
- \param checksum is the checksum to validate.
-
- \returns @c true if the checksum is correct, @c false otherwise.
- */
-struct mod10_validation
+struct mod10_inv_checkdigit
{
- bool operator()(size_t checksum)
+ char operator()(size_t checksum)
{
- return !(checksum % 10);
+ return ((10 - checksum % 10) % 10) + '0';
}
};
- /*!
- \brief Compute the check digit with a simple modulus 10.
-
- \tparam checkdigit is the type of the check digit desired.
- \param checksum is the checksum used to extract the check digit.
- \throws boost::checks::translation_exception if the check digit cannot be translated into the checkdigit type.
-
- \returns The modulus 10 check digit of checksum.
- */
struct mod10_checkdigit
{
- size_t operator()(size_t checksum)
+ char operator()(size_t checksum)
{
- return ((10 - checksum % 10) % 10);
+ return checksum % 10 + '0';
}
};
+
}} // namespace boost namespace checks
#endif //BOOST_CHECKS_MOD10_HPP
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 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -28,28 +28,15 @@
namespace boost{
namespace checks{
-/*! \class modulus11_algorithm
- \brief This class can be used to compute or validate checksum with a basic modulus 11.
-
- \tparam mod11_weight must meet the weight concept requirements.
- \tparam iteration_sense must meet the iteration_sense concept requirements.
- \tparam checkdigit_size Help functions to provide same behavior on sequence with and without check digits. No "real" value in the sequence will be skipped.
-
- \remarks The range of the check digit is [0..10], the tenth element is translated as the letter 'X'.
-*/
-
- /*!
- \brief Validate a checksum with a simple modulus 11.
-
- \param checksum is the checksum to validate.
-
- \returns true if the checksum is correct, false otherwise.
- */
-struct mod11_validation
+struct mod11_checkdigit
{
- bool operator()(size_t checksum)
+ char operator()(size_t checksum)
{
- return !(checksum % 11);
+ checksum %= 11;
+ if(checksum == 10)
+ return 'X';
+ else
+ return checksum + '0';
}
};
/*!
@@ -64,9 +51,13 @@
*/
struct mod11_checkdigit
{
- size_t operator()(size_t checksum)
+ char operator()(size_t checksum)
{
- return ((11 - checksum % 11)% 11);
+ checksum = ((11 - checksum % 11)% 11);
+ if(checksum == 10)
+ return 'X';
+ else
+ return checksum + '0';
}
};
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 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -23,6 +23,8 @@
#include <boost/checks/luhn.hpp>
#include <boost/checks/checkdigit.hpp>
+#include <boost/checks/precheck.hpp>
+
/*!
\brief This macro defines the size of a Visa number.
*/
Modified: sandbox/SOC/2011/checks/libs/checks/doc/checks.qbk
==============================================================================
--- sandbox/SOC/2011/checks/libs/checks/doc/checks.qbk (original)
+++ sandbox/SOC/2011/checks/libs/checks/doc/checks.qbk 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -1,6 +1,6 @@
[article Checks
[quickbook 1.5]
- [copyright 2011 Pierre Talbot]
+ [copyright 2011-2013 Pierre Talbot]
[license
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
@@ -43,11 +43,27 @@
[def __ISBN [@http://en.wikipedia.org/wiki/Isbn International Standard Book Number (ISBN)]]
[def __ISSN [@http://en.wikipedia.org/wiki/International_Standard_Serial_Number International Standard Serial Number (ISSN)]]
[def __IBAN [@http://en.wikipedia.org/wiki/International_Bank_Account_Number International Bank Account Number (IBAN)]]
-[def __VIN [@http://en.wikipedia.org/wiki/Vehicle_identification_number Vehicle Identification Number (VIN)]]
-[def __RTN [@http://en.wikipedia.org/wiki/Routing_transit_number Routing transit number (RTN)]]
[def __CODE39 [@http://en.wikipedia.org/wiki/Code_39 Code 39]]
[def __EAN [@http://en.wikipedia.org/wiki/European_Article_Number International Article Number (EAN)]]
+[/ Links to C++ reference.]
+[def __BOOST_REGEX__ [@http://www.boost.org/doc/libs/1_52_0/libs/regex/doc/html/index.html Boost regex]]
+[def __STD_REGEX__ [@http://en.cppreference.com/w/cpp/regex Standard regex]]
+
+[/ Links to different check description]
+[def __VIN__ [@http://en.wikipedia.org/wiki/Vehicle_identification_number Vehicle Identification Number (VIN)]]
+[def __AMEX__ [@http://en.wikipedia.org/wiki/American_Express American Express (AMEX)]]
+[def __RTN__ [@http://en.wikipedia.org/wiki/Routing_transit_number Routing transit number (RTN)]]
+
+[/ Links to different sections]
+[def __DESCRIPTION__ [link checks.checks.description Description]]
+[def __QUICKSTART__ [link checks.checks.quick_start Quick start]]
+[def __MODIFYING_COMPONENTS__ [link checks.checks.modifying_components Modifying components]]
+[def __EXTENDING_LIBRARY__ [link checks.checks.extending_the_library Extending the library]]
+[def __INTERNATIONALIZATION__ [link checks.checks.internationalization Internationalization]]
+[def __REFERENCE__ [link checks.checks.reference Reference]]
+[def __TYPE_ADAPTOR__ [link checks.checks.reference.type_adaptor Type adaptor]]
+
[def __types_of_errors [link checks.checks.errors types of error]]
[def __overview [link checks.checks.overview Overview]]
[def __overview [link checks.checks.preface Preface]]
@@ -125,7 +141,7 @@
The author thanks
-* Paul A. Bristow who was the mentor of this Google Summer of Code project for his infinite patience and his wise advice.
+* Paul A. Bristow who was the mentor of this Google Summer of Code project for his infinite patience and its wise advices.
* Google for funding this Google Summer of Code 2011 project.
* Joseph A Gallian for his assistance with tables of values for the Verhoeff algorithm.
Added: sandbox/SOC/2011/checks/libs/checks/doc/description.qbk
==============================================================================
--- (empty file)
+++ sandbox/SOC/2011/checks/libs/checks/doc/description.qbk 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -0,0 +1,38 @@
+[/
+Copyright Pierre Talbot 2013.
+
+Use, modification and distribution are subject to the
+Boost Software License, Version 1.0.
+(See accompanying file LICENSE_1_0.txt
+or copy at http://www.boost.org/LICENSE_1_0.txt)
+]
+
+[section:description Description]
+
+This chapter will give you a basic approach to understand this library.
+
+For the re-usability and genericity sake, all algorithms are split in several pieces of code:
+
+* Filtering undesirable characters. [*Default:] Everything that is not expected is skipped.
+* Transliterate characters to digits, possibly with the help of a table. [*Default:] The characters are transliterated into unsigned digits, an exception can be raised if the transliteration is impossible.
+* Processing the checksum.
+* An optional size checking.
+* Producing the check digit with the checksum.
+
+There is also other modifiable pieces of code:
+
+* The position and length of the check digit. [*Default: ] Last digit in the sequence.
+* The traversal type, forward or backward. [*Default: ] Backward traversal.
+
+These steps are done in a single iteration.
+
+You might want to read the section that fit your use:
+
+* If you want to use functions provided by the library, see __QUICKSTART__ ;
+* If you want to modify existing algorithm, see __MODIFYING_COMPONENTS__ ;
+* If you want to check international string, see __INTERNATIONALIZATION__ ;
+* Finally, if you want a guideline to write your own check function, see __EXTENDING_LIBRARY__.
+
+A __REFERENCE__ section is available and you can find further documentation on specific algorithm or component.
+
+[endsect][/section:description Description]
\ No newline at end of file
Added: sandbox/SOC/2011/checks/libs/checks/doc/extending_library.qbk
==============================================================================
--- (empty file)
+++ sandbox/SOC/2011/checks/libs/checks/doc/extending_library.qbk 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -0,0 +1,178 @@
+[/
+Copyright Pierre Talbot 2013.
+
+Use, modification and distribution are subject to the
+Boost Software License, Version 1.0.
+(See accompanying file LICENSE_1_0.txt
+or copy at http://www.boost.org/LICENSE_1_0.txt)
+]
+
+[import ../example/vin.hpp]
+[import ../example/rtn.hpp]
+
+[section:extending_the_library Extending the library]
+
+You may use the facilities of this library to implement functions not provided yet by this library.
+
+[note Please do not hesitate to contact us if you think your check function is not a part (yet) of Boost.Checks.]
+
+The first example is the implementation of the __RTN__, which is really easy and will not ask a lot of effort to code.
+The second is the __VIN__'s implementation, it's a bit more difficult to code due to the specificities
+of this algorithm. But it's an excellent example to show you how to extend this library.
+
+[section:implement_rtn Implement the RTN algorithm]
+
+We begin with the implementation of the __RTN__ algorithm. We follow the __DESCRIPTION__ given earlier to find similar point with it and our algorithm.
+
+[h4]Filter[/h4]
+
+We don't need any specific filter, the default "filter everything" is enough here.
+
+[h4]Check digit[/h4]
+
+The check digit is very basic because it's the checksum modulus 10.
+
+[rtn_checkdigit]
+
+[h4]Checksum[/h4]
+
+The transliteration convert character into digit which is the default behavior.
+The size of the RTN is 9 and the traversal is from right to left which is also the default.
+The core algorithm is a weighted sum with the weight sequence [3,7,1].
+Finally, we obtain the following checksum type.
+
+[rtn_checksum]
+
+[h4]RTN algorithm[/h4]
+
+The last step is to put all type definition together to form a algorithm type.
+
+[rtn_algorithm]
+
+[h4]Example[/h4]
+
+We can now use the RTN algorithm with `compute_checkdigit` and `validate`.
+
+[rtn_example]
+
+and the output is
+
+[rtn_example_res]
+
+[section:implement_vin Implement the VIN algorithm]
+
+This section will guide you though the implementation of the __VIN__ algorithm.
+The VIN is especially difficult to implement in a generic context
+due to its numerous corner cases. Because of it, the design of Boost.Check has been changed few times,
+and hopefully improved to handle a larger set of check functions, including this one.
+
+The first step is to match the __DESCRIPTION__ we give earlier.
+
+[h4]Filter[/h4]
+
+We don't need any specific filter. So the default filter will fit our use.
+
+[h4]Check digit[/h4]
+
+The check digit is quite special in this algorithm because it isn't in the end, but in the middle.
+More precisely, it is at the eighth position starting from end and it's a single digit.
+
+Furthermore, the function that compute the check digit is a classic mod11 calculation. The VIN use a simple `checksum % 11`.
+
+Now we can compose the check digit structure of VIN. Here the sample of code doing this task.
+
+[vin_checkdigit]
+
+We use the default values for the size of the check digit (which is 1).
+For description purpose, we use a `#define` for the check digit size.
+
+[h4]Checksum[/h4]
+
+Now we must code the core of the algorithm.
+
+[h5]Transliteration[/h5]
+First, the VIN checksum algorithm work with transliterated value, corresponding to the following table:
+
+[table:id Letter to digit VIN conversion table
+ [[Conversion value] [1] [2] [3] [4] [5] [6] [7] [8] [9] ]
+ [[] [A (1)] [B (2)] [C (3)] [D (4)] [E (5)] [F (6)] [G (7)] [H (8)] [ I (N/A) ] ]
+ [[] [J (10)] [K (11)] [L (12)] [M (13)] [N (14)] [O (N/A)] [P (16)] [Q (N/A)] [R (18)] ]
+ [[] [] [S (19)] [T (20)] [U (21)] [V (22)] [W (23)] [X (24)] [Y (25)] [Z (26)] ]
+]
+
+As we know we receive ASCII encoded character, the conversion is quite easy. We could use
+a table with an array `table`, where `table[character - 'a'] == value`. We just have to check if
+`character` is a digit or a letter (and put it in lower case). It's ok to do it like this, but we found
+a function that permit us to avoid the memory cost of the table.
+
+Another problem is that I, O and Q have no matching value. If they appear, the number is not valid.
+You might ask why we don't check it in the filter function. This is because this rule is a part of
+the algorithm and the filter just skip the character. But we don't want to skip it, we want to stop the
+algorithm if we encountered such a value. Here is the transliteration function.
+
+[vin_transliteration]
+
+[h5]Processor[/h5]
+
+The processor is the function that compute the checksum. VIN is a weighted sum algorithm, it's nice
+because a re-usable piece of code doing this job already exist. Because we go though the sequence from
+the end, we can say that the weight sequence is [2,3,4,5,6,7,8,9,10]. Note that the check digit is not
+processed. The processor is quite easy to code, as you can see below.
+
+[vin_processor]
+
+[h5]Sequence size[/h5]
+
+The sequence size is important here. The VIN must exactly be composed of 17 digits.
+But what are we doing if the sequence is not of exactly 17 digits ? You can return an
+error code or send an exception. The policy used return an error code (or simply
+false if you validate).
+
+[vin_size]
+
+[h5]Traversal[/h5]
+
+The traversal is very important, the position of the check digit and the weight sequence depend on it.
+Most of the check algorithm will use a right to left traversal, so it's the default value. The VIN algorithm is quite basic here and use this default value.
+
+[h5]Checksum[/h5]
+
+Finally, we can create the checksum type of this algorithm.
+
+[vin_checksum]
+
+[h4]VIN algorithm[/h4]
+
+Finally we have all the pieces and we need to put these together. A last type definition is
+used to describe our VIN algorithm.
+
+[vin_algorithm]
+
+It's a VIN algorithm that raises exception on transliteration error and returns error
+code on size error. You can easily change what you want in it.
+
+We defined several function and few typedef, but the VIN is very specific, and you shouldn't have
+so many works on other check algorithms.
+
+[h4]Tests[/h4]
+
+The tests are very important but also quite difficult to find. The easiest solution is to get
+some existing and trusted generators and to compare your algorithm with it. If you can't find some,
+just compute some examples by hand.
+
+In some cases, other solutions exists. For the VIN, a test is to check the result of the sequence
+of seventies 1 digit. The check digit should be 1.
+
+[h4]Example[/h4]
+
+You can use it in the same way you use all the other.
+
+[vin_example]
+
+and the output is
+
+[vin_example_res]
+
+[endsect][/section:implement_vin Implement the VIN algorithm]
+
+[endsect][/section:extending_the_library Extending the library]
Added: sandbox/SOC/2011/checks/libs/checks/doc/internationalization.qbk
==============================================================================
--- (empty file)
+++ sandbox/SOC/2011/checks/libs/checks/doc/internationalization.qbk 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -0,0 +1,22 @@
+[/
+Copyright Pierre Talbot 2013.
+
+Use, modification and distribution are subject to the
+Boost Software License, Version 1.0.
+(See accompanying file LICENSE_1_0.txt
+or copy at http://www.boost.org/LICENSE_1_0.txt)
+]
+
+[section:internationalization Internationalization]
+
+Each check sequence has a minimal character type in which every characters can be represented.
+For most, the char type is sufficient.
+
+If the minimal character type is char, your sequence is supposed to be of that type. If it's not,
+you must transcode this character into the minimal character type of the algorithm.
+
+There is no decoding/encoding in Boost.Check as we assume we manipulate a sequence of the type required by the algorithm.
+
+The transformation of your specific encoding into ASCII-char is not handle by the core functionnalities.
+
+[endsect][/section:description Description]
\ No newline at end of file
Added: sandbox/SOC/2011/checks/libs/checks/doc/modifying_components.qbk
==============================================================================
--- (empty file)
+++ sandbox/SOC/2011/checks/libs/checks/doc/modifying_components.qbk 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -0,0 +1,28 @@
+[/
+Copyright Pierre Talbot 2013.
+
+Use, modification and distribution are subject to the
+Boost Software License, Version 1.0.
+(See accompanying file LICENSE_1_0.txt
+or copy at http://www.boost.org/LICENSE_1_0.txt)
+]
+
+[section:modifying_components Modifying Components]
+
+You sometimes want to change one or two pieces of code of an existing algorithm.
+For example, you would like a filter which only accept the separator '-' and digits.
+This functor look like:
+
+[strict_filter]
+
+You can now use the type adaptor to change this component in VISA.
+
+[type_adaptor]
+
+And finally the example:
+
+[visa_example]
+
+You can find all the modifying tag in the __TYPE_ADAPTOR__ reference section.
+
+[endsect][/section:modifying_components Modifying Components]
\ No newline at end of file
Added: sandbox/SOC/2011/checks/libs/checks/doc/quickstart.qbk
==============================================================================
--- (empty file)
+++ sandbox/SOC/2011/checks/libs/checks/doc/quickstart.qbk 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -0,0 +1,50 @@
+[/
+Copyright Pierre Talbot 2011.
+
+Use, modification and distribution are subject to the
+Boost Software License, Version 1.0.
+(See accompanying file LICENSE_1_0.txt
+or copy at http://www.boost.org/LICENSE_1_0.txt)
+]
+
+[import ../example/checks_examples.cpp]
+[import ../example/checks_tutorial.cpp]
+[import ../example/rtn.hpp]
+[import ../example/vin.hpp]
+
+[section:quickstart Quick Start]
+There are two main functionalities provided by this library. The first is used to
+[*validate] a sequence of character :
+
+``
+template <typename check_algo,
+ typename range>
+bool check_sequence(const range &x);
+``
+
+If `x` doesn't match the set of rules specified by `check_algo` it returns false. Generally,
+it's because `x` doesn't have the size required or because the check digit is wrong.
+
+The second aimed to [*calculate] the check digit.
+
+``
+template <typename check_algo,
+ typename range>
+boost::optional<typename check_algo::checkdigit_type>
+compute_checkdigit(const range &x);
+``
+
+The return value contains the check digit if `x` is a valid sequence according to `check_algo`.
+A simple example of these two functions is provided below with the __AMEX__ algorithm.
+
+[amex_example]
+
+[note You noticed the template parameter `check_algo` is exactly the same for validation or computation.]
+
+[warning Boost.Check [*does not provide] support for string pattern matching such as a require starting number (example: "978" or "979" in an ISBN number). Refer to __BOOST_REGEX__ or __STD_REGEX__ or a homecraft pre-checking.]
+
+Basically, the `check_algo` is the acronym of the algorithm you want to use.
+
+The default behavior is to skip every character not recognized as belonging to the number. So you can use your favorite digit separator.
+
+[endsect] [/section:quickstart Quick Start]
Added: sandbox/SOC/2011/checks/libs/checks/doc/reference.qbk
==============================================================================
--- (empty file)
+++ sandbox/SOC/2011/checks/libs/checks/doc/reference.qbk 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -0,0 +1,12 @@
+[/
+Copyright Pierre Talbot 2013.
+
+Use, modification and distribution are subject to the
+Boost Software License, Version 1.0.
+(See accompanying file LICENSE_1_0.txt
+or copy at http://www.boost.org/LICENSE_1_0.txt)
+]
+
+[section:reference Reference]
+
+[endsect][/section:reference Reference]
\ No newline at end of file
Added: sandbox/SOC/2011/checks/libs/checks/example/amex_example.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2011/checks/libs/checks/example/amex_example.cpp 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -0,0 +1,16 @@
+//[amex_example
+#include <string>
+#include <boost/optional.hpp>
+#include <boost/checks/amex.hpp>
+
+using namespace boost::checks;
+
+int main()
+{
+ std::string amex_no("3782-822463-1000");
+ boost::optional<char> checkdigit = compute_checkdigit<amex>(amex_no);
+ amex_no += *checkdigit;
+ if(validate<amex>(amex_no))
+ std::cout << amex_no << " is a valid american express card number." << std::endl;
+}
+//]
\ No newline at end of file
Modified: sandbox/SOC/2011/checks/libs/checks/example/checks_tutorial.cpp
==============================================================================
--- sandbox/SOC/2011/checks/libs/checks/example/checks_tutorial.cpp (original)
+++ sandbox/SOC/2011/checks/libs/checks/example/checks_tutorial.cpp 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -29,7 +29,7 @@
// Files containing the 'new' checksum definitions.
#include "rtn.hpp"
-//#include "vin.hpp"
+#include "vin.hpp"
int main()
{
@@ -43,12 +43,14 @@
//]
//[vin_example
- /*std::string vin = "1M8GDM9AXKP042788";
+ std::string vin = "1M8GDM9A6KP042788";
+ vin = "11111111111111111";
if(check_vin(vin))
std::cout << "The Vehicle Identification Number (VIN) \"" << vin << "\" is valid." << std::endl;
vin = "1M8GDM9AKP042788";
- std::cout << "The check digit of \"" << vin << "\" is " << compute_vin(vin) << std::endl;*/
+ vin = "1111111111111111";
+ std::cout << "The check digit of \"" << vin << "\" is " << compute_vin(vin) << std::endl;
//]
return 0;
}
Added: sandbox/SOC/2011/checks/libs/checks/example/filter_example.cpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2011/checks/libs/checks/example/filter_example.cpp 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -0,0 +1,55 @@
+#include <string>
+#include <exception>
+#include <boost/optional.hpp>
+#include <boost/checks/visa.hpp>
+#include <boost/checks/type_adaptor.hpp>
+
+using namespace boost::checks;
+
+//[strict_filter
+class invalid_character : public std::exception
+{
+ std::string what() const
+ {
+ return "An invalid character has been hit.";
+ }
+
+ ~invalid_character(){}
+};
+
+struct strict_filter
+{
+ bool operator(char x)
+ {
+ if(!isdigit(x)){
+ if(x != '-'){
+ throw invalid_character();
+ }
+ return false;
+ }
+ return true;
+ }
+};
+//]
+
+int main()
+{
+ //[type_adaptor
+ typedef type_adaptor<visa, filter_tag, strict_filter>::type strict_visa;
+ //]
+
+ //[visa_example
+ std::string visa_no("4111-111-11111-1111");
+
+ if(validate<strict_visa>(visa_no))
+ std::cout << visa_no << " is a valid VISA card number." << std::endl;
+
+ std::string visa_no("4111 111 11111 1111");
+ try{
+ validate<strict_visa>(visa_no);
+ }catch(invalid_character& e)
+ {
+ std::cout << e.what() << std::endl;
+ }
+ //]
+}
\ No newline at end of file
Modified: sandbox/SOC/2011/checks/libs/checks/example/rtn.hpp
==============================================================================
--- sandbox/SOC/2011/checks/libs/checks/example/rtn.hpp (original)
+++ sandbox/SOC/2011/checks/libs/checks/example/rtn.hpp 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -16,51 +16,72 @@
#ifndef BOOST_CHECKS_RTN_HPP
#define BOOST_CHECKS_RTN_HPP
-//[rtn_include_files
-#include <cstddef>
#include <boost/checks/modulus10.hpp>
#include <boost/checks/checksum.hpp>
+#include <boost/checks/precheck.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
-//]
-
-//[rtn_preprocessor_tools
-#define RTN_SIZE 9
namespace boost{
namespace checks{
-typedef weighted_sum<weight<3,7,1> > rtn_processor;
+//[rtn_checkdigit
+typedef checkdigit
+<
+ mod10_checkdigit
+>
+rtn_checkdigit;
//]
-//[rtn_preprocessor_algorithm
+//[rtn_checksum
+#define RTN_SIZE 9
+typedef strict_size_policy<RTN_SIZE> rtn_size;
+typedef weighted_sum<weight<3,7,1> > rtn_processor;
+
typedef checksum
<
rtn_processor,
- mod10_validation,
- mod10_checkdigit
-> rtn_checksum;
+ rtn_checkdigit,
+ rtn_size
+>
+rtn_checksum;
+//]
-typedef features
+//[rtn_algorithm
+typedef check_algorithm
<
- rtn_checksum,
- RTN_SIZE
-> rtn;
-
+ rtn_checksum
+>
+rtn;
//]
-//[rtn_functions
-template <typename check_range>
-bool check_rtn(const check_range& check_seq)
+/*
+//[rtn_example
+int main()
{
- return check_sequence<rtn>(check_seq);
-}
+ using namespace boost::checks;
+ std::string rtn_number("111000025");
+ std::string rtn_number_without_checkdigit("11100002");
-template <typename check_range>
-size_t compute_rtn(const check_range& check_seq)
-{
- return compute_checkdigit<rtn>(check_seq);
+ if(validate<rtn>(rtn_number))
+ std::cout << rtn_number << " is a valid RTN." << std::endl;
+
+ boost::optional<rtn::checkdigit_type> checkdigit =
+ compute_checkdigit<rtn>(rtn_number_without_checkdigit);
+ if(checkdigit)
+ std::cout << "The check digit of the RTN \'" << rtn_number_without_checkdigit
+ << "\' is \'" << *checkdigit << "\'.\n";
+
+
+ return 0;
}
//]
+
+//[rtn_example_res
+111000025 is a valid RTN.
+The check digit of the RTN '11100002' is '5'.
+//]
+*/
+
}}
#endif
Modified: sandbox/SOC/2011/checks/libs/checks/example/vin.hpp
==============================================================================
--- sandbox/SOC/2011/checks/libs/checks/example/vin.hpp (original)
+++ sandbox/SOC/2011/checks/libs/checks/example/vin.hpp 2013-01-28 03:52:39 EST (Mon, 28 Jan 2013)
@@ -17,114 +17,102 @@
#define BOOST_CHECKS_VIN_HPP
//[vin_preprocessor_tools
+#include <stdexcept>
+#include <boost/checks/precheck.hpp>
+#include <boost/checks/checksum.hpp>
#include <boost/checks/modulus11.hpp>
-#include <boost/checks/basic_checks.hpp>
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
-#define VIN_SIZE 17
-#define VIN_SIZE_WITHOUT_CHECKDIGIT 16
+namespace boost{
+namespace checks{
+
+
+//[vin_checkdigit
#define VIN_CHECKDIGIT_POS 8
+#define VIN_CHECKDIGIT_SIZE 1
-typedef boost::checks::weight<2,3,4,5,6,7,8,9,10> vin_weight;
+typedef checkdigit
+<
+ mod11_checkdigit,
+ VIN_CHECKDIGIT_POS
+>
+vin_checkdigit;
//]
-//[vin_struct_header
-template <unsigned int number_of_virtual_value_skipped = 0>
-struct vin_algorithm : boost::checks::modulus11_algorithm<vin_weight, number_of_virtual_value_skipped>
-//]
+//[vin_transliteration
+struct vin_transliteration
{
- //[vin_translation_module
- template <typename value>
- static int translate_to_valid_value(const value ¤t_value)
+ size_t operator()(char value) const
{
- int valid_value = 0;
- try
- {
- valid_value = boost::lexical_cast<int>(current_value);
- }
- catch(boost::bad_lexical_cast)
- {
- // Transform the value to be between 1 and 26.
- if(current_value >= 'a' && current_value <= 'z')
- valid_value = current_value - 'a' + 1;
- else if(current_value >= 'A' && current_value <= 'Z')
- valid_value = current_value - 'A' + 1;
- else
- throw boost::checks::translation_exception();
-
- if(valid_value == 9 || valid_value == 15 || valid_value == 17)
- throw std::invalid_argument("The letters I, O and Q are not allowed.");
-
- if(valid_value_counter == VIN_CHECKDIGIT_POS && number_of_virtual_value_skipped == 0)
- {
- if(valid_value != 24)
- throw std::invalid_argument("The check digit should be a digit or X or x.");
- else
- valid_value = 10;
- valid_value = 11 - valid_value;
- }
- else
- valid_value = valid_value % 10 + valid_value / 10 + (valid_value > 18);
- }
- if(valid_value > 10)
- throw boost::checks::translation_exception();
+ char lower_value = std::tolower(value);
+ if(value_lower == 'o' || value_lower == 'i' || value_lower == 'q')
+ throw transliteration_exception("The letters I, O and Q are not allowed.");
+ // If the value is less or equal than 9, we are sure it's a number thanks to the filter.
+ if(value <= '9')
+ return value - '0';
- return valid_value;
+ size_t res = value_lower - 'a' + 1;
+ return res % 10 + res / 10 + (res > 18);
}
+};
+//]
- static void filter_valid_value_with_pos(unsigned int current_valid_value, unsigned int current_value_position)
- {
- if(valid_value_counter == VIN_CHECKDIGIT_POS && number_of_virtual_value_skipped == 0)
- {
- if(valid_value != 24)
- throw std::invalid_argument("The check digit should be a digit or X or x.");
- }
- }
- //]
+//[vin_size
+#define VIN_SIZE 17
- //[vin_operation_module
- static void operate_on_valid_value(const int current_valid_value, const unsigned int valid_value_counter, int &checksum)
- {
- if(number_of_virtual_value_skipped == 0 && valid_value_counter == VIN_CHECKDIGIT_POS)
- checksum += current_valid_value;
- else
- {
- unsigned int weight_position = valid_value_counter - (number_of_virtual_value_skipped == 0 && valid_value_counter > VIN_CHECKDIGIT_POS);
- int current_weight = vin_weight::weight_associated_with_pos(weight_position);
- checksum += current_valid_value * current_weight;
- }
- }
- //]
+typedef strict_size_policy<VIN_SIZE> vin_size;
+//]
- //[vin_compute_checkdigit
- template <typename checkdigit>
- static typename checkdigit compute_checkdigit(int checksum)
- {
- typedef typename boost::checks::modulus11_algorithm<vin_weight, number_of_virtual_value_skipped> mod11;
- return mod11::translate_checkdigit<checkdigit>(checksum % 11);
- }
- //]
-};
+//[vin_processor
+typedef weight<2,3,4,5,6,7,8,9,10> vin_weight;
+typedef weighted_sum<vin_weight> vin_processor;
+//]
-//[vin_preprocessor_algorithm
-typedef vin_algorithm <0> vin_check_algorithm;
-typedef vin_algorithm <1> vin_compute_algorithm;
+//[vin_checksum
+typedef checksum
+<
+ vin_processor,
+ vin_checkdigit,
+ vin_size,
+ vin_transliteration
+>
+vin_checksum;
//]
-//[vin_functions
-template <typename check_range>
-bool check_vin(const check_range& check_seq)
-{
- return boost::checks::check_sequence<vin_check_algorithm, VIN_SIZE>(boost::rbegin(check_seq), boost::rend(check_seq));
-}
+//[vin_algorithm
+typedef check_algorithm
+<
+ vin_checksum
+>
+vin;
+//]
-template <typename check_range>
-typename vin_compute_algorithm::checkdigit<check_range>::type compute_vin(const check_range& check_seq)
+/*
+//[vin_example
+int main()
{
- return boost::checks::compute_checkdigit<vin_compute_algorithm, VIN_SIZE_WITHOUT_CHECKDIGIT>(boost::rbegin(check_seq), boost::rend(check_seq));
+ using namespace boost::checks;
+ std::string vin_number("11111111 1 11111111");
+ std::string vin_number_without_checkdigit("11111111 11111111");
+
+ if(validate<vin>(vin_number))
+ std::cout << vin_number << " is a valid VIN." << std::endl;
+
+ boost::optional<vin::checkdigit_type> checkdigit =
+ compute_checkdigit<vin>(vin_number_without_checkdigit);
+ if(checkdigit)
+ std::cout << "The check digit of the VIN \'" << vin_number_without_checkdigit
+ << "\' is \'" << *checkdigit << "\'.\n";
+
+ return 0;
}
//]
-
+//[vin_example_res
+11111111 1 11111111 is a valid VIN.
+The check digit of the VIN '11111111 11111111' is '1'.
+//]
+*/
+}}
#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