From ea14e9de4c157338ad919e8d947dc4344f3a4581 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Wed, 18 Jan 2006 15:21:10 +0000 Subject: [PATCH] adding base36 functions --- lib/WebGUI/Utility.pm | 49 +++++++++++++++++++++++++++++++++++- sbin/preload.exclude.example | 3 ++- t/Utility.t | 5 +++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/WebGUI/Utility.pm b/lib/WebGUI/Utility.pm index cfba47d51..9f6e4f049 100644 --- a/lib/WebGUI/Utility.pm +++ b/lib/WebGUI/Utility.pm @@ -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; diff --git a/sbin/preload.exclude.example b/sbin/preload.exclude.example index 0eafc051d..a4ed05df2 100644 --- a/sbin/preload.exclude.example +++ b/sbin/preload.exclude.example @@ -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 diff --git a/t/Utility.t b/t/Utility.t index af0b3cc79..b86729acd 100644 --- a/t/Utility.t +++ b/t/Utility.t @@ -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');