2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl;
9 import java.io.BufferedInputStream;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.util.MissingResourceException;
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;
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
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
35 public final class UCharacterName
37 // public data members ----------------------------------------------
40 * public singleton instance
42 public static final UCharacterName INSTANCE;
46 INSTANCE = new UCharacterName();
47 } catch (IOException e) {
49 throw new MissingResourceException("Could not construct UCharacterName. Missing unames.icu","","");
55 * Number of lines per group
58 public static final int LINES_PER_GROUP_ = 1 << 5;
60 * Maximum number of groups
62 public int m_groupcount_ = 0;
64 // public methods ---------------------------------------------------
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
71 * The name contains only "invariant" characters
72 * like A-Z, 0-9, space, and '-'.
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
78 public String getName(int ch, int choice)
80 if (ch < UCharacter.MIN_VALUE || ch > UCharacter.MAX_VALUE ||
81 choice > UCharacterNameChoice.CHAR_NAME_CHOICE_COUNT) {
87 result = getAlgName(ch, choice);
89 // getting normal character name
90 if (result == null || result.length() == 0) {
91 if (choice == UCharacterNameChoice.EXTENDED_CHAR_NAME) {
92 result = getExtendedName(ch);
94 result = getGroupName(ch, choice);
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
108 public int getCharFromName(int choice, String name)
110 // checks for illegal arguments
111 if (choice >= UCharacterNameChoice.CHAR_NAME_CHOICE_COUNT ||
112 name == null || name.length() == 0) {
116 // try extended names first
117 int result = getExtendedChar(name.toLowerCase(), choice);
122 String upperCaseName = name.toUpperCase();
123 // try algorithmic names first, if fails then try group names
124 // int result = getAlgorithmChar(choice, uppercasename);
126 if (choice == UCharacterNameChoice.UNICODE_CHAR_NAME ||
127 choice == UCharacterNameChoice.EXTENDED_CHAR_NAME
130 if (m_algorithm_ != null) {
131 count = m_algorithm_.length;
133 for (count --; count >= 0; count --) {
134 result = m_algorithm_[count].getChar(upperCaseName);
141 if (choice == UCharacterNameChoice.EXTENDED_CHAR_NAME) {
142 result = getGroupChar(upperCaseName,
143 UCharacterNameChoice.UNICODE_CHAR_NAME);
145 result = getGroupChar(upperCaseName,
146 UCharacterNameChoice.UNICODE_10_CHAR_NAME);
149 result = getGroupChar(upperCaseName,
150 UCharacterNameChoice.CHAR_NAME_ALIAS);
154 result = getGroupChar(upperCaseName, choice);
159 // these are all UCharacterNameIterator use methods -------------------
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
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
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
177 public int getGroupLengths(int index, char offsets[], char lengths[])
179 char length = 0xffff;
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_]);
190 // all 32 lengths must be read to get the offset of the first group
192 for (int i = 0; i < LINES_PER_GROUP_; stringoffset ++) {
193 b = m_groupstring_[stringoffset];
198 n = (byte)((b >> shift) & 0x0F);
199 if (length == 0xffff && n > SINGLE_NIBBLE_MAX_) {
200 length = (char)((n - 12) << 4);
203 if (length != 0xffff) {
204 lengths[i] = (char)((length | n) + 12);
207 lengths[i] = (char)n;
210 if (i < LINES_PER_GROUP_) {
211 offsets[i + 1] = (char)(offsets[i] + lengths[i]);
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
238 public String getGroupName(int index, int length, int choice)
240 if (choice != UCharacterNameChoice.UNICODE_CHAR_NAME &&
241 choice != UCharacterNameChoice.EXTENDED_CHAR_NAME
243 if (';' >= m_tokentable_.length || m_tokentable_[';'] == 0xFFFF) {
245 * skip the modern name if it is not requested _and_
246 * if the semicolon byte value is a character, not a token number
248 int fieldIndex= choice==UCharacterNameChoice.ISO_COMMENT_ ? 2 : choice;
250 int oldindex = index;
251 index += UCharacterUtility.skipByteSubString(m_groupstring_,
252 index, length, (byte)';');
253 length -= (index - oldindex);
254 } while(--fieldIndex>0);
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
264 synchronized (m_utilStringBuffer_) {
265 m_utilStringBuffer_.delete(0, m_utilStringBuffer_.length());
268 for (int i = 0; i < length;) {
269 b = m_groupstring_[index + i];
272 if (b >= m_tokentable_.length) {
276 m_utilStringBuffer_.append(b); // implicit letter
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)];
286 if (token == 0xFFFF) {
288 // skip the semicolon if we are seeking extended
289 // names and there was no 2.0 name but there
291 if (m_utilStringBuffer_.length() == 0 && choice ==
292 UCharacterNameChoice.EXTENDED_CHAR_NAME) {
298 m_utilStringBuffer_.append((char)(b & 0x00ff));
300 else { // write token word
301 UCharacterUtility.getNullTermByteSubString(
302 m_utilStringBuffer_, m_tokenstring_, token);
307 if (m_utilStringBuffer_.length() > 0) {
308 return m_utilStringBuffer_.toString();
315 * Retrieves the extended name
317 public String getExtendedName(int ch)
319 String result = getName(ch, UCharacterNameChoice.UNICODE_CHAR_NAME);
320 if (result == null) {
321 if (getType(ch) == UCharacterCategory.CONTROL) {
323 UCharacterNameChoice.UNICODE_10_CHAR_NAME);
325 if (result == null) {
326 result = getExtendedOr10Name(ch);
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.
337 public int getGroup(int codepoint)
339 int endGroup = m_groupcount_;
340 int msb = getCodepointMSB(codepoint);
342 // binary search for the group of names that contains the one for
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)) {
358 * Gets the extended and 1.0 name when the most current unicode names
360 * @param ch codepoint
361 * @return name of codepoint extended or 1.0
363 public String getExtendedOr10Name(int ch)
365 String result = null;
366 if (getType(ch) == UCharacterCategory.CONTROL) {
368 UCharacterNameChoice.UNICODE_10_CHAR_NAME);
370 if (result == null) {
371 int type = getType(ch);
372 // Return unknown if the table of names above is not up to
374 if (type >= TYPE_NAMES_.length) {
375 result = UNKNOWN_TYPE_NAME_;
378 result = TYPE_NAMES_[type];
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();
388 m_utilStringBuffer_.append('0');
391 m_utilStringBuffer_.append(chStr);
392 m_utilStringBuffer_.append('>');
393 result = m_utilStringBuffer_.toString();
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
404 public int getGroupMSB(int gindex)
406 if (gindex >= m_groupcount_) {
409 return m_groupinfo_[gindex * m_groupsize_];
413 * Gets the MSB of the codepoint
414 * @param codepoint The codepoint value.
415 * @return the MSB of the codepoint
417 public static int getCodepointMSB(int codepoint)
419 return codepoint >> GROUP_SHIFT_;
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
427 public static int getGroupLimit(int msb)
429 return (msb << GROUP_SHIFT_) + LINES_PER_GROUP_;
433 * Gets the minimum codepoint of the group
434 * @param msb most significant byte of the group
435 * @return minimum codepoint of the group
437 public static int getGroupMin(int msb)
439 return msb << GROUP_SHIFT_;
443 * Gets the offset to a group
444 * @param codepoint The codepoint value.
445 * @return offset to a group
447 public static int getGroupOffset(int codepoint)
449 return codepoint & GROUP_MASK_;
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
458 public static int getGroupMinFromCodepoint(int codepoint)
460 return codepoint & ~GROUP_MASK_;
465 * Get the Algorithm range length
466 * @return Algorithm range length
468 public int getAlgorithmLength()
470 return m_algorithm_.length;
474 * Gets the start of the range
475 * @param index algorithm index
476 * @return algorithm range start
478 public int getAlgorithmStart(int index)
480 return m_algorithm_[index].m_rangestart_;
484 * Gets the end of the range
485 * @param index algorithm index
486 * @return algorithm range end
488 public int getAlgorithmEnd(int index)
490 return m_algorithm_[index].m_rangeend_;
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
499 public String getAlgorithmName(int index, int codepoint)
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();
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
515 public synchronized String getGroupName(int ch, int choice)
518 int msb = getCodepointMSB(ch);
519 int group = getGroup(ch);
521 // return this if it is an exact match
522 if (msb == m_groupinfo_[group * m_groupsize_]) {
523 int index = getGroupLengths(group, m_groupoffsets_,
525 int offset = ch & GROUP_MASK_;
526 return getGroupName(index + m_groupoffsets_[offset],
527 m_grouplengths_[offset], choice);
533 // these are transliterator use methods ---------------------------------
536 * Gets the maximum length of any codepoint name.
537 * Equivalent to uprv_getMaxCharNameLength.
538 * @return the maximum length of any codepoint name
540 public int getMaxCharNameLength()
542 if (initNameSetsLengths()) {
543 return m_maxNameLength_;
551 * Gets the maximum length of any iso comments.
552 * Equivalent to uprv_getMaxISOCommentLength.
553 * @return the maximum length of any codepoint name
556 public int getMaxISOCommentLength()
558 if (initNameSetsLengths()) {
559 return m_maxISOCommentLength_;
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.
572 public void getCharNameCharacters(UnicodeSet set)
574 convert(m_nameSet_, set);
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.
583 public void getISOCommentCharacters(UnicodeSet set)
585 convert(m_ISOCommentSet_, set);
589 // package private inner class --------------------------------------
592 * Algorithmic name class
594 static final class AlgorithmName
596 // package private data members ----------------------------------
599 * Constant type value of the different AlgorithmName
601 static final int TYPE_0_ = 0;
602 static final int TYPE_1_ = 1;
604 // package private constructors ----------------------------------
613 // package private methods ---------------------------------------
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
625 boolean setInfo(int rangestart, int rangeend, byte type, byte variant)
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;
633 m_variant_ = variant;
640 * Sets the factor data
641 * @param factor Array of factor
642 * @return true if factors are valid
644 boolean setFactor(char factor[])
646 if (factor.length == m_variant_) {
654 * Sets the name prefix
656 * @return true if prefix is set
658 boolean setPrefix(String prefix)
660 if (prefix != null && prefix.length() > 0) {
668 * Sets the variant factorized name data
669 * @param string variant factorized name data
670 * @return true if values are set
672 boolean setFactorString(byte string[])
674 // factor and variant string can be empty for things like
675 // hanggul code points
676 m_factorstring_ = string;
681 * Checks if code point lies in Algorithm object at index
682 * @param ch code point
684 boolean contains(int ch)
686 return m_rangestart_ <= ch && ch <= m_rangeend_;
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
696 void appendName(int ch, StringBuffer str)
698 str.append(m_prefix_);
702 // prefix followed by hex digits indicating variants
703 str.append(Utility.hex(ch,m_variant_));
706 // prefix followed by factorized-elements
707 int offset = ch - m_rangestart_;
708 int indexes[] = m_utilIntBuffer_;
711 // write elements according to the factors
712 // the factorized elements are determined by modulo
714 synchronized (m_utilIntBuffer_) {
715 for (int i = m_variant_ - 1; i > 0; i --)
717 factor = m_factor_[i] & 0x00FF;
718 indexes[i] = offset % factor;
722 // we don't need to calculate the last modulus because
723 // start <= code <= end guarantees here that
724 // code <= factors[0]
727 // joining up the factorized strings
728 str.append(getFactorString(indexes, m_variant_));
735 * Gets the character for the argument algorithmic name
736 * @return the algorithmic char or -1 otherwise.
738 int getChar(String name)
740 int prefixlen = m_prefix_.length();
741 if (name.length() < prefixlen ||
742 !m_prefix_.equals(name.substring(0, prefixlen))) {
751 int result = Integer.parseInt(name.substring(prefixlen),
753 // does it fit into the range?
754 if (m_rangestart_ <= result && result <= m_rangeend_) {
758 catch (NumberFormatException e)
764 // repetitative suffix name comparison done here
765 // offset is the character code - start
766 for (int ch = m_rangestart_; ch <= m_rangeend_; ch ++)
768 int offset = ch - m_rangestart_;
769 int indexes[] = m_utilIntBuffer_;
772 // write elements according to the factors
773 // the factorized elements are determined by modulo
775 synchronized (m_utilIntBuffer_) {
776 for (int i = m_variant_ - 1; i > 0; i --)
778 factor = m_factor_[i] & 0x00FF;
779 indexes[i] = offset % factor;
783 // we don't need to calculate the last modulus
784 // because start <= code <= end guarantees here that
785 // code <= factors[0]
788 // joining up the factorized strings
789 if (compareFactorString(indexes, m_variant_, name,
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
808 int add(int set[], int maxlength)
811 int length = UCharacterName.add(set, m_prefix_);
814 // name = prefix + (range->variant times) hex-digits
816 length += m_variant_;
818 * addString(set, (const char *)(range + 1))
823 // name = prefix factorized-elements
824 // get the set and maximum factor suffix length for each
826 for (int i = m_variant_ - 1; i > 0; i --)
828 int maxfactorlength = 0;
830 for (int factor = m_factor_[i]; factor > 0; -- factor) {
831 synchronized (m_utilStringBuffer_) {
832 m_utilStringBuffer_.delete(0,
833 m_utilStringBuffer_.length());
835 = UCharacterUtility.getNullTermByteSubString(
837 m_factorstring_, count);
838 UCharacterName.add(set, m_utilStringBuffer_);
839 if (m_utilStringBuffer_.length()
843 = m_utilStringBuffer_.length();
847 length += maxfactorlength;
851 if (length > maxlength) {
857 // private data members ------------------------------------------
860 * Algorithmic data information
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_[];
870 * Utility StringBuffer
872 private StringBuffer m_utilStringBuffer_ = new StringBuffer();
876 private int m_utilIntBuffer_[] = new int[256];
878 // private methods -----------------------------------------------
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
887 private String getFactorString(int index[], int length)
889 int size = m_factor_.length;
890 if (index == null || length != size) {
894 synchronized (m_utilStringBuffer_) {
895 m_utilStringBuffer_.delete(0, m_utilStringBuffer_.length());
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_,
907 count = UCharacterUtility.skipNullTermByteSubString(
908 m_factorstring_, count,
909 factor - index[i] - 1);
912 return m_utilStringBuffer_.toString();
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
925 private boolean compareFactorString(int index[], int length, String str,
928 int size = m_factor_.length;
929 if (index == null || length != size)
933 int strcount = offset;
936 for (int i = 0; i <= size; i ++)
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);
948 count = UCharacterUtility.skipNullTermByteSubString(
949 m_factorstring_, count, factor - index[i]);
952 if (strcount != str.length()) {
959 // package private data members --------------------------------------
962 * Size of each groups
964 int m_groupsize_ = 0;
966 // package private methods --------------------------------------------
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
974 boolean setToken(char token[], byte tokenstring[])
976 if (token != null && tokenstring != null && token.length > 0 &&
977 tokenstring.length > 0) {
978 m_tokentable_ = token;
979 m_tokenstring_ = tokenstring;
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
990 boolean setAlgorithm(AlgorithmName alg[])
992 if (alg != null && alg.length != 0) {
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
1005 boolean setGroupCountSize(int count, int size)
1007 if (count <= 0 || size <= 0) {
1010 m_groupcount_ = count;
1011 m_groupsize_ = size;
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
1021 boolean setGroup(char group[], byte groupstring[])
1023 if (group != null && groupstring != null && group.length > 0 &&
1024 groupstring.length > 0) {
1025 m_groupinfo_ = group;
1026 m_groupstring_ = groupstring;
1032 // private data members ----------------------------------------------
1035 * Data used in unames.icu
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_[];
1044 * Group use. Note - access must be synchronized.
1046 private char m_groupoffsets_[] = new char[LINES_PER_GROUP_ + 1];
1047 private char m_grouplengths_[] = new char[LINES_PER_GROUP_ + 1];
1050 * Default name of the name datafile
1052 private static final String NAME_FILE_NAME_ = ICUResourceBundle.ICU_BUNDLE+"/unames.icu";
1054 * Shift count to retrieve group information
1056 private static final int GROUP_SHIFT_ = 5;
1058 * Mask to retrieve the offset for a particular character within a group
1060 private static final int GROUP_MASK_ = LINES_PER_GROUP_ - 1;
1062 * Default buffer size of datafile
1064 private static final int NAME_BUFFER_SIZE_ = 100000;
1067 * Position of offsethigh in group information array
1069 private static final int OFFSET_HIGH_OFFSET_ = 1;
1072 * Position of offsetlow in group information array
1074 private static final int OFFSET_LOW_OFFSET_ = 2;
1076 * Double nibble indicator, any nibble > this number has to be combined
1077 * with its following nibble
1079 private static final int SINGLE_NIBBLE_MAX_ = 11;
1082 * Maximum length of character names (regular & 1.0).
1084 //private static int MAX_NAME_LENGTH_ = 0;
1086 * Maximum length of ISO comments.
1088 //private static int MAX_ISO_COMMENT_LENGTH_ = 0;
1091 * Set of chars used in character names (regular & 1.0).
1092 * Chars are platform-dependent (can be EBCDIC).
1094 private int m_nameSet_[] = new int[8];
1096 * Set of chars used in ISO comments. (regular & 1.0).
1097 * Chars are platform-dependent (can be EBCDIC).
1099 private int m_ISOCommentSet_[] = new int[8];
1101 * Utility StringBuffer
1103 private StringBuffer m_utilStringBuffer_ = new StringBuffer();
1105 * Utility int buffer
1107 private int m_utilIntBuffer_[] = new int[2];
1109 * Maximum ISO comment length
1111 private int m_maxISOCommentLength_;
1113 * Maximum name length
1115 private int m_maxNameLength_;
1117 * Type names used for extended names
1119 private static final String TYPE_NAMES_[] = {"unassigned",
1127 "combining spacing mark",
1128 "decimal digit number",
1133 "paragraph separator",
1139 "start punctuation",
1141 "connector punctuation",
1142 "other punctuation",
1147 "initial punctuation",
1148 "final punctuation",
1155 private static final String UNKNOWN_TYPE_NAME_ = "unknown";
1157 * Not a character type
1159 private static final int NON_CHARACTER_
1160 = UCharacterCategory.CHAR_CATEGORY_COUNT;
1162 * Lead surrogate type
1164 private static final int LEAD_SURROGATE_
1165 = UCharacterCategory.CHAR_CATEGORY_COUNT + 1;
1167 * Trail surrogate type
1169 private static final int TRAIL_SURROGATE_
1170 = UCharacterCategory.CHAR_CATEGORY_COUNT + 2;
1172 * Extended category count
1174 static final int EXTENDED_CATEGORY_
1175 = UCharacterCategory.CHAR_CATEGORY_COUNT + 3;
1177 // private constructor ------------------------------------------------
1180 * <p>Protected constructor for use in UCharacter.</p>
1181 * @exception IOException thrown when data reading fails
1183 private UCharacterName() throws IOException
1185 InputStream is = ICUData.getRequiredStream(NAME_FILE_NAME_);
1186 BufferedInputStream b = new BufferedInputStream(is, NAME_BUFFER_SIZE_);
1187 UCharacterNameReader reader = new UCharacterNameReader(b);
1192 // private methods ---------------------------------------------------
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
1200 private String getAlgName(int ch, int choice)
1202 /* Only the normative character name can be algorithmic. */
1203 if (choice == UCharacterNameChoice.UNICODE_CHAR_NAME ||
1204 choice == UCharacterNameChoice.EXTENDED_CHAR_NAME
1206 // index in terms integer index
1207 synchronized (m_utilStringBuffer_) {
1208 m_utilStringBuffer_.delete(0, m_utilStringBuffer_.length());
1210 for (int index = m_algorithm_.length - 1; index >= 0; index --)
1212 if (m_algorithm_[index].contains(ch)) {
1213 m_algorithm_[index].appendName(ch, m_utilStringBuffer_);
1214 return m_utilStringBuffer_.toString();
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
1228 private synchronized int getGroupChar(String name, int choice)
1230 for (int i = 0; i < m_groupcount_; i ++) {
1231 // populating the data set of grouptable
1233 int startgpstrindex = getGroupLengths(i, m_groupoffsets_,
1236 // shift out to function
1237 int result = getGroupChar(startgpstrindex, m_grouplengths_, name,
1240 return (m_groupinfo_[i * m_groupsize_] << GROUP_SHIFT_)
1248 * Compares and retrieve character if name is found within the argument
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
1257 private int getGroupChar(int index, char length[], String name,
1263 int namelen = name.length();
1267 for (int result = 0; result <= LINES_PER_GROUP_; result ++) {
1269 len = length[result];
1271 if (choice != UCharacterNameChoice.UNICODE_CHAR_NAME &&
1272 choice != UCharacterNameChoice.EXTENDED_CHAR_NAME
1275 * skip the modern name if it is not requested _and_
1276 * if the semicolon byte value is a character, not a token number
1278 int fieldIndex= choice==UCharacterNameChoice.ISO_COMMENT_ ? 2 : choice;
1280 int oldindex = index;
1281 index += UCharacterUtility.skipByteSubString(m_groupstring_,
1282 index, len, (byte)';');
1283 len -= (index - oldindex);
1284 } while(--fieldIndex>0);
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;
1291 b = m_groupstring_[index + count];
1294 if (b >= m_tokentable_.length) {
1295 if (name.charAt(nindex ++) != (b & 0xFF)) {
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)];
1307 if (token == 0xFFFF) {
1308 if (name.charAt(nindex ++) != (b & 0xFF)) {
1313 // compare token with name
1314 nindex = UCharacterUtility.compareNullTermByteSubString(
1315 name, m_tokenstring_, nindex, token);
1320 if (namelen == nindex &&
1321 (count == len || m_groupstring_[index + count] == ';')) {
1331 * Gets the character extended type
1332 * @param ch character to be tested
1333 * @return extended type it is associated with
1335 private static int getType(int ch)
1337 if (UCharacterUtility.isNonCharacter(ch)) {
1338 // not a character we return a invalid category count
1339 return NON_CHARACTER_;
1341 int result = UCharacter.getType(ch);
1342 if (result == UCharacterCategory.SURROGATE) {
1343 if (ch <= UTF16.LEAD_SURROGATE_MAX_VALUE) {
1344 result = LEAD_SURROGATE_;
1347 result = TRAIL_SURROGATE_;
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.
1360 private static int getExtendedChar(String name, int choice)
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.
1371 result = Integer.parseInt(
1372 name.substring(startIndex, endIndex),
1375 catch (NumberFormatException e) {
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) {
1398 // sets of name characters, maximum name lengths -----------------------
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
1406 private static void add(int set[], char ch)
1408 set[ch >>> 5] |= 1 << (ch & 0x1f);
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
1418 private static boolean contains(int set[], char ch)
1420 return (set[ch >>> 5] & (1 << (ch & 0x1f))) != 0;
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
1429 private static int add(int set[], String str)
1431 int result = str.length();
1433 for (int i = result - 1; i >= 0; i --) {
1434 add(set, str.charAt(i));
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
1445 private static int add(int set[], StringBuffer str)
1447 int result = str.length();
1449 for (int i = result - 1; i >= 0; i --) {
1450 add(set, str.charAt(i));
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.
1462 private int addAlgorithmName(int maxlength)
1465 for (int i = m_algorithm_.length - 1; i >= 0; i --) {
1466 result = m_algorithm_[i].add(m_nameSet_, maxlength);
1467 if (result > maxlength) {
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.
1480 private int addExtendedName(int maxlength)
1482 for (int i = TYPE_NAMES_.length - 1; i >= 0; i --) {
1483 // for each category, count the length of the category name
1487 // 6 for most hex digits per code point
1488 int length = 9 + add(m_nameSet_, TYPE_NAMES_[i]);
1489 if (length > maxlength) {
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
1506 private int[] addGroupName(int offset, int length, byte tokenlength[],
1509 int resultnlength = 0;
1510 int resultplength = 0;
1511 while (resultplength < length) {
1512 char b = (char)(m_groupstring_[offset + resultplength] & 0xff);
1518 if (b >= m_tokentable_.length) {
1519 add(set, b); // implicit letter
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]
1528 token = m_tokentable_[b];
1531 if (token == 0xFFFF) {
1537 // use cached token length
1538 byte tlength = tokenlength[b];
1540 synchronized (m_utilStringBuffer_) {
1541 m_utilStringBuffer_.delete(0,
1542 m_utilStringBuffer_.length());
1543 UCharacterUtility.getNullTermByteSubString(
1544 m_utilStringBuffer_, m_tokenstring_,
1546 tlength = (byte)add(set, m_utilStringBuffer_);
1548 tokenlength[b] = tlength;
1550 resultnlength += tlength;
1554 m_utilIntBuffer_[0] = resultnlength;
1555 m_utilIntBuffer_[1] = resultplength;
1556 return m_utilIntBuffer_;
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
1566 private void addGroupName(int maxlength)
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];
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;
1580 for (int linenumber = 0; linenumber < LINES_PER_GROUP_;
1582 int lineoffset = offset + offsets[linenumber];
1583 int length = lengths[linenumber];
1588 // read regular name
1589 int parsed[] = addGroupName(lineoffset, length, tokenlengths,
1591 if (parsed[0] > maxlength) {
1592 // 0 for name length
1593 maxlength = parsed[0];
1595 lineoffset += parsed[1];
1596 if (parsed[1] >= length) {
1597 // 1 for parsed group string length
1600 length -= parsed[1];
1601 // read Unicode 1.0 name
1602 parsed = addGroupName(lineoffset, length, tokenlengths,
1604 if (parsed[0] > maxlength) {
1605 // 0 for name length
1606 maxlength = parsed[0];
1608 lineoffset += parsed[1];
1609 if (parsed[1] >= length) {
1610 // 1 for parsed group string length
1613 length -= parsed[1];
1615 parsed = addGroupName(lineoffset, length, tokenlengths,
1617 if (parsed[1] > maxisolength) {
1618 maxisolength = length;
1623 // set gMax... - name length last for threading
1624 m_maxISOCommentLength_ = maxisolength;
1625 m_maxNameLength_ = maxlength;
1629 * Sets up the name sets and the calculation of the maximum lengths.
1630 * Equivalent to calcNameSetsLengths.
1632 private boolean initNameSetsLengths()
1634 if (m_maxNameLength_ > 0) {
1638 String extra = "0123456789ABCDEF<>-";
1639 // set hex digits, used in various names, and <>-, used in extended
1641 for (int i = extra.length() - 1; i >= 0; i --) {
1642 add(m_nameSet_, extra.charAt(i));
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_);
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.
1660 private void convert(int set[], UnicodeSet uset)
1663 if (!initNameSetsLengths()) {
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)) {