Merge "Add a std::string overload to Next." am: f530dbf671

am: 928db53b44

Change-Id: I360318fd68235ca0cc524fd482249a12bc7f2257
This commit is contained in:
Elliott Hughes 2019-05-23 13:11:35 -07:00 committed by android-build-merger
commit b186dd4e58
5 changed files with 20 additions and 8 deletions

View file

@ -25,6 +25,7 @@
#include <sys/cdefs.h>
#include <sys/types.h>
#include <string>
#include <string_view>
#include "android-base/off64_t.h"
@ -35,6 +36,7 @@ enum {
kCompressDeflated = 8, // standard deflate
};
// TODO: remove this when everyone's moved over to std::string.
struct ZipString {
const uint8_t* name;
uint16_t name_length;
@ -187,6 +189,8 @@ int32_t StartIteration(ZipArchiveHandle archive, void** cookie_ptr,
* Returns 0 on success, -1 if there are no more elements in this
* 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, ZipString* name);
/*

View file

@ -255,9 +255,8 @@ static void ProcessAll(ZipArchiveHandle zah) {
}
ZipEntry entry;
ZipString string;
while ((err = Next(cookie, &entry, &string)) >= 0) {
std::string name(string.name, string.name + string.name_length);
std::string name;
while ((err = Next(cookie, &entry, &name)) >= 0) {
if (ShouldInclude(name)) ProcessOne(zah, entry, name);
}

View file

@ -747,6 +747,15 @@ int32_t FindEntry(const ZipArchiveHandle archive, const std::string_view entryNa
return FindEntry(archive, static_cast<uint32_t>(ent), data);
}
int32_t Next(void* cookie, ZipEntry* data, std::string* 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);
}
return result;
}
int32_t Next(void* cookie, ZipEntry* data, ZipString* name) {
IterationHandle* handle = reinterpret_cast<IterationHandle*>(cookie);
if (handle == NULL) {

View file

@ -71,7 +71,7 @@ static void Iterate_all_files(benchmark::State& state) {
ZipArchiveHandle handle;
void* iteration_cookie;
ZipEntry data;
ZipString name;
std::string name;
while (state.KeepRunning()) {
OpenArchive(temp_file->path, &handle);

View file

@ -118,10 +118,10 @@ static void AssertIterationOrder(const std::string_view prefix, const std::strin
ZipEntry data;
std::vector<std::string> names;
ZipString name;
std::string name;
for (size_t i = 0; i < expected_names_sorted.size(); ++i) {
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
names.push_back(std::string(reinterpret_cast<const char*>(name.name), name.name_length));
names.push_back(name);
}
// End of iteration.
@ -167,7 +167,7 @@ TEST(ziparchive, IterationWithBadPrefixAndSuffix) {
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, "x", "y"));
ZipEntry data;
ZipString name;
std::string name;
// End of iteration.
ASSERT_EQ(-1, Next(iteration_cookie, &data, &name));
@ -224,7 +224,7 @@ TEST(ziparchive, TestInvalidDeclaredLength) {
void* iteration_cookie;
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie));
ZipString name;
std::string name;
ZipEntry data;
ASSERT_EQ(Next(iteration_cookie, &data, &name), 0);