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