diff options
Diffstat (limited to 'tools/aapt/StringPool.cpp')
-rw-r--r-- | tools/aapt/StringPool.cpp | 84 |
1 files changed, 54 insertions, 30 deletions
diff --git a/tools/aapt/StringPool.cpp b/tools/aapt/StringPool.cpp index 06769e4..e3ec0ae 100644 --- a/tools/aapt/StringPool.cpp +++ b/tools/aapt/StringPool.cpp @@ -3,25 +3,30 @@ // // Build resource files from raw assets. // - #include "StringPool.h" -#include "ResourceTable.h" #include <utils/ByteOrder.h> #include <utils/SortedVector.h> -#include "qsort_r_compat.h" +#include <algorithm> + +#include "ResourceTable.h" + +// SSIZE: mingw does not have signed size_t == ssize_t. #if HAVE_PRINTF_ZD # define ZD "%zd" # define ZD_TYPE ssize_t +# define SSIZE(x) x #else # define ZD "%ld" # define ZD_TYPE long +# define SSIZE(x) (signed size_t)x #endif -#define NOISY(x) //x +// Set to true for noisy debug output. +static const bool kIsDebug = false; -void strcpy16_htod(uint16_t* dst, const uint16_t* src) +void strcpy16_htod(char16_t* dst, const char16_t* src) { while (*src) { char16_t s = htods(*src); @@ -140,8 +145,10 @@ ssize_t StringPool::add(const String16& value, if (configTypeName != NULL) { entry& ent = mEntries.editItemAt(eidx); - NOISY(printf("*** adding config type name %s, was %s\n", - configTypeName->string(), ent.configTypeName.string())); + if (kIsDebug) { + printf("*** adding config type name %s, was %s\n", + configTypeName->string(), ent.configTypeName.string()); + } if (ent.configTypeName.size() <= 0) { ent.configTypeName = *configTypeName; } else if (ent.configTypeName != *configTypeName) { @@ -157,14 +164,18 @@ ssize_t StringPool::add(const String16& value, int cmp = ent.configs.itemAt(addPos).compareLogical(*config); if (cmp >= 0) { if (cmp > 0) { - NOISY(printf("*** inserting config: %s\n", config->toString().string())); + if (kIsDebug) { + printf("*** inserting config: %s\n", config->toString().string()); + } ent.configs.insertAt(*config, addPos); } break; } } if (addPos >= ent.configs.size()) { - NOISY(printf("*** adding config: %s\n", config->toString().string())); + if (kIsDebug) { + printf("*** adding config: %s\n", config->toString().string()); + } ent.configs.add(*config); } } @@ -181,9 +192,11 @@ ssize_t StringPool::add(const String16& value, ent.indices.add(pos); } - NOISY(printf("Adding string %s to pool: pos=%d eidx=%d vidx=%d\n", - String8(value).string(), pos, eidx, vidx)); - + if (kIsDebug) { + printf("Adding string %s to pool: pos=%zd eidx=%zd vidx=%zd\n", + String8(value).string(), SSIZE(pos), SSIZE(eidx), SSIZE(vidx)); + } + return pos; } @@ -222,12 +235,15 @@ status_t StringPool::addStyleSpan(size_t idx, const entry_style_span& span) return NO_ERROR; } -int StringPool::config_sort(void* state, const void* lhs, const void* rhs) +StringPool::ConfigSorter::ConfigSorter(const StringPool& pool) : pool(pool) +{ +} + +bool StringPool::ConfigSorter::operator()(size_t l, size_t r) { - StringPool* pool = (StringPool*)state; - const entry& lhe = pool->mEntries[pool->mEntryArray[*static_cast<const size_t*>(lhs)]]; - const entry& rhe = pool->mEntries[pool->mEntryArray[*static_cast<const size_t*>(rhs)]]; - return lhe.compare(rhe); + const StringPool::entry& lhe = pool.mEntries[pool.mEntryArray[l]]; + const StringPool::entry& rhe = pool.mEntries[pool.mEntryArray[r]]; + return lhe.compare(rhe) < 0; } void StringPool::sortByConfig() @@ -247,12 +263,14 @@ void StringPool::sortByConfig() } // Sort the array. - NOISY(printf("SORTING STRINGS BY CONFIGURATION...\n")); - // Vector::sort uses insertion sort, which is very slow for this data set. - // Use quicksort instead because we don't need a stable sort here. - qsort_r_compat(newPosToOriginalPos.editArray(), N, sizeof(size_t), this, config_sort); - //newPosToOriginalPos.sort(config_sort, this); - NOISY(printf("DONE SORTING STRINGS BY CONFIGURATION.\n")); + if (kIsDebug) { + printf("SORTING STRINGS BY CONFIGURATION...\n"); + } + ConfigSorter sorter(*this); + std::sort(newPosToOriginalPos.begin(), newPosToOriginalPos.end(), sorter); + if (kIsDebug) { + printf("DONE SORTING STRINGS BY CONFIGURATION.\n"); + } // Create the reverse mapping from the original position in the array // to the new position where it appears in the sorted array. This is @@ -455,9 +473,9 @@ status_t StringPool::writeStringBlock(const sp<AaptFile>& pool) strncpy((char*)strings, encStr, encSize+1); } else { - uint16_t* strings = (uint16_t*)dat; + char16_t* strings = (char16_t*)dat; - ENCODE_LENGTH(strings, sizeof(uint16_t), strSize) + ENCODE_LENGTH(strings, sizeof(char16_t), strSize) strcpy16_htod(strings, ent.value); } @@ -549,9 +567,13 @@ status_t StringPool::writeStringBlock(const sp<AaptFile>& pool) for (i=0; i<ENTRIES; i++) { entry& ent = mEntries.editItemAt(mEntryArray[i]); *index++ = htodl(ent.offset); - NOISY(printf("Writing entry #%d: \"%s\" ent=%d off=%d\n", i, - String8(ent.value).string(), - mEntryArray[i], ent.offset)); + if (kIsDebug) { + printf("Writing entry #%zu: \"%s\" ent=%zu off=%zu\n", + i, + String8(ent.value).string(), + mEntryArray[i], + ent.offset); + } } // Write style index array. @@ -567,8 +589,10 @@ ssize_t StringPool::offsetForString(const String16& val) const { const Vector<size_t>* indices = offsetsForString(val); ssize_t res = indices != NULL && indices->size() > 0 ? indices->itemAt(0) : -1; - NOISY(printf("Offset for string %s: %d (%s)\n", String8(val).string(), res, - res >= 0 ? String8(mEntries[mEntryArray[res]].value).string() : String8())); + if (kIsDebug) { + printf("Offset for string %s: %zd (%s)\n", String8(val).string(), SSIZE(res), + res >= 0 ? String8(mEntries[mEntryArray[res]].value).string() : String8()); + } return res; } |