perl-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stas Bekman <s...@stason.org>
Subject [patch] size_string for compat.pm
Date Mon, 28 Jan 2002 06:41:40 GMT
since apr_strfsize returns only 4 chars and looses the .X precision 
point, we cannot use the wrapper to provide fully compatible version of 
size_string. So I've added a pure perl implementation (copied from the C 
code) and added tests.

Index: lib/Apache/compat.pm
===================================================================
RCS file: /home/cvs/modperl-2.0/lib/Apache/compat.pm,v
retrieving revision 1.32
diff -u -r1.32 compat.pm
--- lib/Apache/compat.pm	20 Dec 2001 01:31:24 -0000	1.32
+++ lib/Apache/compat.pm	28 Jan 2002 06:37:34 -0000
@@ -351,6 +351,33 @@
  # the following functions now live in Apache::RequestRec
  # * mtime

+package Apache::Util;
+
+sub size_string {
+    my ($size) = shift;
+
+    if (!$size) {
+ 
$size = "   0k";
+    }
+    elsif ($size == -1) {
+        $size = "    -";
+    }
+    elsif ($size < 1024) {
+ 
$size = "   1k";
+    }
+    elsif ($size < 1048576) {
+ 
$size = sprintf "%4dk", ($size + 512) / 1024;
+    }
+    elsif (size < 103809024) {
+ 
$size = sprintf "%4.1fM", $size / 1048576.0;
+    }
+    else {
+ 
$size = sprintf "%4dM", ($size + 524288) / 1048576;
+    }
+
+    return $size;
+
+}

  1;
  __END__
Index: t/response/TestApache/compat2.pm
===================================================================
RCS file: /home/cvs/modperl-2.0/t/response/TestApache/compat2.pm,v
retrieving revision 1.1
diff -u -r1.1 compat2.pm
--- t/response/TestApache/compat2.pm	21 Jan 2002 08:32:46 -0000	1.1
+++ t/response/TestApache/compat2.pm	28 Jan 2002 06:37:34 -0000
@@ -12,10 +12,19 @@
  use Apache::compat ();
  use Apache::Constants qw(OK);

+my %string_size = (
+    '-1'            => "    -",
+    0               => "   0k",
+    42              => "   1k",
+    42_000          => "  41k",
+    42_000_000      => "40.1M",
+    42_000_000_000  => "40054.3M",
+);
+
  sub handler {
      my $r = shift;

-    plan $r, tests => 28, todo => [23];
+    plan $r, tests => 34, todo => [23];

      $r->send_http_header('text/plain');

@@ -144,6 +153,7 @@
                   "\$r->set_content_length($csize) w/ setting explicit 
size");

          $r->set_content_length();
+        # TODO
          ok t_cmp(0, # XXX: $r->finfo->csize is not available yet
                   $r->headers_out->{"Content-length"},
                   "\$r->set_content_length() w/o setting explicit size");
@@ -169,6 +179,13 @@
          $r->set_last_modified($time);
          ok t_cmp($time, $r->mtime, "\$r->set_last_modified(\$time)");

+    }
+
+    # Apache::Util::size_string
+    {
+        while (my($k, $v) = each %string_size) {
+            ok t_cmp($v, Apache::Util::size_string($k));
+        }
      }

      Apache::OK;



_____________________________________________________________________
Stas Bekman             JAm_pH      --   Just Another mod_perl Hacker
http://stason.org/      mod_perl Guide   http://perl.apache.org/guide
mailto:stas@stason.org  http://ticketmaster.com http://apacheweek.com
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Mime
View raw message