Make remote::URL use stack memory.

This commit is contained in:
J-D-K 2025-09-08 21:53:04 -04:00
parent 42f42dbf79
commit 505e640f46
3 changed files with 36 additions and 64 deletions

View File

@ -15,26 +15,6 @@ namespace remote
/// @param base String_view containing the base URL.
URL(std::string_view base);
/// @brief Copy constructor.
/// @param url URL to copy.
URL(const URL &url);
/// @brief Move constructor.
/// @param url URL to move.
URL(URL &&url) noexcept;
/// @brief Makes a copy of the URL passed.
/// @param url remote::URL instance to make a copy of.
URL &operator=(const URL &url);
/// @brief Move operator.
/// @param url URL to move.
URL &operator=(URL &&url) noexcept;
/// @brief Sets the base URL. Basically resets the string back to square 0.
/// @param base Base URL to start with.
URL &set_base(std::string_view base);
/// @brief Appends the string passed as a path to the URL
/// @param path Path to append;
URL &append_path(std::string_view path);
@ -51,10 +31,12 @@ namespace remote
const char *get() const noexcept;
private:
/// @brief This is where the actual URL is held.
std::string m_url{};
static inline constexpr int SIZE_URL_BUFFER = 0x800;
/// @brief This checks and appends the necessary separator to the URL string.
void append_separator();
/// @brief This is where the actual URL is held.
char m_urlBuffer[SIZE_URL_BUFFER] = {0};
// Current offset in the buffer.
int m_offset{};
};
} // namespace remote

View File

@ -1,20 +1,19 @@
#include "remote/Form.hpp"
#include <algorithm>
#include <cstring>
remote::Form &remote::Form::append_parameter(std::string_view param, std::string_view value)
{
if (m_offset > 0 && m_formBuffer[m_offset] != '&') { m_formBuffer[m_offset++] = '&'; }
const size_t paramLength = param.length();
std::memcpy(&m_formBuffer[m_offset], param.data(), paramLength);
m_offset += paramLength;
std::copy(param.begin(), param.end(), &m_formBuffer[m_offset]);
m_offset += param.length();
m_formBuffer[m_offset++] = '=';
const size_t valueLength = value.length();
std::memcpy(&m_formBuffer[m_offset], value.data(), valueLength);
m_offset += valueLength;
std::copy(value.begin(), value.end(), &m_formBuffer[m_offset]);
m_offset += value.length();
return *this;
}

View File

@ -1,56 +1,47 @@
#include "remote/URL.hpp"
#include <algorithm>
remote::URL::URL(std::string_view base)
: m_url(base) {};
remote::URL::URL(const URL &url) { m_url = url.m_url; }
remote::URL::URL(URL &&url) { m_url = std::move(url.m_url); }
remote::URL &remote::URL::operator=(const remote::URL &url)
{
m_url = url.m_url;
return *this;
}
remote::URL &remote::URL::operator=(remote::URL &&url) noexcept
{
m_url = std::move(url.m_url);
return *this;
}
remote::URL &remote::URL::set_base(std::string_view base)
{
m_url = base;
return *this;
std::copy(base.begin(), base.end(), &m_urlBuffer[m_offset]);
m_offset += base.length();
}
remote::URL &remote::URL::append_path(std::string_view path)
{
// Check both just to be sure because this makes WebDav easier to tackle.
if (m_url.back() != '/' && path.front() != '/') { m_url.append("/"); }
if (path.empty()) { return *this; }
m_url.append(path);
if (m_urlBuffer[m_offset] != '/' && path.front() != '/') { m_urlBuffer[m_offset++] = '/'; }
std::copy(path.begin(), path.end(), &m_urlBuffer[m_offset]);
m_offset += path.length();
return *this;
}
remote::URL &remote::URL::append_parameter(std::string_view param, std::string_view value)
{
URL::append_separator();
m_url.append(param).append("=").append(value);
const char *find = std::char_traits<char>::find(m_urlBuffer, m_offset, '?');
if (!find) { m_urlBuffer[m_offset++] = '?'; }
else { m_urlBuffer[m_offset++] = '&'; }
std::copy(param.begin(), param.end(), &m_urlBuffer[m_offset]);
m_offset += param.length();
m_urlBuffer[m_offset++] = '=';
std::copy(value.begin(), value.end(), &m_urlBuffer[m_offset]);
m_offset += value.length();
return *this;
}
remote::URL &remote::URL::append_slash()
{
if (m_url.back() != '/') { m_url.append("/"); }
if (m_urlBuffer[m_offset] != '/') { m_urlBuffer[m_offset++] = '/'; }
return *this;
}
const char *remote::URL::get() const noexcept { return m_url.c_str(); }
void remote::URL::append_separator()
{
if (m_url.find('?') == m_url.npos) { m_url.append("?"); }
else { m_url.append("&"); }
}
const char *remote::URL::get() const noexcept { return m_urlBuffer; }