Path: blob/master/libs/icui18n/collationfastlatinbuilder.cpp
12343 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4* Copyright (C) 2013-2015, International Business Machines5* Corporation and others. All Rights Reserved.6*******************************************************************************7* collationfastlatinbuilder.cpp8*9* created on: 2013aug0910* created by: Markus W. Scherer11*/1213#define DEBUG_COLLATION_FAST_LATIN_BUILDER 0 // 0 or 1 or 214#if DEBUG_COLLATION_FAST_LATIN_BUILDER15#include <stdio.h>16#include <string>17#endif1819#include "unicode/utypes.h"2021#if !UCONFIG_NO_COLLATION2223#include "unicode/ucol.h"24#include "unicode/ucharstrie.h"25#include "unicode/unistr.h"26#include "unicode/uobject.h"27#include "unicode/uscript.h"28#include "cmemory.h"29#include "collation.h"30#include "collationdata.h"31#include "collationfastlatin.h"32#include "collationfastlatinbuilder.h"33#include "uassert.h"34#include "uvectr64.h"3536U_NAMESPACE_BEGIN3738struct CollationData;3940namespace {4142/**43* Compare two signed int64_t values as if they were unsigned.44*/45int32_t46compareInt64AsUnsigned(int64_t a, int64_t b) {47if((uint64_t)a < (uint64_t)b) {48return -1;49} else if((uint64_t)a > (uint64_t)b) {50return 1;51} else {52return 0;53}54}5556// TODO: Merge this with the near-identical version in collationbasedatabuilder.cpp57/**58* Like Java Collections.binarySearch(List, String, Comparator).59*60* @return the index>=0 where the item was found,61* or the index<0 for inserting the string at ~index in sorted order62*/63int32_t64binarySearch(const int64_t list[], int32_t limit, int64_t ce) {65if (limit == 0) { return ~0; }66int32_t start = 0;67for (;;) {68int32_t i = (start + limit) / 2;69int32_t cmp = compareInt64AsUnsigned(ce, list[i]);70if (cmp == 0) {71return i;72} else if (cmp < 0) {73if (i == start) {74return ~start; // insert ce before i75}76limit = i;77} else {78if (i == start) {79return ~(start + 1); // insert ce after i80}81start = i;82}83}84}8586} // namespace8788CollationFastLatinBuilder::CollationFastLatinBuilder(UErrorCode &errorCode)89: ce0(0), ce1(0),90contractionCEs(errorCode), uniqueCEs(errorCode),91miniCEs(NULL),92firstDigitPrimary(0), firstLatinPrimary(0), lastLatinPrimary(0),93firstShortPrimary(0), shortPrimaryOverflow(false),94headerLength(0) {95}9697CollationFastLatinBuilder::~CollationFastLatinBuilder() {98uprv_free(miniCEs);99}100101UBool102CollationFastLatinBuilder::forData(const CollationData &data, UErrorCode &errorCode) {103if(U_FAILURE(errorCode)) { return false; }104if(!result.isEmpty()) { // This builder is not reusable.105errorCode = U_INVALID_STATE_ERROR;106return false;107}108if(!loadGroups(data, errorCode)) { return false; }109110// Fast handling of digits.111firstShortPrimary = firstDigitPrimary;112getCEs(data, errorCode);113if(!encodeUniqueCEs(errorCode)) { return false; }114if(shortPrimaryOverflow) {115// Give digits long mini primaries,116// so that there are more short primaries for letters.117firstShortPrimary = firstLatinPrimary;118resetCEs();119getCEs(data, errorCode);120if(!encodeUniqueCEs(errorCode)) { return false; }121}122// Note: If we still have a short-primary overflow but not a long-primary overflow,123// then we could calculate how many more long primaries would fit,124// and set the firstShortPrimary to that many after the current firstShortPrimary,125// and try again.126// However, this might only benefit the en_US_POSIX tailoring,127// and it is simpler to suppress building fast Latin data for it in genrb,128// or by returning false here if shortPrimaryOverflow.129130UBool ok = !shortPrimaryOverflow &&131encodeCharCEs(errorCode) && encodeContractions(errorCode);132contractionCEs.removeAllElements(); // might reduce heap memory usage133uniqueCEs.removeAllElements();134return ok;135}136137UBool138CollationFastLatinBuilder::loadGroups(const CollationData &data, UErrorCode &errorCode) {139if(U_FAILURE(errorCode)) { return false; }140headerLength = 1 + NUM_SPECIAL_GROUPS;141uint32_t r0 = (CollationFastLatin::VERSION << 8) | headerLength;142result.append((UChar)r0);143// The first few reordering groups should be special groups144// (space, punct, ..., digit) followed by Latn, then Grek and other scripts.145for(int32_t i = 0; i < NUM_SPECIAL_GROUPS; ++i) {146lastSpecialPrimaries[i] = data.getLastPrimaryForGroup(UCOL_REORDER_CODE_FIRST + i);147if(lastSpecialPrimaries[i] == 0) {148// missing data149return false;150}151result.append((UChar)0); // reserve a slot for this group152}153154firstDigitPrimary = data.getFirstPrimaryForGroup(UCOL_REORDER_CODE_DIGIT);155firstLatinPrimary = data.getFirstPrimaryForGroup(USCRIPT_LATIN);156lastLatinPrimary = data.getLastPrimaryForGroup(USCRIPT_LATIN);157if(firstDigitPrimary == 0 || firstLatinPrimary == 0) {158// missing data159return false;160}161return true;162}163164UBool165CollationFastLatinBuilder::inSameGroup(uint32_t p, uint32_t q) const {166// Both or neither need to be encoded as short primaries,167// so that we can test only one and use the same bit mask.168if(p >= firstShortPrimary) {169return q >= firstShortPrimary;170} else if(q >= firstShortPrimary) {171return false;172}173// Both or neither must be potentially-variable,174// so that we can test only one and determine if both are variable.175uint32_t lastVariablePrimary = lastSpecialPrimaries[NUM_SPECIAL_GROUPS - 1];176if(p > lastVariablePrimary) {177return q > lastVariablePrimary;178} else if(q > lastVariablePrimary) {179return false;180}181// Both will be encoded with long mini primaries.182// They must be in the same special reordering group,183// so that we can test only one and determine if both are variable.184U_ASSERT(p != 0 && q != 0);185for(int32_t i = 0;; ++i) { // will terminate186uint32_t lastPrimary = lastSpecialPrimaries[i];187if(p <= lastPrimary) {188return q <= lastPrimary;189} else if(q <= lastPrimary) {190return false;191}192}193}194195void196CollationFastLatinBuilder::resetCEs() {197contractionCEs.removeAllElements();198uniqueCEs.removeAllElements();199shortPrimaryOverflow = false;200result.truncate(headerLength);201}202203void204CollationFastLatinBuilder::getCEs(const CollationData &data, UErrorCode &errorCode) {205if(U_FAILURE(errorCode)) { return; }206int32_t i = 0;207for(UChar c = 0;; ++i, ++c) {208if(c == CollationFastLatin::LATIN_LIMIT) {209c = CollationFastLatin::PUNCT_START;210} else if(c == CollationFastLatin::PUNCT_LIMIT) {211break;212}213const CollationData *d;214uint32_t ce32 = data.getCE32(c);215if(ce32 == Collation::FALLBACK_CE32) {216d = data.base;217ce32 = d->getCE32(c);218} else {219d = &data;220}221if(getCEsFromCE32(*d, c, ce32, errorCode)) {222charCEs[i][0] = ce0;223charCEs[i][1] = ce1;224addUniqueCE(ce0, errorCode);225addUniqueCE(ce1, errorCode);226} else {227// bail out for c228charCEs[i][0] = ce0 = Collation::NO_CE;229charCEs[i][1] = ce1 = 0;230}231if(c == 0 && !isContractionCharCE(ce0)) {232// Always map U+0000 to a contraction.233// Write a contraction list with only a default value if there is no real contraction.234U_ASSERT(contractionCEs.isEmpty());235addContractionEntry(CollationFastLatin::CONTR_CHAR_MASK, ce0, ce1, errorCode);236charCEs[0][0] = ((int64_t)Collation::NO_CE_PRIMARY << 32) | CONTRACTION_FLAG;237charCEs[0][1] = 0;238}239}240// Terminate the last contraction list.241contractionCEs.addElement(CollationFastLatin::CONTR_CHAR_MASK, errorCode);242}243244UBool245CollationFastLatinBuilder::getCEsFromCE32(const CollationData &data, UChar32 c, uint32_t ce32,246UErrorCode &errorCode) {247if(U_FAILURE(errorCode)) { return false; }248ce32 = data.getFinalCE32(ce32);249ce1 = 0;250if(Collation::isSimpleOrLongCE32(ce32)) {251ce0 = Collation::ceFromCE32(ce32);252} else {253switch(Collation::tagFromCE32(ce32)) {254case Collation::LATIN_EXPANSION_TAG:255ce0 = Collation::latinCE0FromCE32(ce32);256ce1 = Collation::latinCE1FromCE32(ce32);257break;258case Collation::EXPANSION32_TAG: {259const uint32_t *ce32s = data.ce32s + Collation::indexFromCE32(ce32);260int32_t length = Collation::lengthFromCE32(ce32);261if(length <= 2) {262ce0 = Collation::ceFromCE32(ce32s[0]);263if(length == 2) {264ce1 = Collation::ceFromCE32(ce32s[1]);265}266break;267} else {268return false;269}270}271case Collation::EXPANSION_TAG: {272const int64_t *ces = data.ces + Collation::indexFromCE32(ce32);273int32_t length = Collation::lengthFromCE32(ce32);274if(length <= 2) {275ce0 = ces[0];276if(length == 2) {277ce1 = ces[1];278}279break;280} else {281return false;282}283}284// Note: We could support PREFIX_TAG (assert c>=0)285// by recursing on its default CE32 and checking that none of the prefixes starts286// with a fast Latin character.287// However, currently (2013) there are only the L-before-middle-dot288// prefix mappings in the Latin range, and those would be rejected anyway.289case Collation::CONTRACTION_TAG:290U_ASSERT(c >= 0);291return getCEsFromContractionCE32(data, ce32, errorCode);292case Collation::OFFSET_TAG:293U_ASSERT(c >= 0);294ce0 = data.getCEFromOffsetCE32(c, ce32);295break;296default:297return false;298}299}300// A mapping can be completely ignorable.301if(ce0 == 0) { return ce1 == 0; }302// We do not support an ignorable ce0 unless it is completely ignorable.303uint32_t p0 = (uint32_t)(ce0 >> 32);304if(p0 == 0) { return false; }305// We only support primaries up to the Latin script.306if(p0 > lastLatinPrimary) { return false; }307// We support non-common secondary and case weights only together with short primaries.308uint32_t lower32_0 = (uint32_t)ce0;309if(p0 < firstShortPrimary) {310uint32_t sc0 = lower32_0 & Collation::SECONDARY_AND_CASE_MASK;311if(sc0 != Collation::COMMON_SECONDARY_CE) { return false; }312}313// No below-common tertiary weights.314if((lower32_0 & Collation::ONLY_TERTIARY_MASK) < Collation::COMMON_WEIGHT16) { return false; }315if(ce1 != 0) {316// Both primaries must be in the same group,317// or both must get short mini primaries,318// or a short-primary CE is followed by a secondary CE.319// This is so that we can test the first primary and use the same mask for both,320// and determine for both whether they are variable.321uint32_t p1 = (uint32_t)(ce1 >> 32);322if(p1 == 0 ? p0 < firstShortPrimary : !inSameGroup(p0, p1)) { return false; }323uint32_t lower32_1 = (uint32_t)ce1;324// No tertiary CEs.325if((lower32_1 >> 16) == 0) { return false; }326// We support non-common secondary and case weights327// only for secondary CEs or together with short primaries.328if(p1 != 0 && p1 < firstShortPrimary) {329uint32_t sc1 = lower32_1 & Collation::SECONDARY_AND_CASE_MASK;330if(sc1 != Collation::COMMON_SECONDARY_CE) { return false; }331}332// No below-common tertiary weights.333if((lower32_1 & Collation::ONLY_TERTIARY_MASK) < Collation::COMMON_WEIGHT16) { return false; }334}335// No quaternary weights.336if(((ce0 | ce1) & Collation::QUATERNARY_MASK) != 0) { return false; }337return true;338}339340UBool341CollationFastLatinBuilder::getCEsFromContractionCE32(const CollationData &data, uint32_t ce32,342UErrorCode &errorCode) {343if(U_FAILURE(errorCode)) { return false; }344const UChar *p = data.contexts + Collation::indexFromCE32(ce32);345ce32 = CollationData::readCE32(p); // Default if no suffix match.346// Since the original ce32 is not a prefix mapping,347// the default ce32 must not be another contraction.348U_ASSERT(!Collation::isContractionCE32(ce32));349int32_t contractionIndex = contractionCEs.size();350if(getCEsFromCE32(data, U_SENTINEL, ce32, errorCode)) {351addContractionEntry(CollationFastLatin::CONTR_CHAR_MASK, ce0, ce1, errorCode);352} else {353// Bail out for c-without-contraction.354addContractionEntry(CollationFastLatin::CONTR_CHAR_MASK, Collation::NO_CE, 0, errorCode);355}356// Handle an encodable contraction unless the next contraction is too long357// and starts with the same character.358int32_t prevX = -1;359UBool addContraction = false;360UCharsTrie::Iterator suffixes(p + 2, 0, errorCode);361while(suffixes.next(errorCode)) {362const UnicodeString &suffix = suffixes.getString();363int32_t x = CollationFastLatin::getCharIndex(suffix.charAt(0));364if(x < 0) { continue; } // ignore anything but fast Latin text365if(x == prevX) {366if(addContraction) {367// Bail out for all contractions starting with this character.368addContractionEntry(x, Collation::NO_CE, 0, errorCode);369addContraction = false;370}371continue;372}373if(addContraction) {374addContractionEntry(prevX, ce0, ce1, errorCode);375}376ce32 = (uint32_t)suffixes.getValue();377if(suffix.length() == 1 && getCEsFromCE32(data, U_SENTINEL, ce32, errorCode)) {378addContraction = true;379} else {380addContractionEntry(x, Collation::NO_CE, 0, errorCode);381addContraction = false;382}383prevX = x;384}385if(addContraction) {386addContractionEntry(prevX, ce0, ce1, errorCode);387}388if(U_FAILURE(errorCode)) { return false; }389// Note: There might not be any fast Latin contractions, but390// we need to enter contraction handling anyway so that we can bail out391// when there is a non-fast-Latin character following.392// For example: Danish &Y<<u+umlaut, when we compare Y vs. u\u0308 we need to see the393// following umlaut and bail out, rather than return the difference of Y vs. u.394ce0 = ((int64_t)Collation::NO_CE_PRIMARY << 32) | CONTRACTION_FLAG | contractionIndex;395ce1 = 0;396return true;397}398399void400CollationFastLatinBuilder::addContractionEntry(int32_t x, int64_t cce0, int64_t cce1,401UErrorCode &errorCode) {402contractionCEs.addElement(x, errorCode);403contractionCEs.addElement(cce0, errorCode);404contractionCEs.addElement(cce1, errorCode);405addUniqueCE(cce0, errorCode);406addUniqueCE(cce1, errorCode);407}408409void410CollationFastLatinBuilder::addUniqueCE(int64_t ce, UErrorCode &errorCode) {411if(U_FAILURE(errorCode)) { return; }412if(ce == 0 || (uint32_t)(ce >> 32) == Collation::NO_CE_PRIMARY) { return; }413ce &= ~(int64_t)Collation::CASE_MASK; // blank out case bits414int32_t i = binarySearch(uniqueCEs.getBuffer(), uniqueCEs.size(), ce);415if(i < 0) {416uniqueCEs.insertElementAt(ce, ~i, errorCode);417}418}419420uint32_t421CollationFastLatinBuilder::getMiniCE(int64_t ce) const {422ce &= ~(int64_t)Collation::CASE_MASK; // blank out case bits423int32_t index = binarySearch(uniqueCEs.getBuffer(), uniqueCEs.size(), ce);424U_ASSERT(index >= 0);425return miniCEs[index];426}427428UBool429CollationFastLatinBuilder::encodeUniqueCEs(UErrorCode &errorCode) {430if(U_FAILURE(errorCode)) { return false; }431uprv_free(miniCEs);432miniCEs = (uint16_t *)uprv_malloc(uniqueCEs.size() * 2);433if(miniCEs == NULL) {434errorCode = U_MEMORY_ALLOCATION_ERROR;435return false;436}437int32_t group = 0;438uint32_t lastGroupPrimary = lastSpecialPrimaries[group];439// The lowest unique CE must be at least a secondary CE.440U_ASSERT(((uint32_t)uniqueCEs.elementAti(0) >> 16) != 0);441uint32_t prevPrimary = 0;442uint32_t prevSecondary = 0;443uint32_t pri = 0;444uint32_t sec = 0;445uint32_t ter = CollationFastLatin::COMMON_TER;446for(int32_t i = 0; i < uniqueCEs.size(); ++i) {447int64_t ce = uniqueCEs.elementAti(i);448// Note: At least one of the p/s/t weights changes from one unique CE to the next.449// (uniqueCEs does not store case bits.)450uint32_t p = (uint32_t)(ce >> 32);451if(p != prevPrimary) {452while(p > lastGroupPrimary) {453U_ASSERT(pri <= CollationFastLatin::MAX_LONG);454// Set the group's header entry to the455// last "long primary" in or before the group.456result.setCharAt(1 + group, (UChar)pri);457if(++group < NUM_SPECIAL_GROUPS) {458lastGroupPrimary = lastSpecialPrimaries[group];459} else {460lastGroupPrimary = 0xffffffff;461break;462}463}464if(p < firstShortPrimary) {465if(pri == 0) {466pri = CollationFastLatin::MIN_LONG;467} else if(pri < CollationFastLatin::MAX_LONG) {468pri += CollationFastLatin::LONG_INC;469} else {470#if DEBUG_COLLATION_FAST_LATIN_BUILDER471printf("long-primary overflow for %08x\n", p);472#endif473miniCEs[i] = CollationFastLatin::BAIL_OUT;474continue;475}476} else {477if(pri < CollationFastLatin::MIN_SHORT) {478pri = CollationFastLatin::MIN_SHORT;479} else if(pri < (CollationFastLatin::MAX_SHORT - CollationFastLatin::SHORT_INC)) {480// Reserve the highest primary weight for U+FFFF.481pri += CollationFastLatin::SHORT_INC;482} else {483#if DEBUG_COLLATION_FAST_LATIN_BUILDER484printf("short-primary overflow for %08x\n", p);485#endif486shortPrimaryOverflow = true;487miniCEs[i] = CollationFastLatin::BAIL_OUT;488continue;489}490}491prevPrimary = p;492prevSecondary = Collation::COMMON_WEIGHT16;493sec = CollationFastLatin::COMMON_SEC;494ter = CollationFastLatin::COMMON_TER;495}496uint32_t lower32 = (uint32_t)ce;497uint32_t s = lower32 >> 16;498if(s != prevSecondary) {499if(pri == 0) {500if(sec == 0) {501sec = CollationFastLatin::MIN_SEC_HIGH;502} else if(sec < CollationFastLatin::MAX_SEC_HIGH) {503sec += CollationFastLatin::SEC_INC;504} else {505miniCEs[i] = CollationFastLatin::BAIL_OUT;506continue;507}508prevSecondary = s;509ter = CollationFastLatin::COMMON_TER;510} else if(s < Collation::COMMON_WEIGHT16) {511if(sec == CollationFastLatin::COMMON_SEC) {512sec = CollationFastLatin::MIN_SEC_BEFORE;513} else if(sec < CollationFastLatin::MAX_SEC_BEFORE) {514sec += CollationFastLatin::SEC_INC;515} else {516miniCEs[i] = CollationFastLatin::BAIL_OUT;517continue;518}519} else if(s == Collation::COMMON_WEIGHT16) {520sec = CollationFastLatin::COMMON_SEC;521} else {522if(sec < CollationFastLatin::MIN_SEC_AFTER) {523sec = CollationFastLatin::MIN_SEC_AFTER;524} else if(sec < CollationFastLatin::MAX_SEC_AFTER) {525sec += CollationFastLatin::SEC_INC;526} else {527miniCEs[i] = CollationFastLatin::BAIL_OUT;528continue;529}530}531prevSecondary = s;532ter = CollationFastLatin::COMMON_TER;533}534U_ASSERT((lower32 & Collation::CASE_MASK) == 0); // blanked out in uniqueCEs535uint32_t t = lower32 & Collation::ONLY_TERTIARY_MASK;536if(t > Collation::COMMON_WEIGHT16) {537if(ter < CollationFastLatin::MAX_TER_AFTER) {538++ter;539} else {540miniCEs[i] = CollationFastLatin::BAIL_OUT;541continue;542}543}544if(CollationFastLatin::MIN_LONG <= pri && pri <= CollationFastLatin::MAX_LONG) {545U_ASSERT(sec == CollationFastLatin::COMMON_SEC);546miniCEs[i] = (uint16_t)(pri | ter);547} else {548miniCEs[i] = (uint16_t)(pri | sec | ter);549}550}551#if DEBUG_COLLATION_FAST_LATIN_BUILDER552printf("last mini primary: %04x\n", pri);553#endif554#if DEBUG_COLLATION_FAST_LATIN_BUILDER >= 2555for(int32_t i = 0; i < uniqueCEs.size(); ++i) {556int64_t ce = uniqueCEs.elementAti(i);557printf("unique CE 0x%016lx -> 0x%04x\n", ce, miniCEs[i]);558}559#endif560return U_SUCCESS(errorCode);561}562563UBool564CollationFastLatinBuilder::encodeCharCEs(UErrorCode &errorCode) {565if(U_FAILURE(errorCode)) { return false; }566int32_t miniCEsStart = result.length();567for(int32_t i = 0; i < CollationFastLatin::NUM_FAST_CHARS; ++i) {568result.append((UChar)0); // initialize to completely ignorable569}570int32_t indexBase = result.length();571for(int32_t i = 0; i < CollationFastLatin::NUM_FAST_CHARS; ++i) {572int64_t ce = charCEs[i][0];573if(isContractionCharCE(ce)) { continue; } // defer contraction574uint32_t miniCE = encodeTwoCEs(ce, charCEs[i][1]);575if(miniCE > 0xffff) {576// Note: There is a chance that this new expansion is the same as a previous one,577// and if so, then we could reuse the other expansion.578// However, that seems unlikely.579int32_t expansionIndex = result.length() - indexBase;580if(expansionIndex > (int32_t)CollationFastLatin::INDEX_MASK) {581miniCE = CollationFastLatin::BAIL_OUT;582} else {583result.append((UChar)(miniCE >> 16)).append((UChar)miniCE);584miniCE = CollationFastLatin::EXPANSION | expansionIndex;585}586}587result.setCharAt(miniCEsStart + i, (UChar)miniCE);588}589return U_SUCCESS(errorCode);590}591592UBool593CollationFastLatinBuilder::encodeContractions(UErrorCode &errorCode) {594// We encode all contraction lists so that the first word of a list595// terminates the previous list, and we only need one additional terminator at the end.596if(U_FAILURE(errorCode)) { return false; }597int32_t indexBase = headerLength + CollationFastLatin::NUM_FAST_CHARS;598int32_t firstContractionIndex = result.length();599for(int32_t i = 0; i < CollationFastLatin::NUM_FAST_CHARS; ++i) {600int64_t ce = charCEs[i][0];601if(!isContractionCharCE(ce)) { continue; }602int32_t contractionIndex = result.length() - indexBase;603if(contractionIndex > (int32_t)CollationFastLatin::INDEX_MASK) {604result.setCharAt(headerLength + i, CollationFastLatin::BAIL_OUT);605continue;606}607UBool firstTriple = true;608for(int32_t index = (int32_t)ce & 0x7fffffff;; index += 3) {609int32_t x = static_cast<int32_t>(contractionCEs.elementAti(index));610if((uint32_t)x == CollationFastLatin::CONTR_CHAR_MASK && !firstTriple) { break; }611int64_t cce0 = contractionCEs.elementAti(index + 1);612int64_t cce1 = contractionCEs.elementAti(index + 2);613uint32_t miniCE = encodeTwoCEs(cce0, cce1);614if(miniCE == CollationFastLatin::BAIL_OUT) {615result.append((UChar)(x | (1 << CollationFastLatin::CONTR_LENGTH_SHIFT)));616} else if(miniCE <= 0xffff) {617result.append((UChar)(x | (2 << CollationFastLatin::CONTR_LENGTH_SHIFT)));618result.append((UChar)miniCE);619} else {620result.append((UChar)(x | (3 << CollationFastLatin::CONTR_LENGTH_SHIFT)));621result.append((UChar)(miniCE >> 16)).append((UChar)miniCE);622}623firstTriple = false;624}625// Note: There is a chance that this new contraction list is the same as a previous one,626// and if so, then we could truncate the result and reuse the other list.627// However, that seems unlikely.628result.setCharAt(headerLength + i,629(UChar)(CollationFastLatin::CONTRACTION | contractionIndex));630}631if(result.length() > firstContractionIndex) {632// Terminate the last contraction list.633result.append((UChar)CollationFastLatin::CONTR_CHAR_MASK);634}635if(result.isBogus()) {636errorCode = U_MEMORY_ALLOCATION_ERROR;637return false;638}639#if DEBUG_COLLATION_FAST_LATIN_BUILDER640printf("** fast Latin %d * 2 = %d bytes\n", result.length(), result.length() * 2);641puts(" header & below-digit groups map");642int32_t i = 0;643for(; i < headerLength; ++i) {644printf(" %04x", result[i]);645}646printf("\n char mini CEs");647U_ASSERT(CollationFastLatin::NUM_FAST_CHARS % 16 == 0);648for(; i < indexBase; i += 16) {649UChar32 c = i - headerLength;650if(c >= CollationFastLatin::LATIN_LIMIT) {651c = CollationFastLatin::PUNCT_START + c - CollationFastLatin::LATIN_LIMIT;652}653printf("\n %04x:", c);654for(int32_t j = 0; j < 16; ++j) {655printf(" %04x", result[i + j]);656}657}658printf("\n expansions & contractions");659for(; i < result.length(); ++i) {660if((i - indexBase) % 16 == 0) { puts(""); }661printf(" %04x", result[i]);662}663puts("");664#endif665return true;666}667668uint32_t669CollationFastLatinBuilder::encodeTwoCEs(int64_t first, int64_t second) const {670if(first == 0) {671return 0; // completely ignorable672}673if(first == Collation::NO_CE) {674return CollationFastLatin::BAIL_OUT;675}676U_ASSERT((uint32_t)(first >> 32) != Collation::NO_CE_PRIMARY);677678uint32_t miniCE = getMiniCE(first);679if(miniCE == CollationFastLatin::BAIL_OUT) { return miniCE; }680if(miniCE >= CollationFastLatin::MIN_SHORT) {681// Extract & copy the case bits.682// Shift them from normal CE bits 15..14 to mini CE bits 4..3.683uint32_t c = (((uint32_t)first & Collation::CASE_MASK) >> (14 - 3));684// Only in mini CEs: Ignorable case bits = 0, lowercase = 1.685c += CollationFastLatin::LOWER_CASE;686miniCE |= c;687}688if(second == 0) { return miniCE; }689690uint32_t miniCE1 = getMiniCE(second);691if(miniCE1 == CollationFastLatin::BAIL_OUT) { return miniCE1; }692693uint32_t case1 = (uint32_t)second & Collation::CASE_MASK;694if(miniCE >= CollationFastLatin::MIN_SHORT &&695(miniCE & CollationFastLatin::SECONDARY_MASK) == CollationFastLatin::COMMON_SEC) {696// Try to combine the two mini CEs into one.697uint32_t sec1 = miniCE1 & CollationFastLatin::SECONDARY_MASK;698uint32_t ter1 = miniCE1 & CollationFastLatin::TERTIARY_MASK;699if(sec1 >= CollationFastLatin::MIN_SEC_HIGH && case1 == 0 &&700ter1 == CollationFastLatin::COMMON_TER) {701// sec1>=sec_high implies pri1==0.702return (miniCE & ~CollationFastLatin::SECONDARY_MASK) | sec1;703}704}705706if(miniCE1 <= CollationFastLatin::SECONDARY_MASK || CollationFastLatin::MIN_SHORT <= miniCE1) {707// Secondary CE, or a CE with a short primary, copy the case bits.708case1 = (case1 >> (14 - 3)) + CollationFastLatin::LOWER_CASE;709miniCE1 |= case1;710}711return (miniCE << 16) | miniCE1;712}713714U_NAMESPACE_END715716#endif // !UCONFIG_NO_COLLATION717718719