Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 2011-2013, International Business Machines Corporation and * |
michael@0 | 4 | * others. All Rights Reserved. * |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef __TZNAMES_IMPL_H__ |
michael@0 | 9 | #define __TZNAMES_IMPL_H__ |
michael@0 | 10 | |
michael@0 | 11 | |
michael@0 | 12 | /** |
michael@0 | 13 | * \file |
michael@0 | 14 | * \brief C++ API: TimeZoneNames object |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #include "unicode/utypes.h" |
michael@0 | 18 | |
michael@0 | 19 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 20 | |
michael@0 | 21 | #include "unicode/tznames.h" |
michael@0 | 22 | #include "unicode/ures.h" |
michael@0 | 23 | #include "unicode/locid.h" |
michael@0 | 24 | #include "uhash.h" |
michael@0 | 25 | #include "uvector.h" |
michael@0 | 26 | #include "umutex.h" |
michael@0 | 27 | |
michael@0 | 28 | U_NAMESPACE_BEGIN |
michael@0 | 29 | |
michael@0 | 30 | /* |
michael@0 | 31 | * ZNStringPool Pool of (UChar *) strings. Provides for sharing of repeated |
michael@0 | 32 | * zone strings. |
michael@0 | 33 | */ |
michael@0 | 34 | struct ZNStringPoolChunk; |
michael@0 | 35 | class U_I18N_API ZNStringPool: public UMemory { |
michael@0 | 36 | public: |
michael@0 | 37 | ZNStringPool(UErrorCode &status); |
michael@0 | 38 | ~ZNStringPool(); |
michael@0 | 39 | |
michael@0 | 40 | /* Get the pooled string that is equal to the supplied string s. |
michael@0 | 41 | * Copy the string into the pool if it is not already present. |
michael@0 | 42 | * |
michael@0 | 43 | * Life time of the returned string is that of the pool. |
michael@0 | 44 | */ |
michael@0 | 45 | const UChar *get(const UChar *s, UErrorCode &status); |
michael@0 | 46 | |
michael@0 | 47 | /* Get the pooled string that is equal to the supplied string s. |
michael@0 | 48 | * Copy the string into the pool if it is not already present. |
michael@0 | 49 | */ |
michael@0 | 50 | const UChar *get(const UnicodeString &s, UErrorCode &status); |
michael@0 | 51 | |
michael@0 | 52 | /* Adopt a string into the pool, without copying it. |
michael@0 | 53 | * Used for strings from resource bundles, which will persist without copying. |
michael@0 | 54 | */ |
michael@0 | 55 | const UChar *adopt(const UChar *s, UErrorCode &status); |
michael@0 | 56 | |
michael@0 | 57 | /* Freeze the string pool. Discards the hash table that is used |
michael@0 | 58 | * for looking up a string. All pointers to pooled strings remain valid. |
michael@0 | 59 | */ |
michael@0 | 60 | void freeze(); |
michael@0 | 61 | |
michael@0 | 62 | private: |
michael@0 | 63 | ZNStringPoolChunk *fChunks; |
michael@0 | 64 | UHashtable *fHash; |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | /* |
michael@0 | 68 | * Character node used by TextTrieMap |
michael@0 | 69 | */ |
michael@0 | 70 | struct CharacterNode { |
michael@0 | 71 | // No constructor or destructor. |
michael@0 | 72 | // We malloc and free an uninitalized array of CharacterNode objects |
michael@0 | 73 | // and clear and delete them ourselves. |
michael@0 | 74 | |
michael@0 | 75 | void clear(); |
michael@0 | 76 | void deleteValues(UObjectDeleter *valueDeleter); |
michael@0 | 77 | |
michael@0 | 78 | void addValue(void *value, UObjectDeleter *valueDeleter, UErrorCode &status); |
michael@0 | 79 | inline UBool hasValues() const; |
michael@0 | 80 | inline int32_t countValues() const; |
michael@0 | 81 | inline const void *getValue(int32_t index) const; |
michael@0 | 82 | |
michael@0 | 83 | void *fValues; // Union of one single value vs. UVector of values. |
michael@0 | 84 | UChar fCharacter; // UTF-16 code unit. |
michael@0 | 85 | uint16_t fFirstChild; // 0 if no children. |
michael@0 | 86 | uint16_t fNextSibling; // 0 terminates the list. |
michael@0 | 87 | UBool fHasValuesVector; |
michael@0 | 88 | UBool fPadding; |
michael@0 | 89 | |
michael@0 | 90 | // No value: fValues == NULL and fHasValuesVector == FALSE |
michael@0 | 91 | // One value: fValues == value and fHasValuesVector == FALSE |
michael@0 | 92 | // >=2 values: fValues == UVector of values and fHasValuesVector == TRUE |
michael@0 | 93 | }; |
michael@0 | 94 | |
michael@0 | 95 | inline UBool CharacterNode::hasValues() const { |
michael@0 | 96 | return (UBool)(fValues != NULL); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | inline int32_t CharacterNode::countValues() const { |
michael@0 | 100 | return |
michael@0 | 101 | fValues == NULL ? 0 : |
michael@0 | 102 | !fHasValuesVector ? 1 : |
michael@0 | 103 | ((const UVector *)fValues)->size(); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | inline const void *CharacterNode::getValue(int32_t index) const { |
michael@0 | 107 | if (!fHasValuesVector) { |
michael@0 | 108 | return fValues; // Assume index == 0. |
michael@0 | 109 | } else { |
michael@0 | 110 | return ((const UVector *)fValues)->elementAt(index); |
michael@0 | 111 | } |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | /* |
michael@0 | 115 | * Search result handler callback interface used by TextTrieMap search. |
michael@0 | 116 | */ |
michael@0 | 117 | class TextTrieMapSearchResultHandler : public UMemory { |
michael@0 | 118 | public: |
michael@0 | 119 | virtual UBool handleMatch(int32_t matchLength, |
michael@0 | 120 | const CharacterNode *node, UErrorCode& status) = 0; |
michael@0 | 121 | virtual ~TextTrieMapSearchResultHandler(); //added to avoid warning |
michael@0 | 122 | }; |
michael@0 | 123 | |
michael@0 | 124 | /** |
michael@0 | 125 | * TextTrieMap is a trie implementation for supporting |
michael@0 | 126 | * fast prefix match for the string key. |
michael@0 | 127 | */ |
michael@0 | 128 | class U_I18N_API TextTrieMap : public UMemory { |
michael@0 | 129 | public: |
michael@0 | 130 | TextTrieMap(UBool ignoreCase, UObjectDeleter *valeDeleter); |
michael@0 | 131 | virtual ~TextTrieMap(); |
michael@0 | 132 | |
michael@0 | 133 | void put(const UnicodeString &key, void *value, ZNStringPool &sp, UErrorCode &status); |
michael@0 | 134 | void put(const UChar*, void *value, UErrorCode &status); |
michael@0 | 135 | void search(const UnicodeString &text, int32_t start, |
michael@0 | 136 | TextTrieMapSearchResultHandler *handler, UErrorCode& status) const; |
michael@0 | 137 | int32_t isEmpty() const; |
michael@0 | 138 | |
michael@0 | 139 | private: |
michael@0 | 140 | UBool fIgnoreCase; |
michael@0 | 141 | CharacterNode *fNodes; |
michael@0 | 142 | int32_t fNodesCapacity; |
michael@0 | 143 | int32_t fNodesCount; |
michael@0 | 144 | |
michael@0 | 145 | UVector *fLazyContents; |
michael@0 | 146 | UBool fIsEmpty; |
michael@0 | 147 | UObjectDeleter *fValueDeleter; |
michael@0 | 148 | |
michael@0 | 149 | UBool growNodes(); |
michael@0 | 150 | CharacterNode* addChildNode(CharacterNode *parent, UChar c, UErrorCode &status); |
michael@0 | 151 | CharacterNode* getChildNode(CharacterNode *parent, UChar c) const; |
michael@0 | 152 | |
michael@0 | 153 | void putImpl(const UnicodeString &key, void *value, UErrorCode &status); |
michael@0 | 154 | void buildTrie(UErrorCode &status); |
michael@0 | 155 | void search(CharacterNode *node, const UnicodeString &text, int32_t start, |
michael@0 | 156 | int32_t index, TextTrieMapSearchResultHandler *handler, UErrorCode &status) const; |
michael@0 | 157 | }; |
michael@0 | 158 | |
michael@0 | 159 | |
michael@0 | 160 | |
michael@0 | 161 | class ZNames; |
michael@0 | 162 | class TZNames; |
michael@0 | 163 | class TextTrieMap; |
michael@0 | 164 | |
michael@0 | 165 | class TimeZoneNamesImpl : public TimeZoneNames { |
michael@0 | 166 | public: |
michael@0 | 167 | TimeZoneNamesImpl(const Locale& locale, UErrorCode& status); |
michael@0 | 168 | |
michael@0 | 169 | virtual ~TimeZoneNamesImpl(); |
michael@0 | 170 | |
michael@0 | 171 | virtual UBool operator==(const TimeZoneNames& other) const; |
michael@0 | 172 | virtual TimeZoneNames* clone() const; |
michael@0 | 173 | |
michael@0 | 174 | StringEnumeration* getAvailableMetaZoneIDs(UErrorCode& status) const; |
michael@0 | 175 | StringEnumeration* getAvailableMetaZoneIDs(const UnicodeString& tzID, UErrorCode& status) const; |
michael@0 | 176 | |
michael@0 | 177 | UnicodeString& getMetaZoneID(const UnicodeString& tzID, UDate date, UnicodeString& mzID) const; |
michael@0 | 178 | UnicodeString& getReferenceZoneID(const UnicodeString& mzID, const char* region, UnicodeString& tzID) const; |
michael@0 | 179 | |
michael@0 | 180 | UnicodeString& getMetaZoneDisplayName(const UnicodeString& mzID, UTimeZoneNameType type, UnicodeString& name) const; |
michael@0 | 181 | UnicodeString& getTimeZoneDisplayName(const UnicodeString& tzID, UTimeZoneNameType type, UnicodeString& name) const; |
michael@0 | 182 | |
michael@0 | 183 | UnicodeString& getExemplarLocationName(const UnicodeString& tzID, UnicodeString& name) const; |
michael@0 | 184 | |
michael@0 | 185 | TimeZoneNames::MatchInfoCollection* find(const UnicodeString& text, int32_t start, uint32_t types, UErrorCode& status) const; |
michael@0 | 186 | |
michael@0 | 187 | static UnicodeString& getDefaultExemplarLocationName(const UnicodeString& tzID, UnicodeString& name); |
michael@0 | 188 | |
michael@0 | 189 | private: |
michael@0 | 190 | |
michael@0 | 191 | Locale fLocale; |
michael@0 | 192 | |
michael@0 | 193 | UResourceBundle* fZoneStrings; |
michael@0 | 194 | |
michael@0 | 195 | UHashtable* fTZNamesMap; |
michael@0 | 196 | UHashtable* fMZNamesMap; |
michael@0 | 197 | |
michael@0 | 198 | UBool fNamesTrieFullyLoaded; |
michael@0 | 199 | TextTrieMap fNamesTrie; |
michael@0 | 200 | |
michael@0 | 201 | void initialize(const Locale& locale, UErrorCode& status); |
michael@0 | 202 | void cleanup(); |
michael@0 | 203 | |
michael@0 | 204 | void loadStrings(const UnicodeString& tzCanonicalID); |
michael@0 | 205 | |
michael@0 | 206 | ZNames* loadMetaZoneNames(const UnicodeString& mzId); |
michael@0 | 207 | TZNames* loadTimeZoneNames(const UnicodeString& mzId); |
michael@0 | 208 | }; |
michael@0 | 209 | |
michael@0 | 210 | U_NAMESPACE_END |
michael@0 | 211 | |
michael@0 | 212 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
michael@0 | 213 | |
michael@0 | 214 | #endif // __TZNAMES_IMPL_H__ |
michael@0 | 215 | //eof |
michael@0 | 216 | // |