X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b331163bffd790ced0e88b73f44f86d49ccc48a5..a01113dcd0f39d5da295ef82785beff9ed86fe38:/icuSources/i18n/transreg.cpp diff --git a/icuSources/i18n/transreg.cpp b/icuSources/i18n/transreg.cpp index 445a26ac..5cc29625 100644 --- a/icuSources/i18n/transreg.cpp +++ b/icuSources/i18n/transreg.cpp @@ -1,3 +1,5 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** * Copyright (c) 2001-2014, International Business Machines @@ -44,11 +46,35 @@ static const UChar LOCALE_SEP = 95; // '_' //static const UChar VARIANT_SEP = 0x002F; // '/' // String constants -static const UChar ANY[] = { 65, 110, 121, 0 }; // Any +static const UChar ANY[] = { 0x41, 0x6E, 0x79, 0 }; // Any +static const UChar LAT[] = { 0x4C, 0x61, 0x74, 0 }; // Lat // empty string #define NO_VARIANT UnicodeString() +// initial estimate for specDAG size +// ICU 60 Transliterator::countAvailableSources() +//#define SPECDAG_INIT_SIZE 149 +// Apple adjustment +#define SPECDAG_INIT_SIZE 134 + +// initial estimate for number of variant names +#define VARIANT_LIST_INIT_SIZE 11 +#define VARIANT_LIST_MAX_SIZE 31 + +// initial estimate for availableIDs count (default estimate is 8 => multiple reallocs) +// ICU 60 Transliterator::countAvailableIDs() +//#define AVAILABLE_IDS_INIT_SIZE 641 +// Apple adjustment +#define AVAILABLE_IDS_INIT_SIZE 493 + +// initial estimate for number of targets for source "Any", "Lat" +// ICU 60 Transliterator::countAvailableTargets("Any")/("Latn") +//#define ANY_TARGETS_INIT_SIZE 125 +// Apple adjustmennt +#define ANY_TARGETS_INIT_SIZE 102 +#define LAT_TARGETS_INIT_SIZE 23 + /** * Resource bundle key for the RuleBasedTransliterator rule. */ @@ -166,8 +192,7 @@ Transliterator* TransliteratorAlias::create(UParseError& pe, } break; case RULES: - U_ASSERT(FALSE); // don't call create() if isRuleBased() returns TRUE! - break; + UPRV_UNREACHABLE; // don't call create() if isRuleBased() returns TRUE! } return t; } @@ -515,10 +540,17 @@ U_CDECL_END TransliteratorRegistry::TransliteratorRegistry(UErrorCode& status) : registry(TRUE, status), - specDAG(TRUE, status), - availableIDs(status) + specDAG(TRUE, SPECDAG_INIT_SIZE, status), + variantList(VARIANT_LIST_INIT_SIZE, status), + availableIDs(AVAILABLE_IDS_INIT_SIZE, status) { registry.setValueDeleter(deleteEntry); + variantList.setDeleter(uprv_deleteUObject); + variantList.setComparer(uhash_compareCaselessUnicodeString); + UnicodeString *emptyString = new UnicodeString(); + if (emptyString != NULL) { + variantList.addElement(emptyString, status); + } availableIDs.setDeleter(uprv_deleteUObject); availableIDs.setComparer(uhash_compareCaselessUnicodeString); specDAG.setValueDeleter(uhash_deleteHashtable); @@ -779,9 +811,15 @@ int32_t TransliteratorRegistry::countAvailableVariants(const UnicodeString& sour if (targets == 0) { return 0; } - UVector *variants = (UVector*) targets->get(target); - // variants may be 0 if the source/target are invalid - return (variants == 0) ? 0 : variants->size(); + uint32_t varMask = targets->geti(target); + int32_t varCount = 0; + while (varMask > 0) { + if (varMask & 1) { + varCount++; + } + varMask >>= 1; + } + return varCount; } UnicodeString& TransliteratorRegistry::getAvailableVariant(int32_t index, @@ -793,17 +831,25 @@ UnicodeString& TransliteratorRegistry::getAvailableVariant(int32_t index, result.truncate(0); // invalid source return result; } - UVector *variants = (UVector*) targets->get(target); - if (variants == 0) { - result.truncate(0); // invalid target - return result; - } - UnicodeString *v = (UnicodeString*) variants->elementAt(index); - if (v == 0) { - result.truncate(0); // invalid index - } else { - result = *v; + uint32_t varMask = targets->geti(target); + int32_t varCount = 0; + int32_t varListIndex = 0; + while (varMask > 0) { + if (varMask & 1) { + if (varCount == index) { + UnicodeString *v = (UnicodeString*) variantList.elementAt(varListIndex); + if (v != NULL) { + result = *v; + return result; + } + break; + } + varCount++; + } + varMask >>= 1; + varListIndex++; } + result.truncate(0); // invalid target or index return result; } @@ -909,9 +955,9 @@ void TransliteratorRegistry::registerEntry(const UnicodeString& ID, UnicodeString *newID = (UnicodeString *)ID.clone(); // Check to make sure newID was created. if (newID != NULL) { - // NUL-terminate the ID string - newID->getTerminatedBuffer(); - availableIDs.addElement(newID, status); + // NUL-terminate the ID string + newID->getTerminatedBuffer(); + availableIDs.addElement(newID, status); } } } else { @@ -922,9 +968,7 @@ void TransliteratorRegistry::registerEntry(const UnicodeString& ID, /** * Register a source-target/variant in the specDAG. Variant may be - * empty, but source and target must not be. If variant is empty then - * the special variant NO_VARIANT is stored in slot zero of the - * UVector of variants. + * empty, but source and target must not be. */ void TransliteratorRegistry::registerSTV(const UnicodeString& source, const UnicodeString& target, @@ -934,39 +978,38 @@ void TransliteratorRegistry::registerSTV(const UnicodeString& source, UErrorCode status = U_ZERO_ERROR; Hashtable *targets = (Hashtable*) specDAG.get(source); if (targets == 0) { - targets = new Hashtable(TRUE, status); - if (U_FAILURE(status) || targets == 0) { + int32_t size = 3; + if (source.compare(ANY,3) == 0) { + size = ANY_TARGETS_INIT_SIZE; + } else if (source.compare(LAT,3) == 0) { + size = LAT_TARGETS_INIT_SIZE; + } + targets = new Hashtable(TRUE, size, status); + if (U_FAILURE(status) || targets == NULL) { return; } - targets->setValueDeleter(uprv_deleteUObject); specDAG.put(source, targets, status); } - UVector *variants = (UVector*) targets->get(target); - if (variants == 0) { - variants = new UVector(uprv_deleteUObject, - uhash_compareCaselessUnicodeString, status); - if (variants == 0) { + int32_t variantListIndex = variantList.indexOf((void*) &variant, 0); + if (variantListIndex < 0) { + if (variantList.size() >= VARIANT_LIST_MAX_SIZE) { + // can't handle any more variants return; } - targets->put(target, variants, status); - } - // assert(NO_VARIANT == ""); - // We add the variant string. If it is the special "no variant" - // string, that is, the empty string, we add it at position zero. - if (!variants->contains((void*) &variant)) { - UnicodeString *tempus; // Used for null pointer check. - if (variant.length() > 0) { - tempus = new UnicodeString(variant); - if (tempus != NULL) { - variants->addElement(tempus, status); - } - } else { - tempus = new UnicodeString(); // = NO_VARIANT - if (tempus != NULL) { - variants->insertElementAt(tempus, 0, status); - } + UnicodeString *variantEntry = new UnicodeString(variant); + if (variantEntry != NULL) { + variantList.addElement(variantEntry, status); + if (U_SUCCESS(status)) { + variantListIndex = variantList.size() - 1; + } + } + if (variantListIndex < 0) { + return; } } + uint32_t addMask = 1 << variantListIndex; + uint32_t varMask = targets->geti(target); + targets->puti(target, varMask | addMask, status); } /** @@ -977,17 +1020,24 @@ void TransliteratorRegistry::removeSTV(const UnicodeString& source, const UnicodeString& variant) { // assert(source.length() > 0); // assert(target.length() > 0); -// UErrorCode status = U_ZERO_ERROR; + UErrorCode status = U_ZERO_ERROR; Hashtable *targets = (Hashtable*) specDAG.get(source); - if (targets == 0) { + if (targets == NULL) { + return; // should never happen for valid s-t/v + } + uint32_t varMask = targets->geti(target); + if (varMask == 0) { return; // should never happen for valid s-t/v } - UVector *variants = (UVector*) targets->get(target); - if (variants == 0) { + int32_t variantListIndex = variantList.indexOf((void*) &variant, 0); + if (variantListIndex < 0) { return; // should never happen for valid s-t/v } - variants->removeElement((void*) &variant); - if (variants->size() == 0) { + int32_t remMask = 1 << variantListIndex; + varMask &= (~remMask); + if (varMask != 0) { + targets->puti(target, varMask, status); + } else { targets->remove(target); // should delete variants if (targets->count() == 0) { specDAG.remove(source); // should delete targets @@ -1279,18 +1329,18 @@ Transliterator* TransliteratorRegistry::instantiateEntry(const UnicodeString& ID UVector* rbts = new UVector(entry->u.dataVector->size(), status); // Check for null pointer if (rbts == NULL) { - status = U_MEMORY_ALLOCATION_ERROR; - return NULL; + status = U_MEMORY_ALLOCATION_ERROR; + return NULL; } int32_t passNumber = 1; for (int32_t i = 0; U_SUCCESS(status) && i < entry->u.dataVector->size(); i++) { // TODO: Should passNumber be turned into a decimal-string representation (1 -> "1")? - Transliterator* t = new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING) + UnicodeString(passNumber++), + Transliterator* tl = new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING) + UnicodeString(passNumber++), (TransliterationRuleData*)(entry->u.dataVector->elementAt(i)), FALSE); - if (t == 0) + if (tl == 0) status = U_MEMORY_ALLOCATION_ERROR; else - rbts->addElement(t, status); + rbts->addElement(tl, status); } if (U_FAILURE(status)) { delete rbts; @@ -1351,8 +1401,7 @@ Transliterator* TransliteratorRegistry::instantiateEntry(const UnicodeString& ID } return 0; default: - U_ASSERT(FALSE); // can't get here - return 0; + UPRV_UNREACHABLE; // can't get here } } U_NAMESPACE_END