Fix image filename sorter with filenames that have lots of numbers.
authorCarl Hetherington <cth@carlh.net>
Tue, 24 Jul 2018 09:18:44 +0000 (10:18 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 24 Jul 2018 09:18:44 +0000 (10:18 +0100)
src/lib/image_filename_sorter.cc
src/lib/image_filename_sorter.h
test/image_filename_sorter_test.cc

index c32b07115245145061bd4c87c206db9c565cb3a0..47f46e81d75ecbe5aa589c4b1fd592241a5f150d 100644 (file)
@@ -33,16 +33,22 @@ using boost::optional;
 bool
 ImageFilenameSorter::operator() (boost::filesystem::path a, boost::filesystem::path b)
 {
-       optional<int> na = extract_numbers (a);
-       optional<int> nb = extract_numbers (b);
-       if (!na || !nb) {
-               return a.string() < b.string();
+       string an = extract_numbers (a);
+       string bn = extract_numbers (b);
+
+       int const anl = an.length ();
+       int const bnl = bn.length ();
+
+       if (anl > bnl) {
+               bn = string(anl - bnl, '0') + bn;
+       } else if (bnl > anl) {
+               an = string(bnl - anl, '0') + an;
        }
 
-       return *na < *nb;
+       return an < bn;
 }
 
-optional<int>
+string
 ImageFilenameSorter::extract_numbers (boost::filesystem::path p)
 {
        string numbers;
@@ -52,13 +58,5 @@ ImageFilenameSorter::extract_numbers (boost::filesystem::path p)
                        numbers += ps[i];
                }
        }
-
-       if (numbers.empty ()) {
-               return optional<int> ();
-       }
-
-       /* locale_convert is quicker than raw_convert and numbers can only contain
-          things which are isdigit() so locale_convert is fine to use.
-       */
-       return locale_convert<int> (numbers);
+       return numbers;
 }
index 2a15639cea4a487f8af0c07d1d992ccf748ec0a5..960b1d8a70253e6b596056431d1c51b9ba39d083 100644 (file)
@@ -27,5 +27,5 @@ public:
        bool operator() (boost::filesystem::path a, boost::filesystem::path b);
 
 private:
-       boost::optional<int> extract_numbers (boost::filesystem::path p);
+       std::string extract_numbers (boost::filesystem::path p);
 };
index f2af9000c0149f4f6e7d8e29c8d0286d68a0b210..7512fdffbc289dbdc82b3508f6497450f291e0f9 100644 (file)
@@ -43,6 +43,7 @@ BOOST_AUTO_TEST_CASE (image_filename_sorter_test1)
        BOOST_CHECK (x ("/my/numeric999/path/00057.tif", "/my/numeric999/path/00166.tif"));
        BOOST_CHECK (x ("1_01.tif", "1_02.tif"));
        BOOST_CHECK (x ("EWS_DCP_092815_000000.j2c", "EWS_DCP_092815_000001.j2c"));
+       BOOST_CHECK (x ("ap_trlr_178_uhd_bt1886_txt_e5c1_033115.86352.dpx", "ap_trlr_178_uhd_bt1886_txt_e5c1_033115.86353.dpx"));
 
        BOOST_CHECK (!x ("abc0000000002", "abc0000000001"));
        BOOST_CHECK (!x ("2", "1"));
@@ -53,6 +54,7 @@ BOOST_AUTO_TEST_CASE (image_filename_sorter_test1)
        BOOST_CHECK (!x ("1_02.tif", "1_01.tif"));
        BOOST_CHECK (!x ("EWS_DCP_092815_000000.j2c", "EWS_DCP_092815_000000.j2c"));
        BOOST_CHECK (!x ("EWS_DCP_092815_000100.j2c", "EWS_DCP_092815_000000.j2c"));
+       BOOST_CHECK (!x ("ap_trlr_178_uhd_bt1886_txt_e5c1_033115.86353.dpx", "ap_trlr_178_uhd_bt1886_txt_e5c1_033115.86352.dpx"));
 }
 
 /** Test a sort of a lot of paths.  Mostly useful for profiling. */