| 1 |
// Copyright 2016-2017 Glyn Matthews. |
| 2 |
// Distributed under the Boost Software License, Version 1.0. |
| 3 |
// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
#include "uri_advance_parts.hpp" |
| 7 |
#include <iterator> |
| 8 |
#include <utility> |
| 9 |
#include <limits> |
| 10 |
|
| 11 |
namespace network_detail = network::detail; |
| 12 |
using network::string_view; |
| 13 |
using network::detail::uri_part; |
| 14 |
|
| 15 |
namespace { |
| 16 |
template <class Iterator> |
| 17 |
uri_part copy_part(Iterator first, Iterator last, |
| 18 |
string_view::const_iterator &it) { |
| 19 |
auto part_first = it; |
| 20 |
std::advance(it, std::distance(first, last)); |
| 21 |
return network_detail::uri_part(part_first, it); |
| 22 |
} |
| 23 |
} // namespace |
| 24 |
|
| 25 |
uri_part network_detail::copy_part(const std::string &uri, |
| 26 |
string_view::const_iterator &it) { |
| 27 |
return ::copy_part(std::begin(uri), std::end(uri), it); |
| 28 |
} |
| 29 |
|
| 30 |
void network_detail::advance_parts(string_view uri_view, uri_parts &parts, |
| 31 |
const uri_parts &existing_parts) { |
| 32 |
auto first = std::begin(uri_view); |
| 33 |
|
| 34 |
auto it = first; |
| 35 |
if (auto scheme = existing_parts.scheme) { |
| 36 |
parts.scheme = ::copy_part(std::begin(*scheme), std::end(*scheme), it); |
| 37 |
|
| 38 |
// ignore : for all URIs |
| 39 |
if (*it == ':') { |
| 40 |
++it; |
| 41 |
} |
| 42 |
|
| 43 |
// ignore // for hierarchical URIs |
| 44 |
if (existing_parts.hier_part.host) { |
| 45 |
std::advance(it, 2); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if (auto user_info = existing_parts.hier_part.user_info) { |
| 50 |
parts.hier_part.user_info = |
| 51 |
::copy_part(std::begin(*user_info), std::end(*user_info), it); |
| 52 |
++it; // ignore @ |
| 53 |
} |
| 54 |
|
| 55 |
if (auto host = existing_parts.hier_part.host) { |
| 56 |
parts.hier_part.host = ::copy_part(std::begin(*host), std::end(*host), it); |
| 57 |
} |
| 58 |
|
| 59 |
if (auto port = existing_parts.hier_part.port) { |
| 60 |
++it; // ignore : |
| 61 |
parts.hier_part.port = ::copy_part(std::begin(*port), std::end(*port), it); |
| 62 |
} |
| 63 |
|
| 64 |
if (auto path = existing_parts.hier_part.path) { |
| 65 |
parts.hier_part.path = ::copy_part(std::begin(*path), std::end(*path), it); |
| 66 |
} |
| 67 |
|
| 68 |
if (auto query = existing_parts.query) { |
| 69 |
++it; // ignore ? |
| 70 |
parts.query = ::copy_part(std::begin(*query), std::end(*query), it); |
| 71 |
} |
| 72 |
|
| 73 |
if (auto fragment = existing_parts.fragment) { |
| 74 |
++it; // ignore # |
| 75 |
parts.fragment = |
| 76 |
::copy_part(std::begin(*fragment), std::end(*fragment), it); |
| 77 |
} |
| 78 |
} |