Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ************************************************************************** |
michael@0 | 3 | * Copyright (c) 2002-2010, International Business Machines Corporation * |
michael@0 | 4 | * and others. All Rights Reserved. * |
michael@0 | 5 | ************************************************************************** |
michael@0 | 6 | * Date Name Description * |
michael@0 | 7 | * 01/28/2002 aliu Creation. * |
michael@0 | 8 | ************************************************************************** |
michael@0 | 9 | */ |
michael@0 | 10 | #ifndef TRIDPARS_H |
michael@0 | 11 | #define TRIDPARS_H |
michael@0 | 12 | |
michael@0 | 13 | #include "unicode/utypes.h" |
michael@0 | 14 | |
michael@0 | 15 | #if !UCONFIG_NO_TRANSLITERATION |
michael@0 | 16 | |
michael@0 | 17 | #include "unicode/uobject.h" |
michael@0 | 18 | #include "unicode/unistr.h" |
michael@0 | 19 | |
michael@0 | 20 | U_NAMESPACE_BEGIN |
michael@0 | 21 | |
michael@0 | 22 | class Transliterator; |
michael@0 | 23 | class UnicodeSet; |
michael@0 | 24 | class UVector; |
michael@0 | 25 | |
michael@0 | 26 | /** |
michael@0 | 27 | * Parsing component for transliterator IDs. This class contains only |
michael@0 | 28 | * static members; it cannot be instantiated. Methods in this class |
michael@0 | 29 | * parse various ID formats, including the following: |
michael@0 | 30 | * |
michael@0 | 31 | * A basic ID, which contains source, target, and variant, but no |
michael@0 | 32 | * filter and no explicit inverse. Examples include |
michael@0 | 33 | * "Latin-Greek/UNGEGN" and "Null". |
michael@0 | 34 | * |
michael@0 | 35 | * A single ID, which is a basic ID plus optional filter and optional |
michael@0 | 36 | * explicit inverse. Examples include "[a-zA-Z] Latin-Greek" and |
michael@0 | 37 | * "Lower (Upper)". |
michael@0 | 38 | * |
michael@0 | 39 | * A compound ID, which is a sequence of one or more single IDs, |
michael@0 | 40 | * separated by semicolons, with optional forward and reverse global |
michael@0 | 41 | * filters. The global filters are UnicodeSet patterns prepended or |
michael@0 | 42 | * appended to the IDs, separated by semicolons. An appended filter |
michael@0 | 43 | * must be enclosed in parentheses and applies in the reverse |
michael@0 | 44 | * direction. |
michael@0 | 45 | * |
michael@0 | 46 | * @author Alan Liu |
michael@0 | 47 | */ |
michael@0 | 48 | class TransliteratorIDParser /* not : public UObject because all methods are static */ { |
michael@0 | 49 | |
michael@0 | 50 | public: |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * A structure containing the parsed data of a filtered ID, that |
michael@0 | 54 | * is, a basic ID optionally with a filter. |
michael@0 | 55 | * |
michael@0 | 56 | * 'source' and 'target' will always be non-null. The 'variant' |
michael@0 | 57 | * will be non-null only if a non-empty variant was parsed. |
michael@0 | 58 | * |
michael@0 | 59 | * 'sawSource' is true if there was an explicit source in the |
michael@0 | 60 | * parsed id. If there was no explicit source, then an implied |
michael@0 | 61 | * source of ANY is returned and 'sawSource' is set to false. |
michael@0 | 62 | * |
michael@0 | 63 | * 'filter' is the parsed filter pattern, or null if there was no |
michael@0 | 64 | * filter. |
michael@0 | 65 | */ |
michael@0 | 66 | class Specs : public UMemory { |
michael@0 | 67 | public: |
michael@0 | 68 | UnicodeString source; // not null |
michael@0 | 69 | UnicodeString target; // not null |
michael@0 | 70 | UnicodeString variant; // may be null |
michael@0 | 71 | UnicodeString filter; // may be null |
michael@0 | 72 | UBool sawSource; |
michael@0 | 73 | Specs(const UnicodeString& s, const UnicodeString& t, |
michael@0 | 74 | const UnicodeString& v, UBool sawS, |
michael@0 | 75 | const UnicodeString& f); |
michael@0 | 76 | |
michael@0 | 77 | private: |
michael@0 | 78 | |
michael@0 | 79 | Specs(const Specs &other); // forbid copying of this class |
michael@0 | 80 | Specs &operator=(const Specs &other); // forbid copying of this class |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * A structure containing the canonicalized data of a filtered ID, |
michael@0 | 85 | * that is, a basic ID optionally with a filter. |
michael@0 | 86 | * |
michael@0 | 87 | * 'canonID' is always non-null. It may be the empty string "". |
michael@0 | 88 | * It is the id that should be assigned to the created |
michael@0 | 89 | * transliterator. It _cannot_ be instantiated directly. |
michael@0 | 90 | * |
michael@0 | 91 | * 'basicID' is always non-null and non-empty. It is always of |
michael@0 | 92 | * the form S-T or S-T/V. It is designed to be fed to low-level |
michael@0 | 93 | * instantiation code that only understands these two formats. |
michael@0 | 94 | * |
michael@0 | 95 | * 'filter' may be null, if there is none, or non-null and |
michael@0 | 96 | * non-empty. |
michael@0 | 97 | */ |
michael@0 | 98 | class SingleID : public UMemory { |
michael@0 | 99 | public: |
michael@0 | 100 | UnicodeString canonID; |
michael@0 | 101 | UnicodeString basicID; |
michael@0 | 102 | UnicodeString filter; |
michael@0 | 103 | SingleID(const UnicodeString& c, const UnicodeString& b, |
michael@0 | 104 | const UnicodeString& f); |
michael@0 | 105 | SingleID(const UnicodeString& c, const UnicodeString& b); |
michael@0 | 106 | Transliterator* createInstance(); |
michael@0 | 107 | |
michael@0 | 108 | private: |
michael@0 | 109 | |
michael@0 | 110 | SingleID(const SingleID &other); // forbid copying of this class |
michael@0 | 111 | SingleID &operator=(const SingleID &other); // forbid copying of this class |
michael@0 | 112 | }; |
michael@0 | 113 | |
michael@0 | 114 | /** |
michael@0 | 115 | * Parse a filter ID, that is, an ID of the general form |
michael@0 | 116 | * "[f1] s1-t1/v1", with the filters optional, and the variants optional. |
michael@0 | 117 | * @param id the id to be parsed |
michael@0 | 118 | * @param pos INPUT-OUTPUT parameter. On input, the position of |
michael@0 | 119 | * the first character to parse. On output, the position after |
michael@0 | 120 | * the last character parsed. |
michael@0 | 121 | * @return a SingleID object or null if the parse fails |
michael@0 | 122 | */ |
michael@0 | 123 | static SingleID* parseFilterID(const UnicodeString& id, int32_t& pos); |
michael@0 | 124 | |
michael@0 | 125 | /** |
michael@0 | 126 | * Parse a single ID, that is, an ID of the general form |
michael@0 | 127 | * "[f1] s1-t1/v1 ([f2] s2-t3/v2)", with the parenthesized element |
michael@0 | 128 | * optional, the filters optional, and the variants optional. |
michael@0 | 129 | * @param id the id to be parsed |
michael@0 | 130 | * @param pos INPUT-OUTPUT parameter. On input, the position of |
michael@0 | 131 | * the first character to parse. On output, the position after |
michael@0 | 132 | * the last character parsed. |
michael@0 | 133 | * @param dir the direction. If the direction is REVERSE then the |
michael@0 | 134 | * SingleID is constructed for the reverse direction. |
michael@0 | 135 | * @return a SingleID object or null |
michael@0 | 136 | */ |
michael@0 | 137 | static SingleID* parseSingleID(const UnicodeString& id, int32_t& pos, |
michael@0 | 138 | int32_t dir, UErrorCode& status); |
michael@0 | 139 | |
michael@0 | 140 | /** |
michael@0 | 141 | * Parse a global filter of the form "[f]" or "([f])", depending |
michael@0 | 142 | * on 'withParens'. |
michael@0 | 143 | * @param id the pattern the parse |
michael@0 | 144 | * @param pos INPUT-OUTPUT parameter. On input, the position of |
michael@0 | 145 | * the first character to parse. On output, the position after |
michael@0 | 146 | * the last character parsed. |
michael@0 | 147 | * @param dir the direction. |
michael@0 | 148 | * @param withParens INPUT-OUTPUT parameter. On entry, if |
michael@0 | 149 | * withParens[0] is 0, then parens are disallowed. If it is 1, |
michael@0 | 150 | * then parens are required. If it is -1, then parens are |
michael@0 | 151 | * optional, and the return result will be set to 0 or 1. |
michael@0 | 152 | * @param canonID OUTPUT parameter. The pattern for the filter |
michael@0 | 153 | * added to the canonID, either at the end, if dir is FORWARD, or |
michael@0 | 154 | * at the start, if dir is REVERSE. The pattern will be enclosed |
michael@0 | 155 | * in parentheses if appropriate, and will be suffixed with an |
michael@0 | 156 | * ID_DELIM character. May be null. |
michael@0 | 157 | * @return a UnicodeSet object or null. A non-null results |
michael@0 | 158 | * indicates a successful parse, regardless of whether the filter |
michael@0 | 159 | * applies to the given direction. The caller should discard it |
michael@0 | 160 | * if withParens != (dir == REVERSE). |
michael@0 | 161 | */ |
michael@0 | 162 | static UnicodeSet* parseGlobalFilter(const UnicodeString& id, int32_t& pos, |
michael@0 | 163 | int32_t dir, |
michael@0 | 164 | int32_t& withParens, |
michael@0 | 165 | UnicodeString* canonID); |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * Parse a compound ID, consisting of an optional forward global |
michael@0 | 169 | * filter, a separator, one or more single IDs delimited by |
michael@0 | 170 | * separators, an an optional reverse global filter. The |
michael@0 | 171 | * separator is a semicolon. The global filters are UnicodeSet |
michael@0 | 172 | * patterns. The reverse global filter must be enclosed in |
michael@0 | 173 | * parentheses. |
michael@0 | 174 | * @param id the pattern the parse |
michael@0 | 175 | * @param dir the direction. |
michael@0 | 176 | * @param canonID OUTPUT parameter that receives the canonical ID, |
michael@0 | 177 | * consisting of canonical IDs for all elements, as returned by |
michael@0 | 178 | * parseSingleID(), separated by semicolons. Previous contents |
michael@0 | 179 | * are discarded. |
michael@0 | 180 | * @param list OUTPUT parameter that receives a list of SingleID |
michael@0 | 181 | * objects representing the parsed IDs. Previous contents are |
michael@0 | 182 | * discarded. |
michael@0 | 183 | * @param globalFilter OUTPUT parameter that receives a pointer to |
michael@0 | 184 | * a newly created global filter for this ID in this direction, or |
michael@0 | 185 | * null if there is none. |
michael@0 | 186 | * @return true if the parse succeeds, that is, if the entire |
michael@0 | 187 | * id is consumed without syntax error. |
michael@0 | 188 | */ |
michael@0 | 189 | static UBool parseCompoundID(const UnicodeString& id, int32_t dir, |
michael@0 | 190 | UnicodeString& canonID, |
michael@0 | 191 | UVector& list, |
michael@0 | 192 | UnicodeSet*& globalFilter); |
michael@0 | 193 | |
michael@0 | 194 | /** |
michael@0 | 195 | * Convert the elements of the 'list' vector, which are SingleID |
michael@0 | 196 | * objects, into actual Transliterator objects. In the course of |
michael@0 | 197 | * this, some (or all) entries may be removed. If all entries |
michael@0 | 198 | * are removed, the Null transliterator will be added. |
michael@0 | 199 | * |
michael@0 | 200 | * Delete entries with empty basicIDs; these are generated by |
michael@0 | 201 | * elements like "(A)" in the forward direction, or "A()" in |
michael@0 | 202 | * the reverse. THIS MAY RESULT IN AN EMPTY VECTOR. Convert |
michael@0 | 203 | * SingleID entries to actual transliterators. |
michael@0 | 204 | * |
michael@0 | 205 | * @param list vector of SingleID objects. On exit, vector |
michael@0 | 206 | * of one or more Transliterators. |
michael@0 | 207 | * @param ec Output param to receive a success or an error code. |
michael@0 | 208 | * @return new value of insertIndex. The index will shift if |
michael@0 | 209 | * there are empty items, like "(Lower)", with indices less than |
michael@0 | 210 | * insertIndex. |
michael@0 | 211 | */ |
michael@0 | 212 | static void instantiateList(UVector& list, |
michael@0 | 213 | UErrorCode& ec); |
michael@0 | 214 | |
michael@0 | 215 | /** |
michael@0 | 216 | * Parse an ID into pieces. Take IDs of the form T, T/V, S-T, |
michael@0 | 217 | * S-T/V, or S/V-T. If the source is missing, return a source of |
michael@0 | 218 | * ANY. |
michael@0 | 219 | * @param id the id string, in any of several forms |
michael@0 | 220 | * @param source the given source. |
michael@0 | 221 | * @param target the given target. |
michael@0 | 222 | * @param variant the given variant |
michael@0 | 223 | * @param isSourcePresent If TRUE then the source is present. |
michael@0 | 224 | * If the source is not present, ANY will be |
michael@0 | 225 | * given as the source, and isSourcePresent will be null |
michael@0 | 226 | * @return an array of 4 strings: source, target, variant, and |
michael@0 | 227 | * isSourcePresent. If the source is not present, ANY will be |
michael@0 | 228 | * given as the source, and isSourcePresent will be null. Otherwise |
michael@0 | 229 | * isSourcePresent will be non-null. The target may be empty if the |
michael@0 | 230 | * id is not well-formed. The variant may be empty. |
michael@0 | 231 | */ |
michael@0 | 232 | static void IDtoSTV(const UnicodeString& id, |
michael@0 | 233 | UnicodeString& source, |
michael@0 | 234 | UnicodeString& target, |
michael@0 | 235 | UnicodeString& variant, |
michael@0 | 236 | UBool& isSourcePresent); |
michael@0 | 237 | |
michael@0 | 238 | /** |
michael@0 | 239 | * Given source, target, and variant strings, concatenate them into a |
michael@0 | 240 | * full ID. If the source is empty, then "Any" will be used for the |
michael@0 | 241 | * source, so the ID will always be of the form s-t/v or s-t. |
michael@0 | 242 | */ |
michael@0 | 243 | static void STVtoID(const UnicodeString& source, |
michael@0 | 244 | const UnicodeString& target, |
michael@0 | 245 | const UnicodeString& variant, |
michael@0 | 246 | UnicodeString& id); |
michael@0 | 247 | |
michael@0 | 248 | /** |
michael@0 | 249 | * Register two targets as being inverses of one another. For |
michael@0 | 250 | * example, calling registerSpecialInverse("NFC", "NFD", true) causes |
michael@0 | 251 | * Transliterator to form the following inverse relationships: |
michael@0 | 252 | * |
michael@0 | 253 | * <pre>NFC => NFD |
michael@0 | 254 | * Any-NFC => Any-NFD |
michael@0 | 255 | * NFD => NFC |
michael@0 | 256 | * Any-NFD => Any-NFC</pre> |
michael@0 | 257 | * |
michael@0 | 258 | * (Without the special inverse registration, the inverse of NFC |
michael@0 | 259 | * would be NFC-Any.) Note that NFD is shorthand for Any-NFD, but |
michael@0 | 260 | * that the presence or absence of "Any-" is preserved. |
michael@0 | 261 | * |
michael@0 | 262 | * <p>The relationship is symmetrical; registering (a, b) is |
michael@0 | 263 | * equivalent to registering (b, a). |
michael@0 | 264 | * |
michael@0 | 265 | * <p>The relevant IDs must still be registered separately as |
michael@0 | 266 | * factories or classes. |
michael@0 | 267 | * |
michael@0 | 268 | * <p>Only the targets are specified. Special inverses always |
michael@0 | 269 | * have the form Any-Target1 <=> Any-Target2. The target should |
michael@0 | 270 | * have canonical casing (the casing desired to be produced when |
michael@0 | 271 | * an inverse is formed) and should contain no whitespace or other |
michael@0 | 272 | * extraneous characters. |
michael@0 | 273 | * |
michael@0 | 274 | * @param target the target against which to register the inverse |
michael@0 | 275 | * @param inverseTarget the inverse of target, that is |
michael@0 | 276 | * Any-target.getInverse() => Any-inverseTarget |
michael@0 | 277 | * @param bidirectional if true, register the reverse relation |
michael@0 | 278 | * as well, that is, Any-inverseTarget.getInverse() => Any-target |
michael@0 | 279 | */ |
michael@0 | 280 | static void registerSpecialInverse(const UnicodeString& target, |
michael@0 | 281 | const UnicodeString& inverseTarget, |
michael@0 | 282 | UBool bidirectional, |
michael@0 | 283 | UErrorCode &status); |
michael@0 | 284 | |
michael@0 | 285 | /** |
michael@0 | 286 | * Free static memory. |
michael@0 | 287 | */ |
michael@0 | 288 | static void cleanup(); |
michael@0 | 289 | |
michael@0 | 290 | private: |
michael@0 | 291 | //---------------------------------------------------------------- |
michael@0 | 292 | // Private implementation |
michael@0 | 293 | //---------------------------------------------------------------- |
michael@0 | 294 | |
michael@0 | 295 | // forbid instantiation |
michael@0 | 296 | TransliteratorIDParser(); |
michael@0 | 297 | |
michael@0 | 298 | /** |
michael@0 | 299 | * Parse an ID into component pieces. Take IDs of the form T, |
michael@0 | 300 | * T/V, S-T, S-T/V, or S/V-T. If the source is missing, return a |
michael@0 | 301 | * source of ANY. |
michael@0 | 302 | * @param id the id string, in any of several forms |
michael@0 | 303 | * @param pos INPUT-OUTPUT parameter. On input, pos[0] is the |
michael@0 | 304 | * offset of the first character to parse in id. On output, |
michael@0 | 305 | * pos[0] is the offset after the last parsed character. If the |
michael@0 | 306 | * parse failed, pos[0] will be unchanged. |
michael@0 | 307 | * @param allowFilter if true, a UnicodeSet pattern is allowed |
michael@0 | 308 | * at any location between specs or delimiters, and is returned |
michael@0 | 309 | * as the fifth string in the array. |
michael@0 | 310 | * @return a Specs object, or null if the parse failed. If |
michael@0 | 311 | * neither source nor target was seen in the parsed id, then the |
michael@0 | 312 | * parse fails. If allowFilter is true, then the parsed filter |
michael@0 | 313 | * pattern is returned in the Specs object, otherwise the returned |
michael@0 | 314 | * filter reference is null. If the parse fails for any reason |
michael@0 | 315 | * null is returned. |
michael@0 | 316 | */ |
michael@0 | 317 | static Specs* parseFilterID(const UnicodeString& id, int32_t& pos, |
michael@0 | 318 | UBool allowFilter); |
michael@0 | 319 | |
michael@0 | 320 | /** |
michael@0 | 321 | * Givens a Specs object, convert it to a SingleID object. The |
michael@0 | 322 | * Spec object is a more unprocessed parse result. The SingleID |
michael@0 | 323 | * object contains information about canonical and basic IDs. |
michael@0 | 324 | * @param specs the given Specs object. |
michael@0 | 325 | * @param dir either FORWARD or REVERSE. |
michael@0 | 326 | * @return a SingleID; never returns null. Returned object always |
michael@0 | 327 | * has 'filter' field of null. |
michael@0 | 328 | */ |
michael@0 | 329 | static SingleID* specsToID(const Specs* specs, int32_t dir); |
michael@0 | 330 | |
michael@0 | 331 | /** |
michael@0 | 332 | * Given a Specs object, return a SingleID representing the |
michael@0 | 333 | * special inverse of that ID. If there is no special inverse |
michael@0 | 334 | * then return null. |
michael@0 | 335 | * @param specs the given Specs. |
michael@0 | 336 | * @return a SingleID or null. Returned object always has |
michael@0 | 337 | * 'filter' field of null. |
michael@0 | 338 | */ |
michael@0 | 339 | static SingleID* specsToSpecialInverse(const Specs& specs, UErrorCode &status); |
michael@0 | 340 | |
michael@0 | 341 | /** |
michael@0 | 342 | * Glue method to get around access problems in C++. |
michael@0 | 343 | * @param id the id string for the transliterator, in any of several forms |
michael@0 | 344 | * @param canonID the given canonical ID |
michael@0 | 345 | */ |
michael@0 | 346 | static Transliterator* createBasicInstance(const UnicodeString& id, |
michael@0 | 347 | const UnicodeString* canonID); |
michael@0 | 348 | |
michael@0 | 349 | /** |
michael@0 | 350 | * Initialize static memory. |
michael@0 | 351 | */ |
michael@0 | 352 | static void init(UErrorCode &status); |
michael@0 | 353 | |
michael@0 | 354 | friend class SingleID; |
michael@0 | 355 | }; |
michael@0 | 356 | |
michael@0 | 357 | U_NAMESPACE_END |
michael@0 | 358 | |
michael@0 | 359 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ |
michael@0 | 360 | |
michael@0 | 361 | #endif |