File Coverage

blib/lib/Log/Parallel/Sql/Trim.pm
Criterion Covered Total %
statement 25 30 83.3
branch 4 12 33.3
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 36 51 70.5


line stmt bran cond sub pod time code
1              
2             package Log::Parallel::Sql::Trim;
3              
4 1     1   21821 use strict;
  1         3  
  1         44  
5 1     1   5 use warnings;
  1         2  
  1         27  
6 1     1   990 use Encode;
  1         12044  
  1         108  
7 1     1   772 use URI::Escape qw(uri_unescape uri_escape);
  1         1426  
  1         99  
8             require Exporter;
9              
10             our @ISA = qw(Exporter);
11             our @EXPORT = qw(encode_and_trim);
12              
13              
14             sub bytelen
15             {
16 1     1   5 use bytes;
  1         2  
  1         5  
17 2     2 0 11 return length($_[0]);
18             }
19              
20             sub encode_and_trim
21             {
22 1     1 0 13 my ($string, $length) = @_;
23 1 50       5 return '' unless defined $string;
24 1         7 $string = encode('UTF-8' => $string);
25 1 50       206 return $string unless $length;
26 1 50       5 return $string unless bytelen($string) > $length;
27              
28 1         5 $string = substr($string, 0, $length);
29              
30 1         2 for (;;) {
31 1         3 my $diff = bytelen($string) - $length;
32 1 50       10 return $string unless $diff > 0;
33              
34             #
35             # okay, it's too big and the difference is in the multi-byte characters
36             #
37              
38 0           my $l = length($string);
39 0           my $chop = $diff / $l;
40 0 0         $chop = $l/100 if $chop < $l/100;
41 0 0         $chop = 1 if $chop < 1;
42              
43 0           substr($string, -$chop) = '';
44             }
45             }
46              
47             1;
48              
49             __END__