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
This commit is contained in:
Elliott Hughes 2019-06-12 12:12:47 -07:00
parent e59703319f
commit 1e40c30b0c
3 changed files with 33 additions and 3 deletions

View File

@ -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);
/*

View File

@ -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;
}

View File

@ -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;