• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 **********************************************************************
3 *   Copyright (c) 2002-2006, 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 
Specs(const UnicodeString & s,const UnicodeString & t,const UnicodeString & v,UBool sawS,const UnicodeString & f)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 
SingleID(const UnicodeString & c,const UnicodeString & b,const UnicodeString & f)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 
SingleID(const UnicodeString & c,const UnicodeString & b)67 TransliteratorIDParser::SingleID::SingleID(const UnicodeString& c, const UnicodeString& b) {
68     canonID = c;
69     basicID = b;
70 }
71 
createInstance()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*
parseSingleID(const UnicodeString & id,int32_t & pos,int32_t dir,UErrorCode & status)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*
parseFilterID(const UnicodeString & id,int32_t & pos)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  */
parseGlobalFilter(const UnicodeString & id,int32_t & pos,int32_t dir,int32_t & withParens,UnicodeString * canonID)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
_deleteSingleID(void * obj)300 static void U_CALLCONV _deleteSingleID(void* obj) {
301     delete (TransliteratorIDParser::SingleID*) obj;
302 }
303 
_deleteTransliteratorTrIDPars(void * obj)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  */
parseCompoundID(const UnicodeString & id,int32_t dir,UnicodeString & canonID,UVector & list,UnicodeSet * & globalFilter)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  * @param list vector of SingleID objects.  On exit, vector
441  * of one or more Transliterators.
442  * @return new value of insertIndex.  The index will shift if
443  * there are empty items, like "(Lower)", with indices less than
444  * insertIndex.
445  */
instantiateList(UVector & list,UErrorCode & ec)446 void TransliteratorIDParser::instantiateList(UVector& list,
447                                                 UErrorCode& ec) {
448     UVector tlist(ec);
449     if (U_FAILURE(ec)) {
450         goto RETURN;
451     }
452     tlist.setDeleter(_deleteTransliteratorTrIDPars);
453 
454     Transliterator* t;
455     int32_t i;
456     for (i=0; i<=list.size(); ++i) { // [sic]: i<=list.size()
457         // We run the loop too long by one, so we can
458         // do an insert after the last element
459         if (i==list.size()) {
460             break;
461         }
462 
463         SingleID* single = (SingleID*) list.elementAt(i);
464         if (single->basicID.length() != 0) {
465             t = single->createInstance();
466             if (t == NULL) {
467                 ec = U_INVALID_ID;
468                 goto RETURN;
469             }
470             tlist.addElement(t, ec);
471             if (U_FAILURE(ec)) {
472                 delete t;
473                 goto RETURN;
474             }
475         }
476     }
477 
478     // An empty list is equivalent to a NULL transliterator.
479     if (tlist.size() == 0) {
480         t = createBasicInstance(ANY_NULL, NULL);
481         if (t == NULL) {
482             // Should never happen
483             ec = U_INTERNAL_TRANSLITERATOR_ERROR;
484         }
485         tlist.addElement(t, ec);
486         if (U_FAILURE(ec)) {
487             delete t;
488         }
489     }
490 
491  RETURN:
492 
493     UObjectDeleter *save = list.setDeleter(_deleteSingleID);
494     list.removeAllElements();
495 
496     if (U_SUCCESS(ec)) {
497         list.setDeleter(_deleteTransliteratorTrIDPars);
498 
499         while (tlist.size() > 0) {
500             t = (Transliterator*) tlist.orphanElementAt(0);
501             list.addElement(t, ec);
502             if (U_FAILURE(ec)) {
503                 delete t;
504                 list.removeAllElements();
505                 break;
506             }
507         }
508     }
509 
510     list.setDeleter(save);
511 }
512 
513 /**
514  * Parse an ID into pieces.  Take IDs of the form T, T/V, S-T,
515  * S-T/V, or S/V-T.  If the source is missing, return a source of
516  * ANY.
517  * @param id the id string, in any of several forms
518  * @return an array of 4 strings: source, target, variant, and
519  * isSourcePresent.  If the source is not present, ANY will be
520  * given as the source, and isSourcePresent will be NULL.  Otherwise
521  * isSourcePresent will be non-NULL.  The target may be empty if the
522  * id is not well-formed.  The variant may be empty.
523  */
IDtoSTV(const UnicodeString & id,UnicodeString & source,UnicodeString & target,UnicodeString & variant,UBool & isSourcePresent)524 void TransliteratorIDParser::IDtoSTV(const UnicodeString& id,
525                                      UnicodeString& source,
526                                      UnicodeString& target,
527                                      UnicodeString& variant,
528                                      UBool& isSourcePresent) {
529     source = ANY;
530     target.truncate(0);
531     variant.truncate(0);
532 
533     int32_t sep = id.indexOf(TARGET_SEP);
534     int32_t var = id.indexOf(VARIANT_SEP);
535     if (var < 0) {
536         var = id.length();
537     }
538     isSourcePresent = FALSE;
539 
540     if (sep < 0) {
541         // Form: T/V or T (or /V)
542         id.extractBetween(0, var, target);
543         id.extractBetween(var, id.length(), variant);
544     } else if (sep < var) {
545         // Form: S-T/V or S-T (or -T/V or -T)
546         if (sep > 0) {
547             id.extractBetween(0, sep, source);
548             isSourcePresent = TRUE;
549         }
550         id.extractBetween(++sep, var, target);
551         id.extractBetween(var, id.length(), variant);
552     } else {
553         // Form: (S/V-T or /V-T)
554         if (var > 0) {
555             id.extractBetween(0, var, source);
556             isSourcePresent = TRUE;
557         }
558         id.extractBetween(var, sep++, variant);
559         id.extractBetween(sep, id.length(), target);
560     }
561 
562     if (variant.length() > 0) {
563         variant.remove(0, 1);
564     }
565 }
566 
567 /**
568  * Given source, target, and variant strings, concatenate them into a
569  * full ID.  If the source is empty, then "Any" will be used for the
570  * source, so the ID will always be of the form s-t/v or s-t.
571  */
STVtoID(const UnicodeString & source,const UnicodeString & target,const UnicodeString & variant,UnicodeString & id)572 void TransliteratorIDParser::STVtoID(const UnicodeString& source,
573                                      const UnicodeString& target,
574                                      const UnicodeString& variant,
575                                      UnicodeString& id) {
576     id = source;
577     if (id.length() == 0) {
578         id = ANY;
579     }
580     id.append(TARGET_SEP).append(target);
581     if (variant.length() != 0) {
582         id.append(VARIANT_SEP).append(variant);
583     }
584     // NUL-terminate the ID string for getTerminatedBuffer.
585     // This prevents valgrind and Purify warnings.
586     id.append((UChar)0);
587     id.truncate(id.length()-1);
588 }
589 
590 /**
591  * Register two targets as being inverses of one another.  For
592  * example, calling registerSpecialInverse("NFC", "NFD", TRUE) causes
593  * Transliterator to form the following inverse relationships:
594  *
595  * <pre>NFC => NFD
596  * Any-NFC => Any-NFD
597  * NFD => NFC
598  * Any-NFD => Any-NFC</pre>
599  *
600  * (Without the special inverse registration, the inverse of NFC
601  * would be NFC-Any.)  Note that NFD is shorthand for Any-NFD, but
602  * that the presence or absence of "Any-" is preserved.
603  *
604  * <p>The relationship is symmetrical; registering (a, b) is
605  * equivalent to registering (b, a).
606  *
607  * <p>The relevant IDs must still be registered separately as
608  * factories or classes.
609  *
610  * <p>Only the targets are specified.  Special inverses always
611  * have the form Any-Target1 <=> Any-Target2.  The target should
612  * have canonical casing (the casing desired to be produced when
613  * an inverse is formed) and should contain no whitespace or other
614  * extraneous characters.
615  *
616  * @param target the target against which to register the inverse
617  * @param inverseTarget the inverse of target, that is
618  * Any-target.getInverse() => Any-inverseTarget
619  * @param bidirectional if TRUE, register the reverse relation
620  * as well, that is, Any-inverseTarget.getInverse() => Any-target
621  */
registerSpecialInverse(const UnicodeString & target,const UnicodeString & inverseTarget,UBool bidirectional,UErrorCode & status)622 void TransliteratorIDParser::registerSpecialInverse(const UnicodeString& target,
623                                                     const UnicodeString& inverseTarget,
624                                                     UBool bidirectional,
625                                                     UErrorCode &status) {
626     init(status);
627     if (U_FAILURE(status)) {
628         return;
629     }
630 
631     // If target == inverseTarget then force bidirectional => FALSE
632     if (bidirectional && 0==target.caseCompare(inverseTarget, U_FOLD_CASE_DEFAULT)) {
633         bidirectional = FALSE;
634     }
635 
636     umtx_init(&LOCK);
637     Mutex lock(&LOCK);
638 
639     SPECIAL_INVERSES->put(target, new UnicodeString(inverseTarget), status);
640     if (bidirectional) {
641         SPECIAL_INVERSES->put(inverseTarget, new UnicodeString(target), status);
642     }
643 }
644 
645 //----------------------------------------------------------------
646 // Private implementation
647 //----------------------------------------------------------------
648 
649 /**
650  * Parse an ID into component pieces.  Take IDs of the form T,
651  * T/V, S-T, S-T/V, or S/V-T.  If the source is missing, return a
652  * source of ANY.
653  * @param id the id string, in any of several forms
654  * @param pos INPUT-OUTPUT parameter.  On input, pos is the
655  * offset of the first character to parse in id.  On output,
656  * pos is the offset after the last parsed character.  If the
657  * parse failed, pos will be unchanged.
658  * @param allowFilter2 if TRUE, a UnicodeSet pattern is allowed
659  * at any location between specs or delimiters, and is returned
660  * as the fifth string in the array.
661  * @return a Specs object, or NULL if the parse failed.  If
662  * neither source nor target was seen in the parsed id, then the
663  * parse fails.  If allowFilter is TRUE, then the parsed filter
664  * pattern is returned in the Specs object, otherwise the returned
665  * filter reference is NULL.  If the parse fails for any reason
666  * NULL is returned.
667  */
668 TransliteratorIDParser::Specs*
parseFilterID(const UnicodeString & id,int32_t & pos,UBool allowFilter)669 TransliteratorIDParser::parseFilterID(const UnicodeString& id, int32_t& pos,
670                                       UBool allowFilter) {
671     UnicodeString first;
672     UnicodeString source;
673     UnicodeString target;
674     UnicodeString variant;
675     UnicodeString filter;
676     UChar delimiter = 0;
677     int32_t specCount = 0;
678     int32_t start = pos;
679 
680     // This loop parses one of the following things with each
681     // pass: a filter, a delimiter character (either '-' or '/'),
682     // or a spec (source, target, or variant).
683     for (;;) {
684         ICU_Utility::skipWhitespace(id, pos, TRUE);
685         if (pos == id.length()) {
686             break;
687         }
688 
689         // Parse filters
690         if (allowFilter && filter.length() == 0 &&
691             UnicodeSet::resemblesPattern(id, pos)) {
692 
693             ParsePosition ppos(pos);
694             UErrorCode ec = U_ZERO_ERROR;
695             UnicodeSet set(id, ppos, USET_IGNORE_SPACE, NULL, ec);
696             if (U_FAILURE(ec)) {
697                 pos = start;
698                 return NULL;
699             }
700             id.extractBetween(pos, ppos.getIndex(), filter);
701             pos = ppos.getIndex();
702             continue;
703         }
704 
705         if (delimiter == 0) {
706             UChar c = id.charAt(pos);
707             if ((c == TARGET_SEP && target.length() == 0) ||
708                 (c == VARIANT_SEP && variant.length() == 0)) {
709                 delimiter = c;
710                 ++pos;
711                 continue;
712             }
713         }
714 
715         // We are about to try to parse a spec with no delimiter
716         // when we can no longer do so (we can only do so at the
717         // start); break.
718         if (delimiter == 0 && specCount > 0) {
719             break;
720         }
721 
722         UnicodeString spec = ICU_Utility::parseUnicodeIdentifier(id, pos);
723         if (spec.length() == 0) {
724             // Note that if there was a trailing delimiter, we
725             // consume it.  So Foo-, Foo/, Foo-Bar/, and Foo/Bar-
726             // are legal.
727             break;
728         }
729 
730         switch (delimiter) {
731         case 0:
732             first = spec;
733             break;
734         case TARGET_SEP:
735             target = spec;
736             break;
737         case VARIANT_SEP:
738             variant = spec;
739             break;
740         }
741         ++specCount;
742         delimiter = 0;
743     }
744 
745     // A spec with no prior character is either source or target,
746     // depending on whether an explicit "-target" was seen.
747     if (first.length() != 0) {
748         if (target.length() == 0) {
749             target = first;
750         } else {
751             source = first;
752         }
753     }
754 
755     // Must have either source or target
756     if (source.length() == 0 && target.length() == 0) {
757         pos = start;
758         return NULL;
759     }
760 
761     // Empty source or target defaults to ANY
762     UBool sawSource = TRUE;
763     if (source.length() == 0) {
764         source = ANY;
765         sawSource = FALSE;
766     }
767     if (target.length() == 0) {
768         target = ANY;
769     }
770 
771     return new Specs(source, target, variant, sawSource, filter);
772 }
773 
774 /**
775  * Givens a Spec object, convert it to a SingleID object.  The
776  * Spec object is a more unprocessed parse result.  The SingleID
777  * object contains information about canonical and basic IDs.
778  * @return a SingleID; never returns NULL.  Returned object always
779  * has 'filter' field of NULL.
780  */
781 TransliteratorIDParser::SingleID*
specsToID(const Specs * specs,int32_t dir)782 TransliteratorIDParser::specsToID(const Specs* specs, int32_t dir) {
783     UnicodeString canonID;
784     UnicodeString basicID;
785     UnicodeString basicPrefix;
786     if (specs != NULL) {
787         UnicodeString buf;
788         if (dir == FORWARD) {
789             if (specs->sawSource) {
790                 buf.append(specs->source).append(TARGET_SEP);
791             } else {
792                 basicPrefix = specs->source;
793                 basicPrefix.append(TARGET_SEP);
794             }
795             buf.append(specs->target);
796         } else {
797             buf.append(specs->target).append(TARGET_SEP).append(specs->source);
798         }
799         if (specs->variant.length() != 0) {
800             buf.append(VARIANT_SEP).append(specs->variant);
801         }
802         basicID = basicPrefix;
803         basicID.append(buf);
804         if (specs->filter.length() != 0) {
805             buf.insert(0, specs->filter);
806         }
807         canonID = buf;
808     }
809     return new SingleID(canonID, basicID);
810 }
811 
812 /**
813  * Given a Specs object, return a SingleID representing the
814  * special inverse of that ID.  If there is no special inverse
815  * then return NULL.
816  * @return a SingleID or NULL.  Returned object always has
817  * 'filter' field of NULL.
818  */
819 TransliteratorIDParser::SingleID*
specsToSpecialInverse(const Specs & specs,UErrorCode & status)820 TransliteratorIDParser::specsToSpecialInverse(const Specs& specs, UErrorCode &status) {
821     if (0!=specs.source.caseCompare(ANY, U_FOLD_CASE_DEFAULT)) {
822         return NULL;
823     }
824     init(status);
825 
826     UnicodeString* inverseTarget;
827 
828     umtx_init(&LOCK);
829     umtx_lock(&LOCK);
830     inverseTarget = (UnicodeString*) SPECIAL_INVERSES->get(specs.target);
831     umtx_unlock(&LOCK);
832 
833     if (inverseTarget != NULL) {
834         // If the original ID contained "Any-" then make the
835         // special inverse "Any-Foo"; otherwise make it "Foo".
836         // So "Any-NFC" => "Any-NFD" but "NFC" => "NFD".
837         UnicodeString buf;
838         if (specs.filter.length() != 0) {
839             buf.append(specs.filter);
840         }
841         if (specs.sawSource) {
842             buf.append(ANY).append(TARGET_SEP);
843         }
844         buf.append(*inverseTarget);
845 
846         UnicodeString basicID(ANY);
847         basicID.append(TARGET_SEP).append(*inverseTarget);
848 
849         if (specs.variant.length() != 0) {
850             buf.append(VARIANT_SEP).append(specs.variant);
851             basicID.append(VARIANT_SEP).append(specs.variant);
852         }
853         return new SingleID(buf, basicID);
854     }
855     return NULL;
856 }
857 
858 /**
859  * Glue method to get around access problems in C++.  This would
860  * ideally be inline but we want to avoid a circular header
861  * dependency.
862  */
createBasicInstance(const UnicodeString & id,const UnicodeString * canonID)863 Transliterator* TransliteratorIDParser::createBasicInstance(const UnicodeString& id, const UnicodeString* canonID) {
864     return Transliterator::createBasicInstance(id, canonID);
865 }
866 
867 /**
868  * Initialize static memory.
869  */
init(UErrorCode & status)870 void TransliteratorIDParser::init(UErrorCode &status) {
871     if (SPECIAL_INVERSES != NULL) {
872         return;
873     }
874 
875     Hashtable* special_inverses = new Hashtable(TRUE, status);
876     special_inverses->setValueDeleter(uhash_deleteUnicodeString);
877 
878     umtx_init(&LOCK);
879     umtx_lock(&LOCK);
880     if (SPECIAL_INVERSES == NULL) {
881         SPECIAL_INVERSES = special_inverses;
882         special_inverses = NULL;
883     }
884     umtx_unlock(&LOCK);
885     delete special_inverses; /*null instance*/
886 
887     ucln_i18n_registerCleanup(UCLN_I18N_TRANSLITERATOR, transliterator_cleanup);
888 }
889 
890 /**
891  * Free static memory.
892  */
cleanup()893 void TransliteratorIDParser::cleanup() {
894     if (SPECIAL_INVERSES) {
895         delete SPECIAL_INVERSES;
896         SPECIAL_INVERSES = NULL;
897     }
898     umtx_destroy(&LOCK);
899 }
900 
901 U_NAMESPACE_END
902 
903 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
904 
905 //eof
906