Module isin::checksum[][src]

isin::checksum

Functions to implement the ISIN checksum formula, known as “modulus 10 ‘double-add-double’ check digit”. The basic idea is to:

  1. assign a numeric value to each character
  2. split that value into digits
  3. iterate over the digits in reverse order
  4. double every other digit starting with the right-most one
  5. split that result into digits
  6. sum over all the digits
  7. calculate the sum mod 10
  8. compute 10 minus that value
  9. if the value is 10, return 0 else return the value itself

There is an implementation in a functional style, checksum_functional() that is used internally for tests and as a comparison for performance benchmarks. It is more expensive than the table-driven style because it does digit expansion on the fly. But, it is easier to understand. The implementation maps directly to the description above.

There is also an implementation in a table-driven style, checksum_table() which is the one actually used when parsing and validating ISINs. The tables are pre-calculated for the net effect each character has on the checksum accumulator at that point and how it effects whether the next character is in a doubling position or not.

Benchmarking shows the table-driven implementation to be around 100 times faster than the functional style (on the test system, average run time decreases from around 2,015 ns with the functional style to around 19 ns with the table-driven style). Input-dependent variability in run time decreases also from about +/- 14% for the functional-style to about +/- 3% for the table-driven style.

Functions

checksum_functional

A direct translation of the formula definition, in the functional style.

checksum_table

Compute the checksum for a u8 array. No attempt is made to ensure the input string is in the ISIN payload format or length.