LunaHook-mirror/include/stringutils.h

74 lines
2.7 KiB
C
Raw Normal View History

2024-02-07 20:59:24 +08:00
#ifndef __LUNA_STRINGUILTS_H
#define __LUNA_STRINGUILTS_H
2024-07-21 21:07:05 +08:00
enum
{
VNR_TEXT_CAPACITY = 1500
}; // estimated max number of bytes allowed in VNR, slightly larger than VNR's text limit (1000)
2024-02-07 20:59:24 +08:00
2024-07-21 21:07:05 +08:00
template <class StringT>
StringT stolower(StringT s)
{
2024-02-08 16:18:33 +08:00
std::transform(s.begin(), s.end(), s.begin(), tolower);
return s;
}
2024-02-07 20:59:24 +08:00
LPCSTR reverse_search_begin(const char *s, int maxsize = VNR_TEXT_CAPACITY);
bool all_ascii(const char *s, int maxsize = VNR_TEXT_CAPACITY);
bool all_ascii(const wchar_t *s, int maxsize = VNR_TEXT_CAPACITY);
2024-07-21 21:07:05 +08:00
void strReplace(std::string &str, const std::string &oldStr, const std::string &newStr);
void strReplace(std::wstring &str, const std::wstring &oldStr, const std::wstring &newStr);
std::vector<std::string> strSplit(const std::string &s, const std::string &delim);
std::vector<std::wstring> strSplit(const std::wstring &s, const std::wstring &delim);
bool startWith(const std::string &s, const std::string &s2);
bool startWith(const std::wstring &s, const std::wstring &s2);
bool endWith(const std::string &s, const std::string &s2);
bool endWith(const std::wstring &s, const std::wstring &s2);
2024-02-07 20:59:24 +08:00
2024-07-21 21:07:05 +08:00
std::wstring utf32_to_utf16(uint32_t *u32str, size_t size);
std::basic_string<uint32_t> utf16_to_utf32(const wchar_t *u16str, size_t size);
2024-07-21 21:07:05 +08:00
std::string WideStringToString(const std::wstring &text, UINT cp = CP_UTF8);
std::wstring StringToWideString(const std::string &text);
std::optional<std::wstring> StringToWideString(const std::string &text, UINT encoding);
2024-02-07 20:59:24 +08:00
2024-07-26 19:30:04 +08:00
std::string wcasta(const std::wstring& x);
std::wstring acastw(const std::string& x);
2024-07-21 21:07:05 +08:00
size_t u32strlen(uint32_t *data);
2024-11-04 09:46:14 +08:00
int utf8charlen(char *data);
2024-07-21 21:07:05 +08:00
inline bool disable_mbwc = false;
inline bool disable_wcmb = false;
template <class ST>
inline void Trim(ST &text)
2024-02-07 20:59:24 +08:00
{
text.erase(text.begin(), std::find_if_not(text.begin(), text.end(), iswspace));
text.erase(std::find_if_not(text.rbegin(), text.rend(), iswspace).base(), text.end());
}
2024-07-21 21:07:05 +08:00
template <typename T>
inline auto FormatArg(T arg) { return arg; }
template <typename C>
inline auto FormatArg(const std::basic_string<C> &arg) { return arg.c_str(); }
2024-02-07 20:59:24 +08:00
#pragma warning(push)
2024-07-21 21:07:05 +08:00
#pragma warning(disable : 4996)
2024-02-07 20:59:24 +08:00
template <typename... Args>
2024-07-21 21:07:05 +08:00
inline std::string FormatString(const char *format, const Args &...args)
2024-02-07 20:59:24 +08:00
{
std::string buffer(snprintf(nullptr, 0, format, FormatArg(args)...), '\0');
sprintf(buffer.data(), format, FormatArg(args)...);
return buffer;
}
template <typename... Args>
2024-07-21 21:07:05 +08:00
inline std::wstring FormatString(const wchar_t *format, const Args &...args)
2024-02-07 20:59:24 +08:00
{
std::wstring buffer(_snwprintf(nullptr, 0, format, FormatArg(args)...), L'\0');
_swprintf(buffer.data(), format, FormatArg(args)...);
return buffer;
}
2024-02-08 21:48:24 +08:00
2024-07-21 21:07:05 +08:00
std::optional<std::wstring> commonparsestring(void *, size_t, void *, DWORD);
2024-02-07 20:59:24 +08:00
#pragma warning(pop)
#endif