Skip to content

Commit d5168df

Browse files
committed
tryCompress and tryDecompress returns std::optional<std::string> instead of just std::string
1 parent 1de3eff commit d5168df

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

vpr/src/server/zlibutils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#include <cstring> // Include cstring for memset
44
#include <zlib.h>
55

6-
std::string tryCompress(const std::string& decompressed)
6+
std::optional<std::string> tryCompress(const std::string& decompressed)
77
{
88
z_stream zs;
99
memset(&zs, 0, sizeof(zs));
1010

1111
if (deflateInit(&zs, Z_BEST_COMPRESSION) != Z_OK) {
12-
return "";
12+
return std::nullopt;
1313
}
1414

1515
zs.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(decompressed.data()));
@@ -35,19 +35,19 @@ std::string tryCompress(const std::string& decompressed)
3535
deflateEnd(&zs);
3636

3737
if (retCode != Z_STREAM_END) {
38-
return "";
38+
return std::nullopt;
3939
}
4040

4141
return result;
4242
}
4343

44-
std::string tryDecompress(const std::string& compressed)
44+
std::optional<std::string> tryDecompress(const std::string& compressed)
4545
{
4646
z_stream zs;
4747
memset(&zs, 0, sizeof(zs));
4848

4949
if (inflateInit(&zs) != Z_OK) {
50-
return "";
50+
return std::nullopt;
5151
}
5252

5353
zs.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(compressed.data()));
@@ -74,7 +74,7 @@ std::string tryDecompress(const std::string& compressed)
7474
inflateEnd(&zs);
7575

7676
if (retCode != Z_STREAM_END) {
77-
return "";
77+
return std::nullopt;
7878
}
7979

8080
return result;

vpr/src/server/zlibutils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string>
55
#include <optional>
66

7-
std::string tryCompress(const std::string& decompressed);
8-
std::string tryDecompress(const std::string& compressed);
7+
std::optional<std::string> tryCompress(const std::string& decompressed);
8+
std::optional<std::string> tryDecompress(const std::string& compressed);
99

1010
#endif /* ZLIBUTILS_H */

vpr/test/test_server_zlibutils.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ TEST_CASE("test_server_zlib_utils", "[vpr]")
77
{
88
const std::string orig{"This string is going to be compressed now"};
99

10-
std::string compressed = tryCompress(orig);
11-
std::string decompressed = tryDecompress(compressed);
10+
std::optional<std::string> compressedOpt = tryCompress(orig);
11+
REQUIRE(compressedOpt);
12+
REQUIRE(orig != compressedOpt.value());
1213

13-
REQUIRE(orig != compressed);
14-
REQUIRE(orig == decompressed);
14+
std::optional<std::string> decompressedOpt = tryDecompress(compressedOpt.value());
15+
REQUIRE(decompressedOpt);
16+
17+
REQUIRE(orig == decompressedOpt.value());
1518
}
1619

1720

0 commit comments

Comments
 (0)