add class 'StringUtility'.

This commit is contained in:
luocai 2023-07-21 15:46:12 +08:00
parent a6b3939777
commit e1558d53a2
4 changed files with 172 additions and 0 deletions

17
.clang-format Normal file
View File

@ -0,0 +1,17 @@
BasedOnStyle: LLVM
ObjCBlockIndentWidth: 4
IndentWidth: 4
TabWidth: 4
AccessModifierOffset: -4
ColumnLimit: 120
#模板声明后换行
AlwaysBreakTemplateDeclarations: true
# 是否允许短if单行 If true, if (a) return; 可以放到同一行
AllowShortIfStatementsOnASingleLine: true
#短句 while (true) continue; 能被放到单行。
AllowShortLoopsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false

View File

@ -8,6 +8,7 @@ add_library(Universal
MessageManager.h MessageManager.inl MessageManager.cpp
Singleton.h
StreamFormat.h StreamFormat.inl StreamFormat.cpp
StringUtility.h StringUtility.cpp
)
target_include_directories(Universal

View File

@ -0,0 +1,86 @@
#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

68
Universal/StringUtility.h Normal file
View File

@ -0,0 +1,68 @@
#ifndef STRINGUTILITY_H
#define STRINGUTILITY_H
#include <string>
#include <vector>
namespace std {
template <class CharT, class Traits>
std::basic_string<CharT, Traits> to_string(const std::basic_string_view<CharT, Traits> &str) {
return std::basic_string<CharT, Traits>(str.begin(), str.end());
}
} // namespace std
namespace Amass {
namespace StringUtility {
template <typename CharT>
std::vector<std::basic_string<CharT>> split(const std::basic_string<CharT> &string,
const std::basic_string_view<CharT> &delimiter) {
std::vector<std::basic_string<CharT>> ret;
if (delimiter.empty() || string.empty()) return ret;
auto buf = string;
size_t pos = std::string::npos;
size_t delimiterLength = delimiter.length();
while (true) {
pos = buf.find(delimiter);
if (pos != std::basic_string<CharT>::npos) {
auto substr = buf.substr(0, pos);
if (!substr.empty()) ret.push_back(substr);
buf = buf.substr(pos + delimiterLength);
} else {
if (!buf.empty()) ret.push_back(buf);
break;
}
}
return ret;
}
inline std::vector<std::string> split(const std::string &string, const std::string_view &delimiter) {
return split<char>(string, delimiter);
}
std::string &replace(std::string &string, const std::string &before, const std::string &after);
template <typename CharT>
std::basic_string<CharT> trimmed(const std::basic_string<CharT> &string) {
static const CharT *whitespace = std::is_same_v<CharT, char> ? reinterpret_cast<const CharT *>("\t\n\v\f\r ")
: reinterpret_cast<const CharT *>(L"\t\n\v\f\r ");
auto begin = string.find_first_not_of(whitespace);
if (begin == std::basic_string<CharT>::npos) return std::basic_string<CharT>();
auto end = string.find_last_not_of(whitespace);
if (end == std::basic_string<CharT>::npos) return std::basic_string<CharT>();
return string.substr(begin, end - begin + 1);
}
bool equal(std::string_view lhs, std::string_view rhs, bool caseSensitivity = true);
size_t utf8Length(const std::string &text);
std::string_view utf8At(const std::string &text, size_t index);
size_t utf8CharacterByteSize(const char *character);
std::wstring stringToWString(const std::string &string);
} // namespace StringUtility
} // namespace Amass
#endif // STRINGUTILITY_H