mirror of
https://gitlab.com/niansa/asbots.git
synced 2025-03-06 20:48:25 +01:00
66 lines
1.8 KiB
C++
66 lines
1.8 KiB
C++
#include "utility.hpp"
|
|
#include "uid.hpp"
|
|
#include "instance.hpp"
|
|
#include "exceptions.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> splitOnce(std::string_view s, std::string_view at) {
|
|
// Find the colon
|
|
auto colonPos = s.find(at);
|
|
if (colonPos == s.npos) {
|
|
return {s, ""};
|
|
}
|
|
// Split there
|
|
return {s.substr(0, colonPos), s.substr(colonPos+2, s.size()-1)};
|
|
}
|
|
|
|
void argsSizeCheck(std::string_view where, std::vector<std::string_view> args, size_t expected) {
|
|
if (args.size() < expected) {
|
|
throw InsufficientArgsError(where, expected, args.size());
|
|
}
|
|
}
|
|
|
|
std::string lowers(std::string_view str) {
|
|
std::string fres(str);
|
|
for (auto& character : fres) {
|
|
if (isalpha(character)) {
|
|
character = std::tolower(character);
|
|
}
|
|
}
|
|
return fres;
|
|
}
|
|
}
|
|
|
|
|
|
static const char UUIDChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
UUID Instance::UUIDGen() {
|
|
size_t numUUID = ++lastUUID;
|
|
UUID fres = config.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;
|
|
}
|