001: /**
002: *******************************************************************************
003: * Copyright (C) 1996-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.impl;
007:
008: /**
009: * Internal character utility class for simple data type conversion and String
010: * parsing functions. Does not have an analog in the JDK.
011: * @author Syn Wee Quek
012: * @since sep2900
013: */
014:
015: public final class UCharacterUtility {
016: // public methods -----------------------------------------------------
017:
018: /**
019: * Determines if codepoint is a non character
020: * @param ch codepoint
021: * @return true if codepoint is a non character false otherwise
022: */
023: public static boolean isNonCharacter(int ch) {
024: if ((ch & NON_CHARACTER_SUFFIX_MIN_3_0_) == NON_CHARACTER_SUFFIX_MIN_3_0_) {
025: return true;
026: }
027:
028: return ch >= NON_CHARACTER_MIN_3_1_
029: && ch <= NON_CHARACTER_MAX_3_1_;
030: }
031:
032: // package private methods ---------------------------------------------
033:
034: /**
035: * joining 2 chars to form an int
036: * @param msc most significant char
037: * @param lsc least significant char
038: * @return int form
039: */
040: static int toInt(char msc, char lsc) {
041: return ((msc << 16) | lsc);
042: }
043:
044: /**
045: * Retrieves a null terminated substring from an array of bytes.
046: * Substring is a set of non-zero bytes starting from argument start to the
047: * next zero byte. If the first byte is a zero, the next byte will be taken as
048: * the first byte.
049: * @param str stringbuffer to store data in, data will be store with each
050: * byte as a char
051: * @param array byte array
052: * @param index to start substring in byte count
053: * @return the end position of the substring within the character array
054: */
055: static int getNullTermByteSubString(StringBuffer str, byte[] array,
056: int index) {
057: byte b = 1;
058:
059: while (b != 0) {
060: b = array[index];
061: if (b != 0) {
062: str.append((char) (b & 0x00FF));
063: }
064: index++;
065: }
066: return index;
067: }
068:
069: /**
070: * Compares a null terminated substring from an array of bytes.
071: * Substring is a set of non-zero bytes starting from argument start to the
072: * next zero byte. if the first byte is a zero, the next byte will be taken as
073: * the first byte.
074: * @param str string to compare
075: * @param array byte array
076: * @param strindex index within str to start comparing
077: * @param aindex array index to start in byte count
078: * @return the end position of the substring within str if matches otherwise
079: * a -1
080: */
081: static int compareNullTermByteSubString(String str, byte[] array,
082: int strindex, int aindex) {
083: byte b = 1;
084: int length = str.length();
085:
086: while (b != 0) {
087: b = array[aindex];
088: aindex++;
089: if (b == 0) {
090: break;
091: }
092: // if we have reached the end of the string and yet the array has not
093: // reached the end of their substring yet, abort
094: if (strindex == length
095: || (str.charAt(strindex) != (char) (b & 0xFF))) {
096: return -1;
097: }
098: strindex++;
099: }
100: return strindex;
101: }
102:
103: /**
104: * Skip null terminated substrings from an array of bytes.
105: * Substring is a set of non-zero bytes starting from argument start to the
106: * next zero byte. If the first byte is a zero, the next byte will be taken as
107: * the first byte.
108: * @param array byte array
109: * @param index to start substrings in byte count
110: * @param skipcount number of null terminated substrings to skip
111: * @return the end position of the substrings within the character array
112: */
113: static int skipNullTermByteSubString(byte[] array, int index,
114: int skipcount) {
115: byte b;
116: for (int i = 0; i < skipcount; i++) {
117: b = 1;
118: while (b != 0) {
119: b = array[index];
120: index++;
121: }
122: }
123: return index;
124: }
125:
126: /**
127: * skip substrings from an array of characters, where each character is a set
128: * of 2 bytes. substring is a set of non-zero bytes starting from argument
129: * start to the byte of the argument value. skips up to a max number of
130: * characters
131: * @param array byte array to parse
132: * @param index to start substrings in byte count
133: * @param length the max number of bytes to skip
134: * @param skipend value of byte to skip to
135: * @return the number of bytes skipped
136: */
137: static int skipByteSubString(byte[] array, int index, int length,
138: byte skipend) {
139: int result;
140: byte b;
141:
142: for (result = 0; result < length; result++) {
143: b = array[index + result];
144: if (b == skipend) {
145: result++;
146: break;
147: }
148: }
149:
150: return result;
151: }
152:
153: // private data member --------------------------------------------------
154:
155: /**
156: * Minimum suffix value that indicates if a character is non character.
157: * Unicode 3.0 non characters
158: */
159: private static final int NON_CHARACTER_SUFFIX_MIN_3_0_ = 0xFFFE;
160: /**
161: * New minimum non character in Unicode 3.1
162: */
163: private static final int NON_CHARACTER_MIN_3_1_ = 0xFDD0;
164: /**
165: * New non character range in Unicode 3.1
166: */
167: private static final int NON_CHARACTER_MAX_3_1_ = 0xFDEF;
168:
169: // private constructor --------------------------------------------------
170:
171: ///CLOVER:OFF
172: /**
173: * private constructor to avoid initialisation
174: */
175: private UCharacterUtility() {
176: }
177: ///CLOVER:ON
178: }
|