87 lines
2.2 KiB
C++
87 lines
2.2 KiB
C++
#include "StringUtility.h"
|
|
#include "BoostLog.h"
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <codecvt>
|
|
|
|
namespace Amass {
|
|
|
|
namespace StringUtility {
|
|
|
|
std::string &replace(std::string &string, const std::string &before, const std::string &after) {
|
|
for (size_t pos = 0; pos != std::string::npos; pos += after.length()) {
|
|
pos = string.find(before, pos);
|
|
if (pos != std::string::npos)
|
|
string.replace(pos, before.length(), after);
|
|
else
|
|
break;
|
|
}
|
|
return string;
|
|
}
|
|
|
|
size_t utf8Length(const std::string &text) {
|
|
size_t ret = 0;
|
|
for (uint32_t i = 0; i < text.length();) {
|
|
uint32_t byte_length = utf8CharacterByteSize(text.c_str() + i);
|
|
ret++;
|
|
i += byte_length;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
std::string_view utf8At(const std::string &text, size_t index) {
|
|
const char *ret = text.c_str();
|
|
for (uint32_t i = 0; i < index; i++) {
|
|
uint32_t byte_length = utf8CharacterByteSize(ret);
|
|
ret += byte_length;
|
|
}
|
|
return std::string_view(ret, utf8CharacterByteSize(ret));
|
|
}
|
|
|
|
size_t utf8CharacterByteSize(const char *character) {
|
|
if (character == nullptr) return 0;
|
|
uint32_t ret = 0;
|
|
uint8_t temp = character[0];
|
|
while (temp & 0x80) {
|
|
ret++;
|
|
temp <<= 1;
|
|
}
|
|
return ret > 0 ? ret : 1;
|
|
}
|
|
|
|
std::wstring stringToWString(const std::string &string) {
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
|
return converter.from_bytes(string);
|
|
}
|
|
|
|
bool equal(std::string_view lhs, std::string_view rhs, bool caseSensitivity) {
|
|
auto n = lhs.size();
|
|
if (rhs.size() != n) return false;
|
|
auto p1 = lhs.data();
|
|
auto p2 = rhs.data();
|
|
char a, b;
|
|
|
|
while (n--) { // fast loop
|
|
a = *p1++;
|
|
b = *p2++;
|
|
if (a != b) {
|
|
if (caseSensitivity) {
|
|
return false;
|
|
} else {
|
|
goto slow;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
slow:
|
|
do {
|
|
if (std::tolower(a) != std::tolower(b)) return false;
|
|
a = *p1++;
|
|
b = *p2++;
|
|
} while (n--);
|
|
return true;
|
|
}
|
|
|
|
} // namespace StringUtility
|
|
} // namespace Amass
|