mirror of
https://gitlab.com/niansa/asbots.git
synced 2025-03-06 20:48:25 +01:00
48 lines
1.4 KiB
C++
48 lines
1.4 KiB
C++
#include "utility.hpp"
|
|
#include "uid.hpp"
|
|
#include "instance.hpp"
|
|
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
#include <tuple>
|
|
|
|
|
|
|
|
namespace Utility {
|
|
std::vector<std::string_view> strSplit(std::string_view s, char delimiter, size_t times) {
|
|
std::vector<std::string_view> to_return;
|
|
decltype(s.size()) start = 0, finish = 0;
|
|
while ((finish = s.find_first_of(delimiter, start)) != std::string_view::npos) {
|
|
to_return.emplace_back(s.substr(start, finish - start));
|
|
start = finish + 1;
|
|
if (to_return.size() == times) { break; }
|
|
}
|
|
to_return.emplace_back(s.substr(start));
|
|
return to_return;
|
|
}
|
|
|
|
std::tuple<std::string_view, std::string_view> colonSplit(std::string_view s) {
|
|
// Find the colon
|
|
auto colonPos = s.find(" :");
|
|
if (colonPos == s.npos) {
|
|
return {s, ""};
|
|
}
|
|
// Split there
|
|
return {s.substr(0, colonPos), s.substr(colonPos+2, s.size()-1)};
|
|
}
|
|
}
|
|
|
|
static const char UUIDChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
UUID Instance::UUIDGen() {
|
|
size_t numUUID = ++lastUUID;
|
|
UUID fres = config.get().server.uid.str();
|
|
for (auto it = fres.array.end() - 1; it != fres.array.begin()+SUID_len-1; it--) {
|
|
auto idx = std::min(numUUID, sizeof(UUIDChars)-1);
|
|
*it = UUIDChars[idx];
|
|
if (idx != sizeof(UUIDGen())-1) {
|
|
numUUID -= idx;
|
|
}
|
|
}
|
|
return fres;
|
|
}
|