Boost logo

Boost Users :

Subject: Re: [Boost-users] Plans to add parameter parsing in Beast?
From: David Demelier (markand_at_[hidden])
Date: 2018-05-30 07:55:01


On Tue, 2018-05-29 at 13:06 -0700, Vinnie Falco via Boost-users wrote:
> Yes, a uri parser/serializer is a planned feature. No timeline on
> that yet.

FWIW, I have tried some encode/decode at the moment:

#include <cctype>
#include <ios>
#include <iomanip>
#include <sstream>
#include <stdexcept>

#include "uri.hpp"

namespace uri {

std::string encode(const std::string& input)
{
    std::ostringstream oss;

    for (auto c : input) {
        if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c ==
'~')
            oss << c;
        else {
            oss << "%";
            oss << std::hex << std::uppercase << std::setw(2);
            oss << static_cast<int>(static_cast<unsigned char>(c));
            oss << std::nouppercase;
        }
    }

    return oss.str();
}

std::string decode(const std::string& input)
{
    std::ostringstream oss;

    for (auto it = input.cbegin(); it != input.cend(); ) {
        if (*it != '%')
            oss << *it++;
        else {
            if (input.end() - it < 3)
                throw std::invalid_argument("truncated string");

            const auto substr = input.substr(it - input.begin() + 1,
2);

            oss << static_cast<char>(std::stoull(substr, nullptr, 16));
            it += 3;
        }
    }

    return oss.str();
}

} // !uri

Regards,

-- 
David

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net