diff --git a/gtk2_ardour/editor.cc b/gtk2_ardour/editor.cc index ca19c0a4ef..d2f5fbe5de 100644 --- a/gtk2_ardour/editor.cc +++ b/gtk2_ardour/editor.cc @@ -3132,14 +3132,13 @@ Editor::convert_drop_to_paths ( if ((*i).substr (0,7) == "file://") { - string p = *i; - PBD::url_decode (p); + string const p = PBD::url_decode (*i); // scan forward past three slashes string::size_type slashcnt = 0; string::size_type n = 0; - string::iterator x = p.begin(); + string::const_iterator x = p.begin(); while (slashcnt < 3 && x != p.end()) { if ((*x) == '/') { diff --git a/libs/pbd/convert.cc b/libs/pbd/convert.cc index 49650ab6e9..3d968d0627 100644 --- a/libs/pbd/convert.cc +++ b/libs/pbd/convert.cc @@ -178,94 +178,23 @@ int_from_hex (char hic, char loc) return lo + (16 * hi); } -void -url_decode (string& url) +string +url_decode (string const & url) { - string::iterator last; - string::iterator next; + string decoded; - for (string::iterator i = url.begin(); i != url.end(); ++i) { - if ((*i) == '+') { - *i = ' '; - } - } - - if (url.length() <= 3) { - return; - } - - last = url.end(); - - --last; /* points at last char */ - --last; /* points at last char - 1 */ - - for (string::iterator i = url.begin(); i != last; ) { - - if (*i == '%') { - - next = i; - - url.erase (i); - - i = next; - ++next; - - if (isxdigit (*i) && isxdigit (*next)) { - /* replace first digit with char */ - *i = int_from_hex (*i,*next); - ++i; /* points at 2nd of 2 digits */ - url.erase (i); - } + for (string::size_type i = 0; i < url.length(); ++i) { + if (url[i] == '+') { + decoded += ' '; + } else if (url[i] == '%' && i <= url.length() - 3) { + decoded += char (int_from_hex (url[i + 1], url[i + 2])); + i += 2; } else { - ++i; - } - } -} - -void -url_decode (ustring& url) -{ - ustring::iterator last; - ustring::iterator next; - - for (ustring::iterator i = url.begin(); i != url.end(); ++i) { - if ((*i) == '+') { - next = i; - ++next; - url.replace (i, next, 1, ' '); + decoded += url[i]; } } - if (url.length() <= 3) { - return; - } - - last = url.end(); - - --last; /* points at last char */ - --last; /* points at last char - 1 */ - - for (ustring::iterator i = url.begin(); i != last; ) { - - if (*i == '%') { - - next = i; - - url.erase (i); - - i = next; - ++next; - - if (isxdigit (*i) && isxdigit (*next)) { - /* replace first digit with char */ - url.replace (i, next, 1, (gunichar) int_from_hex (*i,*next)); - ++i; /* points at 2nd of 2 digits */ - url.erase (i); - } - } else { - ++i; - } - } + return decoded; } #if 0 diff --git a/libs/pbd/pbd/convert.h b/libs/pbd/pbd/convert.h index 7bf922e94f..937b5a7a9d 100644 --- a/libs/pbd/pbd/convert.h +++ b/libs/pbd/pbd/convert.h @@ -35,8 +35,7 @@ int atoi (const std::string&); int32_t atol (const std::string&); int64_t atoll (const std::string&); double atof (const std::string&); -void url_decode (std::string&); -void url_decode (Glib::ustring&); +std::string url_decode (std::string const &); std::string capitalize (const std::string&); diff --git a/libs/pbd/test/convert_test.cc b/libs/pbd/test/convert_test.cc index 43e6546716..8b818deea1 100644 --- a/libs/pbd/test/convert_test.cc +++ b/libs/pbd/test/convert_test.cc @@ -8,7 +8,6 @@ using namespace std; void ConvertTest::testUrlDecode () { - string url = "http://foo.bar.baz/A%20B%20C%20.html"; - PBD::url_decode (url); - CPPUNIT_ASSERT_EQUAL (url, string ("http://foo.bar.baz/A B C .html")); + string const url = "http://foo.bar.baz/A%20B%20C%20+42.html"; + CPPUNIT_ASSERT_EQUAL (PBD::url_decode (url), string ("http://foo.bar.baz/A B C 42.html")); }