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)
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
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
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
;
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
;
109 int end
= iter
.getEndIndex();
110 if (iter
.getIndex() == end
)
114 for (char prev
= CharacterIterator
.DONE
; iter
.getIndex() < end
; prev
= c
)
117 if (c
== CharacterIterator
.DONE
)
119 int type
= Character
.getType(c
);
121 // Break after paragraph separators.
122 if (type
== Character
.PARAGRAPH_SEPARATOR
)
125 // Now we need some lookahead.
126 char ahead
= iter
.next();
128 if (ahead
== CharacterIterator
.DONE
)
130 int aheadType
= Character
.getType(ahead
);
132 if (aheadType
!= Character
.NON_SPACING_MARK
133 && ! isLowSurrogate (ahead
)
136 if (! isLVT (c
) && isLVT (ahead
))
138 if (isL (c
) && ! isLVT (ahead
)
139 && aheadType
!= Character
.NON_SPACING_MARK
)
141 if (isV (c
) && ! isV (ahead
) && !isT (ahead
)
142 && aheadType
!= Character
.NON_SPACING_MARK
)
144 if (isT (c
) && ! isT (ahead
)
145 && aheadType
!= Character
.NON_SPACING_MARK
)
148 if (! isHighSurrogate (c
) && isLowSurrogate (ahead
))
150 if (isHighSurrogate (c
) && ! isLowSurrogate (ahead
))
152 if (! isHighSurrogate (prev
) && isLowSurrogate (c
))
156 return iter
.getIndex();
159 public int previous ()
161 if (iter
.getIndex() == iter
.getBeginIndex())
164 int start
= iter
.getBeginIndex();
165 while (iter
.getIndex() >= iter
.getBeginIndex())
167 char c
= iter
.previous();
168 if (c
== CharacterIterator
.DONE
)
170 int type
= Character
.getType(c
);
172 if (type
!= Character
.NON_SPACING_MARK
173 && ! isLowSurrogate (c
)
177 // Now we need some lookahead.
178 char ahead
= iter
.previous();
179 if (ahead
== CharacterIterator
.DONE
)
184 char ahead2
= iter
.previous();
187 if (ahead2
== CharacterIterator
.DONE
)
189 int aheadType
= Character
.getType(ahead
);
191 if (aheadType
== Character
.PARAGRAPH_SEPARATOR
)
194 if (isLVT (c
) && ! isLVT (ahead
))
196 if (! isLVT (c
) && type
!= Character
.NON_SPACING_MARK
199 if (! isV (c
) && ! isT (c
) && type
!= Character
.NON_SPACING_MARK
202 if (! isT (c
) && type
!= Character
.NON_SPACING_MARK
206 if (isLowSurrogate (c
) && ! isHighSurrogate (ahead
))
208 if (! isLowSurrogate (c
) && isHighSurrogate (ahead
))
210 if (isLowSurrogate (ahead
) && ! isHighSurrogate (ahead2
))
214 return iter
.getIndex();