KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > coffi > Utf8_Enumeration


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997 Clark Verbrugge
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31
32 package soot.coffi;
33
34 import java.util.Enumeration JavaDoc;
35
36 /** An enumeration of a Utf8 allows one to run through the characters in a
37  * unicode string; used primarily for comparing unicode strings. Note that
38  * unlike regular enumerations, and to be efficient (ie to avoid allocating
39  * an object in each call to nextElement), the return value is accessed by
40  * this object's 'c' field, and nextElement merely returns this.
41  * @see CONSTANT_Utf8_info
42  * @see Utf8_Enumeration#c
43  * @see Utf8_Enumeration#nextElement
44  * @author Clark Verbrugge
45  */

46 public class Utf8_Enumeration implements Enumeration JavaDoc {
47
48    /** The latest character, as determined by nextElement.
49     * @see Utf8_Enumeration#nextElement
50     */

51    public int c; // latest character
52

53    private short curindex;
54    private short length;
55    private byte bytes[];
56
57    /** For creating an empty enumeration; you must use reset() after this
58     * to initialize the enumeration.
59     * @see Utf8_Enumeration#reset
60     */

61    public Utf8_Enumeration() {}
62    /** For creating a normal enumeration of the given Utf8 string.
63     * @param b array of bytes in Utf8 format.
64     */

65    public Utf8_Enumeration(byte b[]) {
66       bytes = b;
67       curindex = (short)2;
68       length = (short)(((((int)(bytes[0]))&0xff)<<8) + (((int)(bytes[1]))&0xff) + 2);
69    }
70    /** Resets this object to be an enumeration of the given Utf8 string.
71     * @param b array of bytes in Utf8 format.
72     */

73    public void reset(byte b[]) {
74       bytes = b;
75       curindex = (short)2;
76       length = (short)(((((int)(bytes[0]))&0xff)<<8) + (((int)(bytes[1]))&0xff) + 2);
77    }
78
79    /** <i>true</i> if the entire string hasn't been enumerated yet. */
80    public boolean hasMoreElements() {
81       if (curindex<length) return true;
82       return false;
83    }
84
85    /** Determines the next Utf8 character, and stores it in c.
86     * @return <i>this</i>
87     * @see Utf8_Enumeration#c
88     */

89    public Object JavaDoc nextElement() {
90       byte b;
91       b = bytes[curindex++];
92       if ((b&((byte)0x80))==0) { // one-byte character
93
c = b;
94       } else if ((b&((byte)0xe0))==0xc0) { // two-byte character
95
c = ((int)(b&((byte)0x1f)))<<6;
96          b = bytes[curindex++];
97          c |= (int)(b&((byte)0x3f));
98       } else { // three-byte character
99
c = ((int)(b&((byte)0x0f)))<<12;
100          b = bytes[curindex++];
101          c |= ((int)(b&((byte)0x3f)))<<6;
102          b = bytes[curindex++];
103          c |= (int)(b&((byte)0x3f));
104       }
105       return this;
106    }
107 }
108
Popular Tags