File Coverage

blib/lib/Data/GUID/URLSafe.pm
Criterion Covered Total %
statement 21 21 100.0
branch 1 2 50.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 30 31 96.7


line stmt bran cond sub pod time code
1 1     1   848 use strict;
  1         1  
  1         38  
2 1     1   4 use warnings;
  1         2  
  1         58  
3             package Data::GUID::URLSafe;
4             {
5             $Data::GUID::URLSafe::VERSION = '0.006';
6             }
7             # ABSTRACT: url-safe base64-encoded GUIDs
8              
9              
10 1     1   915 use Data::GUID ();
  1         17720  
  1         46  
11 1         9 use Sub::Exporter -setup => {
12             into => 'Data::GUID',
13             exports => [ qw(as_base64_urlsafe from_base64_urlsafe) ],
14             groups => [ default => [ -all ] ],
15 1     1   11 };
  1         2  
16              
17              
18             sub as_base64_urlsafe {
19 1     1 1 2058 my ($self) = @_;
20 1         6 my $base64 = $self->as_base64;
21 1         56 $base64 =~ tr{+/=}{-_}d;
22              
23 1         3 return $base64;
24             }
25              
26              
27             sub from_base64_urlsafe {
28 1     1 1 1287 my ($self, $string) = @_;
29              
30             # +/ should not be handled, so convert them to invalid chars
31             # also, remove spaces (\t..\r and SP) so as to calc padding len
32 1         2 $string =~ tr{-_\t-\x0d }{+/}d;
33 1 50       7 if (my $mod4 = length($string) % 4) {
34 1         2 $string .= substr('====', $mod4);
35             }
36              
37 1         15 return $self->from_base64($string);
38             }
39              
40             1;
41              
42             __END__