epubgrep/src/helpers.cpp

39 lines
924 B
C++

#include "helpers.hpp"
#include <boost/regex.hpp>
#include <codecvt>
#include <locale>
#include <map>
#include <string_view>
namespace epubgrep::helpers
{
bool is_whitespace(const char check)
{
const std::array<char, 4> whitespace{' ', '\n', '\r', '\t'};
return std::any_of(whitespace.begin(), whitespace.end(),
[&check](const char ws) { return check == ws; });
}
std::string urldecode(const std::string_view url)
{ // RFC 3986, section 2.1.
size_t pos{0};
size_t lastpos{0};
std::string decoded;
while ((pos = url.find('%', pos)) != std::string_view::npos)
{
decoded += url.substr(lastpos, pos - lastpos);
decoded += static_cast<char>(
std::stoul(std::string(url.substr(pos + 1, 2)), nullptr, 16));
pos += 3;
lastpos = pos;
}
decoded += url.substr(lastpos);
return decoded;
}
} // namespace epubgrep::helpers