// 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 . #include #include #include namespace ia::iae { Vector Utils::Inflate(IN PCUINT8 data, IN SIZE_T dataSize) { STATIC UINT8 TMP_BUFFER[16384]; Vector 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 Utils::Deflate(IN PCUINT8 data, IN SIZE_T dataSize) { Vector 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 groupTransformer) { std::string text = input.c_str(); std::smatch match; std::string result; std::string::const_iterator search_start(text.cbegin()); SIZE_T t = 0; while (std::regex_search(search_start, text.cend(), match, std::regex(pattern.c_str()))) { for(SIZE_T i = 1; i < match.size(); i++) { const auto p = match.position(i) + (search_start - text.cbegin()); const auto l = match.length(i); result += text.substr(t, p - t); result += groupTransformer(i - 1, text.substr(p, l).c_str()).c_str(); t = p + l; } search_start = text.cbegin() + t; } result += text.substr(t, text.size() - t); return result.c_str(); } } // namespace ia::iae