]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/core/src/com/ibm/icu/impl/UCharacterName.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / core / src / com / ibm / icu / impl / UCharacterName.java
1 /**
2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4 * others. All Rights Reserved.                                                *
5 *******************************************************************************
6 */
7 package com.ibm.icu.impl;
8
9 import java.io.BufferedInputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.util.MissingResourceException;
13
14 import com.ibm.icu.lang.UCharacter;
15 import com.ibm.icu.lang.UCharacterCategory;
16 import com.ibm.icu.text.UTF16;
17 import com.ibm.icu.text.UnicodeSet;
18
19 /**
20 * Internal class to manage character names.
21 * Since data for names are stored
22 * in an array of char, by default indexes used in this class is refering to
23 * a 2 byte count, unless otherwise stated. Cases where the index is refering
24 * to a byte count, the index is halved and depending on whether the index is
25 * even or odd, the MSB or LSB of the result char at the halved index is
26 * returned. For indexes to an array of int, the index is multiplied by 2,
27 * result char at the multiplied index and its following char is returned as an
28 * int.
29 * <a href=../lang/UCharacter.html>UCharacter</a> acts as a public facade for this class
30 * Note : 0 - 0x1F are control characters without names in Unicode 3.0
31 * @author Syn Wee Quek
32 * @since nov0700
33 */
34
35 public final class UCharacterName
36 {
37     // public data members ----------------------------------------------
38
39     /*
40      * public singleton instance
41      */
42     public static final UCharacterName INSTANCE;
43
44     static {
45         try {
46             INSTANCE = new UCharacterName();
47         } catch (IOException e) {
48             ///CLOVER:OFF
49             throw new MissingResourceException("Could not construct UCharacterName. Missing unames.icu","","");
50             ///CLOVER:ON
51         }
52     }
53
54     /**
55     * Number of lines per group
56     * 1 << GROUP_SHIFT_
57     */
58     public static final int LINES_PER_GROUP_ = 1 << 5;
59     /**
60      * Maximum number of groups
61      */
62     public int m_groupcount_ = 0;
63
64     // public methods ---------------------------------------------------
65
66     /**
67     * Retrieve the name of a Unicode code point.
68     * Depending on <code>choice</code>, the character name written into the
69     * buffer is the "modern" name or the name that was defined in Unicode
70     * version 1.0.
71     * The name contains only "invariant" characters
72     * like A-Z, 0-9, space, and '-'.
73     *
74     * @param ch the code point for which to get the name.
75     * @param choice Selector for which name to get.
76     * @return if code point is above 0x1fff, null is returned
77     */
78     public String getName(int ch, int choice)
79     {
80         if (ch < UCharacter.MIN_VALUE || ch > UCharacter.MAX_VALUE ||
81             choice > UCharacterNameChoice.CHAR_NAME_CHOICE_COUNT) {
82             return null;
83         }
84
85         String result = null;
86
87         result = getAlgName(ch, choice);
88
89         // getting normal character name
90         if (result == null || result.length() == 0) {
91             if (choice == UCharacterNameChoice.EXTENDED_CHAR_NAME) {
92                 result = getExtendedName(ch);
93             } else {
94                 result = getGroupName(ch, choice);
95             }
96         }
97
98         return result;
99     }
100
101     /**
102     * Find a character by its name and return its code point value
103     * @param choice selector to indicate if argument name is a Unicode 1.0
104     *        or the most current version
105     * @param name the name to search for
106     * @return code point
107     */
108     public int getCharFromName(int choice, String name)
109     {
110         // checks for illegal arguments
111         if (choice >= UCharacterNameChoice.CHAR_NAME_CHOICE_COUNT ||
112             name == null || name.length() == 0) {
113             return -1;
114         }
115
116         // try extended names first
117         int result = getExtendedChar(name.toLowerCase(), choice);
118         if (result >= -1) {
119             return result;
120         }
121
122         String upperCaseName = name.toUpperCase();
123         // try algorithmic names first, if fails then try group names
124         // int result = getAlgorithmChar(choice, uppercasename);
125
126         if (choice == UCharacterNameChoice.UNICODE_CHAR_NAME ||
127             choice == UCharacterNameChoice.EXTENDED_CHAR_NAME
128         ) {
129             int count = 0;
130             if (m_algorithm_ != null) {
131                 count = m_algorithm_.length;
132             }
133             for (count --; count >= 0; count --) {
134                 result = m_algorithm_[count].getChar(upperCaseName);
135                 if (result >= 0) {
136                     return result;
137                 }
138             }
139         }
140
141         if (choice == UCharacterNameChoice.EXTENDED_CHAR_NAME) {
142             result = getGroupChar(upperCaseName,
143                                   UCharacterNameChoice.UNICODE_CHAR_NAME);
144             if (result == -1) {
145                 result = getGroupChar(upperCaseName,
146                                       UCharacterNameChoice.UNICODE_10_CHAR_NAME);
147             }
148             if (result == -1) {
149                 result = getGroupChar(upperCaseName,
150                                       UCharacterNameChoice.CHAR_NAME_ALIAS);
151             }
152         }
153         else {
154             result = getGroupChar(upperCaseName, choice);
155         }
156         return result;
157     }
158
159     // these are all UCharacterNameIterator use methods -------------------
160
161     /**
162     * Reads a block of compressed lengths of 32 strings and expands them into
163     * offsets and lengths for each string. Lengths are stored with a
164     * variable-width encoding in consecutive nibbles:
165     * If a nibble<0xc, then it is the length itself (0 = empty string).
166     * If a nibble>=0xc, then it forms a length value with the following
167     * nibble.
168     * The offsets and lengths arrays must be at least 33 (one more) long
169     * because there is no check here at the end if the last nibble is still
170     * used.
171     * @param index of group string object in array
172     * @param offsets array to store the value of the string offsets
173     * @param lengths array to store the value of the string length
174     * @return next index of the data string immediately after the lengths
175     *         in terms of byte address
176     */
177     public int getGroupLengths(int index, char offsets[], char lengths[])
178     {
179         char length = 0xffff;
180         byte b = 0,
181             n = 0;
182         int shift;
183         index = index * m_groupsize_; // byte count offsets of group strings
184         int stringoffset = UCharacterUtility.toInt(
185                                  m_groupinfo_[index + OFFSET_HIGH_OFFSET_],
186                                  m_groupinfo_[index + OFFSET_LOW_OFFSET_]);
187
188         offsets[0] = 0;
189
190         // all 32 lengths must be read to get the offset of the first group
191         // string
192         for (int i = 0; i < LINES_PER_GROUP_; stringoffset ++) {
193             b = m_groupstring_[stringoffset];
194             shift = 4;
195
196             while (shift >= 0) {
197                 // getting nibble
198                 n = (byte)((b >> shift) & 0x0F);
199                 if (length == 0xffff && n > SINGLE_NIBBLE_MAX_) {
200                     length = (char)((n - 12) << 4);
201                 }
202                 else {
203                     if (length != 0xffff) {
204                        lengths[i] = (char)((length | n) + 12);
205                     }
206                     else {
207                        lengths[i] = (char)n;
208                     }
209
210                     if (i < LINES_PER_GROUP_) {
211                        offsets[i + 1] = (char)(offsets[i] + lengths[i]);
212                     }
213
214                     length = 0xffff;
215                     i ++;
216                 }
217
218                 shift -= 4;
219             }
220         }
221         return stringoffset;
222     }
223
224     /**
225     * Gets the name of the argument group index.
226     * UnicodeData.txt uses ';' as a field separator, so no field can contain
227     * ';' as part of its contents. In unames.icu, it is marked as
228     * token[';'] == -1 only if the semicolon is used in the data file - which
229     * is iff we have Unicode 1.0 names or ISO comments or aliases.
230     * So, it will be token[';'] == -1 if we store U1.0 names/ISO comments/aliases
231     * although we know that it will never be part of a name.
232     * Equivalent to ICU4C's expandName.
233     * @param index of the group name string in byte count
234     * @param length of the group name string
235     * @param choice of Unicode 1.0 name or the most current name
236     * @return name of the group
237     */
238     public String getGroupName(int index, int length, int choice)
239     {
240         if (choice != UCharacterNameChoice.UNICODE_CHAR_NAME &&
241             choice != UCharacterNameChoice.EXTENDED_CHAR_NAME
242         ) {
243             if (';' >= m_tokentable_.length || m_tokentable_[';'] == 0xFFFF) {
244                 /*
245                  * skip the modern name if it is not requested _and_
246                  * if the semicolon byte value is a character, not a token number
247                  */
248                 int fieldIndex= choice==UCharacterNameChoice.ISO_COMMENT_ ? 2 : choice;
249                 do {
250                     int oldindex = index;
251                     index += UCharacterUtility.skipByteSubString(m_groupstring_,
252                                                        index, length, (byte)';');
253                     length -= (index - oldindex);
254                 } while(--fieldIndex>0);
255             }
256             else {
257                 // the semicolon byte is a token number, therefore only modern
258                 // names are stored in unames.dat and there is no such
259                 // requested alternate name here
260                 length = 0;
261             }
262         }
263
264         synchronized (m_utilStringBuffer_) {
265             m_utilStringBuffer_.delete(0, m_utilStringBuffer_.length());
266             byte b;
267             char token;
268             for (int i = 0; i < length;) {
269                 b = m_groupstring_[index + i];
270                 i ++;
271
272                 if (b >= m_tokentable_.length) {
273                     if (b == ';') {
274                         break;
275                     }
276                     m_utilStringBuffer_.append(b); // implicit letter
277                 }
278                 else {
279                     token = m_tokentable_[b & 0x00ff];
280                     if (token == 0xFFFE) {
281                         // this is a lead byte for a double-byte token
282                         token = m_tokentable_[b << 8 |
283                                           (m_groupstring_[index + i] & 0x00ff)];
284                         i ++;
285                     }
286                     if (token == 0xFFFF) {
287                         if (b == ';') {
288                             // skip the semicolon if we are seeking extended
289                             // names and there was no 2.0 name but there
290                             // is a 1.0 name.
291                             if (m_utilStringBuffer_.length() == 0 && choice ==
292                                    UCharacterNameChoice.EXTENDED_CHAR_NAME) {
293                                 continue;
294                             }
295                             break;
296                         }
297                         // explicit letter
298                         m_utilStringBuffer_.append((char)(b & 0x00ff));
299                     }
300                     else { // write token word
301                         UCharacterUtility.getNullTermByteSubString(
302                                 m_utilStringBuffer_, m_tokenstring_, token);
303                     }
304                 }
305             }
306
307             if (m_utilStringBuffer_.length() > 0) {
308                 return m_utilStringBuffer_.toString();
309             }
310         }
311         return null;
312     }
313
314     /**
315     * Retrieves the extended name
316     */
317     public String getExtendedName(int ch)
318     {
319         String result = getName(ch, UCharacterNameChoice.UNICODE_CHAR_NAME);
320         if (result == null) {
321             if (getType(ch) == UCharacterCategory.CONTROL) {
322                 result = getName(ch,
323                                  UCharacterNameChoice.UNICODE_10_CHAR_NAME);
324             }
325             if (result == null) {
326                 result = getExtendedOr10Name(ch);
327             }
328         }
329         return result;
330     }
331
332     /**
333      * Gets the group index for the codepoint, or the group before it.
334      * @param codepoint The codepoint index.
335      * @return group index containing codepoint or the group before it.
336      */
337     public int getGroup(int codepoint)
338     {
339         int endGroup = m_groupcount_;
340         int msb      = getCodepointMSB(codepoint);
341         int result   = 0;
342         // binary search for the group of names that contains the one for
343         // code
344         // find the group that contains codepoint, or the highest before it
345         while (result < endGroup - 1) {
346             int gindex = (result + endGroup) >> 1;
347             if (msb < getGroupMSB(gindex)) {
348                 endGroup = gindex;
349             }
350             else {
351                 result = gindex;
352             }
353         }
354         return result;
355     }
356
357     /**
358      * Gets the extended and 1.0 name when the most current unicode names
359      * fail
360      * @param ch codepoint
361      * @return name of codepoint extended or 1.0
362      */
363     public String getExtendedOr10Name(int ch)
364     {
365         String result = null;
366         if (getType(ch) == UCharacterCategory.CONTROL) {
367             result = getName(ch,
368                              UCharacterNameChoice.UNICODE_10_CHAR_NAME);
369         }
370         if (result == null) {
371             int type = getType(ch);
372             // Return unknown if the table of names above is not up to
373             // date.
374             if (type >= TYPE_NAMES_.length) {
375                 result = UNKNOWN_TYPE_NAME_;
376             }
377             else {
378                 result = TYPE_NAMES_[type];
379             }
380             synchronized (m_utilStringBuffer_) {
381                 m_utilStringBuffer_.delete(0, m_utilStringBuffer_.length());
382                 m_utilStringBuffer_.append('<');
383                 m_utilStringBuffer_.append(result);
384                 m_utilStringBuffer_.append('-');
385                 String chStr = Integer.toHexString(ch).toUpperCase();
386                 int zeros = 4 - chStr.length();
387                 while (zeros > 0) {
388                     m_utilStringBuffer_.append('0');
389                     zeros --;
390                 }
391                 m_utilStringBuffer_.append(chStr);
392                 m_utilStringBuffer_.append('>');
393                 result = m_utilStringBuffer_.toString();
394             }
395         }
396         return result;
397     }
398
399     /**
400      * Gets the MSB from the group index
401      * @param gindex group index
402      * @return the MSB of the group if gindex is valid, -1 otherwise
403      */
404     public int getGroupMSB(int gindex)
405     {
406         if (gindex >= m_groupcount_) {
407             return -1;
408         }
409         return m_groupinfo_[gindex * m_groupsize_];
410     }
411
412     /**
413      * Gets the MSB of the codepoint
414      * @param codepoint The codepoint value.
415      * @return the MSB of the codepoint
416      */
417     public static int getCodepointMSB(int codepoint)
418     {
419         return codepoint >> GROUP_SHIFT_;
420     }
421
422     /**
423      * Gets the maximum codepoint + 1 of the group
424      * @param msb most significant byte of the group
425      * @return limit codepoint of the group
426      */
427     public static int getGroupLimit(int msb)
428     {
429         return (msb << GROUP_SHIFT_) + LINES_PER_GROUP_;
430     }
431
432     /**
433      * Gets the minimum codepoint of the group
434      * @param msb most significant byte of the group
435      * @return minimum codepoint of the group
436      */
437     public static int getGroupMin(int msb)
438     {
439         return msb << GROUP_SHIFT_;
440     }
441
442     /**
443      * Gets the offset to a group
444      * @param codepoint The codepoint value.
445      * @return offset to a group
446      */
447     public static int getGroupOffset(int codepoint)
448     {
449         return codepoint & GROUP_MASK_;
450     }
451
452     /**
453      * Gets the minimum codepoint of a group
454      * @param codepoint The codepoint value.
455      * @return minimum codepoint in the group which codepoint belongs to
456      */
457     ///CLOVER:OFF
458     public static int getGroupMinFromCodepoint(int codepoint)
459     {
460         return codepoint & ~GROUP_MASK_;
461     }
462     ///CLOVER:ON
463
464     /**
465      * Get the Algorithm range length
466      * @return Algorithm range length
467      */
468     public int getAlgorithmLength()
469     {
470         return m_algorithm_.length;
471     }
472
473     /**
474      * Gets the start of the range
475      * @param index algorithm index
476      * @return algorithm range start
477      */
478     public int getAlgorithmStart(int index)
479     {
480         return m_algorithm_[index].m_rangestart_;
481     }
482
483     /**
484      * Gets the end of the range
485      * @param index algorithm index
486      * @return algorithm range end
487      */
488     public int getAlgorithmEnd(int index)
489     {
490         return m_algorithm_[index].m_rangeend_;
491     }
492
493     /**
494      * Gets the Algorithmic name of the codepoint
495      * @param index algorithmic range index
496      * @param codepoint The codepoint value.
497      * @return algorithmic name of codepoint
498      */
499     public String getAlgorithmName(int index, int codepoint)
500     {
501         String result = null;
502         synchronized (m_utilStringBuffer_) {
503             m_utilStringBuffer_.delete(0, m_utilStringBuffer_.length());
504             m_algorithm_[index].appendName(codepoint, m_utilStringBuffer_);
505             result = m_utilStringBuffer_.toString();
506         }
507         return result;
508     }
509
510     /**
511     * Gets the group name of the character
512     * @param ch character to get the group name
513     * @param choice name choice selector to choose a unicode 1.0 or newer name
514     */
515     public synchronized String getGroupName(int ch, int choice)
516     {
517         // gets the msb
518         int msb   = getCodepointMSB(ch);
519         int group = getGroup(ch);
520
521         // return this if it is an exact match
522         if (msb == m_groupinfo_[group * m_groupsize_]) {
523             int index = getGroupLengths(group, m_groupoffsets_,
524                                         m_grouplengths_);
525             int offset = ch & GROUP_MASK_;
526             return getGroupName(index + m_groupoffsets_[offset],
527                                 m_grouplengths_[offset], choice);
528         }
529
530         return null;
531     }
532
533     // these are transliterator use methods ---------------------------------
534
535     /**
536      * Gets the maximum length of any codepoint name.
537      * Equivalent to uprv_getMaxCharNameLength.
538      * @return the maximum length of any codepoint name
539      */
540     public int getMaxCharNameLength()
541     {
542         if (initNameSetsLengths()) {
543             return m_maxNameLength_;
544         }
545         else {
546             return 0;
547         }
548     }
549
550     /**
551      * Gets the maximum length of any iso comments.
552      * Equivalent to uprv_getMaxISOCommentLength.
553      * @return the maximum length of any codepoint name
554      */
555     ///CLOVER:OFF
556     public int getMaxISOCommentLength()
557     {
558         if (initNameSetsLengths()) {
559             return m_maxISOCommentLength_;
560         }
561         else {
562             return 0;
563         }
564     }
565     ///CLOVER:ON
566
567     /**
568      * Fills set with characters that are used in Unicode character names.
569      * Equivalent to uprv_getCharNameCharacters.
570      * @param set USet to receive characters. Existing contents are deleted.
571      */
572     public void getCharNameCharacters(UnicodeSet set)
573     {
574         convert(m_nameSet_, set);
575     }
576
577     /**
578      * Fills set with characters that are used in Unicode character names.
579      * Equivalent to uprv_getISOCommentCharacters.
580      * @param set USet to receive characters. Existing contents are deleted.
581      */
582     ///CLOVER:OFF
583     public void getISOCommentCharacters(UnicodeSet set)
584     {
585         convert(m_ISOCommentSet_, set);
586     }
587     ///CLOVER:ON
588
589     // package private inner class --------------------------------------
590
591     /**
592     * Algorithmic name class
593     */
594     static final class AlgorithmName
595     {
596         // package private data members ----------------------------------
597
598         /**
599         * Constant type value of the different AlgorithmName
600         */
601         static final int TYPE_0_ = 0;
602         static final int TYPE_1_ = 1;
603
604         // package private constructors ----------------------------------
605
606         /**
607         * Constructor
608         */
609         AlgorithmName()
610         {
611         }
612
613         // package private methods ---------------------------------------
614
615         /**
616         * Sets the information for accessing the algorithmic names
617         * @param rangestart starting code point that lies within this name group
618         * @param rangeend end code point that lies within this name group
619         * @param type algorithm type. There's 2 kinds of algorithmic type. First
620         *        which uses code point as part of its name and the other uses
621         *        variant postfix strings
622         * @param variant algorithmic variant
623         * @return true if values are valid
624         */
625         boolean setInfo(int rangestart, int rangeend, byte type, byte variant)
626         {
627             if (rangestart >= UCharacter.MIN_VALUE && rangestart <= rangeend
628                 && rangeend <= UCharacter.MAX_VALUE &&
629                 (type == TYPE_0_ || type == TYPE_1_)) {
630                 m_rangestart_ = rangestart;
631                 m_rangeend_ = rangeend;
632                 m_type_ = type;
633                 m_variant_ = variant;
634                 return true;
635             }
636             return false;
637         }
638
639         /**
640         * Sets the factor data
641         * @param factor Array of factor
642         * @return true if factors are valid
643         */
644         boolean setFactor(char factor[])
645         {
646             if (factor.length == m_variant_) {
647                 m_factor_ = factor;
648                 return true;
649             }
650             return false;
651         }
652
653         /**
654         * Sets the name prefix
655         * @param prefix
656         * @return true if prefix is set
657         */
658         boolean setPrefix(String prefix)
659         {
660             if (prefix != null && prefix.length() > 0) {
661                 m_prefix_ = prefix;
662                 return true;
663             }
664             return false;
665         }
666
667         /**
668         * Sets the variant factorized name data
669         * @param string variant factorized name data
670         * @return true if values are set
671         */
672         boolean setFactorString(byte string[])
673         {
674             // factor and variant string can be empty for things like
675             // hanggul code points
676             m_factorstring_ = string;
677             return true;
678         }
679
680         /**
681         * Checks if code point lies in Algorithm object at index
682         * @param ch code point
683         */
684         boolean contains(int ch)
685         {
686             return m_rangestart_ <= ch && ch <= m_rangeend_;
687         }
688
689         /**
690         * Appends algorithm name of code point into StringBuffer.
691         * Note this method does not check for validity of code point in Algorithm,
692         * result is undefined if code point does not belong in Algorithm.
693         * @param ch code point
694         * @param str StringBuffer to append to
695         */
696         void appendName(int ch, StringBuffer str)
697         {
698             str.append(m_prefix_);
699             switch (m_type_)
700             {
701                 case TYPE_0_:
702                     // prefix followed by hex digits indicating variants
703                 str.append(Utility.hex(ch,m_variant_));
704                     break;
705                 case TYPE_1_:
706                     // prefix followed by factorized-elements
707                     int offset = ch - m_rangestart_;
708                     int indexes[] = m_utilIntBuffer_;
709                     int factor;
710
711                     // write elements according to the factors
712                     // the factorized elements are determined by modulo
713                     // arithmetic
714                     synchronized (m_utilIntBuffer_) {
715                         for (int i = m_variant_ - 1; i > 0; i --)
716                         {
717                             factor = m_factor_[i] & 0x00FF;
718                             indexes[i] = offset % factor;
719                             offset /= factor;
720                         }
721
722                         // we don't need to calculate the last modulus because
723                         // start <= code <= end guarantees here that
724                         // code <= factors[0]
725                         indexes[0] = offset;
726
727                         // joining up the factorized strings
728                         str.append(getFactorString(indexes, m_variant_));
729                     }
730                     break;
731             }
732         }
733
734         /**
735         * Gets the character for the argument algorithmic name
736         * @return the algorithmic char or -1 otherwise.
737         */
738         int getChar(String name)
739         {
740             int prefixlen = m_prefix_.length();
741             if (name.length() < prefixlen ||
742                 !m_prefix_.equals(name.substring(0, prefixlen))) {
743                 return -1;
744             }
745
746             switch (m_type_)
747             {
748                 case TYPE_0_ :
749                 try
750                 {
751                     int result = Integer.parseInt(name.substring(prefixlen),
752                                                   16);
753                     // does it fit into the range?
754                     if (m_rangestart_ <= result && result <= m_rangeend_) {
755                         return result;
756                     }
757                 }
758                 catch (NumberFormatException e)
759                 {
760                     return -1;
761                 }
762                 break;
763                 case TYPE_1_ :
764                     // repetitative suffix name comparison done here
765                     // offset is the character code - start
766                     for (int ch = m_rangestart_; ch <= m_rangeend_; ch ++)
767                     {
768                         int offset = ch - m_rangestart_;
769                         int indexes[] = m_utilIntBuffer_;
770                         int factor;
771
772                         // write elements according to the factors
773                         // the factorized elements are determined by modulo
774                         // arithmetic
775                         synchronized (m_utilIntBuffer_) {
776                             for (int i = m_variant_ - 1; i > 0; i --)
777                             {
778                                 factor = m_factor_[i] & 0x00FF;
779                                 indexes[i] = offset % factor;
780                                 offset /= factor;
781                             }
782
783                             // we don't need to calculate the last modulus
784                             // because start <= code <= end guarantees here that
785                             // code <= factors[0]
786                             indexes[0] = offset;
787
788                             // joining up the factorized strings
789                             if (compareFactorString(indexes, m_variant_, name,
790                                                     prefixlen)) {
791                                 return ch;
792                             }
793                         }
794                     }
795             }
796
797             return -1;
798         }
799
800         /**
801          * Adds all chars in the set of algorithmic names into the set.
802          * Equivalent to part of calcAlgNameSetsLengths.
803          * @param set int set to add the chars of the algorithm names into
804          * @param maxlength maximum length to compare to
805          * @return the length that is either maxlength of the length of this
806          *         algorithm name if it is longer than maxlength
807          */
808         int add(int set[], int maxlength)
809         {
810             // prefix length
811             int length = UCharacterName.add(set, m_prefix_);
812             switch (m_type_) {
813                 case TYPE_0_ : {
814                     // name = prefix + (range->variant times) hex-digits
815                     // prefix
816                     length += m_variant_;
817                     /* synwee to check
818                      * addString(set, (const char *)(range + 1))
819                                        + range->variant;*/
820                     break;
821                 }
822                 case TYPE_1_ : {
823                     // name = prefix factorized-elements
824                     // get the set and maximum factor suffix length for each
825                     // factor
826                     for (int i = m_variant_ - 1; i > 0; i --)
827                     {
828                         int maxfactorlength = 0;
829                         int count = 0;
830                         for (int factor = m_factor_[i]; factor > 0; -- factor) {
831                             synchronized (m_utilStringBuffer_) {
832                                 m_utilStringBuffer_.delete(0,
833                                                 m_utilStringBuffer_.length());
834                                 count
835                                   = UCharacterUtility.getNullTermByteSubString(
836                                                 m_utilStringBuffer_,
837                                                 m_factorstring_, count);
838                                 UCharacterName.add(set, m_utilStringBuffer_);
839                                 if (m_utilStringBuffer_.length()
840                                                             > maxfactorlength)
841                                 {
842                                     maxfactorlength
843                                                 = m_utilStringBuffer_.length();
844                                 }
845                             }
846                         }
847                         length += maxfactorlength;
848                     }
849                 }
850             }
851             if (length > maxlength) {
852                 return length;
853             }
854             return maxlength;
855         }
856
857         // private data members ------------------------------------------
858
859         /**
860         * Algorithmic data information
861         */
862         private int m_rangestart_;
863         private int m_rangeend_;
864         private byte m_type_;
865         private byte m_variant_;
866         private char m_factor_[];
867         private String m_prefix_;
868         private byte m_factorstring_[];
869         /**
870          * Utility StringBuffer
871          */
872         private StringBuffer m_utilStringBuffer_ = new StringBuffer();
873         /**
874          * Utility int buffer
875          */
876         private int m_utilIntBuffer_[] = new int[256];
877
878         // private methods -----------------------------------------------
879
880         /**
881         * Gets the indexth string in each of the argument factor block
882         * @param index array with each index corresponding to each factor block
883         * @param length length of the array index
884         * @return the combined string of the array of indexth factor string in
885         *         factor block
886         */
887         private String getFactorString(int index[], int length)
888         {
889             int size = m_factor_.length;
890             if (index == null || length != size) {
891                 return null;
892             }
893
894             synchronized (m_utilStringBuffer_) {
895                 m_utilStringBuffer_.delete(0, m_utilStringBuffer_.length());
896                 int count = 0;
897                 int factor;
898                 size --;
899                 for (int i = 0; i <= size; i ++) {
900                     factor = m_factor_[i];
901                     count = UCharacterUtility.skipNullTermByteSubString(
902                                              m_factorstring_, count, index[i]);
903                     count = UCharacterUtility.getNullTermByteSubString(
904                                           m_utilStringBuffer_, m_factorstring_,
905                                           count);
906                     if (i != size) {
907                         count = UCharacterUtility.skipNullTermByteSubString(
908                                                        m_factorstring_, count,
909                                                        factor - index[i] - 1);
910                     }
911                 }
912                 return m_utilStringBuffer_.toString();
913             }
914         }
915
916         /**
917         * Compares the indexth string in each of the argument factor block with
918         * the argument string
919         * @param index array with each index corresponding to each factor block
920         * @param length index array length
921         * @param str string to compare with
922         * @param offset of str to start comparison
923         * @return true if string matches
924         */
925         private boolean compareFactorString(int index[], int length, String str,
926                                             int offset)
927         {
928             int size = m_factor_.length;
929             if (index == null || length != size)
930                 return false;
931
932             int count = 0;
933             int strcount = offset;
934             int factor;
935             size --;
936             for (int i = 0; i <= size; i ++)
937             {
938                 factor = m_factor_[i];
939                 count = UCharacterUtility.skipNullTermByteSubString(
940                                           m_factorstring_, count, index[i]);
941                 strcount = UCharacterUtility.compareNullTermByteSubString(str,
942                                           m_factorstring_, strcount, count);
943                 if (strcount < 0) {
944                     return false;
945                 }
946
947                 if (i != size) {
948                     count = UCharacterUtility.skipNullTermByteSubString(
949                                   m_factorstring_, count, factor - index[i]);
950                 }
951             }
952             if (strcount != str.length()) {
953                 return false;
954             }
955             return true;
956         }
957     }
958
959     // package private data members --------------------------------------
960
961     /**
962      * Size of each groups
963      */
964     int m_groupsize_ = 0;
965
966     // package private methods --------------------------------------------
967
968     /**
969     * Sets the token data
970     * @param token array of tokens
971     * @param tokenstring array of string values of the tokens
972     * @return false if there is a data error
973     */
974     boolean setToken(char token[], byte tokenstring[])
975     {
976         if (token != null && tokenstring != null && token.length > 0 &&
977             tokenstring.length > 0) {
978             m_tokentable_ = token;
979             m_tokenstring_ = tokenstring;
980             return true;
981         }
982         return false;
983     }
984
985     /**
986     * Set the algorithm name information array
987     * @param alg Algorithm information array
988     * @return true if the group string offset has been set correctly
989     */
990     boolean setAlgorithm(AlgorithmName alg[])
991     {
992         if (alg != null && alg.length != 0) {
993             m_algorithm_ = alg;
994             return true;
995         }
996         return false;
997     }
998
999     /**
1000     * Sets the number of group and size of each group in number of char
1001     * @param count number of groups
1002     * @param size size of group in char
1003     * @return true if group size is set correctly
1004     */
1005     boolean setGroupCountSize(int count, int size)
1006     {
1007         if (count <= 0 || size <= 0) {
1008             return false;
1009         }
1010         m_groupcount_ = count;
1011         m_groupsize_ = size;
1012         return true;
1013     }
1014
1015     /**
1016     * Sets the group name data
1017     * @param group index information array
1018     * @param groupstring name information array
1019     * @return false if there is a data error
1020     */
1021     boolean setGroup(char group[], byte groupstring[])
1022     {
1023         if (group != null && groupstring != null && group.length > 0 &&
1024             groupstring.length > 0) {
1025             m_groupinfo_ = group;
1026             m_groupstring_ = groupstring;
1027             return true;
1028         }
1029         return false;
1030     }
1031
1032     // private data members ----------------------------------------------
1033
1034     /**
1035     * Data used in unames.icu
1036     */
1037     private char m_tokentable_[];
1038     private byte m_tokenstring_[];
1039     private char m_groupinfo_[];
1040     private byte m_groupstring_[];
1041     private AlgorithmName m_algorithm_[];
1042
1043     /**
1044     * Group use.  Note - access must be synchronized.
1045     */
1046     private char m_groupoffsets_[] = new char[LINES_PER_GROUP_ + 1];
1047     private char m_grouplengths_[] = new char[LINES_PER_GROUP_ + 1];
1048
1049     /**
1050     * Default name of the name datafile
1051     */
1052     private static final String NAME_FILE_NAME_ = ICUResourceBundle.ICU_BUNDLE+"/unames.icu";
1053     /**
1054     * Shift count to retrieve group information
1055     */
1056     private static final int GROUP_SHIFT_ = 5;
1057     /**
1058     * Mask to retrieve the offset for a particular character within a group
1059     */
1060     private static final int GROUP_MASK_ = LINES_PER_GROUP_ - 1;
1061     /**
1062     * Default buffer size of datafile
1063     */
1064     private static final int NAME_BUFFER_SIZE_ = 100000;
1065
1066     /**
1067     * Position of offsethigh in group information array
1068     */
1069     private static final int OFFSET_HIGH_OFFSET_ = 1;
1070
1071     /**
1072     * Position of offsetlow in group information array
1073     */
1074     private static final int OFFSET_LOW_OFFSET_ = 2;
1075     /**
1076     * Double nibble indicator, any nibble > this number has to be combined
1077     * with its following nibble
1078     */
1079     private static final int SINGLE_NIBBLE_MAX_ = 11;
1080
1081     /*
1082      * Maximum length of character names (regular & 1.0).
1083      */
1084     //private static int MAX_NAME_LENGTH_ = 0;
1085     /*
1086      * Maximum length of ISO comments.
1087      */
1088     //private static int MAX_ISO_COMMENT_LENGTH_ = 0;
1089
1090     /**
1091      * Set of chars used in character names (regular & 1.0).
1092      * Chars are platform-dependent (can be EBCDIC).
1093      */
1094     private int m_nameSet_[] = new int[8];
1095     /**
1096      * Set of chars used in ISO comments. (regular & 1.0).
1097      * Chars are platform-dependent (can be EBCDIC).
1098      */
1099     private int m_ISOCommentSet_[] = new int[8];
1100     /**
1101      * Utility StringBuffer
1102      */
1103     private StringBuffer m_utilStringBuffer_ = new StringBuffer();
1104     /**
1105      * Utility int buffer
1106      */
1107     private int m_utilIntBuffer_[] = new int[2];
1108     /**
1109      * Maximum ISO comment length
1110      */
1111     private int m_maxISOCommentLength_;
1112     /**
1113      * Maximum name length
1114      */
1115     private int m_maxNameLength_;
1116     /**
1117      * Type names used for extended names
1118      */
1119     private static final String TYPE_NAMES_[] = {"unassigned",
1120                                                  "uppercase letter",
1121                                                  "lowercase letter",
1122                                                  "titlecase letter",
1123                                                  "modifier letter",
1124                                                  "other letter",
1125                                                  "non spacing mark",
1126                                                  "enclosing mark",
1127                                                  "combining spacing mark",
1128                                                  "decimal digit number",
1129                                                  "letter number",
1130                                                  "other number",
1131                                                  "space separator",
1132                                                  "line separator",
1133                                                  "paragraph separator",
1134                                                  "control",
1135                                                  "format",
1136                                                  "private use area",
1137                                                  "surrogate",
1138                                                  "dash punctuation",
1139                                                  "start punctuation",
1140                                                  "end punctuation",
1141                                                  "connector punctuation",
1142                                                  "other punctuation",
1143                                                  "math symbol",
1144                                                  "currency symbol",
1145                                                  "modifier symbol",
1146                                                  "other symbol",
1147                                                  "initial punctuation",
1148                                                  "final punctuation",
1149                                                  "noncharacter",
1150                                                  "lead surrogate",
1151                                                  "trail surrogate"};
1152     /**
1153      * Unknown type name
1154      */
1155     private static final String UNKNOWN_TYPE_NAME_ = "unknown";
1156     /**
1157      * Not a character type
1158      */
1159     private static final int NON_CHARACTER_
1160                                     = UCharacterCategory.CHAR_CATEGORY_COUNT;
1161     /**
1162     * Lead surrogate type
1163     */
1164     private static final int LEAD_SURROGATE_
1165                                   = UCharacterCategory.CHAR_CATEGORY_COUNT + 1;
1166     /**
1167     * Trail surrogate type
1168     */
1169     private static final int TRAIL_SURROGATE_
1170                                   = UCharacterCategory.CHAR_CATEGORY_COUNT + 2;
1171     /**
1172     * Extended category count
1173     */
1174     static final int EXTENDED_CATEGORY_
1175                                   = UCharacterCategory.CHAR_CATEGORY_COUNT + 3;
1176
1177     // private constructor ------------------------------------------------
1178
1179     /**
1180     * <p>Protected constructor for use in UCharacter.</p>
1181     * @exception IOException thrown when data reading fails
1182     */
1183     private UCharacterName() throws IOException
1184     {
1185         InputStream is = ICUData.getRequiredStream(NAME_FILE_NAME_);
1186         BufferedInputStream b = new BufferedInputStream(is, NAME_BUFFER_SIZE_);
1187         UCharacterNameReader reader = new UCharacterNameReader(b);
1188         reader.read(this);
1189         b.close();
1190     }
1191
1192     // private methods ---------------------------------------------------
1193
1194     /**
1195     * Gets the algorithmic name for the argument character
1196     * @param ch character to determine name for
1197     * @param choice name choice
1198     * @return the algorithmic name or null if not found
1199     */
1200     private String getAlgName(int ch, int choice)
1201     {
1202         /* Only the normative character name can be algorithmic. */
1203         if (choice == UCharacterNameChoice.UNICODE_CHAR_NAME ||
1204             choice == UCharacterNameChoice.EXTENDED_CHAR_NAME
1205         ) {
1206             // index in terms integer index
1207             synchronized (m_utilStringBuffer_) {
1208                 m_utilStringBuffer_.delete(0, m_utilStringBuffer_.length());
1209
1210                 for (int index = m_algorithm_.length - 1; index >= 0; index --)
1211                 {
1212                    if (m_algorithm_[index].contains(ch)) {
1213                       m_algorithm_[index].appendName(ch, m_utilStringBuffer_);
1214                       return m_utilStringBuffer_.toString();
1215                    }
1216                 }
1217             }
1218         }
1219         return null;
1220     }
1221
1222     /**
1223     * Getting the character with the tokenized argument name
1224     * @param name of the character
1225     * @return character with the tokenized argument name or -1 if character
1226     *         is not found
1227     */
1228     private synchronized int getGroupChar(String name, int choice)
1229     {
1230         for (int i = 0; i < m_groupcount_; i ++) {
1231             // populating the data set of grouptable
1232
1233             int startgpstrindex = getGroupLengths(i, m_groupoffsets_,
1234                                                   m_grouplengths_);
1235
1236             // shift out to function
1237             int result = getGroupChar(startgpstrindex, m_grouplengths_, name,
1238                                       choice);
1239             if (result != -1) {
1240                 return (m_groupinfo_[i * m_groupsize_] << GROUP_SHIFT_)
1241                          | result;
1242             }
1243         }
1244         return -1;
1245     }
1246
1247     /**
1248     * Compares and retrieve character if name is found within the argument
1249     * group
1250     * @param index index where the set of names reside in the group block
1251     * @param length list of lengths of the strings
1252     * @param name character name to search for
1253     * @param choice of either 1.0 or the most current unicode name
1254     * @return relative character in the group which matches name, otherwise if
1255     *         not found, -1 will be returned
1256     */
1257     private int getGroupChar(int index, char length[], String name,
1258                              int choice)
1259     {
1260         byte b = 0;
1261         char token;
1262         int len;
1263         int namelen = name.length();
1264         int nindex;
1265         int count;
1266
1267         for (int result = 0; result <= LINES_PER_GROUP_; result ++) {
1268             nindex = 0;
1269             len = length[result];
1270
1271             if (choice != UCharacterNameChoice.UNICODE_CHAR_NAME &&
1272                 choice != UCharacterNameChoice.EXTENDED_CHAR_NAME
1273             ) {
1274                 /*
1275                  * skip the modern name if it is not requested _and_
1276                  * if the semicolon byte value is a character, not a token number
1277                  */
1278                 int fieldIndex= choice==UCharacterNameChoice.ISO_COMMENT_ ? 2 : choice;
1279                 do {
1280                     int oldindex = index;
1281                     index += UCharacterUtility.skipByteSubString(m_groupstring_,
1282                                                          index, len, (byte)';');
1283                     len -= (index - oldindex);
1284                 } while(--fieldIndex>0);
1285             }
1286
1287             // number of tokens is > the length of the name
1288             // write each letter directly, and write a token word per token
1289             for (count = 0; count < len && nindex != -1 && nindex < namelen;
1290                 ) {
1291                 b = m_groupstring_[index + count];
1292                 count ++;
1293
1294                 if (b >= m_tokentable_.length) {
1295                     if (name.charAt(nindex ++) != (b & 0xFF)) {
1296                         nindex = -1;
1297                     }
1298                 }
1299                 else {
1300                     token = m_tokentable_[b & 0xFF];
1301                     if (token == 0xFFFE) {
1302                         // this is a lead byte for a double-byte token
1303                         token = m_tokentable_[b << 8 |
1304                                    (m_groupstring_[index + count] & 0x00ff)];
1305                         count ++;
1306                     }
1307                     if (token == 0xFFFF) {
1308                         if (name.charAt(nindex ++) != (b & 0xFF)) {
1309                             nindex = -1;
1310                         }
1311                     }
1312                     else {
1313                         // compare token with name
1314                         nindex = UCharacterUtility.compareNullTermByteSubString(
1315                                         name, m_tokenstring_, nindex, token);
1316                     }
1317                 }
1318             }
1319
1320             if (namelen == nindex &&
1321                 (count == len || m_groupstring_[index + count] == ';')) {
1322                 return result;
1323             }
1324
1325             index += len;
1326         }
1327         return -1;
1328     }
1329
1330     /**
1331     * Gets the character extended type
1332     * @param ch character to be tested
1333     * @return extended type it is associated with
1334     */
1335     private static int getType(int ch)
1336     {
1337         if (UCharacterUtility.isNonCharacter(ch)) {
1338             // not a character we return a invalid category count
1339             return NON_CHARACTER_;
1340         }
1341         int result = UCharacter.getType(ch);
1342         if (result == UCharacterCategory.SURROGATE) {
1343             if (ch <= UTF16.LEAD_SURROGATE_MAX_VALUE) {
1344                 result = LEAD_SURROGATE_;
1345             }
1346             else {
1347                 result = TRAIL_SURROGATE_;
1348             }
1349         }
1350         return result;
1351     }
1352
1353     /**
1354     * Getting the character with extended name of the form <....>.
1355     * @param name of the character to be found
1356     * @param choice name choice
1357     * @return character associated with the name, -1 if such character is not
1358     *                   found and -2 if we should continue with the search.
1359     */
1360     private static int getExtendedChar(String name, int choice)
1361     {
1362         if (name.charAt(0) == '<') {
1363             if (choice == UCharacterNameChoice.EXTENDED_CHAR_NAME) {
1364                 int endIndex = name.length() - 1;
1365                 if (name.charAt(endIndex) == '>') {
1366                     int startIndex = name.lastIndexOf('-');
1367                     if (startIndex >= 0) { // We've got a category.
1368                         startIndex ++;
1369                         int result = -1;
1370                         try {
1371                             result = Integer.parseInt(
1372                                         name.substring(startIndex, endIndex),
1373                                         16);
1374                         }
1375                         catch (NumberFormatException e) {
1376                             return -1;
1377                         }
1378                         // Now validate the category name. We could use a
1379                         // binary search, or a trie, if we really wanted to.
1380                         String type = name.substring(1, startIndex - 1);
1381                         int length = TYPE_NAMES_.length;
1382                         for (int i = 0; i < length; ++ i) {
1383                             if (type.compareTo(TYPE_NAMES_[i]) == 0) {
1384                                 if (getType(result) == i) {
1385                                     return result;
1386                                 }
1387                                 break;
1388                             }
1389                         }
1390                     }
1391                 }
1392             }
1393             return -1;
1394         }
1395         return -2;
1396     }
1397
1398     // sets of name characters, maximum name lengths -----------------------
1399
1400     /**
1401      * Adds a codepoint into a set of ints.
1402      * Equivalent to SET_ADD.
1403      * @param set set to add to
1404      * @param ch 16 bit char to add
1405      */
1406     private static void add(int set[], char ch)
1407     {
1408         set[ch >>> 5] |= 1 << (ch & 0x1f);
1409     }
1410
1411     /**
1412      * Checks if a codepoint is a part of a set of ints.
1413      * Equivalent to SET_CONTAINS.
1414      * @param set set to check in
1415      * @param ch 16 bit char to check
1416      * @return true if codepoint is part of the set, false otherwise
1417      */
1418     private static boolean contains(int set[], char ch)
1419     {
1420         return (set[ch >>> 5] & (1 << (ch & 0x1f))) != 0;
1421     }
1422
1423     /**
1424      * Adds all characters of the argument str and gets the length
1425      * Equivalent to calcStringSetLength.
1426      * @param set set to add all chars of str to
1427      * @param str string to add
1428      */
1429     private static int add(int set[], String str)
1430     {
1431         int result = str.length();
1432
1433         for (int i = result - 1; i >= 0; i --) {
1434             add(set, str.charAt(i));
1435         }
1436         return result;
1437     }
1438
1439     /**
1440      * Adds all characters of the argument str and gets the length
1441      * Equivalent to calcStringSetLength.
1442      * @param set set to add all chars of str to
1443      * @param str string to add
1444      */
1445     private static int add(int set[], StringBuffer str)
1446     {
1447         int result = str.length();
1448
1449         for (int i = result - 1; i >= 0; i --) {
1450             add(set, str.charAt(i));
1451         }
1452         return result;
1453     }
1454
1455     /**
1456      * Adds all algorithmic names into the name set.
1457      * Equivalent to part of calcAlgNameSetsLengths.
1458      * @param maxlength length to compare to
1459      * @return the maximum length of any possible algorithmic name if it is >
1460      *         maxlength, otherwise maxlength is returned.
1461      */
1462     private int addAlgorithmName(int maxlength)
1463     {
1464         int result = 0;
1465         for (int i = m_algorithm_.length - 1; i >= 0; i --) {
1466             result = m_algorithm_[i].add(m_nameSet_, maxlength);
1467             if (result > maxlength) {
1468                 maxlength = result;
1469             }
1470         }
1471         return maxlength;
1472     }
1473
1474     /**
1475      * Adds all extended names into the name set.
1476      * Equivalent to part of calcExtNameSetsLengths.
1477      * @param maxlength length to compare to
1478      * @return the maxlength of any possible extended name.
1479      */
1480     private int addExtendedName(int maxlength)
1481     {
1482         for (int i = TYPE_NAMES_.length - 1; i >= 0; i --) {
1483             // for each category, count the length of the category name
1484             // plus 9 =
1485             // 2 for <>
1486             // 1 for -
1487             // 6 for most hex digits per code point
1488             int length = 9 + add(m_nameSet_, TYPE_NAMES_[i]);
1489             if (length > maxlength) {
1490                 maxlength = length;
1491             }
1492         }
1493         return maxlength;
1494     }
1495
1496     /**
1497      * Adds names of a group to the argument set.
1498      * Equivalent to calcNameSetLength.
1499      * @param offset of the group name string in byte count
1500      * @param length of the group name string
1501      * @param tokenlength array to store the length of each token
1502      * @param set to add to
1503      * @return the length of the name string and the length of the group
1504      *         string parsed
1505      */
1506     private int[] addGroupName(int offset, int length, byte tokenlength[],
1507                                int set[])
1508     {
1509         int resultnlength = 0;
1510         int resultplength = 0;
1511         while (resultplength < length) {
1512             char b = (char)(m_groupstring_[offset + resultplength] & 0xff);
1513             resultplength ++;
1514             if (b == ';') {
1515                 break;
1516             }
1517
1518             if (b >= m_tokentable_.length) {
1519                 add(set, b); // implicit letter
1520                 resultnlength ++;
1521             }
1522             else {
1523                 char token = m_tokentable_[b & 0x00ff];
1524                 if (token == 0xFFFE) {
1525                     // this is a lead byte for a double-byte token
1526                     b = (char)(b << 8 | (m_groupstring_[offset + resultplength]
1527                                          & 0x00ff));
1528                     token = m_tokentable_[b];
1529                     resultplength ++;
1530                 }
1531                 if (token == 0xFFFF) {
1532                     add(set, b);
1533                     resultnlength ++;
1534                 }
1535                 else {
1536                     // count token word
1537                     // use cached token length
1538                     byte tlength = tokenlength[b];
1539                     if (tlength == 0) {
1540                         synchronized (m_utilStringBuffer_) {
1541                             m_utilStringBuffer_.delete(0,
1542                                                  m_utilStringBuffer_.length());
1543                             UCharacterUtility.getNullTermByteSubString(
1544                                            m_utilStringBuffer_, m_tokenstring_,
1545                                            token);
1546                             tlength = (byte)add(set, m_utilStringBuffer_);
1547                         }
1548                         tokenlength[b] = tlength;
1549                     }
1550                     resultnlength += tlength;
1551                 }
1552             }
1553         }
1554         m_utilIntBuffer_[0] = resultnlength;
1555         m_utilIntBuffer_[1] = resultplength;
1556         return m_utilIntBuffer_;
1557     }
1558
1559     /**
1560      * Adds names of all group to the argument set.
1561      * Sets the data member m_max*Length_.
1562      * Method called only once.
1563      * Equivalent to calcGroupNameSetsLength.
1564      * @param maxlength length to compare to
1565      */
1566     private void addGroupName(int maxlength)
1567     {
1568         int maxisolength = 0;
1569         char offsets[] = new char[LINES_PER_GROUP_ + 2];
1570         char lengths[] = new char[LINES_PER_GROUP_ + 2];
1571         byte tokenlengths[] = new byte[m_tokentable_.length];
1572
1573         // enumerate all groups
1574         // for (int i = m_groupcount_ - 1; i >= 0; i --) {
1575         for (int i = 0; i < m_groupcount_ ; i ++) {
1576             int offset = getGroupLengths(i, offsets, lengths);
1577             // enumerate all lines in each group
1578             // for (int linenumber = LINES_PER_GROUP_ - 1; linenumber >= 0;
1579             //    linenumber --) {
1580             for (int linenumber = 0; linenumber < LINES_PER_GROUP_;
1581                 linenumber ++) {
1582                 int lineoffset = offset + offsets[linenumber];
1583                 int length = lengths[linenumber];
1584                 if (length == 0) {
1585                     continue;
1586                 }
1587
1588                 // read regular name
1589                 int parsed[] = addGroupName(lineoffset, length, tokenlengths,
1590                                             m_nameSet_);
1591                 if (parsed[0] > maxlength) {
1592                     // 0 for name length
1593                     maxlength = parsed[0];
1594                 }
1595                 lineoffset += parsed[1];
1596                 if (parsed[1] >= length) {
1597                     // 1 for parsed group string length
1598                     continue;
1599                 }
1600                 length -= parsed[1];
1601                 // read Unicode 1.0 name
1602                 parsed = addGroupName(lineoffset, length, tokenlengths,
1603                                       m_nameSet_);
1604                 if (parsed[0] > maxlength) {
1605                     // 0 for name length
1606                     maxlength = parsed[0];
1607                 }
1608                 lineoffset += parsed[1];
1609                 if (parsed[1] >= length) {
1610                     // 1 for parsed group string length
1611                     continue;
1612                 }
1613                 length -= parsed[1];
1614                 // read ISO comment
1615                 parsed = addGroupName(lineoffset, length, tokenlengths,
1616                                       m_ISOCommentSet_);
1617                 if (parsed[1] > maxisolength) {
1618                     maxisolength = length;
1619                 }
1620             }
1621         }
1622
1623         // set gMax... - name length last for threading
1624         m_maxISOCommentLength_ = maxisolength;
1625         m_maxNameLength_ = maxlength;
1626     }
1627
1628     /**
1629      * Sets up the name sets and the calculation of the maximum lengths.
1630      * Equivalent to calcNameSetsLengths.
1631      */
1632     private boolean initNameSetsLengths()
1633     {
1634         if (m_maxNameLength_ > 0) {
1635             return true;
1636         }
1637
1638         String extra = "0123456789ABCDEF<>-";
1639         // set hex digits, used in various names, and <>-, used in extended
1640         // names
1641         for (int i = extra.length() - 1; i >= 0; i --) {
1642             add(m_nameSet_, extra.charAt(i));
1643         }
1644
1645         // set sets and lengths from algorithmic names
1646         m_maxNameLength_ = addAlgorithmName(0);
1647         // set sets and lengths from extended names
1648         m_maxNameLength_ = addExtendedName(m_maxNameLength_);
1649         // set sets and lengths from group names, set global maximum values
1650         addGroupName(m_maxNameLength_);
1651         return true;
1652     }
1653
1654     /**
1655      * Converts the char set cset into a Unicode set uset.
1656      * Equivalent to charSetToUSet.
1657      * @param set Set of 256 bit flags corresponding to a set of chars.
1658      * @param uset USet to receive characters. Existing contents are deleted.
1659      */
1660     private void convert(int set[], UnicodeSet uset)
1661     {
1662         uset.clear();
1663         if (!initNameSetsLengths()) {
1664             return;
1665         }
1666
1667         // build a char string with all chars that are used in character names
1668         for (char c = 255; c > 0; c --) {
1669             if (contains(set, c)) {
1670                 uset.add(c);
1671             }
1672         }
1673     }
1674 }