From e4483eca01b48b943cd0461e24a74ae1a3139ed4 Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Wed, 12 Feb 2025 21:57:46 +1100 Subject: Update to most recent version (old initial commit) --- src/shared/shared.cc | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'src/shared/shared.cc') diff --git a/src/shared/shared.cc b/src/shared/shared.cc index 55ccf1f..f5a478b 100644 --- a/src/shared/shared.cc +++ b/src/shared/shared.cc @@ -2,6 +2,12 @@ namespace shared { +float get_duration_seconds(const time_duration_t& duration) noexcept { + const auto fdiff = + std::chrono::duration_cast>(duration); + return fdiff.count(); +} + std::string make_string_lower(std::string str) noexcept { std::ranges::transform(str.begin(), str.end(), str.begin(), [](const auto c) { return std::tolower(c); }); @@ -26,7 +32,7 @@ std::ofstream open_file(const std::string& dir, return ret; } -void compress_string(std::string& str) noexcept { +std::string compress_string(const std::string& str) { std::stringstream input(str); boost::iostreams::filtering_streambuf out; @@ -37,21 +43,26 @@ void compress_string(std::string& str) noexcept { std::stringstream result; boost::iostreams::copy(out, result); - str = result.str(); + return result.str(); } -void decompress_string(std::string& str) noexcept { - std::stringstream input; - input << str; +std::optional +maybe_decompress_string(const std::string& str) noexcept { + try { + std::stringstream input; + input << str; - boost::iostreams::filtering_streambuf in; - in.push(boost::iostreams::gzip_decompressor()); - in.push(input); + boost::iostreams::filtering_streambuf in; + in.push(boost::iostreams::gzip_decompressor()); + in.push(input); - std::stringstream result; - boost::iostreams::copy(in, result); + std::stringstream result; + boost::iostreams::copy(in, result); - str = result.str(); + return result.str(); + } catch (...) { + return std::nullopt; + } } } // namespace shared -- cgit v1.2.3