55// http://www.boost.org/LICENSE_1_0.txt)
66
77#include " uri_resolve.hpp"
8- #include < algorithm >
8+ #include " algorithm_find.hpp "
99
10- #ifdef NETWORK_URI_EXTERNAL_BOOST
11- #include < boost/algorithm/string/find.hpp>
12- #include < boost/algorithm/string/erase.hpp>
13- #include < boost/algorithm/string/replace.hpp>
14- #include < boost/algorithm/string/predicate.hpp>
15- namespace network_boost = boost;
16- #else // NETWORK_URI_EXTERNAL_BOOST
17- #include " ../boost/algorithm/string/find.hpp"
18- #include " ../boost/algorithm/string/erase.hpp"
19- #include " ../boost/algorithm/string/replace.hpp"
20- #include " ../boost/algorithm/string/predicate.hpp"
21- #endif // NETWORK_URI_EXTERNAL_BOOST
10+ using namespace network ::algorithm;
11+ namespace network_detail = network::detail;
12+
13+ namespace {
2214
23- namespace network {
24- namespace detail {
2515// remove_dot_segments
2616inline void remove_last_segment (std::string &path) {
2717 while (!path.empty ()) {
@@ -33,57 +23,64 @@ inline void remove_last_segment(std::string &path) {
3323 }
3424}
3525
26+ inline bool starts_with (std::string const &str, const char *range) {
27+ return str.find (range) == 0 ;
28+ }
29+
3630// implementation of http://tools.ietf.org/html/rfc3986#section-5.2.4
3731static std::string remove_dot_segments (std::string input) {
3832 std::string result;
3933
4034 while (!input.empty ()) {
41- if (network_boost::starts_with (input, " ../" )) {
42- network_boost::erase_head (input, 3 );
43- } else if (network_boost::starts_with (input, " ./" )) {
44- network_boost::erase_head (input, 2 );
45- } else if (network_boost::starts_with (input, " /./" )) {
46- network_boost::replace_head (input, 3 , " /" );
35+ if (starts_with (input, " ../" )) {
36+ input.erase (0 , 3 );
37+ } else if (starts_with (input, " ./" )) {
38+ input.erase (0 , 2 );
39+ } else if (starts_with (input, " /./" )) {
40+ input.erase (0 , 2 );
41+ input.front () = ' /' ;
4742 } else if (input == " /." ) {
48- network_boost::replace_head (input, 2 , " /" );
49- } else if (network_boost::starts_with (input, " /../" )) {
50- network_boost::erase_head (input, 3 );
43+ input.erase (0 , 1 );
44+ input.front () = ' /' ;
45+ } else if (starts_with (input, " /../" )) {
46+ input.erase (0 , 3 );
5147 remove_last_segment (result);
52- } else if (network_boost::starts_with (input, " /.." )) {
53- network_boost::replace_head (input, 3 , " /" );
48+ } else if (starts_with (input, " /.." )) {
49+ input.erase (0 , 2 );
50+ input.front () = ' /' ;
5451 remove_last_segment (result);
55- } else if (network_boost::algorithm::all (
56- input, [](char ch) { return ch == ' .' ; })) {
52+ } else if (all (input, [](char ch) { return ch == ' .' ; })) {
5753 input.clear ();
5854 } else {
5955 int n = (input.front () == ' /' ) ? 1 : 0 ;
60- auto slash = network_boost:: find_nth (input, " / " , n);
61- result.append (std::begin ( input), std::begin ( slash) );
62- input.erase (std::begin (input), std::begin ( slash) );
56+ std::string::const_iterator slash = find_nth (input, ' / ' , n);
57+ result.append (input. cbegin ( ), slash);
58+ input.erase (std::begin (input), slash);
6359 }
6460 }
6561 return result;
6662}
6763
68- std::string remove_dot_segments (string_view path) {
69- return remove_dot_segments (path.to_string ());
64+ } // namespace
65+
66+ std::string network_detail::remove_dot_segments (string_view path) {
67+ return ::remove_dot_segments (path.to_string ());
7068}
7169
7270// implementation of http://tools.ietf.org/html/rfc3986#section-5.2.3
73- std::string merge_paths (const uri &base, const uri &reference) {
71+ std::string network_detail:: merge_paths (const uri &base, const uri &reference) {
7472 std::string result;
7573
7674 if (!base.has_path () || base.path ().empty ()) {
7775 result = " /" ;
7876 } else {
7977 const auto &base_path = base.path ();
80- auto last_slash = network_boost::find_last (base_path, " /" );
81- result.append (std::begin (base_path), std::end (last_slash));
78+ auto last_slash = algorithm::find_last (base_path, ' /' );
79+ if (last_slash != base_path.cend ()) ++last_slash;
80+ result.append (std::begin (base_path), last_slash);
8281 }
8382 if (reference.has_path ()) {
8483 result.append (reference.path ().to_string ());
8584 }
8685 return remove_dot_segments (string_view (result));
8786}
88- } // namespace detail
89- } // namespace network
0 commit comments