From 12e20e180062dbc338e775bd1f5ec7a2af91df7f Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Fri, 17 Jul 2015 00:41:04 +0100 Subject: [PATCH] Improve image filename sorter. --- ChangeLog | 4 +++ src/lib/image_filename_sorter.cc | 40 ++++++++++++++++++++---------- test/image_filename_sorter_test.cc | 2 ++ 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3d149a824..d6ae72fb8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2015-07-17 Carl Hetherington + + * Improve still-image filename sorting. + 2015-07-16 Carl Hetherington * Updated ru_RU translation from Igor Voytovich. diff --git a/src/lib/image_filename_sorter.cc b/src/lib/image_filename_sorter.cc index 5a3e1dcec..037446398 100644 --- a/src/lib/image_filename_sorter.cc +++ b/src/lib/image_filename_sorter.cc @@ -27,35 +27,49 @@ class ImageFilenameSorter public: bool operator() (boost::filesystem::path a, boost::filesystem::path b) { - boost::optional na = extract_number (a); - boost::optional nb = extract_number (b); - if (!na || !nb) { - return a.string() < b.string(); + std::vector na = extract_numbers (a); + std::vector nb = extract_numbers (b); + + std::vector::const_iterator i = na.begin (); + std::vector::const_iterator j = nb.begin (); + + while (true) { + if (i == na.end () || j == nb.end ()) { + return false; + } + + if (*i != *j) { + return *i < *j; + } + + ++i; + ++j; } - return na.get() < nb.get(); + /* NOT REACHED */ + return false; } private: - boost::optional extract_number (boost::filesystem::path p) + std::vector extract_numbers (boost::filesystem::path p) { p = p.leaf (); + std::vector numbers; std::string number; for (size_t i = 0; i < p.string().size(); ++i) { if (isdigit (p.string()[i])) { number += p.string()[i]; - } else { - if (!number.empty ()) { - break; - } + } else if (!number.empty ()) { + numbers.push_back (raw_convert (number)); + number.clear (); } } - if (number.empty ()) { - return boost::optional (); + if (!number.empty ()) { + numbers.push_back (raw_convert (number)); } - return raw_convert (number); + return numbers; } }; diff --git a/test/image_filename_sorter_test.cc b/test/image_filename_sorter_test.cc index 830065f77..4bdd16f55 100644 --- a/test/image_filename_sorter_test.cc +++ b/test/image_filename_sorter_test.cc @@ -31,6 +31,7 @@ BOOST_AUTO_TEST_CASE (image_filename_sorter_test) BOOST_CHECK (x ("1", "999")); BOOST_CHECK (x ("00057.tif", "00166.tif")); 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 ("abc0000000002", "abc0000000001")); BOOST_CHECK (!x ("2", "1")); @@ -38,4 +39,5 @@ BOOST_AUTO_TEST_CASE (image_filename_sorter_test) BOOST_CHECK (!x ("2", "0001")); BOOST_CHECK (!x ("999", "1")); BOOST_CHECK (!x ("/my/numeric999/path/00166.tif", "/my/numeric999/path/00057.tif")); + BOOST_CHECK (!x ("1_02.tif", "1_01.tif")); } -- 2.30.2