1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/*
* Copyright (C) 2006 Lars Knoll <lars@trolltech.com>
* Copyright (C) 2007-2009 Torch Mobile, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#include "config.h"
#include "TextBreakIterator.h"
#include "PlatformString.h"
#include <wtf/StdLibExtras.h>
#include <wtf/unicode/Unicode.h>
using namespace std;
using namespace WTF::Unicode;
namespace WebCore {
// Hack, not entirely correct
static inline bool isCharStop(UChar c)
{
CharCategory charCategory = category(c);
return charCategory != Mark_NonSpacing && (charCategory != Other_Surrogate || (c < 0xd800 || c >= 0xdc00));
}
static inline bool isLineStop(UChar c)
{
return category(c) != Separator_Line;
}
static inline bool isSentenceStop(UChar c)
{
return isPunct(c);
}
class TextBreakIterator {
public:
void reset(const UChar* str, int len)
{
string = str;
length = len;
currentPos = 0;
}
virtual int first() = 0;
virtual int next() = 0;
virtual int previous() = 0;
int following(int position)
{
currentPos = position;
return next();
}
int preceding(int position)
{
currentPos = position;
return previous();
}
int currentPos;
const UChar* string;
int length;
};
struct WordBreakIterator: TextBreakIterator {
virtual int first();
virtual int next();
virtual int previous();
};
struct CharBreakIterator: TextBreakIterator {
virtual int first();
virtual int next();
virtual int previous();
};
struct LineBreakIterator: TextBreakIterator {
virtual int first();
virtual int next();
virtual int previous();
};
struct SentenceBreakIterator : TextBreakIterator {
virtual int first();
virtual int next();
virtual int previous();
};
int WordBreakIterator::first()
{
currentPos = 0;
return currentPos;
}
int WordBreakIterator::next()
{
if (currentPos == length) {
currentPos = -1;
return currentPos;
}
bool haveSpace = false;
while (currentPos < length) {
if (haveSpace && !isSpace(string[currentPos]))
break;
if (isSpace(string[currentPos]))
haveSpace = true;
++currentPos;
}
return currentPos;
}
int WordBreakIterator::previous()
{
if (!currentPos) {
currentPos = -1;
return currentPos;
}
bool haveSpace = false;
while (currentPos > 0) {
if (haveSpace && !isSpace(string[currentPos]))
break;
if (isSpace(string[currentPos]))
haveSpace = true;
--currentPos;
}
return currentPos;
}
int CharBreakIterator::first()
{
currentPos = 0;
return currentPos;
}
int CharBreakIterator::next()
{
if (currentPos >= length)
return -1;
++currentPos;
while (currentPos < length && !isCharStop(string[currentPos]))
++currentPos;
return currentPos;
}
int CharBreakIterator::previous()
{
if (currentPos <= 0)
return -1;
if (currentPos > length)
currentPos = length;
--currentPos;
while (currentPos > 0 && !isCharStop(string[currentPos]))
--currentPos;
return currentPos;
}
int LineBreakIterator::first()
{
currentPos = 0;
return currentPos;
}
int LineBreakIterator::next()
{
if (currentPos == length) {
currentPos = -1;
return currentPos;
}
bool haveSpace = false;
while (currentPos < length) {
if (haveSpace && !isLineStop(string[currentPos]))
break;
if (isLineStop(string[currentPos]))
haveSpace = true;
++currentPos;
}
return currentPos;
}
int LineBreakIterator::previous()
{
if (!currentPos) {
currentPos = -1;
return currentPos;
}
bool haveSpace = false;
while (currentPos > 0) {
if (haveSpace && !isLineStop(string[currentPos]))
break;
if (isLineStop(string[currentPos]))
haveSpace = true;
--currentPos;
}
return currentPos;
}
int SentenceBreakIterator::first()
{
currentPos = 0;
return currentPos;
}
int SentenceBreakIterator::next()
{
if (currentPos == length) {
currentPos = -1;
return currentPos;
}
bool haveSpace = false;
while (currentPos < length) {
if (haveSpace && !isSentenceStop(string[currentPos]))
break;
if (isSentenceStop(string[currentPos]))
haveSpace = true;
++currentPos;
}
return currentPos;
}
int SentenceBreakIterator::previous()
{
if (!currentPos) {
currentPos = -1;
return currentPos;
}
bool haveSpace = false;
while (currentPos > 0) {
if (haveSpace && !isSentenceStop(string[currentPos]))
break;
if (isSentenceStop(string[currentPos]))
haveSpace = true;
--currentPos;
}
return currentPos;
}
TextBreakIterator* wordBreakIterator(const UChar* string, int length)
{
DEFINE_STATIC_LOCAL(WordBreakIterator, iterator, ());
iterator.reset(string, length);
return &iterator;
}
TextBreakIterator* characterBreakIterator(const UChar* string, int length)
{
DEFINE_STATIC_LOCAL(CharBreakIterator, iterator, ());
iterator.reset(string, length);
return &iterator;
}
static TextBreakIterator* staticLineBreakIterator;
TextBreakIterator* acquireLineBreakIterator(const UChar* string, int length)
{
TextBreakIterator* lineBreakIterator = 0;
if (staticLineBreakIterator) {
staticLineBreakIterator->reset(string, length);
swap(staticLineBreakIterator, lineBreakIterator);
}
if (!lineBreakIterator && string && length) {
lineBreakIterator = new LineBreakIterator;
lineBreakIterator->reset(string, length);
}
return lineBreakIterator;
}
void releaseLineBreakIterator(TextBreakIterator* iterator)
{
ASSERT(iterator);
if (!staticLineBreakIterator)
staticLineBreakIterator = iterator;
else
delete iterator;
}
TextBreakIterator* sentenceBreakIterator(const UChar* string, int length)
{
DEFINE_STATIC_LOCAL(SentenceBreakIterator, iterator, ());
iterator.reset(string, length);
return &iterator;
}
int textBreakFirst(TextBreakIterator* breakIterator)
{
return breakIterator->first();
}
int textBreakNext(TextBreakIterator* breakIterator)
{
return breakIterator->next();
}
int textBreakPreceding(TextBreakIterator* breakIterator, int position)
{
return breakIterator->preceding(position);
}
int textBreakFollowing(TextBreakIterator* breakIterator, int position)
{
return breakIterator->following(position);
}
int textBreakCurrent(TextBreakIterator* breakIterator)
{
return breakIterator->currentPos;
}
bool isTextBreak(TextBreakIterator*, int)
{
return true;
}
TextBreakIterator* cursorMovementIterator(const UChar* string, int length)
{
return characterBreakIterator(string, length);
}
} // namespace WebCore
|