File: srchiter.cpp

package info (click to toggle)
icu 2.1-2.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 38,556 kB
  • ctags: 18,435
  • sloc: cpp: 118,545; ansic: 98,775; makefile: 3,759; sh: 3,178; perl: 1,325; lisp: 3
file content (281 lines) | stat: -rw-r--r-- 6,941 bytes parent folder | download
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
/*
**********************************************************************
*   Copyright (C) 1999-2000 IBM and others. All rights reserved.
**********************************************************************
*   Date        Name        Description
*  03/22/2000   helena      Creation.
**********************************************************************
*/

#include "unicode/brkiter.h"
#include "unicode/schriter.h"
#include "srchiter.h"


int32_t const SearchIterator::DONE = -1;
int32_t const SearchIterator::BEFORE = -2;    

SearchIterator::SearchIterator(void) :
    index(0),
    length(0),
    target(0),
    backward(FALSE), /* going forward */
    breaker(NULL),
    overlap(TRUE)
{
    UErrorCode status = U_ZERO_ERROR;
    this->breaker = BreakIterator::createCharacterInstance(Locale::getDefault(), status);
    if (U_FAILURE(status)) return;
}

SearchIterator::SearchIterator(CharacterIterator* target, 
                               BreakIterator* breaker) :
    index(0),
    length(0),
    target(0),
    backward(FALSE), /* going forward */
    breaker(NULL),
    overlap(TRUE)
{
    this->target = target;
    
    this->breaker = breaker;
    this->breaker->adoptText(this->target);
    
    index = this->target->startIndex();
    length = 0;
}

SearchIterator::SearchIterator(const  SearchIterator&   other) :
    length(other.length),
    target(0),
    backward(other.backward), /* going forward */
    breaker(NULL),
    overlap(other.overlap)  
{
    index = other.target->startIndex();
    this->target = other.target->clone();
    
    this->breaker = ((BreakIterator&)other.breaker).clone();
    this->breaker->adoptText(this->target);
}

SearchIterator::~SearchIterator()
{
    // deletion of breaker will delete target
    if (breaker != NULL) {
        delete breaker;
        breaker = 0;
    }
}

UBool SearchIterator::operator == (const SearchIterator& that) const
{
    if (this == &that) return TRUE;
    if (*that.breaker != *breaker) return FALSE;
    else if (*that.target != *target) return FALSE;
    else if (that.backward != backward) return FALSE;
    else if (that.index != index) return FALSE;
    else if (that.length != length) return FALSE;
    else if (that.overlap != overlap) return FALSE;
    else return TRUE;
}

int32_t SearchIterator::first(void) 
{
    setIndex(SearchIterator::BEFORE);
    return next();
}

int32_t SearchIterator::following(int32_t pos) 
{
    setIndex(pos);
    return next();
}
    
int32_t SearchIterator::last(void) 
{
    setIndex(SearchIterator::DONE);
    return previous();
}

int32_t SearchIterator::preceding(int32_t pos) 
{
    setIndex(pos);
    return previous();
}
    
int32_t SearchIterator::next(void) 
{
    if (index == SearchIterator::BEFORE){
        // Starting at the beginning of the text
        index = target->startIndex();
    } else if (index == SearchIterator::DONE) {
        return SearchIterator::DONE;
    } else if (length > 0) {
        // Finding the next match after a previous one
        index += overlap ? 1 : length;
    }
    index -= 1;
    backward = FALSE;
        
    do {
        UErrorCode status = U_ZERO_ERROR;
        length = 0;
        index = handleNext(index + 1, status);
        if (U_FAILURE(status))
        {
            return SearchIterator::DONE;
        }
    } while (index != SearchIterator::DONE && !isBreakUnit(index, index+length));
    
    return index;
}

int32_t SearchIterator::previous(void) 
{
    if (index == SearchIterator::DONE) {
        index = target->endIndex();
    } else if (index == SearchIterator::BEFORE) {
        return SearchIterator::DONE;
    } else if (length > 0) {
        // Finding the previous match before a following one
        index = overlap ? index + length - 1 : index;
    }
    index += 1;
    backward = TRUE;
    
    do {
        UErrorCode status = U_ZERO_ERROR;
        length = 0;
        index = handlePrev(index - 1, status);
        if (U_FAILURE(status))
        {
            return SearchIterator::DONE;
        }
    } while (index != SearchIterator::DONE && !isBreakUnit(index, index+length));

    if (index == SearchIterator::DONE) {
        index = SearchIterator::BEFORE;
    }
    return getIndex();
}


int32_t SearchIterator::getIndex() const
{
    return index == SearchIterator::BEFORE ? SearchIterator::DONE : index;
}

void SearchIterator::setOverlapping(UBool allowOverlap) 
{
     overlap = allowOverlap;
}
    
UBool SearchIterator::isOverlapping(void) const
{
    return overlap;
}
    
int32_t SearchIterator::getMatchLength(void) const
{
    return length;
}

void SearchIterator::reset(void)
{
    length = 0;
    if (backward == FALSE) {
        index = 0;
        target->setToStart();
        breaker->first();
    } else {
        index = SearchIterator::DONE;
        target->setToEnd();
        breaker->last();
    }
    overlap = TRUE;
}

void SearchIterator::setBreakIterator(const BreakIterator* iterator) 
{
    CharacterIterator *buffer = target->clone();
    delete breaker;
    breaker = iterator->clone();
    breaker->adoptText(buffer);
}

const BreakIterator& SearchIterator::getBreakIterator(void) const
{
    return *breaker;
}
 
void SearchIterator::setTarget(const UnicodeString& newText)
{
    if (target != NULL && target->getDynamicClassID()
            == StringCharacterIterator::getStaticClassID()) {
        ((StringCharacterIterator*)target)->setText(newText);
    }
    else {
        delete target;
		target = new StringCharacterIterator(newText);
        target->first();
        breaker->adoptText(target);
    }
}
  
void SearchIterator::adoptTarget(CharacterIterator* iterator) {
    target = iterator;
    breaker->adoptText(target);
    setIndex(SearchIterator::BEFORE);
}

const CharacterIterator& SearchIterator::getTarget(void) const
{
    SearchIterator* nonConstThis = (SearchIterator*)this;
    
    // The iterator is initialized pointing to no text at all, so if this
    // function is called while we're in that state, we have to fudge an
    // an iterator to return.
    if (nonConstThis->target == NULL)
        nonConstThis->target = new StringCharacterIterator("");
    return *nonConstThis->target;
}

void SearchIterator::getMatchedText(UnicodeString& result) 
{
    result.remove();
    if (length > 0) {
        int i = 0;
        for (UChar c = target->setIndex(index); i < length; c = target->next(), i++)
        {
            result += c;
        }
    }
}


void SearchIterator::setMatchLength(int32_t length) 
{
    this->length = length;
}

void SearchIterator::setIndex(int32_t pos) {
    index = pos;
    length = 0;
}

UBool SearchIterator::isBreakUnit(int32_t start, 
                                   int32_t end)
{
    if (breaker == NULL) {
        return TRUE;
    } 
    UBool startBound = breaker->isBoundary(start);
    UBool endBound = (end == target->endIndex()) || breaker->isBoundary(end);
    
    return startBound && endBound;
}