OSDN Git Service

e1777fd9945ffdf40f352524a49e918ef40fca90
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / text / CharacterBreakIterator.java
1 // Default character BreakIterator.
2
3 /* Copyright (C) 1999  Cygnus Solutions
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 package gnu.gcj.text;
12
13 import java.text.BreakIterator;
14 import java.text.CharacterIterator;
15
16 /**
17  * @author Tom Tromey <tromey@cygnus.com>
18  * @date March 19, 1999
19  * Written using The Unicode Standard, Version 2.0.
20  */
21
22 public class CharacterBreakIterator extends BaseBreakIterator
23 {
24   // Hangul Jamo constants from Unicode book.
25   private static final int LBase = 0x1100;
26   private static final int VBase = 0x1161;
27   private static final int TBase = 0x11a7;
28   private static final int LCount = 19;
29   private static final int VCount = 21;
30   private static final int TCount = 28;
31
32   // Information about surrogates.
33   private static final int highSurrogateStart = 0xD800;
34   private static final int highSurrogateEnd = 0xDBFF;
35   private static final int lowSurrogateStart = 0xDC00;
36   private static final int lowSurrogateEnd = 0xDFFF;
37
38   public Object clone ()
39   {
40     return new CharacterBreakIterator (this);
41   }
42
43   public CharacterBreakIterator ()
44   {
45     iter = null;                // FIXME?
46   }
47
48   private CharacterBreakIterator (CharacterBreakIterator other)
49   {
50     iter = (CharacterIterator) other.iter.clone();
51   }
52
53   // Some methods to tell us different properties of characters.
54   private final boolean isL (char c)
55   {
56     return c >= LBase && c <= LBase + LCount;
57   }
58   private final boolean isV (char c)
59   {
60     return c >= VBase && c <= VBase + VCount;
61   }
62   private final boolean isT (char c)
63   {
64     return c >= TBase && c <= TBase + TCount;
65   }
66   private final boolean isLVT (char c)
67   {
68     return isL (c) || isV (c) || isT (c);
69   }
70   private final boolean isHighSurrogate (char c)
71   {
72     return c >= highSurrogateStart && c <= highSurrogateEnd;
73   }
74   private final boolean isLowSurrogate (char c)
75   {
76     return c >= lowSurrogateStart && c <= lowSurrogateEnd;
77   }
78
79   public int next ()
80   {
81     int end = iter.getEndIndex();
82     if (iter.getIndex() == end)
83       return DONE;
84
85     char c;
86     for (char prev = CharacterIterator.DONE; iter.getIndex() < end; prev = c)
87       {
88         c = iter.next();
89         if (c == CharacterIterator.DONE)
90           break;
91         int type = Character.getType(c);
92
93         // Break after paragraph separators.
94         if (type == Character.PARAGRAPH_SEPARATOR)
95           break;
96
97         // Now we need some lookahead.
98         char ahead = iter.next();
99         iter.previous();
100         if (ahead == CharacterIterator.DONE)
101           break;
102         int aheadType = Character.getType(ahead);
103
104         if (aheadType != Character.NON_SPACING_MARK
105             && ! isLowSurrogate (ahead)
106             && ! isLVT (ahead))
107           break;
108         if (! isLVT (c) && isLVT (ahead))
109           break;
110         if (isL (c) && ! isLVT (ahead)
111             && aheadType != Character.NON_SPACING_MARK)
112           break;
113         if (isV (c) && ! isV (ahead) && !isT (ahead)
114             && aheadType != Character.NON_SPACING_MARK)
115           break;
116         if (isT (c) && ! isT (ahead)
117             && aheadType != Character.NON_SPACING_MARK)
118           break;
119
120         if (! isHighSurrogate (c) && isLowSurrogate (ahead))
121           break;
122         if (isHighSurrogate (c) && ! isLowSurrogate (ahead))
123           break;
124         if (! isHighSurrogate (prev) && isLowSurrogate (c))
125           break;
126       }
127
128     return iter.getIndex();
129   }
130
131   public int previous ()
132   {
133     if (iter.getIndex() == iter.getBeginIndex())
134       return DONE;
135
136     int start = iter.getBeginIndex();
137     while (iter.getIndex() >= iter.getBeginIndex())
138       {
139         char c = iter.previous();
140         if (c == CharacterIterator.DONE)
141           break;
142         int type = Character.getType(c);
143
144         if (type != Character.NON_SPACING_MARK
145             && ! isLowSurrogate (c)
146             && ! isLVT (c))
147           break;
148
149         // Now we need some lookahead.
150         char ahead = iter.previous();
151         if (ahead == CharacterIterator.DONE)
152           {
153             iter.next();
154             break;
155           }
156         char ahead2 = iter.previous();
157         iter.next();
158         iter.next();
159         if (ahead2 == CharacterIterator.DONE)
160           break;
161         int aheadType = Character.getType(ahead);
162
163         if (aheadType == Character.PARAGRAPH_SEPARATOR)
164           break;
165
166         if (isLVT (c) && ! isLVT (ahead))
167           break;
168         if (! isLVT (c) && type != Character.NON_SPACING_MARK
169             && isL (ahead))
170           break;
171         if (! isV (c) && ! isT (c) && type != Character.NON_SPACING_MARK
172             && isV (ahead))
173           break;
174         if (! isT (c) && type != Character.NON_SPACING_MARK
175             && isT (ahead))
176           break;
177
178         if (isLowSurrogate (c) && ! isHighSurrogate (ahead))
179           break;
180         if (! isLowSurrogate (c) && isHighSurrogate (ahead))
181           break;
182         if (isLowSurrogate (ahead) && ! isHighSurrogate (ahead2))
183           break;
184       }
185
186     return iter.getIndex();
187   }
188 }