X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/729e4ab9bc6618bc3d8a898e575df7f4019e29ca..a01113dcd0f39d5da295ef82785beff9ed86fe38:/icuSources/i18n/transreg.cpp diff --git a/icuSources/i18n/transreg.cpp b/icuSources/i18n/transreg.cpp index 8e1d4f43..5cc29625 100644 --- a/icuSources/i18n/transreg.cpp +++ b/icuSources/i18n/transreg.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** -* Copyright (c) 2001-2010, International Business Machines +* Copyright (c) 2001-2014, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Date Name Description @@ -44,8 +46,34 @@ static const UChar LOCALE_SEP = 95; // '_' //static const UChar VARIANT_SEP = 0x002F; // '/' // String constants -static const UChar NO_VARIANT[] = { 0 }; // empty string -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. @@ -164,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; } @@ -513,11 +540,18 @@ 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); - availableIDs.setDeleter(uhash_deleteUnicodeString); + 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); } @@ -726,7 +760,7 @@ int32_t TransliteratorRegistry::countAvailableSources(void) const { UnicodeString& TransliteratorRegistry::getAvailableSource(int32_t index, UnicodeString& result) const { - int32_t pos = -1; + int32_t pos = UHASH_FIRST; const UHashElement *e = 0; while (index-- >= 0) { e = specDAG.nextElement(pos); @@ -755,7 +789,7 @@ UnicodeString& TransliteratorRegistry::getAvailableTarget(int32_t index, result.truncate(0); // invalid source return result; } - int32_t pos = -1; + int32_t pos = UHASH_FIRST; const UHashElement *e = 0; while (index-- >= 0) { e = targets->nextElement(pos); @@ -777,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, @@ -791,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; } @@ -868,7 +916,7 @@ void TransliteratorRegistry::registerEntry(const UnicodeString& source, UnicodeString ID; UnicodeString s(source); if (s.length() == 0) { - s = ANY; + s.setTo(TRUE, ANY, 3); } TransliteratorIDParser::STVtoID(source, target, variant, ID); registerEntry(ID, s, target, variant, adopted, visible); @@ -907,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 { @@ -920,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, @@ -932,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(uhash_deleteUObject); specDAG.put(source, targets, status); } - UVector *variants = (UVector*) targets->get(target); - if (variants == 0) { - variants = new UVector(uhash_deleteUnicodeString, - 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); } /** @@ -975,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 @@ -1073,9 +1125,9 @@ TransliteratorEntry* TransliteratorRegistry::findInBundle(const TransliteratorSp // but must be consistent and documented. if (pass == 0) { utag.append(direction == UTRANS_FORWARD ? - TRANSLITERATE_TO : TRANSLITERATE_FROM); + TRANSLITERATE_TO : TRANSLITERATE_FROM, -1); } else { - utag.append(TRANSLITERATE); + utag.append(TRANSLITERATE, -1); } UnicodeString s(specToFind.get()); utag.append(s.toUpper("")); @@ -1277,17 +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++) { - Transliterator* t = new RuleBasedTransliterator(UnicodeString(CompoundTransliterator::PASS_STRING) + (passNumber++), + // TODO: Should passNumber be turned into a decimal-string representation (1 -> "1")? + 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; @@ -1348,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