From c5bdccfecb279ba581e5931f7fd9678d1aaa8f85 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 25 Jul 2020 03:11:14 -0400 Subject: zstd_compression: Make use of std::span in interfaces Allows condensing the data and size parameters into a single argument. --- src/common/zstd_compression.cpp | 13 ++++++------- src/common/zstd_compression.h | 7 +++---- 2 files changed, 9 insertions(+), 11 deletions(-) (limited to 'src/common') diff --git a/src/common/zstd_compression.cpp b/src/common/zstd_compression.cpp index 978526492..770833ee7 100644 --- a/src/common/zstd_compression.cpp +++ b/src/common/zstd_compression.cpp @@ -5,19 +5,18 @@ #include #include -#include "common/assert.h" #include "common/zstd_compression.h" namespace Common::Compression { -std::vector CompressDataZSTD(const u8* source, std::size_t source_size, s32 compression_level) { +std::vector CompressDataZSTD(std::span source, s32 compression_level) { compression_level = std::clamp(compression_level, 1, ZSTD_maxCLevel()); - const std::size_t max_compressed_size = ZSTD_compressBound(source_size); + const std::size_t max_compressed_size = ZSTD_compressBound(source.size()); std::vector compressed(max_compressed_size); - const std::size_t compressed_size = - ZSTD_compress(compressed.data(), compressed.size(), source, source_size, compression_level); + const std::size_t compressed_size = ZSTD_compress( + compressed.data(), compressed.size(), source.data(), source.size(), compression_level); if (ZSTD_isError(compressed_size)) { // Compression failed @@ -29,8 +28,8 @@ std::vector CompressDataZSTD(const u8* source, std::size_t source_size, s32 return compressed; } -std::vector CompressDataZSTDDefault(const u8* source, std::size_t source_size) { - return CompressDataZSTD(source, source_size, ZSTD_CLEVEL_DEFAULT); +std::vector CompressDataZSTDDefault(std::span source) { + return CompressDataZSTD(source, ZSTD_CLEVEL_DEFAULT); } std::vector DecompressDataZSTD(const std::vector& compressed) { diff --git a/src/common/zstd_compression.h b/src/common/zstd_compression.h index e9de941c8..b5edf19e7 100644 --- a/src/common/zstd_compression.h +++ b/src/common/zstd_compression.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "common/common_types.h" @@ -14,23 +15,21 @@ namespace Common::Compression { * Compresses a source memory region with Zstandard and returns the compressed data in a vector. * * @param source the uncompressed source memory region. - * @param source_size the size in bytes of the uncompressed source memory region. * @param compression_level the used compression level. Should be between 1 and 22. * * @return the compressed data. */ -std::vector CompressDataZSTD(const u8* source, std::size_t source_size, s32 compression_level); +std::vector CompressDataZSTD(std::span source, s32 compression_level); /** * Compresses a source memory region with Zstandard with the default compression level and returns * the compressed data in a vector. * * @param source the uncompressed source memory region. - * @param source_size the size in bytes of the uncompressed source memory region. * * @return the compressed data. */ -std::vector CompressDataZSTDDefault(const u8* source, std::size_t source_size); +std::vector CompressDataZSTDDefault(std::span source); /** * Decompresses a source memory region with Zstandard and returns the uncompressed data in a vector. -- cgit v1.2.3