Browse Source

+ Subtitle::Utils

master
Alex 'AdUser' Z 8 years ago
parent
commit
6c5ae23a4c
  1. 12
      lib/Subtitle/BASE.pm
  2. 69
      lib/Subtitle/Utils.pm
  3. 22
      t/utils-string.t

12
lib/Subtitle/BASE.pm

@ -19,12 +19,6 @@ sub log {
return 1;
}
sub chomp {
my ($self, $line) = @_;
return unless defined $line;
return $line =~ s/(^\xEF\xBB\xBF|[\r\n]+$)//ogr;
}
sub parse_timing {
my ($self, $str) = @_;
my $time = 0.0;
@ -75,12 +69,6 @@ sub build_timing {
return sprintf($self->{timing_fmt}, $hrs, $min, $sec, $msec);
}
sub trim {
my ($self, $line) = @_;
return unless defined $line;
return $line =~ s/(^\s+|\s+$)//or;
}
sub events {
my ($self) = @_;

69
lib/Subtitle/Utils.pm

@ -0,0 +1,69 @@
package Subtitle::Utils;
use strict;
use warnings;
use base 'Exporter';
our @EXPORT_OK = qw(
chomp_all strip_bom trim
);
our %EXPORT_TAGS = (
string => [qw(chomp_all strip_bom trim)],
);
## string sunctions
sub chomp_all {
return unless @_;
return $_ =~ s/[\r\n]+$//o for @_;
}
sub strip_bom {
return unless @_;
return $_[0] =~ s/^\xEF\xBB\xBF//o;
}
sub trim {
return unless @_;
$_ =~ s/(^\s+|\s+$)//go for @_;
}
1;
=pod
=head1 NAME
Subtitle::Utils -- usefull generic routines
=head1 SYNOPSYS
use Subtitle::Utils qw(:all);
=head1 FUNCTIONS / STRINGS
use Subtitle::Utils qw(:string);
=head2 C<chomp_all>
chomp($line);
chomp($line1, $line2);
In-place strips newlines (CR/LF) from line.
=head2 C<strip_bom>
strip_bom($line);
In-place strips Unicode's BOM (Byte Order Mark) from line.
=head2 C<trim>
trim($line);
In-place strips leading and trailing spaces from the given string
=cut

22
t/utils-string.t

@ -0,0 +1,22 @@
use strict;
use warnings;
use Test::More tests => 3;
use Subtitle::Utils qw(:string);
my $line;
$line = " test \r\n";
chomp_all($line);
is($line, ' test ');
$line = "\xEF\xBB\xBFtest";
strip_bom($line);
is($line, 'test');
$line = ' test ';
trim($line);
is($line, 'test');
exit 0;
Loading…
Cancel
Save