Algorithm-LUHN_XS

 view release on metacpan or  search on metacpan

LICENSE  view on Meta::CPAN

you have.  You must make sure that they, too, receive or can get the
source code.  And you must tell them their rights.

  We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.

  Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software.  If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.

  The precise terms and conditions for copying, distribution and
modification follow.

                    GNU GENERAL PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. This License Agreement applies to any program or other work which
contains a notice placed by the copyright holder saying it may be

LICENSE  view on Meta::CPAN

the Program under this License.  However, parties who have received
copies, or rights to use copies, from you under this General Public
License will not have their licenses terminated so long as such parties
remain in full compliance.

  5. By copying, distributing or modifying the Program (or any work based
on the Program) you indicate your acceptance of this license to do so,
and all its terms and conditions.

  6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the original
licensor to copy, distribute or modify the Program subject to these
terms and conditions.  You may not impose any further restrictions on the
recipients' exercise of the rights granted herein.

  7. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time.  Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.

Each version is given a distinguishing version number.  If the Program

LICENSE  view on Meta::CPAN

    cost, duplication charges, time of people involved, and so on. (You will
    not be required to justify it to the Copyright Holder, but only to the
    computing community at large as a market that must bear the fee.) 
  - "Freely Available" means that no fee is charged for the item itself, though
    there may be fees involved in handling the item. It also means that
    recipients of the item may redistribute it under the same conditions they
    received it. 

1. You may make and give away verbatim copies of the source form of the
Standard Version of this Package without restriction, provided that you
duplicate all of the original copyright notices and associated disclaimers.

2. You may apply bug fixes, portability fixes and other modifications derived
from the Public Domain or from the Copyright Holder. A Package modified in such
a way shall still be considered the Standard Version.

3. You may otherwise modify your copy of this Package in any way, provided that
you insert a prominent notice in each changed file stating how and when you
changed that file, and provided that you do at least ONE of the following:

  a) place your modifications in the Public Domain or otherwise make them

META.json  view on Meta::CPAN

