intl/icu/source/i18n/tridpars.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (c) 2002-2012, 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/14/2002 aliu Creation.
michael@0 8 **********************************************************************
michael@0 9 */
michael@0 10
michael@0 11 #include "unicode/utypes.h"
michael@0 12
michael@0 13 #if !UCONFIG_NO_TRANSLITERATION
michael@0 14
michael@0 15 #include "tridpars.h"
michael@0 16 #include "hash.h"
michael@0 17 #include "mutex.h"
michael@0 18 #include "ucln_in.h"
michael@0 19 #include "unicode/parsepos.h"
michael@0 20 #include "unicode/translit.h"
michael@0 21 #include "unicode/uchar.h"
michael@0 22 #include "unicode/uniset.h"
michael@0 23 #include "unicode/unistr.h"
michael@0 24 #include "unicode/utrans.h"
michael@0 25 #include "util.h"
michael@0 26 #include "uvector.h"
michael@0 27
michael@0 28 U_NAMESPACE_BEGIN
michael@0 29
michael@0 30 static const UChar ID_DELIM = 0x003B; // ;
michael@0 31 static const UChar TARGET_SEP = 0x002D; // -
michael@0 32 static const UChar VARIANT_SEP = 0x002F; // /
michael@0 33 static const UChar OPEN_REV = 0x0028; // (
michael@0 34 static const UChar CLOSE_REV = 0x0029; // )
michael@0 35
michael@0 36 //static const UChar EMPTY[] = {0}; // ""
michael@0 37 static const UChar ANY[] = {65,110,121,0}; // "Any"
michael@0 38 static const UChar ANY_NULL[] = {65,110,121,45,78,117,108,108,0}; // "Any-Null"
michael@0 39
michael@0 40 static const int32_t FORWARD = UTRANS_FORWARD;
michael@0 41 static const int32_t REVERSE = UTRANS_REVERSE;
michael@0 42
michael@0 43 static Hashtable* SPECIAL_INVERSES = NULL;
michael@0 44
michael@0 45 /**
michael@0 46 * The mutex controlling access to SPECIAL_INVERSES
michael@0 47 */
michael@0 48 static UMutex LOCK = U_MUTEX_INITIALIZER;
michael@0 49
michael@0 50 TransliteratorIDParser::Specs::Specs(const UnicodeString& s, const UnicodeString& t,
michael@0 51 const UnicodeString& v, UBool sawS,
michael@0 52 const UnicodeString& f) {
michael@0 53 source = s;
michael@0 54 target = t;
michael@0 55 variant = v;
michael@0 56 sawSource = sawS;
michael@0 57 filter = f;
michael@0 58 }
michael@0 59
michael@0 60 TransliteratorIDParser::SingleID::SingleID(const UnicodeString& c, const UnicodeString& b,
michael@0 61 const UnicodeString& f) {
michael@0 62 canonID = c;
michael@0 63 basicID = b;
michael@0 64 filter = f;
michael@0 65 }
michael@0 66
michael@0 67 TransliteratorIDParser::SingleID::SingleID(const UnicodeString& c, const UnicodeString& b) {
michael@0 68 canonID = c;
michael@0 69 basicID = b;
michael@0 70 }
michael@0 71
michael@0 72 Transliterator* TransliteratorIDParser::SingleID::createInstance() {
michael@0 73 Transliterator* t;
michael@0 74 if (basicID.length() == 0) {
michael@0 75 t = createBasicInstance(UnicodeString(TRUE, ANY_NULL, 8), &canonID);
michael@0 76 } else {
michael@0 77 t = createBasicInstance(basicID, &canonID);
michael@0 78 }
michael@0 79 if (t != NULL) {
michael@0 80 if (filter.length() != 0) {
michael@0 81 UErrorCode ec = U_ZERO_ERROR;
michael@0 82 UnicodeSet *set = new UnicodeSet(filter, ec);
michael@0 83 if (U_FAILURE(ec)) {
michael@0 84 delete set;
michael@0 85 } else {
michael@0 86 t->adoptFilter(set);
michael@0 87 }
michael@0 88 }
michael@0 89 }
michael@0 90 return t;
michael@0 91 }
michael@0 92
michael@0 93
michael@0 94 /**
michael@0 95 * Parse a single ID, that is, an ID of the general form
michael@0 96 * "[f1] s1-t1/v1 ([f2] s2-t3/v2)", with the parenthesized element
michael@0 97 * optional, the filters optional, and the variants optional.
michael@0 98 * @param id the id to be parsed
michael@0 99 * @param pos INPUT-OUTPUT parameter. On input, the position of
michael@0 100 * the first character to parse. On output, the position after
michael@0 101 * the last character parsed.
michael@0 102 * @param dir the direction. If the direction is REVERSE then the
michael@0 103 * SingleID is constructed for the reverse direction.
michael@0 104 * @return a SingleID object or NULL
michael@0 105 */
michael@0 106 TransliteratorIDParser::SingleID*
michael@0 107 TransliteratorIDParser::parseSingleID(const UnicodeString& id, int32_t& pos,
michael@0 108 int32_t dir, UErrorCode& status) {
michael@0 109
michael@0 110 int32_t start = pos;
michael@0 111
michael@0 112 // The ID will be of the form A, A(), A(B), or (B), where
michael@0 113 // A and B are filter IDs.
michael@0 114 Specs* specsA = NULL;
michael@0 115 Specs* specsB = NULL;
michael@0 116 UBool sawParen = FALSE;
michael@0 117
michael@0 118 // On the first pass, look for (B) or (). If this fails, then
michael@0 119 // on the second pass, look for A, A(B), or A().
michael@0 120 for (int32_t pass=1; pass<=2; ++pass) {
michael@0 121 if (pass == 2) {
michael@0 122 specsA = parseFilterID(id, pos, TRUE);
michael@0 123 if (specsA == NULL) {
michael@0 124 pos = start;
michael@0 125 return NULL;
michael@0 126 }
michael@0 127 }
michael@0 128 if (ICU_Utility::parseChar(id, pos, OPEN_REV)) {
michael@0 129 sawParen = TRUE;
michael@0 130 if (!ICU_Utility::parseChar(id, pos, CLOSE_REV)) {
michael@0 131 specsB = parseFilterID(id, pos, TRUE);
michael@0 132 // Must close with a ')'
michael@0 133 if (specsB == NULL || !ICU_Utility::parseChar(id, pos, CLOSE_REV)) {
michael@0 134 delete specsA;
michael@0 135 pos = start;
michael@0 136 return NULL;
michael@0 137 }
michael@0 138 }
michael@0 139 break;
michael@0 140 }
michael@0 141 }
michael@0 142
michael@0 143 // Assemble return results
michael@0 144 SingleID* single;
michael@0 145 if (sawParen) {
michael@0 146 if (dir == FORWARD) {
michael@0 147 SingleID* b = specsToID(specsB, FORWARD);
michael@0 148 single = specsToID(specsA, FORWARD);
michael@0 149 // Null pointers check
michael@0 150 if (b == NULL || single == NULL) {
michael@0 151 delete b;
michael@0 152 delete single;
michael@0 153 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 154 return NULL;
michael@0 155 }
michael@0 156 single->canonID.append(OPEN_REV)
michael@0 157 .append(b->canonID).append(CLOSE_REV);
michael@0 158 if (specsA != NULL) {
michael@0 159 single->filter = specsA->filter;
michael@0 160 }
michael@0 161 delete b;
michael@0 162 } else {
michael@0 163 SingleID* a = specsToID(specsA, FORWARD);
michael@0 164 single = specsToID(specsB, FORWARD);
michael@0 165 // Check for null pointer.
michael@0 166 if (a == NULL || single == NULL) {
michael@0 167 delete a;
michael@0 168 delete single;
michael@0 169 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 170 return NULL;
michael@0 171 }
michael@0 172 single->canonID.append(OPEN_REV)
michael@0 173 .append(a->canonID).append(CLOSE_REV);
michael@0 174 if (specsB != NULL) {
michael@0 175 single->filter = specsB->filter;
michael@0 176 }
michael@0 177 delete a;
michael@0 178 }
michael@0 179 } else {
michael@0 180 // assert(specsA != NULL);
michael@0 181 if (dir == FORWARD) {
michael@0 182 single = specsToID(specsA, FORWARD);
michael@0 183 } else {
michael@0 184 single = specsToSpecialInverse(*specsA, status);
michael@0 185 if (single == NULL) {
michael@0 186 single = specsToID(specsA, REVERSE);
michael@0 187 }
michael@0 188 }
michael@0 189 // Check for NULL pointer
michael@0 190 if (single == NULL) {
michael@0 191 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 192 return NULL;
michael@0 193 }
michael@0 194 single->filter = specsA->filter;
michael@0 195 }
michael@0 196
michael@0 197 delete specsA;
michael@0 198 delete specsB;
michael@0 199
michael@0 200 return single;
michael@0 201 }
michael@0 202
michael@0 203 /**
michael@0 204 * Parse a filter ID, that is, an ID of the general form
michael@0 205 * "[f1] s1-t1/v1", with the filters optional, and the variants optional.
michael@0 206 * @param id the id to be parsed
michael@0 207 * @param pos INPUT-OUTPUT parameter. On input, the position of
michael@0 208 * the first character to parse. On output, the position after
michael@0 209 * the last character parsed.
michael@0 210 * @return a SingleID object or null if the parse fails
michael@0 211 */
michael@0 212 TransliteratorIDParser::SingleID*
michael@0 213 TransliteratorIDParser::parseFilterID(const UnicodeString& id, int32_t& pos) {
michael@0 214
michael@0 215 int32_t start = pos;
michael@0 216
michael@0 217 Specs* specs = parseFilterID(id, pos, TRUE);
michael@0 218 if (specs == NULL) {
michael@0 219 pos = start;
michael@0 220 return NULL;
michael@0 221 }
michael@0 222
michael@0 223 // Assemble return results
michael@0 224 SingleID* single = specsToID(specs, FORWARD);
michael@0 225 if (single != NULL) {
michael@0 226 single->filter = specs->filter;
michael@0 227 }
michael@0 228 delete specs;
michael@0 229 return single;
michael@0 230 }
michael@0 231
michael@0 232 /**
michael@0 233 * Parse a global filter of the form "[f]" or "([f])", depending
michael@0 234 * on 'withParens'.
michael@0 235 * @param id the pattern the parse
michael@0 236 * @param pos INPUT-OUTPUT parameter. On input, the position of
michael@0 237 * the first character to parse. On output, the position after
michael@0 238 * the last character parsed.
michael@0 239 * @param dir the direction.
michael@0 240 * @param withParens INPUT-OUTPUT parameter. On entry, if
michael@0 241 * withParens is 0, then parens are disallowed. If it is 1,
michael@0 242 * then parens are requires. If it is -1, then parens are
michael@0 243 * optional, and the return result will be set to 0 or 1.
michael@0 244 * @param canonID OUTPUT parameter. The pattern for the filter
michael@0 245 * added to the canonID, either at the end, if dir is FORWARD, or
michael@0 246 * at the start, if dir is REVERSE. The pattern will be enclosed
michael@0 247 * in parentheses if appropriate, and will be suffixed with an
michael@0 248 * ID_DELIM character. May be NULL.
michael@0 249 * @return a UnicodeSet object or NULL. A non-NULL results
michael@0 250 * indicates a successful parse, regardless of whether the filter
michael@0 251 * applies to the given direction. The caller should discard it
michael@0 252 * if withParens != (dir == REVERSE).
michael@0 253 */
michael@0 254 UnicodeSet* TransliteratorIDParser::parseGlobalFilter(const UnicodeString& id, int32_t& pos,
michael@0 255 int32_t dir,
michael@0 256 int32_t& withParens,
michael@0 257 UnicodeString* canonID) {
michael@0 258 UnicodeSet* filter = NULL;
michael@0 259 int32_t start = pos;
michael@0 260
michael@0 261 if (withParens == -1) {
michael@0 262 withParens = ICU_Utility::parseChar(id, pos, OPEN_REV) ? 1 : 0;
michael@0 263 } else if (withParens == 1) {
michael@0 264 if (!ICU_Utility::parseChar(id, pos, OPEN_REV)) {
michael@0 265 pos = start;
michael@0 266 return NULL;
michael@0 267 }
michael@0 268 }
michael@0 269
michael@0 270 ICU_Utility::skipWhitespace(id, pos, TRUE);
michael@0 271
michael@0 272 if (UnicodeSet::resemblesPattern(id, pos)) {
michael@0 273 ParsePosition ppos(pos);
michael@0 274 UErrorCode ec = U_ZERO_ERROR;
michael@0 275 filter = new UnicodeSet(id, ppos, USET_IGNORE_SPACE, NULL, ec);
michael@0 276 /* test for NULL */
michael@0 277 if (filter == 0) {
michael@0 278 pos = start;
michael@0 279 return 0;
michael@0 280 }
michael@0 281 if (U_FAILURE(ec)) {
michael@0 282 delete filter;
michael@0 283 pos = start;
michael@0 284 return NULL;
michael@0 285 }
michael@0 286
michael@0 287 UnicodeString pattern;
michael@0 288 id.extractBetween(pos, ppos.getIndex(), pattern);
michael@0 289 pos = ppos.getIndex();
michael@0 290
michael@0 291 if (withParens == 1 && !ICU_Utility::parseChar(id, pos, CLOSE_REV)) {
michael@0 292 pos = start;
michael@0 293 return NULL;
michael@0 294 }
michael@0 295
michael@0 296 // In the forward direction, append the pattern to the
michael@0 297 // canonID. In the reverse, insert it at zero, and invert
michael@0 298 // the presence of parens ("A" <-> "(A)").
michael@0 299 if (canonID != NULL) {
michael@0 300 if (dir == FORWARD) {
michael@0 301 if (withParens == 1) {
michael@0 302 pattern.insert(0, OPEN_REV);
michael@0 303 pattern.append(CLOSE_REV);
michael@0 304 }
michael@0 305 canonID->append(pattern).append(ID_DELIM);
michael@0 306 } else {
michael@0 307 if (withParens == 0) {
michael@0 308 pattern.insert(0, OPEN_REV);
michael@0 309 pattern.append(CLOSE_REV);
michael@0 310 }
michael@0 311 canonID->insert(0, pattern);
michael@0 312 canonID->insert(pattern.length(), ID_DELIM);
michael@0 313 }
michael@0 314 }
michael@0 315 }
michael@0 316
michael@0 317 return filter;
michael@0 318 }
michael@0 319
michael@0 320 U_CDECL_BEGIN
michael@0 321 static void U_CALLCONV _deleteSingleID(void* obj) {
michael@0 322 delete (TransliteratorIDParser::SingleID*) obj;
michael@0 323 }
michael@0 324
michael@0 325 static void U_CALLCONV _deleteTransliteratorTrIDPars(void* obj) {
michael@0 326 delete (Transliterator*) obj;
michael@0 327 }
michael@0 328 U_CDECL_END
michael@0 329
michael@0 330 /**
michael@0 331 * Parse a compound ID, consisting of an optional forward global
michael@0 332 * filter, a separator, one or more single IDs delimited by
michael@0 333 * separators, an an optional reverse global filter. The
michael@0 334 * separator is a semicolon. The global filters are UnicodeSet
michael@0 335 * patterns. The reverse global filter must be enclosed in
michael@0 336 * parentheses.
michael@0 337 * @param id the pattern the parse
michael@0 338 * @param dir the direction.
michael@0 339 * @param canonID OUTPUT parameter that receives the canonical ID,
michael@0 340 * consisting of canonical IDs for all elements, as returned by
michael@0 341 * parseSingleID(), separated by semicolons. Previous contents
michael@0 342 * are discarded.
michael@0 343 * @param list OUTPUT parameter that receives a list of SingleID
michael@0 344 * objects representing the parsed IDs. Previous contents are
michael@0 345 * discarded.
michael@0 346 * @param globalFilter OUTPUT parameter that receives a pointer to
michael@0 347 * a newly created global filter for this ID in this direction, or
michael@0 348 * NULL if there is none.
michael@0 349 * @return TRUE if the parse succeeds, that is, if the entire
michael@0 350 * id is consumed without syntax error.
michael@0 351 */
michael@0 352 UBool TransliteratorIDParser::parseCompoundID(const UnicodeString& id, int32_t dir,
michael@0 353 UnicodeString& canonID,
michael@0 354 UVector& list,
michael@0 355 UnicodeSet*& globalFilter) {
michael@0 356 UErrorCode ec = U_ZERO_ERROR;
michael@0 357 int32_t i;
michael@0 358 int32_t pos = 0;
michael@0 359 int32_t withParens = 1;
michael@0 360 list.removeAllElements();
michael@0 361 UnicodeSet* filter;
michael@0 362 globalFilter = NULL;
michael@0 363 canonID.truncate(0);
michael@0 364
michael@0 365 // Parse leading global filter, if any
michael@0 366 withParens = 0; // parens disallowed
michael@0 367 filter = parseGlobalFilter(id, pos, dir, withParens, &canonID);
michael@0 368 if (filter != NULL) {
michael@0 369 if (!ICU_Utility::parseChar(id, pos, ID_DELIM)) {
michael@0 370 // Not a global filter; backup and resume
michael@0 371 canonID.truncate(0);
michael@0 372 pos = 0;
michael@0 373 }
michael@0 374 if (dir == FORWARD) {
michael@0 375 globalFilter = filter;
michael@0 376 } else {
michael@0 377 delete filter;
michael@0 378 }
michael@0 379 filter = NULL;
michael@0 380 }
michael@0 381
michael@0 382 UBool sawDelimiter = TRUE;
michael@0 383 for (;;) {
michael@0 384 SingleID* single = parseSingleID(id, pos, dir, ec);
michael@0 385 if (single == NULL) {
michael@0 386 break;
michael@0 387 }
michael@0 388 if (dir == FORWARD) {
michael@0 389 list.addElement(single, ec);
michael@0 390 } else {
michael@0 391 list.insertElementAt(single, 0, ec);
michael@0 392 }
michael@0 393 if (U_FAILURE(ec)) {
michael@0 394 goto FAIL;
michael@0 395 }
michael@0 396 if (!ICU_Utility::parseChar(id, pos, ID_DELIM)) {
michael@0 397 sawDelimiter = FALSE;
michael@0 398 break;
michael@0 399 }
michael@0 400 }
michael@0 401
michael@0 402 if (list.size() == 0) {
michael@0 403 goto FAIL;
michael@0 404 }
michael@0 405
michael@0 406 // Construct canonical ID
michael@0 407 for (i=0; i<list.size(); ++i) {
michael@0 408 SingleID* single = (SingleID*) list.elementAt(i);
michael@0 409 canonID.append(single->canonID);
michael@0 410 if (i != (list.size()-1)) {
michael@0 411 canonID.append(ID_DELIM);
michael@0 412 }
michael@0 413 }
michael@0 414
michael@0 415 // Parse trailing global filter, if any, and only if we saw
michael@0 416 // a trailing delimiter after the IDs.
michael@0 417 if (sawDelimiter) {
michael@0 418 withParens = 1; // parens required
michael@0 419 filter = parseGlobalFilter(id, pos, dir, withParens, &canonID);
michael@0 420 if (filter != NULL) {
michael@0 421 // Don't require trailing ';', but parse it if present
michael@0 422 ICU_Utility::parseChar(id, pos, ID_DELIM);
michael@0 423
michael@0 424 if (dir == REVERSE) {
michael@0 425 globalFilter = filter;
michael@0 426 } else {
michael@0 427 delete filter;
michael@0 428 }
michael@0 429 filter = NULL;
michael@0 430 }
michael@0 431 }
michael@0 432
michael@0 433 // Trailing unparsed text is a syntax error
michael@0 434 ICU_Utility::skipWhitespace(id, pos, TRUE);
michael@0 435 if (pos != id.length()) {
michael@0 436 goto FAIL;
michael@0 437 }
michael@0 438
michael@0 439 return TRUE;
michael@0 440
michael@0 441 FAIL:
michael@0 442 UObjectDeleter *save = list.setDeleter(_deleteSingleID);
michael@0 443 list.removeAllElements();
michael@0 444 list.setDeleter(save);
michael@0 445 delete globalFilter;
michael@0 446 globalFilter = NULL;
michael@0 447 return FALSE;
michael@0 448 }
michael@0 449
michael@0 450 /**
michael@0 451 * Convert the elements of the 'list' vector, which are SingleID
michael@0 452 * objects, into actual Transliterator objects. In the course of
michael@0 453 * this, some (or all) entries may be removed. If all entries
michael@0 454 * are removed, the NULL transliterator will be added.
michael@0 455 *
michael@0 456 * Delete entries with empty basicIDs; these are generated by
michael@0 457 * elements like "(A)" in the forward direction, or "A()" in
michael@0 458 * the reverse. THIS MAY RESULT IN AN EMPTY VECTOR. Convert
michael@0 459 * SingleID entries to actual transliterators.
michael@0 460 *
michael@0 461 * @param list vector of SingleID objects. On exit, vector
michael@0 462 * of one or more Transliterators.
michael@0 463 * @return new value of insertIndex. The index will shift if
michael@0 464 * there are empty items, like "(Lower)", with indices less than
michael@0 465 * insertIndex.
michael@0 466 */
michael@0 467 void TransliteratorIDParser::instantiateList(UVector& list,
michael@0 468 UErrorCode& ec) {
michael@0 469 UVector tlist(ec);
michael@0 470 if (U_FAILURE(ec)) {
michael@0 471 goto RETURN;
michael@0 472 }
michael@0 473 tlist.setDeleter(_deleteTransliteratorTrIDPars);
michael@0 474
michael@0 475 Transliterator* t;
michael@0 476 int32_t i;
michael@0 477 for (i=0; i<=list.size(); ++i) { // [sic]: i<=list.size()
michael@0 478 // We run the loop too long by one, so we can
michael@0 479 // do an insert after the last element
michael@0 480 if (i==list.size()) {
michael@0 481 break;
michael@0 482 }
michael@0 483
michael@0 484 SingleID* single = (SingleID*) list.elementAt(i);
michael@0 485 if (single->basicID.length() != 0) {
michael@0 486 t = single->createInstance();
michael@0 487 if (t == NULL) {
michael@0 488 ec = U_INVALID_ID;
michael@0 489 goto RETURN;
michael@0 490 }
michael@0 491 tlist.addElement(t, ec);
michael@0 492 if (U_FAILURE(ec)) {
michael@0 493 delete t;
michael@0 494 goto RETURN;
michael@0 495 }
michael@0 496 }
michael@0 497 }
michael@0 498
michael@0 499 // An empty list is equivalent to a NULL transliterator.
michael@0 500 if (tlist.size() == 0) {
michael@0 501 t = createBasicInstance(UnicodeString(TRUE, ANY_NULL, 8), NULL);
michael@0 502 if (t == NULL) {
michael@0 503 // Should never happen
michael@0 504 ec = U_INTERNAL_TRANSLITERATOR_ERROR;
michael@0 505 }
michael@0 506 tlist.addElement(t, ec);
michael@0 507 if (U_FAILURE(ec)) {
michael@0 508 delete t;
michael@0 509 }
michael@0 510 }
michael@0 511
michael@0 512 RETURN:
michael@0 513
michael@0 514 UObjectDeleter *save = list.setDeleter(_deleteSingleID);
michael@0 515 list.removeAllElements();
michael@0 516
michael@0 517 if (U_SUCCESS(ec)) {
michael@0 518 list.setDeleter(_deleteTransliteratorTrIDPars);
michael@0 519
michael@0 520 while (tlist.size() > 0) {
michael@0 521 t = (Transliterator*) tlist.orphanElementAt(0);
michael@0 522 list.addElement(t, ec);
michael@0 523 if (U_FAILURE(ec)) {
michael@0 524 delete t;
michael@0 525 list.removeAllElements();
michael@0 526 break;
michael@0 527 }
michael@0 528 }
michael@0 529 }
michael@0 530
michael@0 531 list.setDeleter(save);
michael@0 532 }
michael@0 533
michael@0 534 /**
michael@0 535 * Parse an ID into pieces. Take IDs of the form T, T/V, S-T,
michael@0 536 * S-T/V, or S/V-T. If the source is missing, return a source of
michael@0 537 * ANY.
michael@0 538 * @param id the id string, in any of several forms
michael@0 539 * @return an array of 4 strings: source, target, variant, and
michael@0 540 * isSourcePresent. If the source is not present, ANY will be
michael@0 541 * given as the source, and isSourcePresent will be NULL. Otherwise
michael@0 542 * isSourcePresent will be non-NULL. The target may be empty if the
michael@0 543 * id is not well-formed. The variant may be empty.
michael@0 544 */
michael@0 545 void TransliteratorIDParser::IDtoSTV(const UnicodeString& id,
michael@0 546 UnicodeString& source,
michael@0 547 UnicodeString& target,
michael@0 548 UnicodeString& variant,
michael@0 549 UBool& isSourcePresent) {
michael@0 550 source.setTo(ANY, 3);
michael@0 551 target.truncate(0);
michael@0 552 variant.truncate(0);
michael@0 553
michael@0 554 int32_t sep = id.indexOf(TARGET_SEP);
michael@0 555 int32_t var = id.indexOf(VARIANT_SEP);
michael@0 556 if (var < 0) {
michael@0 557 var = id.length();
michael@0 558 }
michael@0 559 isSourcePresent = FALSE;
michael@0 560
michael@0 561 if (sep < 0) {
michael@0 562 // Form: T/V or T (or /V)
michael@0 563 id.extractBetween(0, var, target);
michael@0 564 id.extractBetween(var, id.length(), variant);
michael@0 565 } else if (sep < var) {
michael@0 566 // Form: S-T/V or S-T (or -T/V or -T)
michael@0 567 if (sep > 0) {
michael@0 568 id.extractBetween(0, sep, source);
michael@0 569 isSourcePresent = TRUE;
michael@0 570 }
michael@0 571 id.extractBetween(++sep, var, target);
michael@0 572 id.extractBetween(var, id.length(), variant);
michael@0 573 } else {
michael@0 574 // Form: (S/V-T or /V-T)
michael@0 575 if (var > 0) {
michael@0 576 id.extractBetween(0, var, source);
michael@0 577 isSourcePresent = TRUE;
michael@0 578 }
michael@0 579 id.extractBetween(var, sep++, variant);
michael@0 580 id.extractBetween(sep, id.length(), target);
michael@0 581 }
michael@0 582
michael@0 583 if (variant.length() > 0) {
michael@0 584 variant.remove(0, 1);
michael@0 585 }
michael@0 586 }
michael@0 587
michael@0 588 /**
michael@0 589 * Given source, target, and variant strings, concatenate them into a
michael@0 590 * full ID. If the source is empty, then "Any" will be used for the
michael@0 591 * source, so the ID will always be of the form s-t/v or s-t.
michael@0 592 */
michael@0 593 void TransliteratorIDParser::STVtoID(const UnicodeString& source,
michael@0 594 const UnicodeString& target,
michael@0 595 const UnicodeString& variant,
michael@0 596 UnicodeString& id) {
michael@0 597 id = source;
michael@0 598 if (id.length() == 0) {
michael@0 599 id.setTo(ANY, 3);
michael@0 600 }
michael@0 601 id.append(TARGET_SEP).append(target);
michael@0 602 if (variant.length() != 0) {
michael@0 603 id.append(VARIANT_SEP).append(variant);
michael@0 604 }
michael@0 605 // NUL-terminate the ID string for getTerminatedBuffer.
michael@0 606 // This prevents valgrind and Purify warnings.
michael@0 607 id.append((UChar)0);
michael@0 608 id.truncate(id.length()-1);
michael@0 609 }
michael@0 610
michael@0 611 /**
michael@0 612 * Register two targets as being inverses of one another. For
michael@0 613 * example, calling registerSpecialInverse("NFC", "NFD", TRUE) causes
michael@0 614 * Transliterator to form the following inverse relationships:
michael@0 615 *
michael@0 616 * <pre>NFC => NFD
michael@0 617 * Any-NFC => Any-NFD
michael@0 618 * NFD => NFC
michael@0 619 * Any-NFD => Any-NFC</pre>
michael@0 620 *
michael@0 621 * (Without the special inverse registration, the inverse of NFC
michael@0 622 * would be NFC-Any.) Note that NFD is shorthand for Any-NFD, but
michael@0 623 * that the presence or absence of "Any-" is preserved.
michael@0 624 *
michael@0 625 * <p>The relationship is symmetrical; registering (a, b) is
michael@0 626 * equivalent to registering (b, a).
michael@0 627 *
michael@0 628 * <p>The relevant IDs must still be registered separately as
michael@0 629 * factories or classes.
michael@0 630 *
michael@0 631 * <p>Only the targets are specified. Special inverses always
michael@0 632 * have the form Any-Target1 <=> Any-Target2. The target should
michael@0 633 * have canonical casing (the casing desired to be produced when
michael@0 634 * an inverse is formed) and should contain no whitespace or other
michael@0 635 * extraneous characters.
michael@0 636 *
michael@0 637 * @param target the target against which to register the inverse
michael@0 638 * @param inverseTarget the inverse of target, that is
michael@0 639 * Any-target.getInverse() => Any-inverseTarget
michael@0 640 * @param bidirectional if TRUE, register the reverse relation
michael@0 641 * as well, that is, Any-inverseTarget.getInverse() => Any-target
michael@0 642 */
michael@0 643 void TransliteratorIDParser::registerSpecialInverse(const UnicodeString& target,
michael@0 644 const UnicodeString& inverseTarget,
michael@0 645 UBool bidirectional,
michael@0 646 UErrorCode &status) {
michael@0 647 init(status);
michael@0 648 if (U_FAILURE(status)) {
michael@0 649 return;
michael@0 650 }
michael@0 651
michael@0 652 // If target == inverseTarget then force bidirectional => FALSE
michael@0 653 if (bidirectional && 0==target.caseCompare(inverseTarget, U_FOLD_CASE_DEFAULT)) {
michael@0 654 bidirectional = FALSE;
michael@0 655 }
michael@0 656
michael@0 657 Mutex lock(&LOCK);
michael@0 658
michael@0 659 UnicodeString *tempus = new UnicodeString(inverseTarget); // Used for null pointer check before usage.
michael@0 660 if (tempus == NULL) {
michael@0 661 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 662 return;
michael@0 663 }
michael@0 664 SPECIAL_INVERSES->put(target, tempus, status);
michael@0 665 if (bidirectional) {
michael@0 666 tempus = new UnicodeString(target);
michael@0 667 if (tempus == NULL) {
michael@0 668 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 669 return;
michael@0 670 }
michael@0 671 SPECIAL_INVERSES->put(inverseTarget, tempus, status);
michael@0 672 }
michael@0 673 }
michael@0 674
michael@0 675 //----------------------------------------------------------------
michael@0 676 // Private implementation
michael@0 677 //----------------------------------------------------------------
michael@0 678
michael@0 679 /**
michael@0 680 * Parse an ID into component pieces. Take IDs of the form T,
michael@0 681 * T/V, S-T, S-T/V, or S/V-T. If the source is missing, return a
michael@0 682 * source of ANY.
michael@0 683 * @param id the id string, in any of several forms
michael@0 684 * @param pos INPUT-OUTPUT parameter. On input, pos is the
michael@0 685 * offset of the first character to parse in id. On output,
michael@0 686 * pos is the offset after the last parsed character. If the
michael@0 687 * parse failed, pos will be unchanged.
michael@0 688 * @param allowFilter2 if TRUE, a UnicodeSet pattern is allowed
michael@0 689 * at any location between specs or delimiters, and is returned
michael@0 690 * as the fifth string in the array.
michael@0 691 * @return a Specs object, or NULL if the parse failed. If
michael@0 692 * neither source nor target was seen in the parsed id, then the
michael@0 693 * parse fails. If allowFilter is TRUE, then the parsed filter
michael@0 694 * pattern is returned in the Specs object, otherwise the returned
michael@0 695 * filter reference is NULL. If the parse fails for any reason
michael@0 696 * NULL is returned.
michael@0 697 */
michael@0 698 TransliteratorIDParser::Specs*
michael@0 699 TransliteratorIDParser::parseFilterID(const UnicodeString& id, int32_t& pos,
michael@0 700 UBool allowFilter) {
michael@0 701 UnicodeString first;
michael@0 702 UnicodeString source;
michael@0 703 UnicodeString target;
michael@0 704 UnicodeString variant;
michael@0 705 UnicodeString filter;
michael@0 706 UChar delimiter = 0;
michael@0 707 int32_t specCount = 0;
michael@0 708 int32_t start = pos;
michael@0 709
michael@0 710 // This loop parses one of the following things with each
michael@0 711 // pass: a filter, a delimiter character (either '-' or '/'),
michael@0 712 // or a spec (source, target, or variant).
michael@0 713 for (;;) {
michael@0 714 ICU_Utility::skipWhitespace(id, pos, TRUE);
michael@0 715 if (pos == id.length()) {
michael@0 716 break;
michael@0 717 }
michael@0 718
michael@0 719 // Parse filters
michael@0 720 if (allowFilter && filter.length() == 0 &&
michael@0 721 UnicodeSet::resemblesPattern(id, pos)) {
michael@0 722
michael@0 723 ParsePosition ppos(pos);
michael@0 724 UErrorCode ec = U_ZERO_ERROR;
michael@0 725 UnicodeSet set(id, ppos, USET_IGNORE_SPACE, NULL, ec);
michael@0 726 if (U_FAILURE(ec)) {
michael@0 727 pos = start;
michael@0 728 return NULL;
michael@0 729 }
michael@0 730 id.extractBetween(pos, ppos.getIndex(), filter);
michael@0 731 pos = ppos.getIndex();
michael@0 732 continue;
michael@0 733 }
michael@0 734
michael@0 735 if (delimiter == 0) {
michael@0 736 UChar c = id.charAt(pos);
michael@0 737 if ((c == TARGET_SEP && target.length() == 0) ||
michael@0 738 (c == VARIANT_SEP && variant.length() == 0)) {
michael@0 739 delimiter = c;
michael@0 740 ++pos;
michael@0 741 continue;
michael@0 742 }
michael@0 743 }
michael@0 744
michael@0 745 // We are about to try to parse a spec with no delimiter
michael@0 746 // when we can no longer do so (we can only do so at the
michael@0 747 // start); break.
michael@0 748 if (delimiter == 0 && specCount > 0) {
michael@0 749 break;
michael@0 750 }
michael@0 751
michael@0 752 UnicodeString spec = ICU_Utility::parseUnicodeIdentifier(id, pos);
michael@0 753 if (spec.length() == 0) {
michael@0 754 // Note that if there was a trailing delimiter, we
michael@0 755 // consume it. So Foo-, Foo/, Foo-Bar/, and Foo/Bar-
michael@0 756 // are legal.
michael@0 757 break;
michael@0 758 }
michael@0 759
michael@0 760 switch (delimiter) {
michael@0 761 case 0:
michael@0 762 first = spec;
michael@0 763 break;
michael@0 764 case TARGET_SEP:
michael@0 765 target = spec;
michael@0 766 break;
michael@0 767 case VARIANT_SEP:
michael@0 768 variant = spec;
michael@0 769 break;
michael@0 770 }
michael@0 771 ++specCount;
michael@0 772 delimiter = 0;
michael@0 773 }
michael@0 774
michael@0 775 // A spec with no prior character is either source or target,
michael@0 776 // depending on whether an explicit "-target" was seen.
michael@0 777 if (first.length() != 0) {
michael@0 778 if (target.length() == 0) {
michael@0 779 target = first;
michael@0 780 } else {
michael@0 781 source = first;
michael@0 782 }
michael@0 783 }
michael@0 784
michael@0 785 // Must have either source or target
michael@0 786 if (source.length() == 0 && target.length() == 0) {
michael@0 787 pos = start;
michael@0 788 return NULL;
michael@0 789 }
michael@0 790
michael@0 791 // Empty source or target defaults to ANY
michael@0 792 UBool sawSource = TRUE;
michael@0 793 if (source.length() == 0) {
michael@0 794 source.setTo(ANY, 3);
michael@0 795 sawSource = FALSE;
michael@0 796 }
michael@0 797 if (target.length() == 0) {
michael@0 798 target.setTo(ANY, 3);
michael@0 799 }
michael@0 800
michael@0 801 return new Specs(source, target, variant, sawSource, filter);
michael@0 802 }
michael@0 803
michael@0 804 /**
michael@0 805 * Givens a Spec object, convert it to a SingleID object. The
michael@0 806 * Spec object is a more unprocessed parse result. The SingleID
michael@0 807 * object contains information about canonical and basic IDs.
michael@0 808 * @return a SingleID; never returns NULL. Returned object always
michael@0 809 * has 'filter' field of NULL.
michael@0 810 */
michael@0 811 TransliteratorIDParser::SingleID*
michael@0 812 TransliteratorIDParser::specsToID(const Specs* specs, int32_t dir) {
michael@0 813 UnicodeString canonID;
michael@0 814 UnicodeString basicID;
michael@0 815 UnicodeString basicPrefix;
michael@0 816 if (specs != NULL) {
michael@0 817 UnicodeString buf;
michael@0 818 if (dir == FORWARD) {
michael@0 819 if (specs->sawSource) {
michael@0 820 buf.append(specs->source).append(TARGET_SEP);
michael@0 821 } else {
michael@0 822 basicPrefix = specs->source;
michael@0 823 basicPrefix.append(TARGET_SEP);
michael@0 824 }
michael@0 825 buf.append(specs->target);
michael@0 826 } else {
michael@0 827 buf.append(specs->target).append(TARGET_SEP).append(specs->source);
michael@0 828 }
michael@0 829 if (specs->variant.length() != 0) {
michael@0 830 buf.append(VARIANT_SEP).append(specs->variant);
michael@0 831 }
michael@0 832 basicID = basicPrefix;
michael@0 833 basicID.append(buf);
michael@0 834 if (specs->filter.length() != 0) {
michael@0 835 buf.insert(0, specs->filter);
michael@0 836 }
michael@0 837 canonID = buf;
michael@0 838 }
michael@0 839 return new SingleID(canonID, basicID);
michael@0 840 }
michael@0 841
michael@0 842 /**
michael@0 843 * Given a Specs object, return a SingleID representing the
michael@0 844 * special inverse of that ID. If there is no special inverse
michael@0 845 * then return NULL.
michael@0 846 * @return a SingleID or NULL. Returned object always has
michael@0 847 * 'filter' field of NULL.
michael@0 848 */
michael@0 849 TransliteratorIDParser::SingleID*
michael@0 850 TransliteratorIDParser::specsToSpecialInverse(const Specs& specs, UErrorCode &status) {
michael@0 851 if (0!=specs.source.caseCompare(ANY, 3, U_FOLD_CASE_DEFAULT)) {
michael@0 852 return NULL;
michael@0 853 }
michael@0 854 init(status);
michael@0 855
michael@0 856 UnicodeString* inverseTarget;
michael@0 857
michael@0 858 umtx_lock(&LOCK);
michael@0 859 inverseTarget = (UnicodeString*) SPECIAL_INVERSES->get(specs.target);
michael@0 860 umtx_unlock(&LOCK);
michael@0 861
michael@0 862 if (inverseTarget != NULL) {
michael@0 863 // If the original ID contained "Any-" then make the
michael@0 864 // special inverse "Any-Foo"; otherwise make it "Foo".
michael@0 865 // So "Any-NFC" => "Any-NFD" but "NFC" => "NFD".
michael@0 866 UnicodeString buf;
michael@0 867 if (specs.filter.length() != 0) {
michael@0 868 buf.append(specs.filter);
michael@0 869 }
michael@0 870 if (specs.sawSource) {
michael@0 871 buf.append(ANY, 3).append(TARGET_SEP);
michael@0 872 }
michael@0 873 buf.append(*inverseTarget);
michael@0 874
michael@0 875 UnicodeString basicID(TRUE, ANY, 3);
michael@0 876 basicID.append(TARGET_SEP).append(*inverseTarget);
michael@0 877
michael@0 878 if (specs.variant.length() != 0) {
michael@0 879 buf.append(VARIANT_SEP).append(specs.variant);
michael@0 880 basicID.append(VARIANT_SEP).append(specs.variant);
michael@0 881 }
michael@0 882 return new SingleID(buf, basicID);
michael@0 883 }
michael@0 884 return NULL;
michael@0 885 }
michael@0 886
michael@0 887 /**
michael@0 888 * Glue method to get around access problems in C++. This would
michael@0 889 * ideally be inline but we want to avoid a circular header
michael@0 890 * dependency.
michael@0 891 */
michael@0 892 Transliterator* TransliteratorIDParser::createBasicInstance(const UnicodeString& id, const UnicodeString* canonID) {
michael@0 893 return Transliterator::createBasicInstance(id, canonID);
michael@0 894 }
michael@0 895
michael@0 896 /**
michael@0 897 * Initialize static memory.
michael@0 898 */
michael@0 899 void TransliteratorIDParser::init(UErrorCode &status) {
michael@0 900 if (SPECIAL_INVERSES != NULL) {
michael@0 901 return;
michael@0 902 }
michael@0 903
michael@0 904 Hashtable* special_inverses = new Hashtable(TRUE, status);
michael@0 905 // Null pointer check
michael@0 906 if (special_inverses == NULL) {
michael@0 907 status = U_MEMORY_ALLOCATION_ERROR;
michael@0 908 return;
michael@0 909 }
michael@0 910 special_inverses->setValueDeleter(uprv_deleteUObject);
michael@0 911
michael@0 912 umtx_lock(&LOCK);
michael@0 913 if (SPECIAL_INVERSES == NULL) {
michael@0 914 SPECIAL_INVERSES = special_inverses;
michael@0 915 special_inverses = NULL;
michael@0 916 }
michael@0 917 umtx_unlock(&LOCK);
michael@0 918 delete special_inverses; /*null instance*/
michael@0 919
michael@0 920 ucln_i18n_registerCleanup(UCLN_I18N_TRANSLITERATOR, utrans_transliterator_cleanup);
michael@0 921 }
michael@0 922
michael@0 923 /**
michael@0 924 * Free static memory.
michael@0 925 */
michael@0 926 void TransliteratorIDParser::cleanup() {
michael@0 927 if (SPECIAL_INVERSES) {
michael@0 928 delete SPECIAL_INVERSES;
michael@0 929 SPECIAL_INVERSES = NULL;
michael@0 930 }
michael@0 931 }
michael@0 932
michael@0 933 U_NAMESPACE_END
michael@0 934
michael@0 935 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
michael@0 936
michael@0 937 //eof

mercurial