diff options
author | Joel Rosdahl <joel@rosdahl.net> | 2023-03-06 21:22:05 +0100 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2023-03-07 20:17:35 +0100 |
commit | 62858602c6aaa8c6c28bf82aa4e1244c1214b869 (patch) | |
tree | 991e7306448957caf5bec44362ee9a835820f499 | |
parent | 38ab9d38b52c873da1338f4d5eb58edb2ad5c2bc (diff) | |
download | ccache-62858602c6aaa8c6c28bf82aa4e1244c1214b869.tar.gz |
enhance: Add util::read_file_part<std::string> implementation
-rw-r--r-- | src/util/file.cpp | 5 | ||||
-rw-r--r-- | src/util/file.hpp | 7 | ||||
-rw-r--r-- | unittest/test_util_file.cpp | 41 |
3 files changed, 36 insertions, 17 deletions
diff --git a/src/util/file.cpp b/src/util/file.cpp index 7b718969..d084f88c 100644 --- a/src/util/file.cpp +++ b/src/util/file.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021-2022 Joel Rosdahl and other contributors +// Copyright (C) 2021-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -255,6 +255,9 @@ read_file_part(const std::string& path, size_t pos, size_t count) template nonstd::expected<util::Bytes, std::string> read_file_part(const std::string& path, size_t pos, size_t count); +template nonstd::expected<std::string, std::string> +read_file_part(const std::string& path, size_t pos, size_t count); + template nonstd::expected<std::vector<uint8_t>, std::string> read_file_part(const std::string& path, size_t pos, size_t count); diff --git a/src/util/file.hpp b/src/util/file.hpp index c74b58dc..0ae0a57f 100644 --- a/src/util/file.hpp +++ b/src/util/file.hpp @@ -61,9 +61,10 @@ nonstd::expected<T, std::string> read_file(const std::string& path, // Return (at most) `count` bytes from `path` starting at position `pos`. // -// `T` should be `util::Bytes` or `std::vector<uint8_t>`. If `T` is -// `std::string` and the content starts with a UTF-16 little-endian BOM on -// Windows then it will be converted to UTF-8. +// `T` should be `util::Bytes` or `std::vector<uint8_t>` for binary data and +// `std::string` for text data. If `T` is `std::string` and the content starts +// with a UTF-16 little-endian BOM on Windows then it will be converted to +// UTF-8. template<typename T> nonstd::expected<T, std::string> read_file_part(const std::string& path, size_t pos, size_t count); diff --git a/unittest/test_util_file.cpp b/unittest/test_util_file.cpp index 3cf09281..77e5c0be 100644 --- a/unittest/test_util_file.cpp +++ b/unittest/test_util_file.cpp @@ -145,17 +145,32 @@ TEST_CASE("util::read_file_part") { CHECK(util::write_file("test", "banana")); - CHECK(util::read_file_part<util::Bytes>("test", 0, 0) == util::to_span("")); - CHECK(util::read_file_part<util::Bytes>("test", 0, 6) - == util::to_span("banana")); - CHECK(util::read_file_part<util::Bytes>("test", 0, 1000) - == util::to_span("banana")); - - CHECK(util::read_file_part<util::Bytes>("test", 3, 0) == util::to_span("")); - CHECK(util::read_file_part<util::Bytes>("test", 3, 2) == util::to_span("an")); - CHECK(util::read_file_part<util::Bytes>("test", 3, 1000) - == util::to_span("ana")); - - CHECK(util::read_file_part<util::Bytes>("test", 1000, 1000) - == util::to_span("")); + SUBCASE("util::Bytes") + { + CHECK(util::read_file_part<util::Bytes>("test", 0, 0) == util::to_span("")); + CHECK(util::read_file_part<util::Bytes>("test", 0, 6) + == util::to_span("banana")); + CHECK(util::read_file_part<util::Bytes>("test", 0, 1000) + == util::to_span("banana")); + + CHECK(util::read_file_part<util::Bytes>("test", 3, 0) == util::to_span("")); + CHECK(util::read_file_part<util::Bytes>("test", 3, 2) + == util::to_span("an")); + CHECK(util::read_file_part<util::Bytes>("test", 3, 1000) + == util::to_span("ana")); + + CHECK(util::read_file_part<util::Bytes>("test", 1000, 1000) + == util::to_span("")); + } + SUBCASE("std::vector<uint8_t>") + { + auto data = util::read_file_part<std::vector<uint8_t>>("test", 3, 2); + CHECK(*data == std::vector<uint8_t>{'a', 'n'}); + } + + SUBCASE("std::string") + { + auto data = util::read_file_part<std::string>("test", 3, 2); + CHECK(*data == "an"); + } } |