]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/tridpars.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / i18n / tridpars.cpp
CommitLineData
b75a7d8f
A
1/*
2**********************************************************************
3* Copyright (c) 2002-2003, 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
28U_NAMESPACE_BEGIN
29
30static const UChar ID_DELIM = 0x003B; // ;
31static const UChar TARGET_SEP = 0x002D; // -
32static const UChar VARIANT_SEP = 0x002F; // /
33static const UChar OPEN_REV = 0x0028; // (
34static const UChar CLOSE_REV = 0x0029; // )
35
36static const UChar EMPTY[] = {0}; // ""
37static const UChar ANY[] = {65,110,121,0}; // "Any"
38static const UChar ANY_NULL[] = {65,110,121,45,78,117,108,108,0}; // "Any-Null"
39
40static const int32_t FORWARD = UTRANS_FORWARD;
41static const int32_t REVERSE = UTRANS_REVERSE;
42
43static Hashtable* SPECIAL_INVERSES = NULL;
44
45/**
46 * The mutex controlling access to SPECIAL_INVERSES
47 */
48static UMTX LOCK = 0;
49
50TransliteratorIDParser::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
60TransliteratorIDParser::SingleID::SingleID(const UnicodeString& c, const UnicodeString& b,
61 const UnicodeString& f) {
62 canonID = c;
63 basicID = b;
64 filter = f;
65}
66
67TransliteratorIDParser::SingleID::SingleID(const UnicodeString& c, const UnicodeString& b) {
68 canonID = c;
69 basicID = b;
70}
71
72Transliterator* 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 */
106TransliteratorIDParser::SingleID*
107TransliteratorIDParser::parseSingleID(const UnicodeString& id, int32_t& pos,
108 int32_t dir) {
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);
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 */
193TransliteratorIDParser::SingleID*
194TransliteratorIDParser::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 */
233UnicodeSet* 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, 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
299U_CDECL_BEGIN
300static void U_CALLCONV _deleteSingleID(void* obj) {
301 delete (TransliteratorIDParser::SingleID*) obj;
302}
303
304static void U_CALLCONV _deleteTransliterator(void* obj) {
305 delete (Transliterator*) obj;
306}
307U_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 */
331UBool 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);
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 */
453int32_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(_deleteTransliterator);
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(_deleteTransliterator);
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 */
544void 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 */
592void 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 */
638void TransliteratorIDParser::registerSpecialInverse(const UnicodeString& target,
639 const UnicodeString& inverseTarget,
640 UBool bidirectional) {
641 init();
642
643 // If target == inverseTarget then force bidirectional => FALSE
644 if (bidirectional && 0==target.caseCompare(inverseTarget, U_FOLD_CASE_DEFAULT)) {
645 bidirectional = FALSE;
646 }
647
648 umtx_init(&LOCK);
649 Mutex lock(&LOCK);
650
651 UErrorCode ec = U_ZERO_ERROR;
652 SPECIAL_INVERSES->put(target, new UnicodeString(inverseTarget), ec);
653 if (bidirectional) {
654 SPECIAL_INVERSES->put(inverseTarget, new UnicodeString(target), ec);
655 }
656}
657
658//----------------------------------------------------------------
659// Private implementation
660//----------------------------------------------------------------
661
662/**
663 * Parse an ID into component pieces. Take IDs of the form T,
664 * T/V, S-T, S-T/V, or S/V-T. If the source is missing, return a
665 * source of ANY.
666 * @param id the id string, in any of several forms
667 * @param pos INPUT-OUTPUT parameter. On input, pos is the
668 * offset of the first character to parse in id. On output,
669 * pos is the offset after the last parsed character. If the
670 * parse failed, pos will be unchanged.
671 * @param allowFilter2 if TRUE, a UnicodeSet pattern is allowed
672 * at any location between specs or delimiters, and is returned
673 * as the fifth string in the array.
674 * @return a Specs object, or NULL if the parse failed. If
675 * neither source nor target was seen in the parsed id, then the
676 * parse fails. If allowFilter is TRUE, then the parsed filter
677 * pattern is returned in the Specs object, otherwise the returned
678 * filter reference is NULL. If the parse fails for any reason
679 * NULL is returned.
680 */
681TransliteratorIDParser::Specs*
682TransliteratorIDParser::parseFilterID(const UnicodeString& id, int32_t& pos,
683 UBool allowFilter) {
684 UnicodeString first;
685 UnicodeString source;
686 UnicodeString target;
687 UnicodeString variant;
688 UnicodeString filter;
689 UChar delimiter = 0;
690 int32_t specCount = 0;
691 int32_t start = pos;
692
693 // This loop parses one of the following things with each
694 // pass: a filter, a delimiter character (either '-' or '/'),
695 // or a spec (source, target, or variant).
696 for (;;) {
697 ICU_Utility::skipWhitespace(id, pos, TRUE);
698 if (pos == id.length()) {
699 break;
700 }
701
702 // Parse filters
703 if (allowFilter && filter.length() == 0 &&
704 UnicodeSet::resemblesPattern(id, pos)) {
705
706 ParsePosition ppos(pos);
707 UErrorCode ec = U_ZERO_ERROR;
708 UnicodeSet set(id, ppos, USET_IGNORE_SPACE, ec);
709 if (U_FAILURE(ec)) {
710 pos = start;
711 return NULL;
712 }
713 id.extractBetween(pos, ppos.getIndex(), filter);
714 pos = ppos.getIndex();
715 continue;
716 }
717
718 if (delimiter == 0) {
719 UChar c = id.charAt(pos);
720 if ((c == TARGET_SEP && target.length() == 0) ||
721 (c == VARIANT_SEP && variant.length() == 0)) {
722 delimiter = c;
723 ++pos;
724 continue;
725 }
726 }
727
728 // We are about to try to parse a spec with no delimiter
729 // when we can no longer do so (we can only do so at the
730 // start); break.
731 if (delimiter == 0 && specCount > 0) {
732 break;
733 }
734
735 UnicodeString spec = ICU_Utility::parseUnicodeIdentifier(id, pos);
736 if (spec.length() == 0) {
737 // Note that if there was a trailing delimiter, we
738 // consume it. So Foo-, Foo/, Foo-Bar/, and Foo/Bar-
739 // are legal.
740 break;
741 }
742
743 switch (delimiter) {
744 case 0:
745 first = spec;
746 break;
747 case TARGET_SEP:
748 target = spec;
749 break;
750 case VARIANT_SEP:
751 variant = spec;
752 break;
753 }
754 ++specCount;
755 delimiter = 0;
756 }
757
758 // A spec with no prior character is either source or target,
759 // depending on whether an explicit "-target" was seen.
760 if (first.length() != 0) {
761 if (target.length() == 0) {
762 target = first;
763 } else {
764 source = first;
765 }
766 }
767
768 // Must have either source or target
769 if (source.length() == 0 && target.length() == 0) {
770 pos = start;
771 return NULL;
772 }
773
774 // Empty source or target defaults to ANY
775 UBool sawSource = TRUE;
776 if (source.length() == 0) {
777 source = ANY;
778 sawSource = FALSE;
779 }
780 if (target.length() == 0) {
781 target = ANY;
782 }
783
784 return new Specs(source, target, variant, sawSource, filter);
785}
786
787/**
788 * Givens a Spec object, convert it to a SingleID object. The
789 * Spec object is a more unprocessed parse result. The SingleID
790 * object contains information about canonical and basic IDs.
791 * @return a SingleID; never returns NULL. Returned object always
792 * has 'filter' field of NULL.
793 */
794TransliteratorIDParser::SingleID*
795TransliteratorIDParser::specsToID(const Specs* specs, int32_t dir) {
796 UnicodeString canonID;
797 UnicodeString basicID;
798 UnicodeString basicPrefix;
799 if (specs != NULL) {
800 UnicodeString buf;
801 if (dir == FORWARD) {
802 if (specs->sawSource) {
803 buf.append(specs->source).append(TARGET_SEP);
804 } else {
805 basicPrefix = specs->source;
806 basicPrefix.append(TARGET_SEP);
807 }
808 buf.append(specs->target);
809 } else {
810 buf.append(specs->target).append(TARGET_SEP).append(specs->source);
811 }
812 if (specs->variant.length() != 0) {
813 buf.append(VARIANT_SEP).append(specs->variant);
814 }
815 basicID = basicPrefix;
816 basicID.append(buf);
817 if (specs->filter.length() != 0) {
818 buf.insert(0, specs->filter);
819 }
820 canonID = buf;
821 }
822 return new SingleID(canonID, basicID);
823}
824
825/**
826 * Given a Specs object, return a SingleID representing the
827 * special inverse of that ID. If there is no special inverse
828 * then return NULL.
829 * @return a SingleID or NULL. Returned object always has
830 * 'filter' field of NULL.
831 */
832TransliteratorIDParser::SingleID*
833TransliteratorIDParser::specsToSpecialInverse(const Specs& specs) {
834 if (0!=specs.source.caseCompare(ANY, U_FOLD_CASE_DEFAULT)) {
835 return NULL;
836 }
837 init();
838
839 UnicodeString* inverseTarget;
840
841 umtx_init(&LOCK);
842 umtx_lock(&LOCK);
843 inverseTarget = (UnicodeString*) SPECIAL_INVERSES->get(specs.target);
844 umtx_unlock(&LOCK);
845
846 if (inverseTarget != NULL) {
847 // If the original ID contained "Any-" then make the
848 // special inverse "Any-Foo"; otherwise make it "Foo".
849 // So "Any-NFC" => "Any-NFD" but "NFC" => "NFD".
850 UnicodeString buf;
851 if (specs.filter.length() != 0) {
852 buf.append(specs.filter);
853 }
854 if (specs.sawSource) {
855 buf.append(ANY).append(TARGET_SEP);
856 }
857 buf.append(*inverseTarget);
858
859 UnicodeString basicID(ANY);
860 basicID.append(TARGET_SEP).append(*inverseTarget);
861
862 if (specs.variant.length() != 0) {
863 buf.append(VARIANT_SEP).append(specs.variant);
864 basicID.append(VARIANT_SEP).append(specs.variant);
865 }
866 return new SingleID(buf, basicID);
867 }
868 return NULL;
869}
870
871/**
872 * Glue method to get around access problems in C++. This would
873 * ideally be inline but we want to avoid a circular header
874 * dependency.
875 */
876Transliterator* TransliteratorIDParser::createBasicInstance(const UnicodeString& id, const UnicodeString* canonID) {
877 return Transliterator::createBasicInstance(id, canonID);
878}
879
880/**
881 * Initialize static memory.
882 */
883void TransliteratorIDParser::init() {
884 if (SPECIAL_INVERSES != NULL) {
885 return;
886 }
887
888 Hashtable* special_inverses = new Hashtable(TRUE);
889 special_inverses->setValueDeleter(uhash_deleteUnicodeString);
890
891 umtx_init(&LOCK);
892 umtx_lock(&LOCK);
893 if (SPECIAL_INVERSES == NULL) {
894 SPECIAL_INVERSES = special_inverses;
895 special_inverses = NULL;
896 }
897 umtx_unlock(&LOCK);
898 delete special_inverses;
899
900 ucln_i18n_registerCleanup();
901}
902
903/**
904 * Free static memory.
905 */
906void TransliteratorIDParser::cleanup() {
907 if (SPECIAL_INVERSES) {
908 delete SPECIAL_INVERSES;
909 SPECIAL_INVERSES = NULL;
910 }
911 umtx_destroy(&LOCK);
912}
913
914U_NAMESPACE_END
915
916#endif /* #if !UCONFIG_NO_TRANSLITERATION */
917
918//eof