From 7550deaa481d962244b6e509152ac90ab022434e Mon Sep 17 00:00:00 2001
From: Sadie Powell
Date: Fri, 3 Apr 2026 19:56:53 +0100
Subject: Move query string code from spanningtree to stringutils.
---
src/stringutils.cpp | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
(limited to 'src/stringutils.cpp')
diff --git a/src/stringutils.cpp b/src/stringutils.cpp
index 71ad8f396..120f8b465 100644
--- a/src/stringutils.cpp
+++ b/src/stringutils.cpp
@@ -24,6 +24,9 @@
* along with this program. If not, see .
*/
+
+#include
+
#include
#include "inspircd.h"
@@ -61,6 +64,21 @@ std::string Percent::Encode(const void* data, size_t length, const char* table,
return buffer;
}
+std::string Percent::EncodeQuery(const Percent::QueryData& data)
+{
+ std::stringstream buffer;
+ for (auto iter = data.begin(); iter != data.end(); ++iter)
+ {
+ if (iter != data.begin())
+ buffer << '&';
+
+ buffer << iter->first;
+ if (!iter->second.empty())
+ buffer << '=' << Percent::Encode(iter->second);
+ }
+ return buffer.str();
+}
+
std::string Percent::Decode(const void* data, size_t length)
{
// Preallocate the output buffer to avoid constant reallocations.
@@ -91,6 +109,23 @@ std::string Percent::Decode(const void* data, size_t length)
return buffer;
}
+Percent::QueryData Percent::DecodeQuery(const std::string_view& str)
+{
+ Percent::QueryData data;
+
+ StringSplitter datastream(str, '&');
+ for (std::string_view datapair; datastream.GetToken(datapair); )
+ {
+ size_t split = datapair.find('=');
+ if (split == std::string::npos)
+ data.emplace(datapair, "");
+ else
+ data.emplace(datapair.substr(0, split), Percent::Decode(datapair.substr(split + 1)));
+ }
+
+ return data;
+}
+
std::string Hex::Encode(const void* data, size_t length, const char* table, char separator)
{
if (!table)
--
cgit v1.3.1-10-gc9f91