Files
IAEngine/Engine/Src/Imp/CPP/Utils.cpp
Isuru Samarathunga 58f2190199 Fixes
2025-10-14 01:50:56 +05:30

95 lines
3.4 KiB
C++

// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <IAEngine/Utils.hpp>
#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)));
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
stream.avail_in = 0;
stream.next_in = Z_NULL;
if (inflateInit(&stream) != Z_OK)
THROW_UNKNOWN("Inflate failed: init zlib inflate");
stream.avail_in = dataSize;
stream.next_in = (Bytef *) data;
while (true)
{
stream.avail_out = sizeof(TMP_BUFFER);
stream.next_out = TMP_BUFFER;
const auto r = inflate(&stream, Z_SYNC_FLUSH);
result.insert(result.end(), sizeof(TMP_BUFFER) - stream.avail_out, TMP_BUFFER);
if (r == Z_STREAM_END)
break;
else if (r != Z_OK)
THROW_INVALID_DATA("Inflate failed: zlib inflation");
}
inflateEnd(&stream);
return result;
}
Vector<UINT8> Utils::Deflate(IN PCUINT8 data, IN SIZE_T dataSize)
{
Vector<UINT8> result;
auto deflateBound = compressBound(dataSize);
result.resize(deflateBound);
compress(result.data(), &deflateBound, data, dataSize);
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