2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-23 18:07:00 +00:00
|
|
|
//
|
2024-07-10 16:04:40 +00:00
|
|
|
// ES-DE Frontend
|
2020-06-23 18:07:00 +00:00
|
|
|
// StringUtil.cpp
|
|
|
|
//
|
|
|
|
// Low-level string functions.
|
|
|
|
//
|
|
|
|
|
2023-07-07 16:32:11 +00:00
|
|
|
// Suppress codecvt deprecation warnings.
|
2021-08-18 16:58:43 +00:00
|
|
|
#if defined(_MSC_VER) // MSVC compiler.
|
|
|
|
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
|
2024-08-09 20:54:57 +00:00
|
|
|
#elif defined(__clang__)
|
2023-07-07 16:32:11 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2021-08-18 16:58:43 +00:00
|
|
|
#endif
|
|
|
|
|
2017-11-15 15:59:39 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2022-08-01 17:51:25 +00:00
|
|
|
#include "Log.h"
|
2022-08-13 14:27:44 +00:00
|
|
|
#include "utils/PlatformUtil.h"
|
2017-11-15 15:59:39 +00:00
|
|
|
|
2024-08-09 15:48:46 +00:00
|
|
|
#include <unicode/brkiter.h>
|
|
|
|
#include <unicode/ustring.h>
|
|
|
|
|
2018-01-26 18:53:19 +00:00
|
|
|
#include <algorithm>
|
2024-07-15 15:39:03 +00:00
|
|
|
#include <cstdarg>
|
2020-07-07 19:25:15 +00:00
|
|
|
#include <locale>
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2017-11-15 15:59:39 +00:00
|
|
|
namespace Utils
|
|
|
|
{
|
2020-06-23 18:07:00 +00:00
|
|
|
namespace String
|
|
|
|
{
|
2021-05-30 18:46:17 +00:00
|
|
|
unsigned int chars2Unicode(const std::string& stringArg, size_t& cursor)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2024-07-16 19:13:50 +00:00
|
|
|
unsigned const char checkCharType {static_cast<unsigned char>(stringArg[cursor])};
|
|
|
|
unsigned int result {'?'};
|
2020-06-23 18:07:00 +00:00
|
|
|
|
|
|
|
// 0xxxxxxx, one byte character.
|
2021-01-28 18:38:27 +00:00
|
|
|
if (checkCharType <= 0x7F) {
|
2020-06-23 18:07:00 +00:00
|
|
|
// 0xxxxxxx
|
2021-07-07 18:31:46 +00:00
|
|
|
result = (stringArg[cursor++]);
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2021-01-28 18:38:27 +00:00
|
|
|
// 11110xxx, four byte character.
|
2021-12-14 16:08:37 +00:00
|
|
|
else if (checkCharType >= 0xF0 && cursor < stringArg.length() - 2) {
|
2021-01-28 18:38:27 +00:00
|
|
|
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
2021-05-30 18:46:17 +00:00
|
|
|
result = (stringArg[cursor++] & 0x07) << 18;
|
|
|
|
result |= (stringArg[cursor++] & 0x3F) << 12;
|
2021-07-07 18:31:46 +00:00
|
|
|
result |= (stringArg[cursor++] & 0x3F) << 6;
|
2021-05-30 18:46:17 +00:00
|
|
|
result |= stringArg[cursor++] & 0x3F;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
// 1110xxxx, three byte character.
|
2021-12-14 16:08:37 +00:00
|
|
|
else if (checkCharType >= 0xE0 && cursor < stringArg.length() - 1) {
|
2020-06-23 18:07:00 +00:00
|
|
|
// 1110xxxx 10xxxxxx 10xxxxxx
|
2021-05-30 18:46:17 +00:00
|
|
|
result = (stringArg[cursor++] & 0x0F) << 12;
|
2021-07-07 18:31:46 +00:00
|
|
|
result |= (stringArg[cursor++] & 0x3F) << 6;
|
2021-05-30 18:46:17 +00:00
|
|
|
result |= stringArg[cursor++] & 0x3F;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2021-01-28 18:38:27 +00:00
|
|
|
// 110xxxxx, two byte character.
|
2021-12-14 16:08:37 +00:00
|
|
|
else if (checkCharType >= 0xC0 && cursor < stringArg.length()) {
|
2021-01-28 18:38:27 +00:00
|
|
|
// 110xxxxx 10xxxxxx
|
2021-07-07 18:31:46 +00:00
|
|
|
result = (stringArg[cursor++] & 0x1F) << 6;
|
2021-05-30 18:46:17 +00:00
|
|
|
result |= stringArg[cursor++] & 0x3F;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-01-28 18:38:27 +00:00
|
|
|
// Error, invalid character.
|
2021-11-17 16:48:49 +00:00
|
|
|
++cursor;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string unicode2Chars(const unsigned int unicodeArg)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
2021-01-28 18:38:27 +00:00
|
|
|
// Normal UTF-8 ASCII character.
|
2021-05-30 18:46:17 +00:00
|
|
|
if (unicodeArg < 0x80) {
|
2021-07-07 18:31:46 +00:00
|
|
|
result += (unicodeArg & 0xFF);
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2021-01-28 18:38:27 +00:00
|
|
|
// Two-byte character.
|
2021-07-07 18:31:46 +00:00
|
|
|
else if (unicodeArg < 0x800) {
|
|
|
|
result += ((unicodeArg >> 6) & 0xFF) | 0xC0;
|
|
|
|
result += (unicodeArg & 0x3F) | 0x80;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2021-01-28 18:38:27 +00:00
|
|
|
// Three-byte character.
|
2021-05-30 18:46:17 +00:00
|
|
|
else if (unicodeArg < 0xFFFF) {
|
|
|
|
result += ((unicodeArg >> 12) & 0xFF) | 0xE0;
|
2021-07-07 18:31:46 +00:00
|
|
|
result += ((unicodeArg >> 6) & 0x3F) | 0x80;
|
|
|
|
result += (unicodeArg & 0x3F) | 0x80;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2021-01-28 18:38:27 +00:00
|
|
|
// Four-byte character.
|
2021-05-30 18:46:17 +00:00
|
|
|
else if (unicodeArg <= 0x1fffff) {
|
|
|
|
result += ((unicodeArg >> 18) & 0xFF) | 0xF0;
|
|
|
|
result += ((unicodeArg >> 12) & 0x3F) | 0x80;
|
2021-07-07 18:31:46 +00:00
|
|
|
result += ((unicodeArg >> 6) & 0x3F) | 0x80;
|
|
|
|
result += (unicodeArg & 0x3F) | 0x80;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-01-28 18:38:27 +00:00
|
|
|
// Error, invalid character.
|
2020-06-23 18:07:00 +00:00
|
|
|
result += '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string getFirstCharacter(const std::string& stringArg, bool toUpper)
|
2021-01-23 15:25:53 +00:00
|
|
|
{
|
|
|
|
std::string firstChar;
|
2024-07-16 19:13:50 +00:00
|
|
|
unsigned const char checkCharType {static_cast<unsigned char>(stringArg.front())};
|
2021-01-23 15:25:53 +00:00
|
|
|
|
2021-01-28 18:38:27 +00:00
|
|
|
// Normal UTF-8 ASCII character.
|
|
|
|
if (checkCharType <= 0x7F)
|
2021-05-30 18:46:17 +00:00
|
|
|
(toUpper) ? firstChar = toupper(stringArg.front()) : firstChar = stringArg.front();
|
2021-01-28 18:38:27 +00:00
|
|
|
// Four-byte Unicode character.
|
|
|
|
else if (checkCharType >= 0xF0)
|
2021-05-30 18:46:17 +00:00
|
|
|
firstChar = stringArg.substr(0, 4);
|
2021-01-28 18:38:27 +00:00
|
|
|
// Three-byte Unicode character.
|
|
|
|
else if (checkCharType >= 0xE0)
|
2021-05-30 18:46:17 +00:00
|
|
|
firstChar = stringArg.substr(0, 3);
|
2021-01-28 18:38:27 +00:00
|
|
|
// Two-byte Unicode character.
|
|
|
|
else if (checkCharType >= 0xC0)
|
2021-05-30 18:46:17 +00:00
|
|
|
firstChar = stringArg.substr(0, 2);
|
2021-01-23 15:25:53 +00:00
|
|
|
|
|
|
|
return firstChar;
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
size_t nextCursor(const std::string& stringArg, const size_t cursor)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2024-07-16 19:13:50 +00:00
|
|
|
size_t result {cursor};
|
2020-06-23 18:07:00 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
while (result < stringArg.length()) {
|
2021-11-17 16:48:49 +00:00
|
|
|
++result;
|
2020-06-23 18:07:00 +00:00
|
|
|
|
|
|
|
// Break if current character is not 10xxxxxx
|
2021-05-30 18:46:17 +00:00
|
|
|
if ((stringArg[result] & 0xC0) != 0x80)
|
2020-06-23 18:07:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
size_t prevCursor(const std::string& stringArg, const size_t cursor)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2024-07-16 19:13:50 +00:00
|
|
|
size_t result {cursor};
|
2020-06-23 18:07:00 +00:00
|
|
|
|
|
|
|
while (result > 0) {
|
2021-11-17 16:48:49 +00:00
|
|
|
--result;
|
2020-06-23 18:07:00 +00:00
|
|
|
|
|
|
|
// Break if current character is not 10xxxxxx
|
2021-05-30 18:46:17 +00:00
|
|
|
if ((stringArg[result] & 0xC0) != 0x80)
|
2020-06-23 18:07:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
size_t moveCursor(const std::string& stringArg, const size_t cursor, const int amount)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2024-07-16 19:13:50 +00:00
|
|
|
size_t result {cursor};
|
2020-06-23 18:07:00 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
if (amount > 0) {
|
2024-07-16 19:13:50 +00:00
|
|
|
for (int i {0}; i < amount; ++i)
|
2021-05-30 18:46:17 +00:00
|
|
|
result = nextCursor(stringArg, result);
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2021-05-30 18:46:17 +00:00
|
|
|
else if (amount < 0) {
|
2024-07-16 19:13:50 +00:00
|
|
|
for (int i {amount}; i < 0; ++i)
|
2021-05-30 18:46:17 +00:00
|
|
|
result = prevCursor(stringArg, result);
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-07-26 19:09:51 +00:00
|
|
|
size_t unicodeLength(const std::string& stringArg)
|
|
|
|
{
|
|
|
|
size_t length {0};
|
|
|
|
size_t charLength {0};
|
|
|
|
|
|
|
|
for (size_t i {0}; i < stringArg.length(); i += charLength) {
|
|
|
|
charLength = moveCursor(stringArg, i, 1) - i;
|
|
|
|
++length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string toLower(const std::string& stringArg)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2024-08-09 15:48:46 +00:00
|
|
|
// IMPORTANT: On Windows specifically the StringPiece constructor which is implicitly
|
|
|
|
// called by fromUTF8() crashes the application if a std::string is passed as the
|
|
|
|
// argument. It's therefore necessary to use c_str() to work around this issue.
|
|
|
|
// This behavior has been observed with ICU 75.1.
|
|
|
|
icu::UnicodeString convert {icu::UnicodeString::fromUTF8(stringArg.c_str())};
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string stringLower;
|
2020-06-23 18:07:00 +00:00
|
|
|
|
2024-08-09 15:48:46 +00:00
|
|
|
convert.toLower();
|
|
|
|
return convert.toUTF8String(stringLower);
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string toUpper(const std::string& stringArg)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2024-08-09 15:48:46 +00:00
|
|
|
icu::UnicodeString convert {icu::UnicodeString::fromUTF8(stringArg.c_str())};
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string stringUpper;
|
2020-06-23 18:07:00 +00:00
|
|
|
|
2024-08-09 15:48:46 +00:00
|
|
|
convert.toUpper();
|
|
|
|
return convert.toUTF8String(stringUpper);
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 19:44:22 +00:00
|
|
|
std::string toCapitalized(const std::string& stringArg)
|
2021-08-20 15:51:36 +00:00
|
|
|
{
|
2022-02-09 19:44:22 +00:00
|
|
|
if (stringArg == "")
|
|
|
|
return stringArg;
|
|
|
|
|
2024-08-09 15:48:46 +00:00
|
|
|
UErrorCode status {U_ZERO_ERROR};
|
|
|
|
std::unique_ptr<icu::BreakIterator> iterator {nullptr};
|
|
|
|
// Since we don't know the actual text language we set it to locale en_US.
|
|
|
|
iterator.reset(icu::BreakIterator::createWordInstance(icu::Locale::getUS(), status));
|
2024-07-16 19:13:50 +00:00
|
|
|
|
2024-08-09 15:48:46 +00:00
|
|
|
if (U_FAILURE(status) || iterator == nullptr)
|
|
|
|
return stringArg;
|
|
|
|
|
|
|
|
icu::UnicodeString iterateString {
|
|
|
|
icu::UnicodeString::fromUTF8(stringArg.c_str()).toLower()};
|
2024-07-16 19:13:50 +00:00
|
|
|
|
2024-08-09 15:48:46 +00:00
|
|
|
if (iterateString != nullptr) {
|
|
|
|
iterator->setText(iterateString);
|
|
|
|
int32_t pos {iterator->first()};
|
|
|
|
int32_t lastPos {pos};
|
|
|
|
|
|
|
|
while (pos != icu::BreakIterator::DONE) {
|
|
|
|
iterateString.replace(lastPos, 1,
|
|
|
|
icu::UnicodeString(iterateString, pos, 1).toUpper());
|
|
|
|
pos = iterator->next();
|
|
|
|
lastPos = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return stringArg;
|
2021-08-20 15:51:36 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 15:48:46 +00:00
|
|
|
std::string stringCapitalized;
|
|
|
|
return iterateString.toUTF8String(stringCapitalized);
|
2021-08-20 15:51:36 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:29:22 +00:00
|
|
|
std::string filterUtf8(const std::string& stringArg)
|
|
|
|
{
|
|
|
|
std::string tempString;
|
|
|
|
utf8::replace_invalid(stringArg.begin(), stringArg.end(), back_inserter(tempString));
|
|
|
|
return tempString;
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string trim(const std::string& stringArg)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2024-07-16 19:13:50 +00:00
|
|
|
std::string trimString {stringArg};
|
2021-09-25 16:01:41 +00:00
|
|
|
|
|
|
|
// Trim leading and trailing whitespaces.
|
|
|
|
trimString.erase(trimString.begin(),
|
|
|
|
std::find_if(trimString.begin(), trimString.end(), [](char c) {
|
|
|
|
return !std::isspace(static_cast<unsigned char>(c));
|
|
|
|
}));
|
|
|
|
trimString.erase(
|
|
|
|
std::find_if(trimString.rbegin(), trimString.rend(),
|
|
|
|
[](char c) { return !std::isspace(static_cast<unsigned char>(c)); })
|
|
|
|
.base(),
|
|
|
|
trimString.end());
|
|
|
|
|
|
|
|
return trimString;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
std::string replace(const std::string& stringArg,
|
2022-02-21 16:14:51 +00:00
|
|
|
const std::string& from,
|
|
|
|
const std::string& to)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2022-04-15 18:33:53 +00:00
|
|
|
std::string result {stringArg};
|
2020-06-23 18:07:00 +00:00
|
|
|
|
2022-04-15 18:33:53 +00:00
|
|
|
// The outer loop makes sure that we're eliminating all repeating occurances
|
|
|
|
// of the 'from' value.
|
|
|
|
while (result.find(from) != std::string::npos) {
|
|
|
|
// Prevent endless loops.
|
|
|
|
if (from == to)
|
|
|
|
break;
|
2020-06-23 18:07:00 +00:00
|
|
|
|
2022-04-15 18:33:53 +00:00
|
|
|
std::string replaced;
|
|
|
|
size_t lastPos {0};
|
|
|
|
size_t findPos {0};
|
|
|
|
|
|
|
|
while ((findPos = result.find(from, lastPos)) != std::string::npos) {
|
|
|
|
replaced.append(result, lastPos, findPos - lastPos).append(to);
|
|
|
|
lastPos = findPos + from.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
replaced.append(result.substr(lastPos));
|
|
|
|
result = replaced;
|
2022-04-30 16:31:53 +00:00
|
|
|
|
|
|
|
// Prevent endless loops.
|
|
|
|
if (to.find(from) != std::string::npos)
|
|
|
|
break;
|
2022-04-15 18:33:53 +00:00
|
|
|
}
|
|
|
|
return result;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 15:39:03 +00:00
|
|
|
std::string format(const std::string stringArg, ...)
|
|
|
|
{
|
|
|
|
if (stringArg.empty())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// Extract all the variadic function arguments.
|
|
|
|
va_list args;
|
|
|
|
va_list copy;
|
|
|
|
|
|
|
|
va_start(args, stringArg);
|
|
|
|
va_copy(copy, args);
|
|
|
|
|
|
|
|
const int length {vsnprintf(nullptr, 0, &stringArg[0], copy)};
|
2024-07-20 20:17:44 +00:00
|
|
|
va_end(copy);
|
2024-07-15 15:39:03 +00:00
|
|
|
std::string buffer(length, '\0');
|
|
|
|
|
2024-07-20 20:17:44 +00:00
|
|
|
va_copy(copy, args);
|
2024-07-15 15:39:03 +00:00
|
|
|
vsnprintf(&buffer[0], length + 1, &stringArg[0], copy);
|
|
|
|
|
|
|
|
va_end(copy);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::wstring stringToWideString(const std::string& stringArg)
|
2020-07-07 19:25:15 +00:00
|
|
|
{
|
2023-07-28 19:44:03 +00:00
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> stringConverter;
|
2022-08-01 17:51:25 +00:00
|
|
|
try {
|
|
|
|
return stringConverter.from_bytes(stringArg);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
LOG(LogError) << "StringUtil::stringToWideString(): Conversion failed, invalid "
|
|
|
|
"characters in source string?";
|
2022-08-13 14:27:44 +00:00
|
|
|
LOG(LogError) << stringArg;
|
|
|
|
Utils::Platform::emergencyShutdown();
|
2022-08-01 17:51:25 +00:00
|
|
|
return L"";
|
|
|
|
}
|
2020-07-07 19:25:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string wideStringToString(const std::wstring& stringArg)
|
2020-07-07 19:25:15 +00:00
|
|
|
{
|
2023-07-28 19:44:03 +00:00
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> stringConverter;
|
2022-08-01 17:51:25 +00:00
|
|
|
try {
|
|
|
|
return stringConverter.to_bytes(stringArg);
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
LOG(LogError) << "StringUtil::wideStringToString(): Conversion failed, invalid "
|
|
|
|
"characters in source string?";
|
2022-08-13 14:27:44 +00:00
|
|
|
Utils::Platform::emergencyShutdown();
|
2022-08-01 17:51:25 +00:00
|
|
|
return "";
|
|
|
|
}
|
2020-07-07 19:25:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
bool startsWith(const std::string& stringArg, const std::string& start)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2021-05-30 18:46:17 +00:00
|
|
|
return (stringArg.find(start) == 0);
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
bool endsWith(const std::string& stringArg, const std::string& end)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2021-05-30 18:46:17 +00:00
|
|
|
return (stringArg.find(end) == (stringArg.size() - end.size()));
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string removeParenthesis(const std::string& stringArg)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2024-07-16 19:13:50 +00:00
|
|
|
static std::vector<char> remove {'(', ')', '[', ']'};
|
|
|
|
std::string stringRemove {stringArg};
|
2020-06-25 17:52:38 +00:00
|
|
|
size_t start;
|
|
|
|
size_t end;
|
2024-07-16 19:13:50 +00:00
|
|
|
bool done {false};
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
while (!done) {
|
|
|
|
done = true;
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2024-07-16 19:13:50 +00:00
|
|
|
for (size_t i {0}; i < remove.size(); i += 2) {
|
2021-05-30 18:46:17 +00:00
|
|
|
end = stringRemove.find_first_of(remove[i + 1]);
|
|
|
|
start = stringRemove.find_last_of(remove[i + 0], end);
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
if ((start != std::string::npos) && (end != std::string::npos)) {
|
2021-05-30 18:46:17 +00:00
|
|
|
stringRemove.erase(start, end - start + 1);
|
2020-06-23 18:07:00 +00:00
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
return trim(stringRemove);
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::vector<std::string> delimitedStringToVector(const std::string& stringArg,
|
2021-07-07 18:31:46 +00:00
|
|
|
const std::string& delimiter,
|
|
|
|
bool sort,
|
|
|
|
bool caseInsensitive)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2021-05-30 18:46:17 +00:00
|
|
|
std::vector<std::string> vectorResult;
|
2024-07-16 19:13:50 +00:00
|
|
|
size_t start {0};
|
|
|
|
size_t delimPos {stringArg.find(delimiter)};
|
2019-07-06 14:50:50 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
while (delimPos != std::string::npos) {
|
|
|
|
vectorResult.push_back(stringArg.substr(start, delimPos - start));
|
|
|
|
start = delimPos + 1;
|
|
|
|
delimPos = stringArg.find(delimiter, start);
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
vectorResult.push_back(stringArg.substr(start));
|
2020-10-22 19:14:15 +00:00
|
|
|
if (sort) {
|
|
|
|
if (caseInsensitive)
|
2021-05-30 18:46:17 +00:00
|
|
|
std::sort(std::begin(vectorResult), std::end(vectorResult),
|
2021-07-07 18:31:46 +00:00
|
|
|
[](std::string a, std::string b) {
|
|
|
|
return std::toupper(a.front()) < std::toupper(b.front());
|
|
|
|
});
|
2020-10-22 19:14:15 +00:00
|
|
|
else
|
2021-05-30 18:46:17 +00:00
|
|
|
std::sort(vectorResult.begin(), vectorResult.end());
|
2020-10-22 19:14:15 +00:00
|
|
|
}
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2022-08-19 15:03:55 +00:00
|
|
|
// Remove any empty elements.
|
|
|
|
vectorResult.erase(remove(vectorResult.begin(), vectorResult.end(), ""),
|
|
|
|
vectorResult.end());
|
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
return vectorResult;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string vectorToDelimitedString(std::vector<std::string> vectorArg,
|
2021-07-07 18:31:46 +00:00
|
|
|
const std::string& delimiter,
|
|
|
|
bool caseInsensitive)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string resultString;
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
if (caseInsensitive) {
|
2021-05-30 18:46:17 +00:00
|
|
|
std::sort(std::begin(vectorArg), std::end(vectorArg),
|
2021-07-07 18:31:46 +00:00
|
|
|
[](std::string a, std::string b) {
|
|
|
|
return std::toupper(a.front()) < std::toupper(b.front());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2021-05-30 18:46:17 +00:00
|
|
|
std::sort(vectorArg.begin(), vectorArg.end());
|
2021-07-07 18:31:46 +00:00
|
|
|
}
|
2018-01-26 18:53:19 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
for (std::vector<std::string>::const_iterator it = vectorArg.cbegin();
|
2021-11-17 16:48:49 +00:00
|
|
|
it != vectorArg.cend(); ++it)
|
2021-05-30 18:46:17 +00:00
|
|
|
resultString += (resultString.length() ? delimiter : "") + (*it);
|
2018-02-15 14:04:46 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
return resultString;
|
2020-06-23 18:07:00 +00:00
|
|
|
}
|
2018-02-15 14:04:46 +00:00
|
|
|
|
2021-05-30 18:46:17 +00:00
|
|
|
std::string scramble(const std::string& input, const std::string& key)
|
2020-06-23 18:07:00 +00:00
|
|
|
{
|
2024-07-16 19:13:50 +00:00
|
|
|
std::string buffer {input};
|
2018-02-15 14:04:46 +00:00
|
|
|
|
2024-07-16 19:13:50 +00:00
|
|
|
for (size_t i {0}; i < input.size(); ++i)
|
2021-05-30 18:46:17 +00:00
|
|
|
buffer[i] = input[i] ^ key[i];
|
2019-01-11 19:51:05 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
2019-01-11 19:51:05 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
} // namespace String
|
2021-05-30 18:46:17 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
} // namespace Utils
|