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

Commit 1d6d8ac9 authored by Yurii Zubrytskyi's avatar Yurii Zubrytskyi
Browse files

[res] Simplify XmlPullParser::FindAttribute() and StringPool::Sort

1. FindAttribute used to do lots of work that C++17 does
   automatically

2. Use function_ref for a more efficient callback passing

Test: aapt2_tests
Flag: EXEMPT minor refactor
Change-Id: I4f172cca16c05422992b107e347ed01479fff898
parent e0855420
Loading
Loading
Loading
Loading
+12 −14
Original line number Diff line number Diff line
@@ -297,24 +297,22 @@ void StringPool::Prune() {
template <typename E>
static void SortEntries(
    std::vector<std::unique_ptr<E>>& entries,
    const std::function<int(const StringPool::Context&, const StringPool::Context&)>& cmp) {
    base::function_ref<int(const StringPool::Context&, const StringPool::Context&)> cmp) {
  using UEntry = std::unique_ptr<E>;

  if (cmp != nullptr) {
    std::sort(entries.begin(), entries.end(), [&cmp](const UEntry& a, const UEntry& b) -> bool {
  std::sort(entries.begin(), entries.end(), [cmp](const UEntry& a, const UEntry& b) -> bool {
    int r = cmp(a->context, b->context);
    if (r == 0) {
      r = a->value.compare(b->value);
    }
    return r < 0;
  });
  } else {
    std::sort(entries.begin(), entries.end(),
              [](const UEntry& a, const UEntry& b) -> bool { return a->value < b->value; });
}

void StringPool::Sort() {
  Sort([](auto&&, auto&&) { return 0; });
}

void StringPool::Sort(const std::function<int(const Context&, const Context&)>& cmp) {
void StringPool::Sort(base::function_ref<int(const Context&, const Context&)> cmp) {
  SortEntries(styles_, cmp);
  SortEntries(strings_, cmp);
  ReAssignIndices();
+3 −2
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
#ifndef _ANDROID_STRING_POOL_H
#define _ANDROID_STRING_POOL_H

#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
@@ -25,6 +24,7 @@

#include "BigBuffer.h"
#include "IDiagnostics.h"
#include "android-base/function_ref.h"
#include "android-base/macros.h"
#include "androidfw/ConfigDescription.h"
#include "androidfw/StringPiece.h"
@@ -205,7 +205,8 @@ class StringPool {
  // Sorts the strings according to their Context using some comparison function.
  // Equal Contexts are further sorted by string value, lexicographically.
  // If no comparison function is provided, values are only sorted lexicographically.
  void Sort(const std::function<int(const Context&, const Context&)>& cmp = nullptr);
  void Sort();
  void Sort(base::function_ref<int(const Context&, const Context&)> cmp);

  // Removes any strings that have no references.
  void Prune();
+17 −2
Original line number Diff line number Diff line
@@ -14,11 +14,13 @@
 * limitations under the License.
 */

#include <iostream>
#include "xml/XmlPullParser.h"

#include <algorithm>
#include <string>
#include <tuple>

#include "util/Util.h"
#include "xml/XmlPullParser.h"
#include "xml/XmlUtil.h"

using ::android::InputStream;
@@ -325,5 +327,18 @@ std::optional<StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser, St
  return {};
}

XmlPullParser::const_iterator XmlPullParser::FindAttribute(android::StringPiece namespace_uri,
                                                           android::StringPiece name) const {
  const auto end_iter = end_attributes();
  const auto iter = std::lower_bound(begin_attributes(), end_iter, std::tuple(namespace_uri, name),
                                     [](const Attribute& attr, const auto& rhs) {
                                       return std::tie(attr.namespace_uri, attr.name) < rhs;
                                     });
  if (iter != end_iter && namespace_uri == iter->namespace_uri && name == iter->name) {
    return iter;
  }
  return end_iter;
}

}  // namespace xml
}  // namespace aapt
+1 −27
Original line number Diff line number Diff line
@@ -19,8 +19,7 @@

#include <expat.h>

#include <algorithm>
#include <istream>
#include <optional>
#include <ostream>
#include <queue>
#include <stack>
@@ -302,31 +301,6 @@ inline bool XmlPullParser::Attribute::operator!=(const Attribute& rhs) const {
  return compare(rhs) != 0;
}

inline XmlPullParser::const_iterator XmlPullParser::FindAttribute(
    android::StringPiece namespace_uri, android::StringPiece name) const {
  const auto end_iter = end_attributes();
  const auto iter = std::lower_bound(
      begin_attributes(), end_iter,
      std::pair<android::StringPiece, android::StringPiece>(namespace_uri, name),
      [](const Attribute& attr,
         const std::pair<android::StringPiece, android::StringPiece>& rhs) -> bool {
        int cmp = attr.namespace_uri.compare(
            0, attr.namespace_uri.size(), rhs.first.data(), rhs.first.size());
        if (cmp < 0) return true;
        if (cmp > 0) return false;
        cmp = attr.name.compare(0, attr.name.size(), rhs.second.data(),
                                rhs.second.size());
        if (cmp < 0) return true;
        return false;
      });

  if (iter != end_iter && namespace_uri == iter->namespace_uri &&
      name == iter->name) {
    return iter;
  }
  return end_iter;
}

}  // namespace xml
}  // namespace aapt