adding base36 functions

This commit is contained in:
JT Smith 2006-01-18 15:21:10 +00:00
parent 2069da5fdb
commit ea14e9de4c
3 changed files with 54 additions and 3 deletions

View file

@ -22,7 +22,8 @@ use Tie::IxHash;
our @ISA = qw(Exporter);
our @EXPORT = qw(&isBetween &makeTabSafe &makeArrayTabSafe &randomizeHash &commify &randomizeArray
&formatBytes &sortHashDescending &sortHash &isIn &makeCommaSafe &makeArrayCommaSafe &randint &round);
&formatBytes &sortHashDescending &sortHash &isIn &makeCommaSafe &makeArrayCommaSafe &randint &round
&fromBase36 &toBase36);
=head1 NAME
@ -38,6 +39,7 @@ This package provides miscellaneous but useful utilities to the WebGUI programme
use WebGUI::Utility;
$string = commify($integer);
$size = formatBytes($integer);
$number = fromBase36($string);
$boolean = isIn($value, @array);
makeArrayCommaSafe(\@array);
makeArrayTabSafe(\@array);
@ -48,6 +50,7 @@ This package provides miscellaneous but useful utilities to the WebGUI programme
$hashRef = randomizeHash(\%hash);
%hash = sortHash(%hash);
%hash = sortHashDescending(%hash);
$string = toBase36($number);
=head1 METHODS
@ -98,7 +101,27 @@ sub formatBytes {
}
}
#-------------------------------------------------------------------
=head2 fromBase36 ( string )
Returns a number that has been decoded from base36.
=head3 string
A base 36 encoded string.
=cut
sub fromBase36 {
my $string = shift;
my $exponent = 0;
my $number;
for (reverse split //, $string) {
$number += ($_ =~ /\d/ ? $_ : (ord($_) - 87)) * (36**$exponent++);
}
return $number;
}
#-------------------------------------------------------------------
@ -362,6 +385,30 @@ sub sortHashDescending {
return %newHash;
}
#-------------------------------------------------------------------
=head2 toBase36 ( number )
Returns a string that is base36 encoded.
=head3 number
A number that you wish to encode.
=cut
sub toBase36 {
my $number = shift;
my $string = "";
while ($number) {
my $quot = $number % 36;
$string = ($quot < 10 ? $quot : chr($quot + 87)) . $string;
$number = int($number / 36);
}
$string = "0$string" while length($string) < 9;
$string;
}
1;

View file

@ -2,6 +2,8 @@ WebGUI::Cache::Memcached
WebGUI::Auth::LDAP
WebGUI::Asset::Wobject::WSClient
WebGUI::Asset::File::ZipArchive
WebGUI::Asset::Template::HTMLTemplateExpr
WebGUI::Asset::Template::TemplateToolkit
WebGUI::Asset::Wobject::Matrix
WebGUI::Asset::Wobject::HttpProxy
WebGUI::Asset::Wobject::Product
@ -34,7 +36,6 @@ WebGUI::Macro::FormParam
WebGUI::Macro::GroupAdd
WebGUI::Macro::GroupDelete
WebGUI::Macro::GroupText
WebGUI::Macro::If
WebGUI::Macro::Include
WebGUI::Macro::LastModified
WebGUI::Macro::Product

View file

@ -16,10 +16,13 @@ use lib "$FindBin::Bin/lib";
use WebGUI::Test;
use WebGUI::Session;
use Test::More tests => 21; # increment this value for each test you create
use Test::More tests => 22; # increment this value for each test you create
my $session = WebGUI::Test->session;
# base 36
is(WebGUI::Utility::fromBase36(WebGUI::Utility::toBase36(1234567890)), 1234567890, "{to/from}Base36()");
# commify
is(WebGUI::Utility::commify(10), "10", 'commify() - no comma needed');
is(WebGUI::Utility::commify(1000), "1,000", 'commify() - single comma');