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;
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;
}
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);
};
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"));
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. */