This commit is contained in:
Isuru Samarathunga
2025-10-14 01:50:56 +05:30
parent 13e8c538c7
commit 58f2190199
13 changed files with 261 additions and 45 deletions

View File

@ -18,13 +18,15 @@
#include <zlib.h>
#include <regex>
namespace ia::iae
{
Vector<UINT8> Utils::Inflate(IN PCUINT8 data, IN SIZE_T dataSize)
{
STATIC UINT8 TMP_BUFFER[16384];
Vector<UINT8> result;
result.reserve(ia_min((SIZE_T)(dataSize * 3), sizeof(TMP_BUFFER)));
result.reserve(ia_min((SIZE_T) (dataSize * 3), sizeof(TMP_BUFFER)));
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
@ -35,7 +37,8 @@ namespace ia::iae
THROW_UNKNOWN("Inflate failed: init zlib inflate");
stream.avail_in = dataSize;
stream.next_in = (Bytef *) data;
while (true) {
while (true)
{
stream.avail_out = sizeof(TMP_BUFFER);
stream.next_out = TMP_BUFFER;
const auto r = inflate(&stream, Z_SYNC_FLUSH);
@ -58,4 +61,35 @@ namespace ia::iae
result.resize(deflateBound);
return result;
}
String Utils::RegexReplaceString(IN CONST String &input, IN CONST String &from, IN CONST String &to)
{
return std::regex_replace(input.c_str(), std::regex(from.c_str()), to.c_str()).c_str();
}
String Utils::RegexReplaceGroups(IN CONST String &input, IN CONST String &pattern,
IN std::function<String(IN INT32, IN CONST String &)> groupTransformer)
{
std::string text = input.c_str();
std::smatch match;
std::string result;
std::string::const_iterator search_start(text.cbegin());
SIZE_T t1 = 0, t2 = 0;
while (std::regex_search(search_start, text.cend(), match, std::regex(pattern.c_str())))
{
for(SIZE_T i = 1; i < match.size(); i++)
{
result += text.substr(t1, match.position(i) - t1);
result += std::string(groupTransformer(i - 1, match.str(i).c_str()).c_str());
t1 = match.position(i);
t2 = match.str(i).length();
}
result += text.substr(match.suffix().first - text.begin() - 1);
search_start = match.suffix().first;
}
return result.c_str();
}
} // namespace ia::iae