utility::string_t uri_components::join()

in Include/cpprestinclude/cpprest/details/uri.hpp [39:158]


utility::string_t uri_components::join()
{
    // canonicalize components first

    // convert scheme to lowercase
    std::transform(m_scheme.begin(), m_scheme.end(), m_scheme.begin(), [](utility::char_t c) {
        return (utility::char_t)tolower(c);
    });

    // convert host to lowercase
    std::transform(m_host.begin(), m_host.end(), m_host.begin(), [](utility::char_t c) {
        return (utility::char_t)tolower(c);
    });

    // canonicalize the path to have a leading slash if it's a full uri
    if (!m_host.empty() && m_path.empty())
    {
        m_path = _XPLATSTR("/");
    }
    else if (!m_host.empty() && m_path[0] != _XPLATSTR('/'))
    {
        m_path.insert(m_path.begin(), 1, _XPLATSTR('/'));
    }

    utility::string_t ret;

#if (defined(_MSC_VER) && (_MSC_VER >= 1800))
    if (!m_scheme.empty())
    {
        ret.append(m_scheme).append({ _XPLATSTR(':') });
    }

    if (!m_host.empty())
    {
        ret.append(_XPLATSTR("//"));

        if (!m_user_info.empty())
        {
            ret.append(m_user_info).append({ _XPLATSTR('@') });
        }

        ret.append(m_host);

        if (m_port > 0)
        {
            ret.append({ _XPLATSTR(':') }).append(utility::conversions::print_string(m_port, std::locale::classic()));
        }
    }

    if (!m_path.empty())
    {
        // only add the leading slash when the host is present
        if (!m_host.empty() && m_path.front() != _XPLATSTR('/'))
        {
            ret.append({ _XPLATSTR('/') });
        }

        ret.append(m_path);
    }

    if (!m_query.empty())
    {
        ret.append({ _XPLATSTR('?') }).append(m_query);
    }

    if (!m_fragment.empty())
    {
        ret.append({ _XPLATSTR('#') }).append(m_fragment);
    }

    return ret;
#else
    utility::ostringstream_t os;
    os.imbue(std::locale::classic());

    if (!m_scheme.empty())
    {
        os << m_scheme << _XPLATSTR(':');
    }

    if (!m_host.empty())
    {
        os << _XPLATSTR("//");

        if (!m_user_info.empty())
        {
            os << m_user_info << _XPLATSTR('@');
        }

        os << m_host;

        if (m_port > 0)
        {
            os << _XPLATSTR(':') << m_port;
        }
    }

    if (!m_path.empty())
    {
        // only add the leading slash when the host is present
        if (!m_host.empty() && m_path.front() != _XPLATSTR('/'))
        {
            os << _XPLATSTR('/');
        }
        os << m_path;
    }

    if (!m_query.empty())
    {
        os << _XPLATSTR('?') << m_query;
    }

    if (!m_fragment.empty())
    {
        os << _XPLATSTR('#') << m_fragment;
    }

    return os.str();
#endif
}