From f278fa10f066a026e3f5d41e02f08f4a670297d0 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Jul 13 2016 10:07:41 +0000 Subject: Avoid making a copy of filepaths just to compare them (#616) Instead compare them in-place. --- diff --git a/toonz/sources/common/tsystem/tsystem.cpp b/toonz/sources/common/tsystem/tsystem.cpp index 9dde853..20d11d7 100644 --- a/toonz/sources/common/tsystem/tsystem.cpp +++ b/toonz/sources/common/tsystem/tsystem.cpp @@ -412,12 +412,27 @@ class CaselessFilepathLess final : public std::binary_function { public: bool operator()(const TFilePath &a, const TFilePath &b) const { - wstring aa = toLower(a.getWideString()); - wstring bb = toLower(b.getWideString()); - if (aa == bb) - return a < b; - else - return aa < bb; + // Perform case sensitive compare, fallback to case insensitive. + const wstring a_str = a.getWideString(); + const wstring b_str = b.getWideString(); + + unsigned int i = 0; + int case_compare = -1; + while (a_str[i] || b_str[i]) { + if (a_str[i] != b_str[i]) { + const wchar_t a_wchar = towlower(a_str[i]); + const wchar_t b_wchar = towlower(b_str[i]); + if (a_wchar < b_wchar) { + return true; + } else if (a_wchar > b_wchar) { + return false; + } else if (case_compare == -1) { + case_compare = a_str[i] < b_str[i]; + } + } + i++; + } + return (case_compare == 1); } };