Home | History | Annotate | Download | only in impl
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  *   Copyright (C) 2002-2010, International Business Machines
      7  *   Corporation and others.  All Rights Reserved.
      8  *******************************************************************************
      9 */
     10 
     11 package android.icu.impl;
     12 /**
     13  * @version     1.1
     14  * @author     Markus W. Scherer
     15  * Ram: Add documentation, remove unwanted methods, improve coverage.
     16  */
     17 
     18 /**
     19  * Simple class for handling serialized USet/UnicodeSet structures
     20  * without object creation. See ICU4C icu/source/common/uset.c.
     21  *
     22  * @hide Only a subset of ICU is exposed in Android
     23  * @hide draft / provisional / internal are hidden on Android
     24  */
     25 public final class USerializedSet {
     26     /**
     27      * Fill in the given serialized set object.
     28      * @param src pointer to start of array
     29      * @param srcStart pointer to start of serialized data (length value)
     30      * @return true if the given array is valid, otherwise false
     31      */
     32     public final boolean getSet(char src[], int srcStart) {
     33         // leave most argument checking up to Java exceptions
     34         array=null;
     35         arrayOffset=bmpLength=length=0;
     36 
     37         length=src[srcStart++];
     38 
     39         if ((length&0x8000) != 0) {
     40             /* there are supplementary values */
     41             length&=0x7fff;
     42             if(src.length<(srcStart+1+length)) {
     43                 length=0;
     44                 throw new IndexOutOfBoundsException();
     45             }
     46             bmpLength=src[srcStart++];
     47         } else {
     48             /* only BMP values */
     49             if(src.length<(srcStart+length)) {
     50                 length=0;
     51                 throw new IndexOutOfBoundsException();
     52             }
     53             bmpLength=length;
     54         }
     55         array = new char[length];
     56         System.arraycopy(src,srcStart,array,0,length);
     57         //arrayOffset=srcStart;
     58         return true;
     59     }
     60 
     61     /**
     62      * Set the USerializedSet to contain the given character (and nothing
     63      * else).
     64      */
     65     public final void setToOne(int c) {
     66         if( 0x10ffff<c) {
     67             return;
     68         }
     69 
     70         if(c<0xffff) {
     71             bmpLength=length=2;
     72             array[0]=(char)c;
     73             array[1]=(char)(c+1);
     74         } else if(c==0xffff) {
     75             bmpLength=1;
     76             length=3;
     77             array[0]=0xffff;
     78             array[1]=1;
     79             array[2]=0;
     80         } else if(c<0x10ffff) {
     81             bmpLength=0;
     82             length=4;
     83             array[0]=(char)(c>>16);
     84             array[1]=(char)c;
     85             ++c;
     86             array[2]=(char)(c>>16);
     87             array[3]=(char)c;
     88         } else /* c==0x10ffff */ {
     89             bmpLength=0;
     90             length=2;
     91             array[0]=0x10;
     92             array[1]=0xffff;
     93         }
     94     }
     95 
     96     /**
     97      * Returns a range of characters contained in the given serialized
     98      * set.
     99      * @param rangeIndex a non-negative integer in the range <code>0..
    100      * getSerializedRangeCount()-1</code>
    101      * @param range variable to receive the data in the range
    102      * @return true if rangeIndex is valid, otherwise false
    103      */
    104     public final boolean getRange(int rangeIndex, int[] range) {
    105         if( rangeIndex<0) {
    106             return false;
    107         }
    108         if(array==null){
    109             array = new char[8];
    110         }
    111         if(range==null || range.length <2){
    112             throw new IllegalArgumentException();
    113         }
    114         rangeIndex*=2; /* address start/limit pairs */
    115         if(rangeIndex<bmpLength) {
    116             range[0]=array[rangeIndex++];
    117             if(rangeIndex<bmpLength) {
    118                 range[1]=array[rangeIndex]-1;
    119             } else if(rangeIndex<length) {
    120                 range[1]=((((int)array[rangeIndex])<<16)|array[rangeIndex+1])-1;
    121             } else {
    122                 range[1]=0x10ffff;
    123             }
    124             return true;
    125         } else {
    126             rangeIndex-=bmpLength;
    127             rangeIndex*=2; /* address pairs of pairs of units */
    128             int suppLength=length-bmpLength;
    129             if(rangeIndex<suppLength) {
    130                 int offset=arrayOffset+bmpLength;
    131                 range[0]=(((int)array[offset+rangeIndex])<<16)|array[offset+rangeIndex+1];
    132                 rangeIndex+=2;
    133                 if(rangeIndex<suppLength) {
    134                     range[1]=((((int)array[offset+rangeIndex])<<16)|array[offset+rangeIndex+1])-1;
    135                 } else {
    136                     range[1]=0x10ffff;
    137                 }
    138                 return true;
    139             } else {
    140                 return false;
    141             }
    142         }
    143     }
    144 
    145     /**
    146      * Returns true if the given USerializedSet contains the given
    147      * character.
    148      * @param c the character to test for
    149      * @return true if set contains c
    150      */
    151     public final boolean contains(int c) {
    152 
    153         if(c>0x10ffff) {
    154             return false;
    155         }
    156 
    157         if(c<=0xffff) {
    158             int i;
    159             /* find c in the BMP part */
    160             for(i=0; i<bmpLength && (char)c>=array[i]; ++i) {}
    161             return ((i&1) != 0);
    162         } else {
    163             int i;
    164             /* find c in the supplementary part */
    165             char high=(char)(c>>16), low=(char)c;
    166             for(i=bmpLength;
    167                 i<length && (high>array[i] || (high==array[i] && low>=array[i+1]));
    168                 i+=2) {}
    169 
    170             /* count pairs of 16-bit units even per BMP and check if the number of pairs is odd */
    171             return (((i+bmpLength)&2)!=0);
    172         }
    173     }
    174 
    175     /**
    176      * Returns the number of disjoint ranges of characters contained in
    177      * the given serialized set.  Ignores any strings contained in the
    178      * set.
    179      * @return a non-negative integer counting the character ranges
    180      * contained in set
    181      */
    182     public final int countRanges() {
    183         return (bmpLength+(length-bmpLength)/2+1)/2;
    184     }
    185 
    186     private char array[] = new char[8];
    187     private int arrayOffset, bmpLength, length;
    188 }
    189