Allow SubripReader::convert_time to take a milliseconds separator.
authorCarl Hetherington <cth@carlh.net>
Sun, 13 Nov 2022 23:38:49 +0000 (00:38 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 13 Nov 2022 23:38:51 +0000 (00:38 +0100)
Maybe this method should just be on its own somewhere now.

src/subrip_reader.cc
src/subrip_reader.h
test/subrip_reader_test.cc

index 926511a3db16c6361037adcf4030f963e73a1b8e..a59b43fbedfe8642f836d3e95160696ef60dd864 100644 (file)
  *  @brief SubripReader class.
  */
 
-#include "subrip_reader.h"
+
+#include "compose.hpp"
 #include "exceptions.h"
-#include "util.h"
-#include "sub_assert.h"
 #include "raw_convert.h"
 #include "ssa_reader.h"
+#include "sub_assert.h"
+#include "subrip_reader.h"
+#include "util.h"
 #include <boost/algorithm/string.hpp>
 #include <boost/algorithm/string_regex.hpp>
+#include <boost/bind.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/regex.hpp>
-#include <boost/bind.hpp>
 #include <cstdio>
-#include <vector>
 #include <iostream>
+#include <vector>
+
 
 using std::string;
 using std::vector;
@@ -125,13 +128,13 @@ SubripReader::read (function<optional<string> ()> get_line)
                        }
 
                        string expected;
-                       auto from = convert_time(p[0], &expected);
+                       auto from = convert_time(p[0], ",", &expected);
                        if (!from) {
                                throw SubripError(p[0], expected, _context);
                        }
                        rs.from = *from;
 
-                       auto to = convert_time(p[2], &expected);
+                       auto to = convert_time(p[2], ",", &expected);
                        if (!to) {
                                throw SubripError(p[2], expected, _context);
                        }
@@ -163,7 +166,7 @@ SubripReader::read (function<optional<string> ()> get_line)
 }
 
 optional<Time>
-SubripReader::convert_time(string t, string* expected)
+SubripReader::convert_time(string t, string milliseconds_separator, string* expected)
 {
        auto report_expected = [expected](string const& s) {
                if (expected) {
@@ -179,9 +182,9 @@ SubripReader::convert_time(string t, string* expected)
        }
 
        vector<string> b;
-       boost::algorithm::split (b, a[2], boost::is_any_of (","));
+       boost::algorithm::split(b, a[2], boost::is_any_of(milliseconds_separator));
        if (b.size() != 2) {
-               report_expected("time in the format h:m:s,ms");
+               report_expected(String::compose("time in the format h:m:s%1ms", milliseconds_separator));
                return {};
        }
 
index e29002f43a7e9e9b33235e51da92cc714c7e97cd..83fefdf7c521aebf06a6cc88e44bd11652435ccb 100644 (file)
@@ -41,7 +41,7 @@ public:
        SubripReader (FILE* f);
        SubripReader (std::string subs);
 
-       static boost::optional<Time> convert_time(std::string t, std::string* expected = nullptr);
+       static boost::optional<Time> convert_time(std::string t, std::string milliseconds_separator, std::string* expected = nullptr);
 
 private:
        /* For tests */
index 6029b237d10ba0b03f366f1a6f962ab758354bb4..aa3760236215cb8e21e2d8739fc28f703857cc31 100644 (file)
@@ -408,10 +408,10 @@ BOOST_AUTO_TEST_CASE (subrip_reader_convert_line_test)
 BOOST_AUTO_TEST_CASE (subrip_reader_convert_time_test)
 {
        sub::SubripReader reader;
-       auto t = reader.convert_time("00:03:10,500");
+       auto t = reader.convert_time("00:03:10,500", ",");
        BOOST_REQUIRE(t);
        BOOST_CHECK_EQUAL(*t, sub::Time::from_hms(0, 3, 10, 500));
-       t = reader.convert_time("04:19:51,782");
+       t = reader.convert_time("04:19:51,782", ",");
        BOOST_REQUIRE(t);
        BOOST_CHECK_EQUAL(*t, sub::Time::from_hms(4, 19, 51, 782));
 }