Browse files

Added multi sub depending on type (Str, Buf, IO)

* Now you can CRC a filehandle or a Buf instead of just Strings
  • Loading branch information...
1 parent bd9be21 commit 6a906cff8ede055a7200de42ccc9ff4b55761204 @cosimo committed May 10, 2011
Showing with 51 additions and 16 deletions.
  1. +12 −3 lib/String/CRC32.pm
  2. +22 −2 lib/String/CRC32.pod
  3. +15 −11 t/crc.t
  4. +2 −0 t/testfile
View
15 lib/String/CRC32.pm
@@ -39,12 +39,11 @@ my @CRC_TABLE =
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d;
-method crc32 (Str $c) {
-
+our multi sub crc32 (Str $s) {
my $crcinit = 0;
my $crc = $crcinit +^ 0xFFFFFFFF;
- for $c.split('') {
+ for $s.split('') {
my $char = $_.ord;
$crc = (($crc +> 8) +& 0x00FFFFFF) +^ @CRC_TABLE[ ($crc +^ $char) +& 0xFF ];
}
@@ -54,3 +53,13 @@ method crc32 (Str $c) {
return $crc;
}
+our multi sub crc32 (Buf $b) {
+ my Str $s = $b.decode('UTF-8');
+ return crc32($s);
+}
+
+our multi sub crc32 (IO $fh) {
+ my Str $s = $fh.slurp();
+ return crc32($s);
+}
+
View
24 lib/String/CRC32.pod
@@ -9,8 +9,14 @@ String::CRC32 - Calculate 32-bit CRC checksum of strings
use v6;
use String::CRC32;
+ # CRC of a string
my $string = 'Hello, world';
- my $crc32 = String::CRC32.crc32($string);
+ my $crc32 = String::CRC32::crc32($string);
+
+ # CRC of a filehandle
+ my $fh = open("myfile", :bin, :r);
+ $crc32 = String::CRC32::crc32($fh);
+ $fh.close();
=head1 DESCRIPTION
@@ -30,10 +36,24 @@ Example:
use String::CRC32;
my $string = 'Hello, world';
- say String::CRC32.crc32($string);
+ say String::CRC32::crc32($string);
# prints '3885672898'
+=head2 C<crc32($fh)>
+
+Reads the whole file and calculates the CRC of
+that file.
+
+Example:
+
+ use v6;
+ use String::CRC32;
+
+ my $fh = open("myfile.txt", :bin, :r);
+ say String::CRC32::crc32($fh);
+ $fh.close();
+
=head1 AUTHOR
Cosimo Streppone, E<lt>cosimo@cpan.orgE<gt>
View
26 t/crc.t
@@ -5,29 +5,33 @@ use String::CRC32;
my $string1 = "This is the test string";
is(
- String::CRC32.crc32($string1), 1835534707,
+ String::CRC32::crc32($string1), 1835534707,
"Test the CRC of a string variable"
);
is(
- String::CRC32.crc32("This is another test string"), 2154698217,
+ String::CRC32::crc32("This is another test string"), 2154698217,
"Test the CRC of another string variable"
);
is(
- String::CRC32.crc32("Perl6 rocks"), 1413143404,
+ String::CRC32::crc32("Perl6 rocks"), 1413143404,
"Test the CRC of an awesome string",
);
+# Test type Buf
+my $buf1 = $string1.encode("UTF-8");
+is(
+ String::CRC32::crc32($buf1), 1835534707,
+ "Test the CRC of a Buf variable",
+);
-# TODO fails because Buf.split() not implemented
-
-## Test type Buf
-#my $buf1 = $string1.encode("UTF-8");
-#is(
-# String::CRC32.crc32($buf1), 1835534707,
-# "Test the CRC of a Buf variable",
-#);
+# Test CRC of a filehandle
+my $fh = open("t/testfile", :bin, :r);
+is(
+ String::CRC32::crc32($fh), 1925609391,
+ "Test the CRC of a file handle"
+);
done;
View
2 t/testfile
@@ -0,0 +1,2 @@
+Do not alter this file!
+Changing this file will result in a failing test!

0 comments on commit 6a906cf

Please sign in to comment.