1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html#License
3 /**
4 *******************************************************************************
5 * Copyright (C) 1996-2004, International Business Machines Corporation and    *
6 * others. All Rights Reserved.                                                *
7 *******************************************************************************
8 */
9 package com.ibm.icu.impl;
10 
11 /**
12 * Internal character utility class for simple data type conversion and String
13 * parsing functions. Does not have an analog in the JDK.
14 * @author Syn Wee Quek
15 * @since sep2900
16 */
17 
18 public final class UCharacterUtility
19 {
20     // public methods -----------------------------------------------------
21 
22     /**
23     * Determines if codepoint is a non character
24     * @param ch codepoint
25     * @return true if codepoint is a non character false otherwise
26     */
isNonCharacter(int ch)27     public static boolean isNonCharacter(int ch)
28     {
29         if ((ch & NON_CHARACTER_SUFFIX_MIN_3_0_) ==
30                                             NON_CHARACTER_SUFFIX_MIN_3_0_) {
31             return true;
32         }
33 
34         return ch >= NON_CHARACTER_MIN_3_1_ && ch <=  NON_CHARACTER_MAX_3_1_;
35     }
36 
37     // package private methods ---------------------------------------------
38 
39     /**
40     * joining 2 chars to form an int
41     * @param msc most significant char
42     * @param lsc least significant char
43     * @return int form
44     */
toInt(char msc, char lsc)45     static int toInt(char msc, char lsc)
46     {
47         return ((msc << 16) | lsc);
48     }
49 
50     /**
51     * Retrieves a null terminated substring from an array of bytes.
52     * Substring is a set of non-zero bytes starting from argument start to the
53     * next zero byte. If the first byte is a zero, the next byte will be taken as
54     * the first byte.
55     * @param str stringbuffer to store data in, data will be store with each
56     *            byte as a char
57     * @param array byte array
58     * @param index to start substring in byte count
59     * @return the end position of the substring within the character array
60     */
getNullTermByteSubString(StringBuffer str, byte[] array, int index)61     static int getNullTermByteSubString(StringBuffer str, byte[] array,
62                                                   int index)
63     {
64         byte b = 1;
65 
66         while (b != 0)
67         {
68             b = array[index];
69             if (b != 0) {
70                 str.append((char)(b & 0x00FF));
71             }
72             index ++;
73         }
74         return index;
75     }
76 
77     /**
78     * Compares a null terminated substring from an array of bytes.
79     * Substring is a set of non-zero bytes starting from argument start to the
80     * next zero byte. if the first byte is a zero, the next byte will be taken as
81     * the first byte.
82     * @param str string to compare
83     * @param array byte array
84     * @param strindex index within str to start comparing
85     * @param aindex array index to start in byte count
86     * @return the end position of the substring within str if matches otherwise
87     *         a -1
88     */
compareNullTermByteSubString(String str, byte[] array, int strindex, int aindex)89     static int compareNullTermByteSubString(String str, byte[] array,
90                                                       int strindex, int aindex)
91     {
92         byte b = 1;
93         int length = str.length();
94 
95         while (b != 0)
96         {
97             b = array[aindex];
98             aindex ++;
99             if (b == 0) {
100                 break;
101             }
102             // if we have reached the end of the string and yet the array has not
103             // reached the end of their substring yet, abort
104             if (strindex == length
105                 || (str.charAt(strindex) != (char)(b & 0xFF))) {
106               return -1;
107             }
108             strindex ++;
109         }
110         return strindex;
111     }
112 
113     /**
114     * Skip null terminated substrings from an array of bytes.
115     * Substring is a set of non-zero bytes starting from argument start to the
116     * next zero byte. If the first byte is a zero, the next byte will be taken as
117     * the first byte.
118     * @param array byte array
119     * @param index to start substrings in byte count
120     * @param skipcount number of null terminated substrings to skip
121     * @return the end position of the substrings within the character array
122     */
skipNullTermByteSubString(byte[] array, int index, int skipcount)123     static int skipNullTermByteSubString(byte[] array, int index,
124                                                    int skipcount)
125     {
126         byte b;
127         for (int i = 0; i < skipcount; i ++)
128         {
129             b = 1;
130             while (b != 0)
131             {
132                 b = array[index];
133                 index ++;
134             }
135         }
136         return index;
137     }
138 
139     /**
140      * skip substrings from an array of characters, where each character is a set
141      * of 2 bytes. substring is a set of non-zero bytes starting from argument
142      * start to the byte of the argument value. skips up to a max number of
143      * characters
144      * @param array byte array to parse
145      * @param index to start substrings in byte count
146      * @param length the max number of bytes to skip
147      * @param skipend value of byte to skip to
148      * @return the number of bytes skipped
149      */
skipByteSubString(byte[] array, int index, int length, byte skipend)150     static int skipByteSubString(byte[] array, int index, int length,
151                                            byte skipend)
152     {
153         int result;
154         byte b;
155 
156         for (result = 0; result < length; result ++)
157         {
158             b = array[index + result];
159             if (b == skipend)
160             {
161                 result ++;
162                 break;
163             }
164         }
165 
166         return result;
167     }
168 
169     // private data member --------------------------------------------------
170 
171     /**
172     * Minimum suffix value that indicates if a character is non character.
173     * Unicode 3.0 non characters
174     */
175     private static final int NON_CHARACTER_SUFFIX_MIN_3_0_ = 0xFFFE;
176     /**
177     * New minimum non character in Unicode 3.1
178     */
179     private static final int NON_CHARACTER_MIN_3_1_ = 0xFDD0;
180     /**
181     * New non character range in Unicode 3.1
182     */
183     private static final int NON_CHARACTER_MAX_3_1_ = 0xFDEF;
184 
185     // private constructor --------------------------------------------------
186 
187     ///CLOVER:OFF
188     /**
189     * private constructor to avoid initialisation
190     */
UCharacterUtility()191     private UCharacterUtility()
192     {
193     }
194     ///CLOVER:ON
195 }
196 
197