diff options
| author | Carl Hetherington <cth@carlh.net> | 2016-09-30 09:35:17 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2016-09-30 09:35:17 +0100 |
| commit | 5d71b86e818f9dfa3f7f573e438f2f6ddd5cdefc (patch) | |
| tree | b96f3b450d09cf86160db90a6fdfe45be9726fdb | |
| parent | 3721e30a5eece6a84276ee02540331393bcbfcfd (diff) | |
Better tidy_for_filename that doesn't screw up with UTF-8.
| -rw-r--r-- | src/lib/util.cc | 13 | ||||
| -rw-r--r-- | test/util_test.cc | 8 |
2 files changed, 11 insertions, 10 deletions
diff --git a/src/lib/util.cc b/src/lib/util.cc index 0b300147d..00f36f217 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -53,6 +53,7 @@ extern "C" { #include <glib.h> #include <pangomm/init.h> #include <boost/algorithm/string.hpp> +#include <boost/range/algorithm/replace_if.hpp> #include <boost/thread.hpp> #include <boost/filesystem.hpp> #ifdef DCPOMATIC_WINDOWS @@ -567,16 +568,8 @@ valid_j2k_file (boost::filesystem::path f) string tidy_for_filename (string f) { - string t; - for (size_t i = 0; i < f.length(); ++i) { - if (isalnum (f[i]) || f[i] == '_' || f[i] == '-') { - t += f[i]; - } else { - t += '_'; - } - } - - return t; + boost::replace_if (f, boost::is_any_of ("\\/:"), '_'); + return f; } dcp::Size diff --git a/test/util_test.cc b/test/util_test.cc index d1f6c77d5..6abc9b5e7 100644 --- a/test/util_test.cc +++ b/test/util_test.cc @@ -86,3 +86,11 @@ BOOST_AUTO_TEST_CASE (seconds_to_approximate_hms_test) BOOST_CHECK_EQUAL (seconds_to_approximate_hms (3600 + 40 * 60), "1h 40m"); BOOST_CHECK_EQUAL (seconds_to_approximate_hms (13 * 3600 + 40 * 60), "14h"); } + +BOOST_AUTO_TEST_CASE (tidy_for_filename_test) +{ + BOOST_CHECK_EQUAL (tidy_for_filename ("fish\\chips"), "fish_chips"); + BOOST_CHECK_EQUAL (tidy_for_filename ("fish:chips\\"), "fish_chips_"); + BOOST_CHECK_EQUAL (tidy_for_filename ("fish/chips\\"), "fish_chips_"); + BOOST_CHECK_EQUAL (tidy_for_filename ("abcdefghï"), "abcdefghï"); +} |
