uri: use vector rather than map for query exploding

This commit is contained in:
Danny Robson 2022-01-27 13:51:40 +10:00
parent 4e4e731cb7
commit 805ba5d1bf
2 changed files with 17 additions and 7 deletions

10
uri.cpp
View File

@ -211,14 +211,14 @@ void uri::clear_fragment ()
///////////////////////////////////////////////////////////////////////////////
std::map<std::string, std::string>
cruft::query_to_map (std::string_view val)
std::vector<std::pair<std::string, std::string>>
cruft::query_to_vector (std::string_view val)
{
std::map<std::string, std::string> res;
std::vector<std::pair<std::string, std::string>> res;
for (auto const tok: cruft::tokeniser (val, '&')) {
auto const &[k, v] = cruft::split_on (tok, '=');
res.emplace (
res.emplace_back (
std::string (k.begin (), k.size ()),
std::string (v.begin (), v.size ())
);
@ -230,7 +230,7 @@ cruft::query_to_map (std::string_view val)
//-----------------------------------------------------------------------------
std::string
cruft::map_to_query (std::map<std::string, std::string> const &val)
cruft::vector_to_query (std::vector<std::pair<std::string, std::string>> const &val)
{
// Test for empty up front so that we can simplify the string
// concatenation below.

14
uri.hpp
View File

@ -114,8 +114,18 @@ namespace cruft {
std::string m_value;
};
std::map<std::string, std::string> query_to_map (std::string_view);
std::string map_to_query (std::map<std::string, std::string> const&);
/// Break a query string into a sequence of key-value pairs.
/// Duplicates are possible.
/// vector is used to that order can be preserved
/// The is the inverse of vector_to_query
std::vector<std::pair<std::string, std::string>>
query_to_vector (std::string_view);
/// Convert a sequence of key-value pairs into a query string
/// All pairs, including duplicates, are serialised in the provided order.
/// This is the inverse of query_to_vector
std::string
vector_to_query (std::vector<std::pair<std::string, std::string>> const&);
bool operator== (uri const&, uri const&) noexcept;