{
   "abstract" : "Very Fast XS Version of the original Algorithm::LUHN",
   "author" : [
      "Kerry Schwab <kschwab@cpan.org>"
   ],
   "dynamic_config" : 1,
   "generated_by" : "ExtUtils::MakeMaker version 7.34, CPAN::Meta::Converter version 2.150010",
   "license" : [
      "perl_5"
   ],
   "meta-spec" : {
      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",

META.yml  view on Meta::CPAN

---
abstract: 'Very Fast XS Version of the original Algorithm::LUHN'
author:
  - 'Kerry Schwab <kschwab@cpan.org>'
build_requires:
  ExtUtils::MakeMaker: '0'
  Test: '0'
configure_requires:
  ExtUtils::MakeMaker: '0'
  XSLoader: '0.16'
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 7.34, CPAN::Meta::Converter version 2.150010'

Makefile.PL  view on Meta::CPAN

use warnings;

use 5.006;

use ExtUtils::MakeMaker;

my $preop =
    'pod2markdown lib/Algorithm/LUHN_XS.pm > README.md;' .
    'pod2text  lib/Algorithm/LUHN_XS.pm > README';
my %WriteMakefileArgs = (
  "ABSTRACT" => "Very Fast XS Version of the original Algorithm::LUHN",
  "AUTHOR" => "Kerry Schwab <kschwab\@cpan.org>",
  "CONFIGURE_REQUIRES" => {
    "ExtUtils::MakeMaker" => 0,
    "XSLoader" => '0.16'
  },
  "DISTNAME" => "Algorithm-LUHN_XS",
  "EXE_FILES" => [],
  "LICENSE" => "perl",
  "MIN_PERL_VERSION" => "5.008001",
  "NAME" => "Algorithm::LUHN_XS",

README  view on Meta::CPAN

NAME
    Algorithm::LUHN_XS - Very Fast XS Version of the original
    Algorithm::LUHN

SYNOPSIS
      use Algorithm::LUHN_XS qw/check_digit is_valid/;

      my $c;
      $c = check_digit("43881234567");
      print "It works\n" if is_valid("43881234567$c");

      $c = check_digit("A2C4E6G8"); # this will return undef

README  view on Meta::CPAN

      my %vc = Algorithm::LUHN_XS::valid_chars();
      for (sort keys %vc) {
        print "$_ => $vc{$_}\n";
      }

      Algorithm::LUHN_XS::valid_chars(map {$_ => ord($_)-ord('A')+10} A..Z);
      $c = check_digit("A2C4E6G8");
      print "It worked again\n" if is_valid("A2C4E6G8$c");

DESCRIPTION
    This module is an XS version of the original Perl Module
    Algorithm::LUHN, which was written by Tim Ayers. It should work exactly
    the same, only substantially faster. The supplied check_digit() routine
    is 100% compatible with the pure Perl Algorithm::LUHN module, while the
    faster check_digit_fast() and really fast check_digit_rff() are not.

    How much faster? Here's a benchmark, running on a 3.4GHz i7-2600:

    "Benchmark: timing 100 iterations"

    "Algorithm::LUHN: 69 secs (69.37 usr 0.00 sys) 1.44/s"

    "check_digit: 2 secs ( 1.98 usr 0.00 sys) 50.51/s"

    "check_digit_fast: 2 secs ( 1.68 usr 0.00 sys) 59.52/s"

    "check_digit_rff: 1 secs ( 1.29 usr 0.00 sys) 77.52/s"

    So, it's 35x to 53x faster than the original pure Perl module, depending
    on how much compatibility with the original module you need.

    The rest of the documentation is mostly a copy of the original docs,
    with some additions for functions that are new.

    This module calculates the Modulus 10 Double Add Double checksum, also
    known as the LUHN Formula. This algorithm is used to verify credit card
    numbers and Standard & Poor's security identifiers such as CUSIP's and
    CSIN's.

    You can find plenty of information about the algorithm by searching the
    web for "modulus 10 double add double".

README  view on Meta::CPAN

        right check digit. If you change it to a '3', it's not a valid card
        number. Ie:

            is_valid('4242424242424242');   # true
            is_valid('4242424242424243');   # false

    is_valid_fast CHECKSUMMED_NUM
    is_valid_rff CHECKSUMMED_NUM
        As with check_digit(), we have 3 versions of is_valid(), each one
        progressively faster than the check_digit() that comes in the
        original pure Perl Algorithm::LUHN module. Here's a benchmark of 1M
        total calls to is_valid():

        "Benchmark: timing 100 iterations"

        "Algorithm::LUHN: 100 secs (100.29 usr 0.01 sys) 1.00/s"

        "is_valid: 3 secs ( 2.46 usr 0.11 sys) 38.91/s"

        "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

README  view on Meta::CPAN


        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 is the original pure perl module this is based on.

    Algorithm::CheckDigits provides a front-end to a large collection of
    modules for working with check digits.

    Business::CreditCard provides three functions for checking credit card
    numbers. Business::CreditCard::Object provides an OO interface to those
    functions.

    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

README  view on Meta::CPAN


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

LICENSE
    This program is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.

CREDITS
    Tim Ayers, for the original pure perl version of Algorithm::LUHN.

    Neil Bowers, the current maintainer of Algorithm::LUHN.

    The inspiration for this module was a PerlMonks post I made here:
    <https://perlmonks.org/?node_id=1226543>, and I received help from
    several PerlMonks members:

        AnomalousMonk <https://perlmonks.org/?node_id=634253>

        BrowserUK <https://perlmonks.org/?node_id=171588>

README.md  view on Meta::CPAN

# NAME

Algorithm::LUHN\_XS - Very Fast XS Version of the original Algorithm::LUHN

# SYNOPSIS

    use Algorithm::LUHN_XS qw/check_digit is_valid/;

    my $c;
    $c = check_digit("43881234567");
    print "It works\n" if is_valid("43881234567$c");

    $c = check_digit("A2C4E6G8"); # this will return undef

README.md  view on Meta::CPAN

    for (sort keys %vc) {
      print "$_ => $vc{$_}\n";
    }

    Algorithm::LUHN_XS::valid_chars(map {$_ => ord($_)-ord('A')+10} A..Z);
    $c = check_digit("A2C4E6G8");
    print "It worked again\n" if is_valid("A2C4E6G8$c");

# DESCRIPTION

This module is an XS version of the original Perl Module Algorithm::LUHN, which
was written by Tim Ayers.  It should work exactly the same, only substantially
 faster. The supplied check\_digit() routine is 100% compatible with the pure
Perl Algorithm::LUHN module, while the faster check\_digit\_fast() and really fast
check\_digit\_rff() are not. 

How much faster? Here's a benchmark, running on a 3.4GHz i7-2600:

`Benchmark: timing 100 iterations`

`Algorithm::LUHN: 69 secs (69.37 usr 0.00 sys)  1.44/s`

`check_digit:      2 secs ( 1.98 usr 0.00 sys) 50.51/s`

`check_digit_fast: 2 secs ( 1.68 usr 0.00 sys) 59.52/s`

`check_digit_rff:  1 secs ( 1.29 usr 0.00 sys) 77.52/s`

So, it's 35x to 53x faster than the original pure Perl module, depending on
how much compatibility with the original module you need.  

The rest of the documentation is mostly a copy of the original docs, with some
additions for functions that are new.

This module calculates the Modulus 10 Double Add Double checksum, also known as
the LUHN Formula. This algorithm is used to verify credit card numbers and
Standard & Poor's security identifiers such as CUSIP's and CSIN's.

You can find plenty of information about the algorithm by searching the web for
"modulus 10 double add double".

# FUNCTION

README.md  view on Meta::CPAN

    which is the right check digit. If you change it to a '3', it's not
    a valid card number. Ie:

        is_valid('4242424242424242');   # true
        is_valid('4242424242424243');   # false

- is\_valid\_fast CHECKSUMMED\_NUM
- is\_valid\_rff CHECKSUMMED\_NUM

    As with check\_digit(), we have 3 versions of is\_valid(), each one progressively
    faster than the check\_digit() that comes in the original pure Perl 
    Algorithm::LUHN module.  Here's a benchmark of 1M total calls to is\_valid():

    `Benchmark: timing 100 iterations`

    `Algorithm::LUHN: 100 secs (100.29 usr 0.01 sys)  1.00/s`

    `is_valid:          3 secs (  2.46 usr 0.11 sys) 38.91/s`

    `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

README.md  view on Meta::CPAN

    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

README.md  view on Meta::CPAN

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

# LICENSE

This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.

# CREDITS

Tim Ayers, for the original pure perl version of Algorithm::LUHN.

Neil Bowers, the current maintainer of Algorithm::LUHN.

The inspiration for this module was a PerlMonks post I made here:
[https://perlmonks.org/?node\_id=1226543](https://perlmonks.org/?node_id=1226543), and I received help 
from several PerlMonks members:

    [AnomalousMonk](https://perlmonks.org/?node_id=634253)

    [BrowserUK](https://perlmonks.org/?node_id=171588)

lib/Algorithm/LUHN_XS.pm  view on Meta::CPAN


# The hash of valid characters.
my %map = map { $_ => $_ } 0..9;
valid_chars(%map);
_al_init_vc(\%map);

=pod

=head1 NAME

Algorithm::LUHN_XS - Very Fast XS Version of the original Algorithm::LUHN

=head1 SYNOPSIS

  use Algorithm::LUHN_XS qw/check_digit is_valid/;

  my $c;
  $c = check_digit("43881234567");
  print "It works\n" if is_valid("43881234567$c");

  $c = check_digit("A2C4E6G8"); # this will return undef

lib/Algorithm/LUHN_XS.pm  view on Meta::CPAN

  for (sort keys %vc) {
    print "$_ => $vc{$_}\n";
  }

  Algorithm::LUHN_XS::valid_chars(map {$_ => ord($_)-ord('A')+10} A..Z);
  $c = check_digit("A2C4E6G8");
  print "It worked again\n" if is_valid("A2C4E6G8$c");

=head1 DESCRIPTION

This module is an XS version of the original Perl Module Algorithm::LUHN, which
was written by Tim Ayers.  It should work exactly the same, only substantially
 faster. The supplied check_digit() routine is 100% compatible with the pure
Perl Algorithm::LUHN module, while the faster check_digit_fast() and really fast
check_digit_rff() are not. 

How much faster? Here's a benchmark, running on a 3.4GHz i7-2600:

C<Benchmark: timing 100 iterations>

C<Algorithm::LUHN: 69 secs (69.37 usr 0.00 sys)  1.44/s>

C<check_digit:      2 secs ( 1.98 usr 0.00 sys) 50.51/s>

C<check_digit_fast: 2 secs ( 1.68 usr 0.00 sys) 59.52/s>

C<check_digit_rff:  1 secs ( 1.29 usr 0.00 sys) 77.52/s>

So, it's 35x to 53x faster than the original pure Perl module, depending on
how much compatibility with the original module you need.  

The rest of the documentation is mostly a copy of the original docs, with some
additions for functions that are new.

This module calculates the Modulus 10 Double Add Double checksum, also known as
the LUHN Formula. This algorithm is used to verify credit card numbers and
Standard & Poor's security identifiers such as CUSIP's and CSIN's.

You can find plenty of information about the algorithm by searching the web for
"modulus 10 double add double".

=head1 FUNCTION

lib/Algorithm/LUHN_XS.pm  view on Meta::CPAN

    is_valid('4242424242424242');   # true
    is_valid('4242424242424243');   # false

=cut

=item is_valid_fast CHECKSUMMED_NUM
=cut
=item is_valid_rff CHECKSUMMED_NUM

As with check_digit(), we have 3 versions of is_valid(), each one progressively
faster than the check_digit() that comes in the original pure Perl 
Algorithm::LUHN module.  Here's a benchmark of 1M total calls to is_valid():

C<Benchmark: timing 100 iterations>

C<Algorithm::LUHN: 100 secs (100.29 usr 0.01 sys)  1.00/s>

C<is_valid:          3 secs (  2.46 usr 0.11 sys) 38.91/s>

C<is_valid_fast:     2 secs (  2.38 usr 0.05 sys) 41.15/s> 

C<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.

=cut

# is_valid is an XS function

=item check_digit NUM

lib/Algorithm/LUHN_XS.pm  view on Meta::CPAN

=cut

__END__

=head1 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: 

=over 4

=item * is_valid_fast() and is_valid_rff() return 0 on failure, but
        is_valid() returns the empty string.

=item * check_digit_fast() and check_digit_rff() return -1 on failure, but
        check_digit() returns undef.

=back

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".


=head1 SEE ALSO

L<Algorithm::LUHN> is the original pure perl module this is based on.

L<Algorithm::CheckDigits> provides a front-end to a large collection
of modules for working with check digits.

L<Business::CreditCard> provides three functions for checking credit
card numbers. L<Business::CreditCard::Object> provides an OO interface
to those functions.

L<Business::CardInfo> provides a class for holding credit card details,
and has a type constraint on the card number, to ensure it passes the

lib/Algorithm/LUHN_XS.pm  view on Meta::CPAN

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

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.

=head1 CREDITS

Tim Ayers, for the original pure perl version of Algorithm::LUHN.

Neil Bowers, the current maintainer of Algorithm::LUHN.

The inspiration for this module was a PerlMonks post I made here:
L<https://perlmonks.org/?node_id=1226543>, and I received help 
from several PerlMonks members:
 
S<    >L<AnomalousMonk|https://perlmonks.org/?node_id=634253>

S<    >L<BrowserUK|https://perlmonks.org/?node_id=171588>



( run in 1.781 second using v1.01-cache-2.11-cpan-1c8d708658b )