2016-07-11 11:14:58 -04:00
|
|
|
#include "STDInclude.hpp"
|
2016-08-29 04:18:58 -04:00
|
|
|
#ifndef DISABLE_BASE128
|
2016-08-28 16:46:23 -04:00
|
|
|
#include "base128.h"
|
2016-08-29 04:18:58 -04:00
|
|
|
#endif
|
2016-07-11 11:14:58 -04:00
|
|
|
|
|
|
|
namespace Utils
|
|
|
|
{
|
|
|
|
namespace String
|
|
|
|
{
|
2016-07-22 06:12:11 -04:00
|
|
|
std::string ToLower(std::string input)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
|
|
|
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2016-07-22 06:12:11 -04:00
|
|
|
std::string ToUpper(std::string input)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
|
|
|
std::transform(input.begin(), input.end(), input.begin(), ::toupper);
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EndsWith(std::string haystack, std::string needle)
|
|
|
|
{
|
|
|
|
return (strstr(haystack.data(), needle.data()) == (haystack.data() + haystack.size() - needle.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DumpHex(std::string data, std::string separator)
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < data.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
result.append(separator);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.append(fmt::sprintf("%02X", data[i] & 0xFF));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-07-22 06:12:11 -04:00
|
|
|
std::string XOR(std::string str, char value)
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < str.size(); ++i)
|
|
|
|
{
|
|
|
|
str[i] ^= value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> Explode(const std::string& str, char delim)
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
std::istringstream iss(str);
|
|
|
|
|
|
|
|
for (std::string token; std::getline(iss, token, delim);)
|
|
|
|
{
|
|
|
|
std::string _entry = std::move(token);
|
|
|
|
|
|
|
|
// Remove trailing 0x0 bytes
|
2016-08-14 10:05:57 -04:00
|
|
|
while (_entry.size() && !_entry.back())
|
2016-07-11 11:14:58 -04:00
|
|
|
{
|
|
|
|
_entry = _entry.substr(0, _entry.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.push_back(_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Replace(std::string &string, std::string find, std::string replace)
|
|
|
|
{
|
|
|
|
size_t nPos = 0;
|
|
|
|
|
|
|
|
while ((nPos = string.find(find, nPos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
string = string.replace(nPos, find.length(), replace);
|
|
|
|
nPos += replace.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StartsWith(std::string haystack, std::string needle)
|
|
|
|
{
|
|
|
|
return (haystack.size() >= needle.size() && !strncmp(needle.data(), haystack.data(), needle.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim from start
|
|
|
|
std::string <rim(std::string &s)
|
|
|
|
{
|
|
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim from end
|
|
|
|
std::string &RTrim(std::string &s)
|
|
|
|
{
|
|
|
|
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim from both ends
|
|
|
|
std::string &Trim(std::string &s)
|
|
|
|
{
|
|
|
|
return LTrim(RTrim(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FormatTimeSpan(int milliseconds)
|
|
|
|
{
|
|
|
|
int secondsTotal = milliseconds / 1000;
|
|
|
|
int seconds = secondsTotal % 60;
|
|
|
|
int minutesTotal = secondsTotal / 60;
|
|
|
|
int minutes = minutesTotal % 60;
|
|
|
|
int hoursTotal = minutesTotal / 60;
|
|
|
|
|
|
|
|
return fmt::sprintf("%02d:%02d:%02d", hoursTotal, minutes, seconds);
|
|
|
|
}
|
2016-08-27 12:18:03 -04:00
|
|
|
|
2016-09-08 15:41:01 -04:00
|
|
|
std::string FormatBandwidth(size_t bytes, int milliseconds)
|
|
|
|
{
|
|
|
|
static char* sizes[] =
|
|
|
|
{
|
|
|
|
"B",
|
|
|
|
"KB",
|
|
|
|
"MB",
|
|
|
|
"GB",
|
|
|
|
"TB"
|
|
|
|
};
|
|
|
|
|
|
|
|
double bytesPerSecond = (1000.0 / milliseconds) * bytes;
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; bytesPerSecond > 1000 && i < ARRAY_SIZE(sizes); ++i) // 1024 or 1000?
|
|
|
|
{
|
|
|
|
bytesPerSecond /= 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt::sprintf("%.2f %s/s", static_cast<float>(bytesPerSecond), sizes[i]);
|
|
|
|
}
|
|
|
|
|
2016-08-27 12:18:03 -04:00
|
|
|
// Encodes a given string in Base64
|
2016-08-30 06:23:53 -04:00
|
|
|
std::string EncodeBase64(const char* input, const unsigned long inputSize)
|
|
|
|
{
|
2016-08-27 12:18:03 -04:00
|
|
|
unsigned long outlen = long(inputSize + (inputSize / 3.0) + 16);
|
|
|
|
unsigned char* outbuf = new unsigned char[outlen]; //Reserve output memory
|
2016-08-30 06:23:53 -04:00
|
|
|
base64_encode(reinterpret_cast<unsigned char*>(const_cast<char*>(input)), inputSize, outbuf, &outlen);
|
2016-08-27 12:18:03 -04:00
|
|
|
std::string ret((char*)outbuf, outlen);
|
|
|
|
delete[] outbuf;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encodes a given string in Base64
|
2016-08-30 06:23:53 -04:00
|
|
|
std::string EncodeBase64(const std::string& input)
|
|
|
|
{
|
|
|
|
return EncodeBase64(input.data(), input.size());
|
2016-08-27 17:46:24 -04:00
|
|
|
}
|
|
|
|
|
2016-08-29 04:18:58 -04:00
|
|
|
#ifndef DISABLE_BASE128
|
2016-08-28 16:46:23 -04:00
|
|
|
// Encodes a given string in Base128
|
2016-08-30 06:23:53 -04:00
|
|
|
std::string EncodeBase128(const std::string& input)
|
|
|
|
{
|
|
|
|
base128 encoder;
|
|
|
|
|
2016-08-29 01:47:51 -04:00
|
|
|
void* inbuffer = const_cast<char*>(input.data());
|
2016-08-30 06:23:53 -04:00
|
|
|
char* buffer = encoder.encode(inbuffer, input.size());
|
2016-08-29 04:11:03 -04:00
|
|
|
/*
|
|
|
|
Interesting to see that the buffer returned by the encoder is not a standalone string copy
|
|
|
|
but instead is a pointer to the internal "encoded" field of the encoder. So if you deinitialize
|
|
|
|
the encoder that string will probably become garbage.
|
|
|
|
*/
|
|
|
|
std::string retval(buffer);
|
|
|
|
return retval;
|
2016-08-28 16:46:23 -04:00
|
|
|
}
|
2016-08-29 04:18:58 -04:00
|
|
|
#endif
|
2016-08-28 16:46:23 -04:00
|
|
|
|
2016-08-28 13:38:16 -04:00
|
|
|
// Generates a UUID and returns the string representation of it
|
2016-08-30 06:23:53 -04:00
|
|
|
std::string GenerateUUIDString()
|
|
|
|
{
|
2016-08-28 13:38:16 -04:00
|
|
|
// Generate UUID data
|
|
|
|
UUID uuid;
|
2016-08-30 20:00:01 -04:00
|
|
|
if (!UuidCreate(&uuid))
|
|
|
|
{
|
|
|
|
// Convert to string representation
|
|
|
|
char* strdata = nullptr;
|
|
|
|
if (!UuidToStringA(&uuid, reinterpret_cast<RPC_CSTR*>(&strdata)))
|
|
|
|
{
|
|
|
|
return std::string(strdata);
|
|
|
|
}
|
|
|
|
}
|
2016-08-28 13:38:16 -04:00
|
|
|
|
2016-08-30 20:00:01 -04:00
|
|
|
return "";
|
2016-08-27 12:18:03 -04:00
|
|
|
}
|
2016-07-11 11:14:58 -04:00
|
|
|
}
|
|
|
|
}
|