1 // Default character BreakIterator.
3 /* Copyright (C) 1999 Cygnus Solutions
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
13 import java.text.BreakIterator;
14 import java.text.CharacterIterator;
17 * @author Tom Tromey <tromey@cygnus.com>
18 * @date March 19, 1999
19 * Written using The Unicode Standard, Version 2.0.
22 public class CharacterBreakIterator extends BaseBreakIterator
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;
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;
38 public Object clone ()
40 return new CharacterBreakIterator (this);
43 public CharacterBreakIterator ()
45 iter = null; // FIXME?
48 private CharacterBreakIterator (CharacterBreakIterator other)
50 iter = (CharacterIterator) other.iter.clone();
53 // Some methods to tell us different properties of characters.
54 private final boolean isL (char c)
56 return c >= LBase && c <= LBase + LCount;
58 private final boolean isV (char c)
60 return c >= VBase && c <= VBase + VCount;
62 private final boolean isT (char c)
64 return c >= TBase && c <= TBase + TCount;
66 private final boolean isLVT (char c)
68 return isL (c) || isV (c) || isT (c);
70 private final boolean isHighSurrogate (char c)
72 return c >= highSurrogateStart && c <= highSurrogateEnd;
74 private final boolean isLowSurrogate (char c)
76 return c >= lowSurrogateStart && c <= lowSurrogateEnd;
81 int end = iter.getEndIndex();
82 if (iter.getIndex() == end)
86 for (char prev = CharacterIterator.DONE; iter.getIndex() < end; prev = c)
89 if (c == CharacterIterator.DONE)
91 int type = Character.getType(c);
93 // Break after paragraph separators.
94 if (type == Character.PARAGRAPH_SEPARATOR)
97 // Now we need some lookahead.
98 char ahead = iter.next();
100 if (ahead == CharacterIterator.DONE)
102 int aheadType = Character.getType(ahead);
104 if (aheadType != Character.NON_SPACING_MARK
105 && ! isLowSurrogate (ahead)
108 if (! isLVT (c) && isLVT (ahead))
110 if (isL (c) && ! isLVT (ahead)
111 && aheadType != Character.NON_SPACING_MARK)
113 if (isV (c) && ! isV (ahead) && !isT (ahead)
114 && aheadType != Character.NON_SPACING_MARK)
116 if (isT (c) && ! isT (ahead)
117 && aheadType != Character.NON_SPACING_MARK)
120 if (! isHighSurrogate (c) && isLowSurrogate (ahead))
122 if (isHighSurrogate (c) && ! isLowSurrogate (ahead))
124 if (! isHighSurrogate (prev) && isLowSurrogate (c))
128 return iter.getIndex();
131 public int previous ()
133 if (iter.getIndex() == iter.getBeginIndex())
136 int start = iter.getBeginIndex();
137 while (iter.getIndex() >= iter.getBeginIndex())
139 char c = iter.previous();
140 if (c == CharacterIterator.DONE)
142 int type = Character.getType(c);
144 if (type != Character.NON_SPACING_MARK
145 && ! isLowSurrogate (c)
149 // Now we need some lookahead.
150 char ahead = iter.previous();
151 if (ahead == CharacterIterator.DONE)
156 char ahead2 = iter.previous();
159 if (ahead2 == CharacterIterator.DONE)
161 int aheadType = Character.getType(ahead);
163 if (aheadType == Character.PARAGRAPH_SEPARATOR)
166 if (isLVT (c) && ! isLVT (ahead))
168 if (! isLVT (c) && type != Character.NON_SPACING_MARK
171 if (! isV (c) && ! isT (c) && type != Character.NON_SPACING_MARK
174 if (! isT (c) && type != Character.NON_SPACING_MARK
178 if (isLowSurrogate (c) && ! isHighSurrogate (ahead))
180 if (! isLowSurrogate (c) && isHighSurrogate (ahead))
182 if (isLowSurrogate (ahead) && ! isHighSurrogate (ahead2))
186 return iter.getIndex();