From 6c5ae23a4ca3b80401e16c6c9319aa7b19f2b058 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 11 Jun 2016 12:19:46 +0000 Subject: [PATCH] + Subtitle::Utils --- lib/Subtitle/BASE.pm | 12 -------- lib/Subtitle/Utils.pm | 69 +++++++++++++++++++++++++++++++++++++++++++ t/utils-string.t | 22 ++++++++++++++ 3 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 lib/Subtitle/Utils.pm create mode 100644 t/utils-string.t diff --git a/lib/Subtitle/BASE.pm b/lib/Subtitle/BASE.pm index b7f4734..4bda324 100644 --- a/lib/Subtitle/BASE.pm +++ b/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) = @_; diff --git a/lib/Subtitle/Utils.pm b/lib/Subtitle/Utils.pm new file mode 100644 index 0000000..42063ca --- /dev/null +++ b/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($line); + chomp($line1, $line2); + +In-place strips newlines (CR/LF) from line. + +=head2 C + + strip_bom($line); + +In-place strips Unicode's BOM (Byte Order Mark) from line. + +=head2 C + + trim($line); + +In-place strips leading and trailing spaces from the given string + +=cut diff --git a/t/utils-string.t b/t/utils-string.t new file mode 100644 index 0000000..1bfc6d2 --- /dev/null +++ b/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;