2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / text / LineBreakIterator.java
blob59c45d7bdca287babfc4eb7b32cf8f35a2479db6
1 /* LineBreakIterator.java - Default word 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 22, 1999
47 * Written using The Unicode Standard, Version 2.0.
50 public class LineBreakIterator extends BaseBreakIterator
52 public Object clone ()
54 return new LineBreakIterator (this);
57 public LineBreakIterator ()
59 iter = null;
62 private LineBreakIterator (LineBreakIterator other)
64 iter = (CharacterIterator) other.iter.clone();
67 // Some methods to tell us different properties of characters.
68 private final boolean isNb (char c)
70 return (c == 0x00a0 // NO-BREAK SPACE
71 || c == 0x2011 // NON-BREAKING HYPHEN
72 || c == 0xfeff); // ZERO WITH NO-BREAK SPACE
74 private final boolean isClose (int type)
76 return (type == Character.END_PUNCTUATION
77 // Unicode book says "comma, period, ...", which I take to
78 // mean "Po" class.
79 || type == Character.OTHER_PUNCTUATION);
81 private final boolean isIdeo (char c)
83 return (c >= 0x3040 && c <= 0x309f // Hiragana
84 || c >= 0x30a0 && c <= 0x30ff // Katakana
85 || c >= 0x4e00 && c <= 0x9fff // Han
86 || c >= 0x3100 && c <= 0x312f); // Bopomofo
89 public int next ()
91 int end = iter.getEndIndex();
92 if (iter.getIndex() == end)
93 return DONE;
95 while (iter.getIndex() < end)
97 char c = iter.current();
98 int type = Character.getType(c);
100 char n = iter.next();
102 if (n == CharacterIterator.DONE
103 || type == Character.PARAGRAPH_SEPARATOR
104 || type == Character.LINE_SEPARATOR)
105 break;
107 // Handle two cases where we must scan for non-spacing marks.
108 int start = iter.getIndex();
109 if (type == Character.SPACE_SEPARATOR
110 || type == Character.START_PUNCTUATION
111 || isIdeo (c))
113 while (n != CharacterIterator.DONE
114 && Character.getType(n) == Character.NON_SPACING_MARK)
115 n = iter.next();
116 if (n == CharacterIterator.DONE)
117 break;
119 if (type == Character.SPACE_SEPARATOR)
121 int nt = Character.getType(n);
122 if (nt != Character.NON_SPACING_MARK
123 && nt != Character.SPACE_SEPARATOR
124 && ! isNb (n))
125 break;
127 else if (type == Character.START_PUNCTUATION)
129 if (isIdeo (n))
131 // Open punctuation followed by non spacing marks
132 // and then ideograph does not have a break in
133 // it. So skip all this.
134 start = iter.getIndex();
137 else
139 // Ideograph preceded this character.
140 if (isClose (Character.getType(n)))
141 break;
144 iter.setIndex(start);
147 return iter.getIndex();
150 public int previous ()
152 int start = iter.getBeginIndex();
153 if (iter.getIndex() == start)
154 return DONE;
156 while (iter.getIndex() >= start)
158 char c = iter.previous();
159 if (c == CharacterIterator.DONE)
160 break;
161 int type = Character.getType(c);
163 char n = iter.previous();
164 if (n == CharacterIterator.DONE)
165 break;
166 iter.next();
168 int nt = Character.getType(n);
169 // Break after paragraph separators.
170 if (nt == Character.PARAGRAPH_SEPARATOR
171 || nt == Character.LINE_SEPARATOR)
172 break;
174 // Skip non-spacing marks.
175 int init = iter.getIndex();
176 while (n != CharacterIterator.DONE && nt == Character.NON_SPACING_MARK)
178 n = iter.previous();
179 nt = Character.getType(n);
182 if (nt == Character.SPACE_SEPARATOR
183 && type != Character.SPACE_SEPARATOR
184 && type != Character.NON_SPACING_MARK
185 && ! isNb (c))
186 break;
187 if (! isClose (type) && isIdeo (n))
188 break;
189 if (isIdeo (c) && nt != Character.START_PUNCTUATION)
190 break;
191 iter.setIndex(init);
194 return iter.getIndex();