Basic release notes support (#2282).
[dcpomatic.git] / src / lib / image_filename_sorter.cc
index c32b07115245145061bd4c87c206db9c565cb3a0..ab0d298fc1bd0f9bcceb881a83744d9157681433 100644 (file)
 
 */
 
+
 #include "image_filename_sorter.h"
 #include <dcp/locale_convert.h>
 #include <boost/filesystem.hpp>
-#include <boost/foreach.hpp>
 #include <boost/optional.hpp>
 #include <iostream>
 
+
 using std::list;
 using std::string;
 using dcp::locale_convert;
 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();
+       auto an = extract_numbers (a);
+       auto 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;
-       string const ps = p.leaf().string();
+       auto const ps = p.leaf().string();
        for (size_t i = 0; i < ps.size(); ++i) {
                if (isdigit (ps[i])) {
                        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;
 }