Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1e40c30b authored by Elliott Hughes's avatar Elliott Hughes
Browse files

ziparchive: add a std::string_view overload to Next.

Recovery wanted this, and frameworks/base/ wants it too.

Bug: http://b/129068177
Test: treehugger
Change-Id: I8ee3f7c058fc9c1cde829da613ed15be5ce7b41e
parent e5970331
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -190,7 +190,8 @@ int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
 * archive and lower negative values on failure.
 */
int32_t Next(void* cookie, ZipEntry* data, std::string* name);
// TODO: remove this when everyone's moved over to std::string.
int32_t Next(void* cookie, ZipEntry* data, std::string_view* name);
// TODO: remove this when everyone's moved over to std::string/std::string_view.
int32_t Next(void* cookie, ZipEntry* data, ZipString* name);

/*
+11 −2
Original line number Diff line number Diff line
@@ -748,10 +748,19 @@ int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryNa
}

int32_t Next(void* cookie, ZipEntry* data, std::string* name) {
  std::string_view sv;
  int32_t result = Next(cookie, data, &sv);
  if (result == 0 && name) {
    *name = std::string(sv);
  }
  return result;
}

int32_t Next(void* cookie, ZipEntry* data, std::string_view* name) {
  ZipString zs;
  int32_t result = Next(cookie, data, &zs);
  if (result == 0) {
    *name = std::string(reinterpret_cast<const char*>(zs.name), zs.name_length);
  if (result == 0 && name) {
    *name = std::string_view(reinterpret_cast<const char*>(zs.name), zs.name_length);
  }
  return result;
}
+20 −0
Original line number Diff line number Diff line
@@ -107,6 +107,26 @@ TEST(ziparchive, OpenDoNotAssumeFdOwnership) {
  close(fd);
}

TEST(ziparchive, Iteration_std_string_view) {
  ZipArchiveHandle handle;
  ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));

  void* iteration_cookie;
  ASSERT_EQ(0, StartIteration(handle, &iteration_cookie));

  ZipEntry data;
  std::vector<std::string_view> names;
  std::string_view name;
  while (Next(iteration_cookie, &data, &name) == 0) names.push_back(name);

  // Assert that the names are as expected.
  std::vector<std::string_view> expected_names{"a.txt", "b.txt", "b/", "b/c.txt", "b/d.txt"};
  std::sort(names.begin(), names.end());
  ASSERT_EQ(expected_names, names);

  CloseArchive(handle);
}

static void AssertIterationOrder(const std::string_view prefix, const std::string_view suffix,
                                 const std::vector<std::string>& expected_names_sorted) {
  ZipArchiveHandle handle;