X-Git-Url: https://git.saurik.com/android/aapt.git/blobdiff_plain/c54cad720473ab3280bdbcc7d74653dae4214154..71ea618365e88756ac7d573a7a2825ae0c265e03:/StringPool.cpp diff --git a/StringPool.cpp b/StringPool.cpp index fe88e37..839eda5 100644 --- a/StringPool.cpp +++ b/StringPool.cpp @@ -9,6 +9,7 @@ #include #include +#include #if HAVE_PRINTF_ZD # define ZD "%zd" @@ -71,14 +72,16 @@ String8 StringPool::entry::makeConfigsString() const { } int StringPool::entry::compare(const entry& o) const { - // Strings with styles go first, to reduce the size of the - // styles array. + // Strings with styles go first, to reduce the size of the styles array. + // We don't care about the relative order of these strings. if (hasStyles) { return o.hasStyles ? 0 : -1; } if (o.hasStyles) { return 1; } + + // Sort unstyled strings by type, then by logical configuration. int comp = configTypeName.compare(o.configTypeName); if (comp != 0) { return comp; @@ -98,39 +101,24 @@ int StringPool::entry::compare(const entry& o) const { return 0; } -StringPool::StringPool(bool sorted, bool utf8) - : mSorted(sorted), mUTF8(utf8), mValues(-1), mIdents(-1) -{ -} - -ssize_t StringPool::add(const String16& value, bool mergeDuplicates, - const String8* configTypeName, const ResTable_config* config) +StringPool::StringPool(bool utf8) : + mUTF8(utf8), mValues(-1) { - return add(String16(), value, mergeDuplicates, configTypeName, config); } ssize_t StringPool::add(const String16& value, const Vector& spans, const String8* configTypeName, const ResTable_config* config) { - ssize_t res = add(String16(), value, false, configTypeName, config); + ssize_t res = add(value, false, configTypeName, config); if (res >= 0) { addStyleSpans(res, spans); } return res; } -ssize_t StringPool::add(const String16& ident, const String16& value, +ssize_t StringPool::add(const String16& value, bool mergeDuplicates, const String8* configTypeName, const ResTable_config* config) { - if (ident.size() > 0) { - ssize_t idx = mIdents.valueFor(ident); - if (idx >= 0) { - fprintf(stderr, "ERROR: Duplicate string identifier %s\n", - String8(mEntries[idx].value).string()); - return UNKNOWN_ERROR; - } - } - ssize_t vidx = mValues.indexOfKey(value); ssize_t pos = vidx >= 0 ? mValues.valueAt(vidx) : -1; ssize_t eidx = pos >= 0 ? mEntryArray.itemAt(pos) : -1; @@ -174,19 +162,15 @@ ssize_t StringPool::add(const String16& ident, const String16& value, } const bool first = vidx < 0; - if (first || !mergeDuplicates) { + const bool styled = (pos >= 0 && (size_t)pos < mEntryStyleArray.size()) ? + mEntryStyleArray[pos].spans.size() : 0; + if (first || styled || !mergeDuplicates) { pos = mEntryArray.add(eidx); if (first) { vidx = mValues.add(value, pos); } - if (!mSorted) { - entry& ent = mEntries.editItemAt(eidx); - ent.indices.add(pos); - } - } - - if (ident.size() > 0) { - mIdents.add(ident, vidx); + entry& ent = mEntries.editItemAt(eidx); + ent.indices.add(pos); } NOISY(printf("Adding string %s to pool: pos=%d eidx=%d vidx=%d\n", @@ -219,8 +203,6 @@ status_t StringPool::addStyleSpans(size_t idx, const Vector& s status_t StringPool::addStyleSpan(size_t idx, const entry_style_span& span) { - LOG_ALWAYS_FATAL_IF(mSorted, "Can't use styles with sorted string pools."); - // Place blank entries in the span array up to this index. while (mEntryStyleArray.size() <= idx) { mEntryStyleArray.add(); @@ -232,37 +214,16 @@ status_t StringPool::addStyleSpan(size_t idx, const entry_style_span& span) return NO_ERROR; } -size_t StringPool::size() const -{ - return mSorted ? mValues.size() : mEntryArray.size(); -} - -const StringPool::entry& StringPool::entryAt(size_t idx) const -{ - if (!mSorted) { - return mEntries[mEntryArray[idx]]; - } else { - return mEntries[mEntryArray[mValues.valueAt(idx)]]; - } -} - -size_t StringPool::countIdentifiers() const -{ - return mIdents.size(); -} - -int StringPool::config_sort(const size_t* lhs, const size_t* rhs, void* state) +int StringPool::config_sort(void* state, const void* lhs, const void* rhs) { StringPool* pool = (StringPool*)state; - const entry& lhe = pool->mEntries[pool->mEntryArray[*lhs]]; - const entry& rhe = pool->mEntries[pool->mEntryArray[*rhs]]; + const entry& lhe = pool->mEntries[pool->mEntryArray[*static_cast(lhs)]]; + const entry& rhe = pool->mEntries[pool->mEntryArray[*static_cast(rhs)]]; return lhe.compare(rhe); } void StringPool::sortByConfig() { - LOG_ALWAYS_FATAL_IF(mSorted, "Can't sort string pool containing identifiers."); - LOG_ALWAYS_FATAL_IF(mIdents.size() > 0, "Can't sort string pool containing identifiers."); LOG_ALWAYS_FATAL_IF(mOriginalPosToNewPos.size() > 0, "Can't sort string pool after already sorted."); const size_t N = mEntryArray.size(); @@ -272,13 +233,17 @@ void StringPool::sortByConfig() // At that point it maps from the new position in the array to the // original position the entry appeared. Vector newPosToOriginalPos; - for (size_t i=0; i& pool) } } - const size_t ENTRIES = size(); + const size_t ENTRIES = mEntryArray.size(); // Now build the pool of unique strings. @@ -564,9 +529,6 @@ status_t StringPool::writeStringBlock(const sp& pool) header->header.size = htodl(pool->getSize()); header->stringCount = htodl(ENTRIES); header->styleCount = htodl(STYLES); - if (mSorted) { - header->flags |= htodl(ResStringPool_header::SORTED_FLAG); - } if (mUTF8) { header->flags |= htodl(ResStringPool_header::UTF8_FLAG); } @@ -576,33 +538,18 @@ status_t StringPool::writeStringBlock(const sp& pool) // Write string index array. uint32_t* index = (uint32_t*)(header+1); - if (mSorted) { - for (i=0; i(entryAt(i)); - ent.indices.clear(); - ent.indices.add(i); - *index++ = htodl(ent.offset); - } - } else { - for (i=0; i