aboutsummaryrefslogtreecommitdiff
path: root/src/inspstring.cpp
diff options
context:
space:
mode:
authorGravatar Sadie Powell2021-05-10 16:47:35 +0100
committerGravatar Sadie Powell2021-05-10 20:40:21 +0100
commit23493b7d3d11e2e5def74694b9ed7454a623b302 (patch)
tree990f21095e5d2ec739fa58e5bffadbbe9cf24054 /src/inspstring.cpp
parentRefactor the Base64 encoding and decoding functions. (diff)
Refactor the hex encoding function.
Diffstat (limited to 'src/inspstring.cpp')
-rw-r--r--src/inspstring.cpp29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/inspstring.cpp b/src/inspstring.cpp
index df93ddd71..bb3eab2bb 100644
--- a/src/inspstring.cpp
+++ b/src/inspstring.cpp
@@ -26,20 +26,27 @@
#include "inspircd.h"
-static const char hextable[] = "0123456789abcdef";
-
-std::string BinToHex(const void* raw, size_t l)
+std::string Hex::Encode(const void* data, size_t length, const char* table, char separator)
{
- const char* data = static_cast<const char*>(raw);
- std::string rv;
- rv.reserve(l * 2);
- for (size_t i = 0; i < l; i++)
+ if (!table)
+ table = Hex::TABLE_LOWER;
+
+ // Preallocate the output buffer to avoid constant reallocations.
+ std::string buffer;
+ buffer.reserve((length * 2) + (!!separator * length));
+
+ const unsigned char* udata = reinterpret_cast<const unsigned char*>(data);
+ for (size_t idx = 0; idx < length; ++idx)
{
- unsigned char c = data[i];
- rv.push_back(hextable[c >> 4]);
- rv.push_back(hextable[c & 0xF]);
+ if (idx && separator)
+ buffer.push_back(separator);
+
+ const unsigned char chr = udata[idx];
+ buffer.push_back(table[chr >> 4]);
+ buffer.push_back(table[chr & 15]);
}
- return rv;
+
+ return buffer;
}
std::string Base64::Encode(const void* data, size_t length, const char* table, char padding)