2 *******************************************************************************
3 * Copyright (C) 2002-2010, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
8 package com.ibm.icu.impl;
11 * @author Markus W. Scherer
12 * Ram: Add documentation, remove unwanted methods, improve coverage.
16 * Simple class for handling serialized USet/UnicodeSet structures
17 * without object creation. See ICU4C icu/source/common/uset.c.
21 public final class USerializedSet {
23 * Fill in the given serialized set object.
24 * @param src pointer to start of array
25 * @param srcStart pointer to start of serialized data (length value)
26 * @return true if the given array is valid, otherwise false
28 public final boolean getSet(char src[], int srcStart) {
29 // leave most argument checking up to Java exceptions
31 arrayOffset=bmpLength=length=0;
33 length=src[srcStart++];
36 if((length&0x8000) >0) {
37 /* there are supplementary values */
39 if(src.length<(srcStart+1+length)) {
41 throw new IndexOutOfBoundsException();
43 bmpLength=src[srcStart++];
46 if(src.length<(srcStart+length)) {
48 throw new IndexOutOfBoundsException();
52 array = new char[length];
53 System.arraycopy(src,srcStart,array,0,length);
54 //arrayOffset=srcStart;
59 * Set the USerializedSet to contain the given character (and nothing
62 public final void setToOne(int c) {
71 } else if(c==0xffff) {
77 } else if(c<0x10ffff) {
80 array[0]=(char)(c>>16);
83 array[2]=(char)(c>>16);
85 } else /* c==0x10ffff */ {
94 * Returns a range of characters contained in the given serialized
96 * @param rangeIndex a non-negative integer in the range <code>0..
97 * getSerializedRangeCount()-1</code>
98 * @param range variable to receive the data in the range
99 * @return true if rangeIndex is valid, otherwise false
101 public final boolean getRange(int rangeIndex, int[] range) {
108 if(range==null || range.length <2){
109 throw new IllegalArgumentException();
111 rangeIndex*=2; /* address start/limit pairs */
112 if(rangeIndex<bmpLength) {
113 range[0]=array[rangeIndex++];
114 if(rangeIndex<bmpLength) {
115 range[1]=array[rangeIndex]-1;
116 } else if(rangeIndex<length) {
117 range[1]=((((int)array[rangeIndex])<<16)|array[rangeIndex+1])-1;
123 rangeIndex-=bmpLength;
124 rangeIndex*=2; /* address pairs of pairs of units */
125 int suppLength=length-bmpLength;
126 if(rangeIndex<suppLength) {
127 int offset=arrayOffset+bmpLength;
128 range[0]=(((int)array[offset+rangeIndex])<<16)|array[offset+rangeIndex+1];
130 if(rangeIndex<suppLength) {
131 range[1]=((((int)array[offset+rangeIndex])<<16)|array[offset+rangeIndex+1])-1;
143 * Returns true if the given USerializedSet contains the given
145 * @param c the character to test for
146 * @return true if set contains c
148 public final boolean contains(int c) {
156 /* find c in the BMP part */
157 for(i=0; i<bmpLength && (char)c>=array[i]; ++i) {}
161 /* find c in the supplementary part */
162 char high=(char)(c>>16), low=(char)c;
164 i<length && (high>array[i] || (high==array[i] && low>=array[i+1]));
167 /* count pairs of 16-bit units even per BMP and check if the number of pairs is odd */
168 return (((i+bmpLength)&2)!=0);
173 * Returns the number of disjoint ranges of characters contained in
174 * the given serialized set. Ignores any strings contained in the
176 * @return a non-negative integer counting the character ranges
179 public final int countRanges() {
180 return (bmpLength+(length-bmpLength)/2+1)/2;
183 private char array[] = new char[8];
184 private int arrayOffset, bmpLength, length;