]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::read_file_part<std::string> implementation
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 6 Mar 2023 20:22:05 +0000 (21:22 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 7 Mar 2023 19:17:35 +0000 (20:17 +0100)
src/util/file.cpp
src/util/file.hpp
unittest/test_util_file.cpp

index 7b718969e42b1dbea540b87575123cc75456f02c..d084f88c43151969357914527515ebb2aad93c12 100644 (file)
@@ -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);
 
index c74b58dc54ba72d4e3027ec54440460a029a243f..0ae0a57f2ed105c364e57bf3a00c3aa0112dce26 100644 (file)
@@ -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);
index 3cf09281d2e0b4b28069b2dc4278743889646194..77e5c0bec8f0079b1413ee6b2c77c39480c1dcaf 100644 (file)
@@ -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");
+  }
 }