2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / text / CharacterBreakIterator.java
blob2febace8301709e9f6a594cda137225edfb88c86
1 /* CharacterBreakIterator.java - Default character BreakIterator.
2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.text;
41 import java.text.BreakIterator;
42 import java.text.CharacterIterator;
44 /**
45 * @author Tom Tromey <tromey@cygnus.com>
46 * @date March 19, 1999
47 * Written using The Unicode Standard, Version 2.0.
50 public class CharacterBreakIterator extends BaseBreakIterator
52 // Hangul Jamo constants from Unicode book.
53 private static final int LBase = 0x1100;
54 private static final int VBase = 0x1161;
55 private static final int TBase = 0x11a7;
56 private static final int LCount = 19;
57 private static final int VCount = 21;
58 private static final int TCount = 28;
60 // Information about surrogates.
61 private static final int highSurrogateStart = 0xD800;
62 private static final int highSurrogateEnd = 0xDBFF;
63 private static final int lowSurrogateStart = 0xDC00;
64 private static final int lowSurrogateEnd = 0xDFFF;
66 public Object clone ()
68 return new CharacterBreakIterator (this);
71 public CharacterBreakIterator ()
73 iter = null; // FIXME?
76 private CharacterBreakIterator (CharacterBreakIterator other)
78 iter = (CharacterIterator) other.iter.clone();
81 // Some methods to tell us different properties of characters.
82 private final boolean isL (char c)
84 return c >= LBase && c <= LBase + LCount;
86 private final boolean isV (char c)
88 return c >= VBase && c <= VBase + VCount;
90 private final boolean isT (char c)
92 return c >= TBase && c <= TBase + TCount;
94 private final boolean isLVT (char c)
96 return isL (c) || isV (c) || isT (c);
98 private final boolean isHighSurrogate (char c)
100 return c >= highSurrogateStart && c <= highSurrogateEnd;
102 private final boolean isLowSurrogate (char c)
104 return c >= lowSurrogateStart && c <= lowSurrogateEnd;
107 public int next ()
109 int end = iter.getEndIndex();
110 if (iter.getIndex() == end)
111 return DONE;
113 char c;
114 for (char prev = CharacterIterator.DONE; iter.getIndex() < end; prev = c)
116 c = iter.next();
117 if (c == CharacterIterator.DONE)
118 break;
119 int type = Character.getType(c);
121 // Break after paragraph separators.
122 if (type == Character.PARAGRAPH_SEPARATOR)
123 break;
125 // Now we need some lookahead.
126 char ahead = iter.next();
127 iter.previous();
128 if (ahead == CharacterIterator.DONE)
129 break;
130 int aheadType = Character.getType(ahead);
132 if (aheadType != Character.NON_SPACING_MARK
133 && ! isLowSurrogate (ahead)
134 && ! isLVT (ahead))
135 break;
136 if (! isLVT (c) && isLVT (ahead))
137 break;
138 if (isL (c) && ! isLVT (ahead)
139 && aheadType != Character.NON_SPACING_MARK)
140 break;
141 if (isV (c) && ! isV (ahead) && !isT (ahead)
142 && aheadType != Character.NON_SPACING_MARK)
143 break;
144 if (isT (c) && ! isT (ahead)
145 && aheadType != Character.NON_SPACING_MARK)
146 break;
148 if (! isHighSurrogate (c) && isLowSurrogate (ahead))
149 break;
150 if (isHighSurrogate (c) && ! isLowSurrogate (ahead))
151 break;
152 if (! isHighSurrogate (prev) && isLowSurrogate (c))
153 break;
156 return iter.getIndex();
159 public int previous ()
161 if (iter.getIndex() == iter.getBeginIndex())
162 return DONE;
164 int start = iter.getBeginIndex();
165 while (iter.getIndex() >= iter.getBeginIndex())
167 char c = iter.previous();
168 if (c == CharacterIterator.DONE)
169 break;
170 int type = Character.getType(c);
172 if (type != Character.NON_SPACING_MARK
173 && ! isLowSurrogate (c)
174 && ! isLVT (c))
175 break;
177 // Now we need some lookahead.
178 char ahead = iter.previous();
179 if (ahead == CharacterIterator.DONE)
181 iter.next();
182 break;
184 char ahead2 = iter.previous();
185 iter.next();
186 iter.next();
187 if (ahead2 == CharacterIterator.DONE)
188 break;
189 int aheadType = Character.getType(ahead);
191 if (aheadType == Character.PARAGRAPH_SEPARATOR)
192 break;
194 if (isLVT (c) && ! isLVT (ahead))
195 break;
196 if (! isLVT (c) && type != Character.NON_SPACING_MARK
197 && isL (ahead))
198 break;
199 if (! isV (c) && ! isT (c) && type != Character.NON_SPACING_MARK
200 && isV (ahead))
201 break;
202 if (! isT (c) && type != Character.NON_SPACING_MARK
203 && isT (ahead))
204 break;
206 if (isLowSurrogate (c) && ! isHighSurrogate (ahead))
207 break;
208 if (! isLowSurrogate (c) && isHighSurrogate (ahead))
209 break;
210 if (isLowSurrogate (ahead) && ! isHighSurrogate (ahead2))
211 break;
214 return iter.getIndex();