mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2024-12-03 14:52:18 -05:00
69 lines
1.4 KiB
C++
69 lines
1.4 KiB
C++
#include "Utils.h"
|
|
#include <cstring>
|
|
#include <algorithm>
|
|
|
|
#ifdef _MSC_VER
|
|
#define strdup _strdup
|
|
#endif
|
|
|
|
namespace Ship {
|
|
namespace Math {
|
|
float clamp(float d, float min, float max) {
|
|
const float t = d < min ? min : d;
|
|
return t > max ? max : t;
|
|
}
|
|
}
|
|
|
|
std::vector<std::string> SplitText(const std::string text, char separator = ' ', bool keep_quotes = false) {
|
|
std::vector<std::string> args;
|
|
char* input = strdup(text.c_str());
|
|
const size_t length = strlen(input);
|
|
|
|
bool inQuotes = false;
|
|
size_t count = 0, from = 0;
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
if (input[i] == '"') {
|
|
inQuotes = !inQuotes;
|
|
}
|
|
else if (input[i] == separator && !inQuotes) {
|
|
size_t strlen = i - from;
|
|
|
|
if (strlen > 0) {
|
|
if (!keep_quotes && input[from] == '"' && input[i - 1] == '"') {
|
|
from++; strlen -= 2;
|
|
}
|
|
|
|
char* tmp = new char[strlen + 1]();
|
|
strncpy(tmp, &input[from], strlen);
|
|
count++;
|
|
args.emplace_back(tmp);
|
|
}
|
|
|
|
from = i + 1;
|
|
}
|
|
}
|
|
|
|
if (from < length) {
|
|
size_t strlen = length - from;
|
|
|
|
if (!keep_quotes && input[from] == L'"' && input[length - 1] == L'"') {
|
|
from++; strlen -= 2;
|
|
}
|
|
|
|
char* tmp = new char[strlen + 1]();
|
|
strncpy(tmp, &input[from], strlen);
|
|
count++;
|
|
args.emplace_back(tmp);
|
|
}
|
|
|
|
return args;
|
|
}
|
|
|
|
std::string toLowerCase(std::string in) {
|
|
std::string cpy(in);
|
|
std::transform(cpy.begin(), cpy.end(), cpy.begin(), ::tolower);
|
|
return cpy;
|
|
}
|
|
}
|