summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2020-09-23 13:20:15 +0100
committerSadie Powell <sadie@witchery.services>2020-09-23 13:20:15 +0100
commita56ff68b0a8b93979b6baf917ff15e03a71ac364 (patch)
tree0b4ad0acf9eb87667b6b530bbb6362238133bc6f
parent5cb71b751a62c4aeadced5311c84d92dfe62db20 (diff)
Refactor GenRandomStr for performance and readability.
-rw-r--r--src/helperfuncs.cpp14
1 files changed, 6 insertions, 8 deletions
diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp
index 64fadc015..ed1e304b5 100644
--- a/src/helperfuncs.cpp
+++ b/src/helperfuncs.cpp
@@ -517,14 +517,12 @@ std::string InspIRCd::TimeString(time_t curtime, const char* format, bool utc)
std::string InspIRCd::GenRandomStr(unsigned int length, bool printable)
{
- char* buf = new char[length];
- GenRandom(buf, length);
- std::string rv;
- rv.resize(length);
- for(size_t i = 0; i < length; i++)
- rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
- delete[] buf;
- return rv;
+ std::vector<char> str(length);
+ GenRandom(&str[0], length);
+ if (printable)
+ for (size_t i = 0; i < length; i++)
+ str[i] = 0x3F + (str[i] & 0x3F);
+ return std::string(&str[0], str.size());
}
// NOTE: this has a slight bias for lower values if max is not a power of 2.