diff --git a/Source/Utility/Text/StringUtil.cpp b/Source/Utility/Text/StringUtil.cpp new file mode 100644 index 00000000..da8eb5d9 --- /dev/null +++ b/Source/Utility/Text/StringUtil.cpp @@ -0,0 +1,52 @@ +#include "Utility/Text/StringUtil.h" + +#include + +// OFFSET: 0x0058A4D0 +// FUNCTION: fn_02757 +int CountCharacters(WORD codePage, const char* text, const char* end) { + if (text == nullptr) { + return 0; + } + + int count = 0; + while (*text != '\0' && (end == nullptr || text < end)) { + text = CharNextExA(codePage, text, 0); + if (end != nullptr && text > end) { + break; + } + ++count; + } + return count; +} + +// OFFSET: 0x0058A620 +// FUNCTION: fn_02758 +bool ContainsText(const std::string& text, std::string_view pattern) { + return text.find(pattern.data(), 0, pattern.size()) != std::string::npos; +} + +// OFFSET: 0x0058AAF0 +// FUNCTION: fn_02761 +bool IsNullOrEmpty(const char* text) { + return text == nullptr || std::strlen(text) == 0; +} + +// OFFSET: 0x0058AC30 +// FUNCTION: fn_02762 +int RemoveAll(std::string& text, std::string_view pattern) { + return ReplaceAll(text, pattern, ""); +} + +// OFFSET: 0x0058AC60 +// FUNCTION: fn_02763 +int ReplaceAll(std::string& text, std::string_view pattern, std::string_view replacement) { + int count = 0; + std::size_t position = 0; + while ((position = text.find(pattern.data(), position, pattern.size())) != std::string::npos) { + text.replace(position, pattern.size(), replacement.data(), replacement.size()); + position += replacement.size(); + ++count; + } + return count; +} diff --git a/Source/Utility/Text/StringUtil.h b/Source/Utility/Text/StringUtil.h new file mode 100644 index 00000000..54d6a5f9 --- /dev/null +++ b/Source/Utility/Text/StringUtil.h @@ -0,0 +1,14 @@ +#ifndef RAGNAROK_SOURCE_UTILITY_TEXT_STRINGUTIL_H +#define RAGNAROK_SOURCE_UTILITY_TEXT_STRINGUTIL_H + +#include +#include +#include + +int CountCharacters(WORD codePage, const char* text, const char* end); +bool ContainsText(const std::string& text, std::string_view pattern); +bool IsNullOrEmpty(const char* text); +int RemoveAll(std::string& text, std::string_view pattern); +int ReplaceAll(std::string& text, std::string_view pattern, std::string_view replacement); + +#endif diff --git a/data/compile.json b/data/compile.json index 1fc72d4f..70f48879 100644 --- a/data/compile.json +++ b/data/compile.json @@ -835,6 +835,7 @@ "Source/Utility/Text/FormatString.cpp": 0, "Source/Utility/Text/GroupedInteger.cpp": 0, "Source/Utility/Text/LanguageCharset.cpp": 0, + "Source/Utility/Text/StringUtil.cpp": 0, "Source/Utility/Text/TextEscapes.cpp": 0, "Source/Utility/Text/TextTokens.cpp": 0, "Source/Utility/Threading/NSyncObject.cpp": 0,