Make remote::Form use stack memory instead of string allocations.

This commit is contained in:
J-D-K
2025-09-08 21:30:34 -04:00
parent fa5a5e3c19
commit 42f42dbf79
4 changed files with 30 additions and 41 deletions

View File

@@ -9,35 +9,26 @@ namespace remote
public:
Form() = default;
/// @brief Copy constructor
/// @param form Form to copy from.
Form(const Form &form);
/// @brief Move constructor.
/// @param form Form to copy from.
Form(Form &&form) noexcept;
/// @brief = Operator.
/// @param form Form to copy.
Form &operator=(const Form &form) noexcept;
/// @brief = Move operator.
/// @param form Form to rob of its life.
Form &operator=(Form &&form);
/// @brief Appends a parameter to the form/URL encoded text.
/// @param param Parameter to append.
/// @param value Value to append.
Form &append_parameter(std::string_view param, std::string_view value);
/// @brief Returns the C string of the form string.
const char *get() const noexcept;
/// @brief Returns m_form.length()
size_t length() const noexcept;
/// @brief Returns m_formBuffer;
/// @return
const char *get() const noexcept;
private:
/// @brief String containing the actual data posted.
std::string m_form{};
/// @brief Size used for the buffer.
static inline constexpr int SIZE_FORM_BUFFER = 0x800;
/// @brief Current offset if the form.
int m_offset{};
/// @brief Buffer for the form.
char m_formBuffer[SIZE_FORM_BUFFER] = {0};
};
} // namespace remote

View File

@@ -1,28 +1,24 @@
#include "remote/Form.hpp"
remote::Form::Form(const remote::Form &form) { m_form = form.m_form; }
remote::Form::Form(remote::Form &&form) noexcept { m_form = std::move(form.m_form); }
remote::Form &remote::Form::operator=(const remote::Form &form)
{
m_form = form.m_form;
return *this;
}
remote::Form &remote::Form::operator=(remote::Form &&form) noexcept
{
m_form = std::move(form.m_form);
return *this;
}
#include <cstring>
remote::Form &remote::Form::append_parameter(std::string_view param, std::string_view value)
{
if (!m_form.empty() && m_form.back() != '&') { m_form.append("&"); }
m_form.append(param).append("=").append(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;
m_formBuffer[m_offset++] = '=';
const size_t valueLength = value.length();
std::memcpy(&m_formBuffer[m_offset], value.data(), valueLength);
m_offset += valueLength;
return *this;
}
const char *remote::Form::get() const noexcept { return m_form.c_str(); }
const char *remote::Form::get() const noexcept { return m_formBuffer; }
size_t remote::Form::length() const noexcept { return m_form.length(); }
size_t remote::Form::length() const noexcept { return m_offset; }

View File

@@ -422,6 +422,7 @@ bool remote::GoogleDrive::get_sign_in_data(std::string &message, std::string &co
curl::set_option(m_curl, CURLOPT_WRITEFUNCTION, curl::write_response_string);
curl::set_option(m_curl, CURLOPT_WRITEDATA, &response);
curl::set_option(m_curl, CURLOPT_POSTFIELDS, post.get());
curl::set_option(m_curl, CURLOPT_POSTFIELDSIZE, post.length());
if (!curl::perform(m_curl)) { return false; }
@@ -473,6 +474,7 @@ bool remote::GoogleDrive::poll_sign_in(std::string_view code)
curl::set_option(m_curl, CURLOPT_WRITEFUNCTION, curl::write_response_string);
curl::set_option(m_curl, CURLOPT_WRITEDATA, &response);
curl::set_option(m_curl, CURLOPT_POSTFIELDS, post.get());
curl::set_option(m_curl, CURLOPT_POSTFIELDSIZE, post.length());
if (!curl::perform(m_curl) || response.empty()) { return false; }

View File

@@ -22,7 +22,7 @@ ui::TitleView::TitleView(data::User *user)
void ui::TitleView::update(bool hasFocus)
{
if (m_titleTiles.empty() || !hasFocus) { return; }
if (m_titleTiles.empty()) { return; }
m_bounding->update(hasFocus);
TitleView::handle_input();