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

Commit 03f311cb authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Revert "Fix loaded apk string pool order""

parents 42596b17 90b7a08a
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -727,7 +727,7 @@ std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const Config
          // This must be a FileReference.
          std::unique_ptr<FileReference> file_ref =
              util::make_unique<FileReference>(dst_pool->MakeRef(
                  str, StringPool::Context(StringPool::Context::kHighPriority, config), data));
                  str, StringPool::Context(StringPool::Context::kHighPriority, config)));
          if (type == ResourceType::kRaw) {
            file_ref->type = ResourceFile::Type::kUnknown;
          } else if (util::EndsWith(*file_ref->path, ".xml")) {
@@ -739,7 +739,7 @@ std::unique_ptr<Item> ParseBinaryResValue(const ResourceType& type, const Config
        }

        // There are no styles associated with this string, so treat it as a simple string.
        return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config), data));
        return util::make_unique<String>(dst_pool->MakeRef(str, StringPool::Context(config)));
      }
    } break;

+5 −17
Original line number Diff line number Diff line
@@ -165,13 +165,12 @@ StringPool::Ref StringPool::MakeRef(const StringPiece& str) {
  return MakeRefImpl(str, Context{}, true);
}

StringPool::Ref StringPool::MakeRef(const StringPiece& str, const Context& context,
                                    Maybe<size_t> index) {
  return MakeRefImpl(str, context, true, index);
StringPool::Ref StringPool::MakeRef(const StringPiece& str, const Context& context) {
  return MakeRefImpl(str, context, true);
}

StringPool::Ref StringPool::MakeRefImpl(const StringPiece& str, const Context& context,
                                        bool unique, Maybe<size_t> index) {
                                        bool unique) {
  if (unique) {
    auto range = indexed_strings_.equal_range(str);
    for (auto iter = range.first; iter != range.second; ++iter) {
@@ -181,26 +180,15 @@ StringPool::Ref StringPool::MakeRefImpl(const StringPiece& str, const Context& c
    }
  }

  const size_t size = strings_.size();
  // Insert the string at the end of the string vector if no index is specified
  const size_t insertion_index = index ? index.value() : size;

  std::unique_ptr<Entry> entry(new Entry());
  entry->value = str.to_string();
  entry->context = context;
  entry->index_ = insertion_index;
  entry->index_ = strings_.size();
  entry->ref_ = 0;
  entry->pool_ = this;

  Entry* borrow = entry.get();
  if (insertion_index == size) {
  strings_.emplace_back(std::move(entry));
  } else {
    // Allocate enough space for the string at the index
    strings_.resize(std::max(insertion_index + 1, size));
    strings_[insertion_index] = std::move(entry);
  }

  indexed_strings_.insert(std::make_pair(StringPiece(borrow->value), borrow));
  return Ref(borrow);
}
+2 −4
Original line number Diff line number Diff line
@@ -166,8 +166,7 @@ class StringPool {

  // Adds a string to the pool, unless it already exists, with a context object that can be used
  // when sorting the string pool. Returns a reference to the string in the pool.
  Ref MakeRef(const android::StringPiece& str, const Context& context,
              Maybe<size_t> index = {});
  Ref MakeRef(const android::StringPiece& str, const Context& context);

  // Adds a string from another string pool. Returns a reference to the string in the string pool.
  Ref MakeRef(const Ref& ref);
@@ -211,8 +210,7 @@ class StringPool {

  static bool Flatten(BigBuffer* out, const StringPool& pool, bool utf8, IDiagnostics* diag);

  Ref MakeRefImpl(const android::StringPiece& str, const Context& context, bool unique,
                  Maybe<size_t> index = {});
  Ref MakeRefImpl(const android::StringPiece& str, const Context& context, bool unique);
  void ReAssignIndices();

  std::vector<std::unique_ptr<Entry>> strings_;
+0 −18
Original line number Diff line number Diff line
@@ -84,24 +84,6 @@ TEST(StringPoolTest, MaintainInsertionOrderIndex) {
  EXPECT_THAT(ref_c.index(), Eq(2u));
}

TEST(StringPoolTest, AssignStringIndex) {
  StringPool pool;

  StringPool::Ref ref_a = pool.MakeRef("0", StringPool::Context{}, 0u);
  StringPool::Ref ref_b = pool.MakeRef("1", StringPool::Context{}, 1u);
  StringPool::Ref ref_c = pool.MakeRef("5", StringPool::Context{}, 5u);
  StringPool::Ref ref_d = pool.MakeRef("2", StringPool::Context{}, 2u);
  StringPool::Ref ref_e = pool.MakeRef("4", StringPool::Context{}, 4u);
  StringPool::Ref ref_f = pool.MakeRef("3", StringPool::Context{}, 3u);

  EXPECT_THAT(ref_a.index(), Eq(0u));
  EXPECT_THAT(ref_b.index(), Eq(1u));
  EXPECT_THAT(ref_d.index(), Eq(2u));
  EXPECT_THAT(ref_f.index(), Eq(3u));
  EXPECT_THAT(ref_e.index(), Eq(4u));
  EXPECT_THAT(ref_c.index(), Eq(5u));
}

TEST(StringPoolTest, PruneStringsWithNoReferences) {
  StringPool pool;

+7 −10
Original line number Diff line number Diff line
@@ -43,7 +43,8 @@ namespace aapt {

class IApkSerializer {
 public:
  IApkSerializer(IAaptContext* context, const Source& source) : context_(context), source_(source) {}
  IApkSerializer(IAaptContext* context, const Source& source) : context_(context),
                                                                source_(source) {}

  virtual bool SerializeXml(const xml::XmlResource* xml, const std::string& path, bool utf16,
                            IArchiveWriter* writer, uint32_t compression_flags) = 0;
@@ -256,9 +257,6 @@ class Context : public IAaptContext {
int Convert(IAaptContext* context, LoadedApk* apk, IArchiveWriter* output_writer,
            ApkFormat output_format, TableFlattenerOptions table_flattener_options,
            XmlFlattenerOptions xml_flattener_options) {
  // Do not change the ordering of strings in the values string pool
  table_flattener_options.sort_stringpool_entries = false;

  unique_ptr<IApkSerializer> serializer;
  if (output_format == ApkFormat::kBinary) {
    serializer.reset(new BinaryApkSerializer(context, apk->GetSource(), table_flattener_options,
@@ -303,8 +301,7 @@ int Convert(IAaptContext* context, LoadedApk* apk, IArchiveWriter* output_writer
              if (files_written.insert(*file->path).second) {
                if (!serializer->SerializeFile(file, output_writer)) {
                  context->GetDiagnostics()->Error(DiagMessage(apk->GetSource())
                                                       << "failed to serialize file "
                                                       << *file->path);
                                                   << "failed to serialize file " << *file->path);
                  return 1;
                }
              }
Loading