-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
171 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "querybuilder.hpp" | ||
|
||
using namespace network; | ||
|
||
static std::string encode(std::string_view value) { | ||
std::string encoded; | ||
encoded.reserve(value.size()); | ||
for (char c : value) { | ||
if (std::isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '_' || c == '.' || c == '~') { | ||
encoded += c; | ||
} else { | ||
encoded += '%'; | ||
encoded += static_cast<char>((c >> 4) < 10 ? '0' + ((c >> 4) & 0xF) : 'A' + (((c >> 4) & 0xF) - 10)); | ||
encoded += static_cast<char>((c & 0xF) < 10 ? '0' + (c & 0xF) : 'A' + ((c & 0xF) - 10)); | ||
} | ||
} | ||
return encoded; | ||
} | ||
|
||
querybuilder &querybuilder::add(const std::string &key, const std::string &value) noexcept { | ||
_parameters.emplace(std::string(key), std::string(value)); | ||
return *this; | ||
} | ||
|
||
std::string querybuilder::build() const noexcept { | ||
if (_parameters.empty()) { | ||
return {}; | ||
} | ||
|
||
std::ostringstream query; | ||
for (auto it = _parameters.begin(); it != _parameters.end(); ++it) { | ||
query << it->first << '=' << encode(it->second); | ||
if (std::next(it) != _parameters.end()) query << '&'; | ||
} | ||
return query.str(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include "common.hpp" | ||
|
||
namespace network { | ||
class querybuilder { | ||
public: | ||
querybuilder &add(const std::string &key, const std::string &value) noexcept; | ||
|
||
[[nodiscard]] std::string build() const noexcept; | ||
|
||
private: | ||
std::unordered_map<std::string, std::string> _parameters; | ||
}; | ||
} |