X-Git-Url: https://git.saurik.com/android/aapt.git/blobdiff_plain/2ff799f0c1a9e168c782f52a13d3e1d1dcd54014..a26716da51b33c5014fba460537db707a9ea84f2:/StringPool.h diff --git a/StringPool.h b/StringPool.h index 7275259..16050b2 100644 --- a/StringPool.h +++ b/StringPool.h @@ -10,9 +10,10 @@ #include "Main.h" #include "AaptAssets.h" -#include +#include #include #include +#include #include #include @@ -20,7 +21,7 @@ #include #include -#include +#include using namespace android; @@ -40,12 +41,28 @@ class StringPool public: struct entry { entry() : offset(0) { } - entry(const String16& _value) : value(_value), offset(0) { } - entry(const entry& o) : value(o.value), offset(o.offset), indices(o.indices) { } + entry(const String16& _value) : value(_value), offset(0), hasStyles(false) { } + entry(const entry& o) : value(o.value), offset(o.offset), + hasStyles(o.hasStyles), indices(o.indices), + configTypeName(o.configTypeName), configs(o.configs) { } String16 value; size_t offset; + bool hasStyles; Vector indices; + String8 configTypeName; + Vector configs; + + String8 makeConfigsString() const; + + int compare(const entry& o) const; + + inline bool operator<(const entry& o) const { return compare(o) < 0; } + inline bool operator<=(const entry& o) const { return compare(o) <= 0; } + inline bool operator==(const entry& o) const { return compare(o) == 0; } + inline bool operator!=(const entry& o) const { return compare(o) != 0; } + inline bool operator>=(const entry& o) const { return compare(o) >= 0; } + inline bool operator>(const entry& o) const { return compare(o) > 0; } }; struct entry_style_span { @@ -63,16 +80,10 @@ public: }; /** - * If 'sorted' is true, then the final strings in the resource data - * structure will be generated in sorted order. This allow for fast - * lookup with ResStringPool::indexOfString() (O(log n)), at the expense - * of support for styled string entries (which requires the same string - * be included multiple times in the pool). - * * If 'utf8' is true, strings will be encoded with UTF-8 instead of * left in Java's native UTF-16. */ - explicit StringPool(bool sorted = false, bool utf8 = false); + explicit StringPool(bool utf8 = false); /** * Add a new string to the pool. If mergeDuplicates is true, thenif @@ -80,27 +91,30 @@ public: * otherwise, or if the value doesn't already exist, a new entry is * created. * - * Returns the index in the entry array of the new string entry. Note that - * if this string pool is sorted, the returned index will not be valid - * when the pool is finally written. + * Returns the index in the entry array of the new string entry. */ - ssize_t add(const String16& value, bool mergeDuplicates = false); - - ssize_t add(const String16& value, const Vector& spans); + ssize_t add(const String16& value, bool mergeDuplicates = false, + const String8* configTypeName = NULL, const ResTable_config* config = NULL); - ssize_t add(const String16& ident, const String16& value, - bool mergeDuplicates = false); + ssize_t add(const String16& value, const Vector& spans, + const String8* configTypeName = NULL, const ResTable_config* config = NULL); status_t addStyleSpan(size_t idx, const String16& name, uint32_t start, uint32_t end); status_t addStyleSpans(size_t idx, const Vector& spans); status_t addStyleSpan(size_t idx, const entry_style_span& span); - size_t size() const; - - const entry& entryAt(size_t idx) const; + // Sort the contents of the string block by the configuration associated + // with each item. After doing this you can use mapOriginalPosToNewPos() + // to find out the new position given the position originally returned by + // add(). + void sortByConfig(); - size_t countIdentifiers() const; + // For use after sortByConfig() to map from the original position of + // a string to its new sorted position. + size_t mapOriginalPosToNewPos(size_t originalPos) const { + return mOriginalPosToNewPos.itemAt(originalPos); + } sp createStringBlock(); @@ -125,27 +139,45 @@ public: const Vector* offsetsForString(const String16& val) const; private: - const bool mSorted; + static int config_sort(void* state, const void* lhs, const void* rhs); + const bool mUTF8; - // Raw array of unique strings, in some arbitrary order. + + // The following data structures represent the actual structures + // that will be generated for the final string pool. + + // Raw array of unique strings, in some arbitrary order. This is the + // actual strings that appear in the final string pool, in the order + // that they will be written. Vector mEntries; // Array of indices into mEntries, in the order they were // added to the pool. This can be different than mEntries // if the same string was added multiple times (it will appear // once in mEntries, with multiple occurrences in this array). + // This is the lookup array that will be written for finding + // the string for each offset/position in the string pool. Vector mEntryArray; // Optional style span information associated with each index of // mEntryArray. Vector mEntryStyleArray; - // Mapping from indices in mEntryArray to indices in mValues. - Vector mEntryArrayToValues; + + // The following data structures are used for book-keeping as the + // string pool is constructed. + // Unique set of all the strings added to the pool, mapped to // the first index of mEntryArray where the value was added. DefaultKeyedVector mValues; - // Unique set of all (optional) identifiers of strings in the - // pool, mapping to indices in mEntries. - DefaultKeyedVector mIdents; + // This array maps from the original position a string was placed at + // in mEntryArray to its new position after being sorted with sortByConfig(). + Vector mOriginalPosToNewPos; +}; +// The entry types are trivially movable because all fields they contain, including +// the vectors and strings, are trivially movable. +namespace android { + ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry); + ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style_span); + ANDROID_TRIVIAL_MOVE_TRAIT(StringPool::entry_style); }; #endif