Algorithm-LUHN_XS

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

    `is_valid_fast:     2 secs (  2.38 usr 0.05 sys) 41.15/s` 

    `is_valid_rff:      2 secs (  1.97 usr 0.08 sys) 48.78/s`

    Algorithm::LUHN\_XS varies from 38x to 48x times faster than the original
    pure perl Algorithm::LUHN module. The is\_valid() routine is 100% compatible
    with the original, returning either '1' for success or the empty string ''
    for failure.   The is\_valid\_fast() routine returns 1 for success and 0 for 
    failure.  Finally, the is\_valid\_rff() function also returns 1 for success 
    and 0 for failure, but only works with numeric input.  If you supply any 
    alpha characters, it will return 0.

- check\_digit NUM

    This function returns the checksum of the given number. If it cannot calculate
    the check\_digit it will return undef and set $Algorithm::LUHN\_XS::ERROR to 
    contain the reason why.  This is much faster than the check\_digit routine
    in the pure perl Algorithm::LUHN module, but only about half as fast as
    the check\_digit\_fast() function in this module, due to the need to return both
    integers and undef, which isn't fast with XS.

- check\_digit\_fast NUM

    This function returns the checksum of the given number. If it cannot calculate
    the check digit it will return -1 and set $Algorithm::LUHN\_XS::ERROR to 
    contain the reason why. It's about 20% faster than check\_digit() because the XS
    code in this case only has to return integers.

- check\_digit\_rff NUM

    This function returns the checksum of the given number. 

    It's about 50% faster than check\_digit() because it doesn't support the valid\_chars() function, and only produces a valid output for numeric input.  If you pass 
    it input with alpha characters, it will return -1. Works great for Credit 
    Cards, but not for things like [CUSIP identifiers](https://en.wikipedia.org/wiki/CUSIP).

- valid\_chars LIST

    By default this module only recognizes 0..9 as valid characters, but sometimes
    you want to consider other characters as valid, e.g. Standard & Poor's
    identifers may contain 0..9, A..Z, @, #, \*. This function allows you to add
    additional characters to the accepted list.

    LIST is a mapping of `character` => `value`.
    For example, Standard & Poor's maps A..Z to 10..35
    so the LIST to add these valid characters would be (A, 10, B, 11, C, 12, ...)

    Please note that this _adds_ or _re-maps_ characters, so any characters
    already considered valid but not in LIST will remain valid.

    If you do not provide LIST,
    this function returns the current valid character map.

    Note that the check\_digit\_rff() and is\_valid\_rff() functions do not support
    the valid\_chars() function.  Both only support numeric inputs, and map them
    to their literal values.

# CAVEATS

This module, because of how valid\_chars() stores data in the XS portion,
is NOT thread safe.

The \_fast and \_rff versions of is\_valid() and check\_digit() don't have the 
same return values for failure as the original Algorithm::LUHN module.
Specifically: 

- is\_valid\_fast() and is\_valid\_rff() return 0 on failure, but
        is\_valid() returns the empty string.
- check\_digit\_fast() and check\_digit\_rff() return -1 on failure, but
        check\_digit() returns undef.

Also, be careful with passing long numbers around.  Perl will, depending 
on the context, convert things like 12345678912345 to 1.2345678912345e+1.
Try to keep things in "string context".

# SEE ALSO

[Algorithm::LUHN](https://metacpan.org/pod/Algorithm::LUHN) is the original pure perl module this is based on.

[Algorithm::CheckDigits](https://metacpan.org/pod/Algorithm::CheckDigits) provides a front-end to a large collection
of modules for working with check digits.

[Business::CreditCard](https://metacpan.org/pod/Business::CreditCard) provides three functions for checking credit
card numbers. [Business::CreditCard::Object](https://metacpan.org/pod/Business::CreditCard::Object) provides an OO interface
to those functions.

[Business::CardInfo](https://metacpan.org/pod/Business::CardInfo) provides a class for holding credit card details,
and has a type constraint on the card number, to ensure it passes the
LUHN check.

[Business::CCCheck](https://metacpan.org/pod/Business::CCCheck) provides a number of functions for checking
credit card numbers.

[Regexp::Common](https://metacpan.org/pod/Regexp::Common) supports combined LUHN and issuer checking
against a card number.

[Algorithm::Damm](https://metacpan.org/pod/Algorithm::Damm) implements a different kind of check digit algorithm,
the [Damm algorithm](https://en.wikipedia.org/wiki/Damm_algorithm)
(Damm, not Damn).

[Math::CheckDigits](https://metacpan.org/pod/Math::CheckDigits) implements yet another approach to check digits.

Neil Bowers has also written a
[review of LUHN modules](http://neilb.org/reviews/luhn.html),
which covers them in more detail than this section.

# REPOSITORY

[https://github.com/krschwab/Algorithm-LUHN\_XS](https://github.com/krschwab/Algorithm-LUHN_XS)

# AUTHOR

This module was written by
Kerry Schwab (http://search.cpan.org/search?author=KSCHWAB).

# COPYRIGHT

Copyright (c) 2018 Kerry Schwab. All rights reserved.
Derived from Algorithm::LUHN, which is (c) 2001 by Tim Ayers.

# LICENSE



( run in 1.147 second using v1.01-cache-2.11-cpan-524268b4103 )