From 08e4f2aebdfb4e6a2933b142f2cb910cb69947f1 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Fri, 20 Feb 2015 18:01:59 +1000 Subject: [PATCH] * move parse_timing to Subtitle::BASE --- lib/Subtitle/BASE.pm | 36 ++++++++++++++++++++++++++++++++++++ lib/Subtitle/SRT.pm | 33 --------------------------------- lib/Subtitle/SSA.pm | 33 --------------------------------- 3 files changed, 36 insertions(+), 66 deletions(-) diff --git a/lib/Subtitle/BASE.pm b/lib/Subtitle/BASE.pm index ffb9700..abb9b7e 100644 --- a/lib/Subtitle/BASE.pm +++ b/lib/Subtitle/BASE.pm @@ -25,6 +25,42 @@ sub chomp { return $line =~ s/(^\xEF\xBB\xBF|[\r\n]+$)//ogr; } +sub parse_timing { + my ($self, $str) = @_; + my $time = 0.0; + return unless $str =~ m/(\d+):(\d+):(\d+)([.,])(\d+)/oi; + my ($hrs, $min, $sec, $delim, $msec) = ($1, $2, $3, $4, $5); + if ($msec < 0 or $msec > 999) { + $self->log(warn => "wrong mseconds part of timing: $msec"); + return; + } + if ($sec < 0 or $sec > 59) { + $self->log(warn => "wrong seconds part of timing: $sec"); + return; + } + if ($min < 0 or $min > 59) { + $self->log(warn => "wrong minutes part of timing: $sec"); + return; + } + if ($hrs < 0) { + $self->log(warn => "wrong minutes part of timing: $sec"); + return; + } + given (length("$msec")) { + when ("3") { $time += $msec * 0.001; } + when ("2") { $time += $msec * 0.01; } + when ("1") { $time += $msec * 0.1; } + default { + $self->log(error => "abnormal length of mseconds part"); + return; + } + } + $time += $sec; + $time += $min * 60; + $time += $hrs * 60 * 60; + return $time; +} + sub trim { my ($self, $line) = @_; return unless defined $line; diff --git a/lib/Subtitle/SRT.pm b/lib/Subtitle/SRT.pm index ee08839..78a67ef 100644 --- a/lib/Subtitle/SRT.pm +++ b/lib/Subtitle/SRT.pm @@ -20,39 +20,6 @@ sub new { return bless($self, $class); } -sub parse_timing { - my ($self, $str) = @_; - my $time = 0.0; - return unless $str =~ m/(\d+):(\d+):(\d+)([.,])(\d+)/oi; - my ($hrs, $min, $sec, $delim, $msec) = ($1, $2, $3, $4, $5); - if ($msec < 0 or $msec > 999) { - $self->log(warn => "wrong mseconds part of timing: $msec"); - return; - } - if ($sec < 0 or $sec > 59) { - $self->log(warn => "wrong seconds part of timing: $sec"); - return; - } - if ($min < 0 or $min > 59) { - $self->log(warn => "wrong minutes part of timing: $sec"); - return; - } - if ($hrs < 0) { - $self->log(warn => "wrong minutes part of timing: $sec"); - return; - } - given (length("$msec")) { - when ("3") { $time += $msec * 0.001; } - when ("2") { $time += $msec * 0.01; } - when ("1") { $time += $msec * 0.1; } - default { die("abnormal length of mseconds part"); } - } - $time += $sec; - $time += $min * 60; - $time += $hrs * 60 * 60; - return $time; -} - sub new_event { return +{ id => undef, timing => undef, text => undef }; } sub parse { diff --git a/lib/Subtitle/SSA.pm b/lib/Subtitle/SSA.pm index e057310..3119b1b 100644 --- a/lib/Subtitle/SSA.pm +++ b/lib/Subtitle/SSA.pm @@ -34,39 +34,6 @@ sub new { return bless($self, $class); } -sub parse_timing { - my ($self, $str) = @_; - my $time = 0.0; - return unless $str =~ m/(\d+):(\d+):(\d+)([.,])(\d+)/oi; - my ($hrs, $min, $sec, $delim, $msec) = ($1, $2, $3, $4, $5); - if ($msec < 0 or $msec > 999) { - $self->log(warn => "wrong mseconds part of timing: $msec"); - return; - } - if ($sec < 0 or $sec > 59) { - $self->log(warn => "wrong seconds part of timing: $sec"); - return; - } - if ($min < 0 or $min > 59) { - $self->log(warn => "wrong minutes part of timing: $sec"); - return; - } - if ($hrs < 0) { - $self->log(warn => "wrong minutes part of timing: $sec"); - return; - } - given (length("$msec")) { - when ("3") { $time += $msec * 0.001; } - when ("2") { $time += $msec * 0.01; } - when ("1") { $time += $msec * 0.1; } - default { die("abnormal length of mseconds part"); } - } - $time += $sec; - $time += $min * 60; - $time += $hrs * 60 * 60; - return $time; -} - sub parse_header { my ($self, $linenum, $line) = @_;