| 1 | package Number::RecordLocator; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | our $VERSION = '0.005'; | |||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use bigint; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | ▶ | use vars qw/%CHAR_TO_INT %INT_TO_CHAR $INITIALIZED %CHAR_REMAP/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Number::RecordLocator - Encodes integers into a short and easy to read and pronounce "locator string" | ||||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||
| 18 | |||||||||||
| 19 | use Number::RecordLocator; | ||||||||||
| 20 | |||||||||||
| 21 | my $generator = Number::RecordLocator->new(); | ||||||||||
| 22 | my $string = $generator->encode("123456"); | ||||||||||
| 23 | |||||||||||
| 24 | # $string = "5RL2"; | ||||||||||
| 25 | |||||||||||
| 26 | my $number = $generator->decode($string); | ||||||||||
| 27 | |||||||||||
| 28 | # $number = "123456"; | ||||||||||
| 29 | |||||||||||
| 30 | |||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||
| 32 | |||||||||||
| 33 | C<Number::RecordLocator> encodes integers into a 32 character "alphabet" | ||||||||||
| 34 | designed to be short and easy to read and pronounce. The encoding maps: | ||||||||||
| 35 | |||||||||||
| 36 | 0 to O | ||||||||||
| 37 | 1 to I | ||||||||||
| 38 | S to F | ||||||||||
| 39 | B to P | ||||||||||
| 40 | |||||||||||
| 41 | With a 32 bit encoding, you can map 33.5 million unique ids into a 5 character | ||||||||||
| 42 | code. | ||||||||||
| 43 | |||||||||||
| 44 | This certainly isn't an exact science and I'm not yet 100% sure of the encoding. | ||||||||||
| 45 | Feedback is much appreciated. | ||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | =cut | ||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | =head2 new | ||||||||||
| 52 | |||||||||||
| 53 | Instantiate a new C<Number::RecordLocator> object. Right now, we don't | ||||||||||
| 54 | actually store any object-specific data, but in the future, we might. | ||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 60 | 2 | my $class = shift; | |||||||||
| 61 | 2 | my $self = {}; | |||||||||
| 62 | 2 | bless $self => $class; | |||||||||
| 63 | 2 | ▶ | $self->init unless ($INITIALIZED); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 2 | return $self; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | |||||||||||
| 68 | =head2 init | ||||||||||
| 69 | |||||||||||
| 70 | Initializes our integer to character and character to integer mapping tables. | ||||||||||
| 71 | |||||||||||
| 72 | =cut | ||||||||||
| 73 | |||||||||||
| 74 | 2 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 75 | |||||||||||
| 76 | 2 | my $counter = 0; | |||||||||
| 77 | 2 | for ( 2 .. 9, 'A', 'C' .. 'R', 'T' .. 'Z' ) { | |||||||||
| 78 | 64 | $CHAR_TO_INT{$_} = $counter; | |||||||||
| 79 | 64 | $INT_TO_CHAR{$counter} = $_; | |||||||||
| 80 | 64 | $counter++; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 2 | $CHAR_REMAP{'0'} = 'O'; | |||||||||
| 84 | 2 | $CHAR_REMAP{'1'} = 'I'; | |||||||||
| 85 | 2 | $CHAR_REMAP{'S'} = 'F'; | |||||||||
| 86 | 2 | $CHAR_REMAP{'B'} = 'P'; | |||||||||
| 87 | |||||||||||
| 88 | 2 | ▶ | while (my ($from, $to) = each %CHAR_REMAP) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 8 | $CHAR_TO_INT{$from} = $CHAR_TO_INT{$to}; | |||||||||
| 90 | } | ||||||||||
| 91 | 2 | $INITIALIZED = 1; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | =head2 encode INTEGER | ||||||||||
| 95 | |||||||||||
| 96 | Takes an integer. Returns a Record Locator string. | ||||||||||
| 97 | |||||||||||
| 98 | =cut | ||||||||||
| 99 | |||||||||||
| 100 | 6 | ▶ | sub encode { | ||||||||
|
Subroutine
encode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 101 | 6 | my $self = shift; | |||||||||
| 102 | 6 | my $integer = shift; | |||||||||
| 103 | 6 | ▶ | return undef unless ($integer =~ /^\d+$/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 5 | my @numbers; | |||||||||
| 105 | 5 | ▶ | while ( $integer != 0 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 24 | unshift @numbers, $integer % 32; | |||||||||
| 107 | 24 | $integer = int( $integer / 32 ); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 5 | my $str = join( '', map { $INT_TO_CHAR{$_} } @numbers ); | |||||||||
| 111 | 5 | return $str; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | =head2 decode STRING | ||||||||||
| 115 | |||||||||||
| 116 | Takes a record locator string and returns an integer. If you pass in | ||||||||||
| 117 | a string containing an invalid character, it returns undef. | ||||||||||
| 118 | |||||||||||
| 119 | =cut | ||||||||||
| 120 | |||||||||||
| 121 | 7 | ▶ | sub decode { | ||||||||
|
Subroutine
decode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 122 | 7 | my $self = shift; | |||||||||
| 123 | 7 | my $str = uc(shift); | |||||||||
| 124 | 7 | my $integer = 0; | |||||||||
| 125 | 7 | foreach my $char (split(//,$str)){ | |||||||||
| 126 | 36 | my $char = $CHAR_TO_INT{$char}; | |||||||||
| 127 | 36 | ▶ | return undef unless defined $char; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 36 | $integer = ($integer * 32) + $char; | |||||||||
| 129 | } | ||||||||||
| 130 | 7 | return $integer; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | =head2 canonicalize STRING | ||||||||||
| 134 | |||||||||||
| 135 | To compare a Record Locator string with another you can do: | ||||||||||
| 136 | |||||||||||
| 137 | print "ALWAYS TRUE\n" if $generator->decode("B0") == $generator->decode("PO"); | ||||||||||
| 138 | |||||||||||
| 139 | However, this method provides an alternative: | ||||||||||
| 140 | |||||||||||
| 141 | my $rl_string = $generator->encode(725); | ||||||||||
| 142 | print "ALWAYS TRUE\n" if $generator->canonicalize("b0") eq $rl_string; | ||||||||||
| 143 | print "ALWAYS TRUE\n" if $generator->canonicalize("BO") eq $rl_string; | ||||||||||
| 144 | print "ALWAYS TRUE\n" if $generator->canonicalize("P0") eq $rl_string; | ||||||||||
| 145 | print "ALWAYS TRUE\n" if $generator->canonicalize("po") eq $rl_string; | ||||||||||
| 146 | |||||||||||
| 147 | This is primarily useful if you store the record locator rather than just the | ||||||||||
| 148 | original integer and don't want to have to decode your strings to do | ||||||||||
| 149 | comparisons. | ||||||||||
| 150 | |||||||||||
| 151 | Takes a general Record Locator string and returns one with character mappings | ||||||||||
| 152 | listed in L</DESCRIPTION> applied to it. This allows string comparisons to work. | ||||||||||
| 153 | This returns C<undef> if a non-alphanumeric character is found in the string. | ||||||||||
| 154 | |||||||||||
| 155 | =cut | ||||||||||
| 156 | |||||||||||
| 157 | 4 | ▶ | sub canonicalize { | ||||||||
|
Subroutine
canonicalize: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 158 | 4 | my $self = shift; | |||||||||
| 159 | 4 | my $str = uc(shift); | |||||||||
| 160 | 4 | my $result = ''; | |||||||||
| 161 | 4 | for my $char (split(//,$str)) { # Would tr/// be better? | |||||||||
| 162 | 16 | ▶ | return undef unless defined $CHAR_TO_INT{$char}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | 16 | ▶ | my $char = defined $CHAR_REMAP{$char} ? $CHAR_REMAP{$char} : $char; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | 16 | $result .= $char; | |||||||||
| 165 | } | ||||||||||
| 166 | 4 | return $result; | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | =head1 BUGS AND LIMITATIONS | ||||||||||
| 170 | |||||||||||
| 171 | No bugs have been reported. | ||||||||||
| 172 | |||||||||||
| 173 | Please report any bugs or feature requests to | ||||||||||
| 174 | C<bug-number-recordlocator@rt.cpan.org>, or through the web interface at | ||||||||||
| 175 | L<http://rt.cpan.org>. | ||||||||||
| 176 | |||||||||||
| 177 | |||||||||||
| 178 | =head1 AUTHOR | ||||||||||
| 179 | |||||||||||
| 180 | Jesse Vincent C<< <jesse@bestpractical.com> >> | ||||||||||
| 181 | |||||||||||
| 182 | |||||||||||
| 183 | =head1 LICENCE AND COPYRIGHT | ||||||||||
| 184 | |||||||||||
| 185 | Copyright (c) 2006, Best Practical Solutions, LLC. All rights reserved. | ||||||||||
| 186 | |||||||||||
| 187 | This module is free software; you can redistribute it and/or | ||||||||||
| 188 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||||||||||
| 189 | |||||||||||
| 190 | =cut | ||||||||||
| 191 | |||||||||||
| 192 | 3 | 1; | |||||||||