/src/mozilla-central/intl/icu/source/common/edits.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2017 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | |
4 | | // edits.cpp |
5 | | // created: 2017feb08 Markus W. Scherer |
6 | | |
7 | | #include "unicode/edits.h" |
8 | | #include "unicode/unistr.h" |
9 | | #include "unicode/utypes.h" |
10 | | #include "cmemory.h" |
11 | | #include "uassert.h" |
12 | | #include "util.h" |
13 | | |
14 | | U_NAMESPACE_BEGIN |
15 | | |
16 | | namespace { |
17 | | |
18 | | // 0000uuuuuuuuuuuu records u+1 unchanged text units. |
19 | | const int32_t MAX_UNCHANGED_LENGTH = 0x1000; |
20 | | const int32_t MAX_UNCHANGED = MAX_UNCHANGED_LENGTH - 1; |
21 | | |
22 | | // 0mmmnnnccccccccc with m=1..6 records ccc+1 replacements of m:n text units. |
23 | | const int32_t MAX_SHORT_CHANGE_OLD_LENGTH = 6; |
24 | | const int32_t MAX_SHORT_CHANGE_NEW_LENGTH = 7; |
25 | | const int32_t SHORT_CHANGE_NUM_MASK = 0x1ff; |
26 | | const int32_t MAX_SHORT_CHANGE = 0x6fff; |
27 | | |
28 | | // 0111mmmmmmnnnnnn records a replacement of m text units with n. |
29 | | // m or n = 61: actual length follows in the next edits array unit. |
30 | | // m or n = 62..63: actual length follows in the next two edits array units. |
31 | | // Bit 30 of the actual length is in the head unit. |
32 | | // Trailing units have bit 15 set. |
33 | | const int32_t LENGTH_IN_1TRAIL = 61; |
34 | | const int32_t LENGTH_IN_2TRAIL = 62; |
35 | | |
36 | | } // namespace |
37 | | |
38 | 0 | void Edits::releaseArray() U_NOEXCEPT { |
39 | 0 | if (array != stackArray) { |
40 | 0 | uprv_free(array); |
41 | 0 | } |
42 | 0 | } |
43 | | |
44 | 0 | Edits &Edits::copyArray(const Edits &other) { |
45 | 0 | if (U_FAILURE(errorCode_)) { |
46 | 0 | length = delta = numChanges = 0; |
47 | 0 | return *this; |
48 | 0 | } |
49 | 0 | if (length > capacity) { |
50 | 0 | uint16_t *newArray = (uint16_t *)uprv_malloc((size_t)length * 2); |
51 | 0 | if (newArray == nullptr) { |
52 | 0 | length = delta = numChanges = 0; |
53 | 0 | errorCode_ = U_MEMORY_ALLOCATION_ERROR; |
54 | 0 | return *this; |
55 | 0 | } |
56 | 0 | releaseArray(); |
57 | 0 | array = newArray; |
58 | 0 | capacity = length; |
59 | 0 | } |
60 | 0 | if (length > 0) { |
61 | 0 | uprv_memcpy(array, other.array, (size_t)length * 2); |
62 | 0 | } |
63 | 0 | return *this; |
64 | 0 | } |
65 | | |
66 | 0 | Edits &Edits::moveArray(Edits &src) U_NOEXCEPT { |
67 | 0 | if (U_FAILURE(errorCode_)) { |
68 | 0 | length = delta = numChanges = 0; |
69 | 0 | return *this; |
70 | 0 | } |
71 | 0 | releaseArray(); |
72 | 0 | if (length > STACK_CAPACITY) { |
73 | 0 | array = src.array; |
74 | 0 | capacity = src.capacity; |
75 | 0 | src.array = src.stackArray; |
76 | 0 | src.capacity = STACK_CAPACITY; |
77 | 0 | src.reset(); |
78 | 0 | return *this; |
79 | 0 | } |
80 | 0 | array = stackArray; |
81 | 0 | capacity = STACK_CAPACITY; |
82 | 0 | if (length > 0) { |
83 | 0 | uprv_memcpy(array, src.array, (size_t)length * 2); |
84 | 0 | } |
85 | 0 | return *this; |
86 | 0 | } |
87 | | |
88 | 0 | Edits &Edits::operator=(const Edits &other) { |
89 | 0 | length = other.length; |
90 | 0 | delta = other.delta; |
91 | 0 | numChanges = other.numChanges; |
92 | 0 | errorCode_ = other.errorCode_; |
93 | 0 | return copyArray(other); |
94 | 0 | } |
95 | | |
96 | 0 | Edits &Edits::operator=(Edits &&src) U_NOEXCEPT { |
97 | 0 | length = src.length; |
98 | 0 | delta = src.delta; |
99 | 0 | numChanges = src.numChanges; |
100 | 0 | errorCode_ = src.errorCode_; |
101 | 0 | return moveArray(src); |
102 | 0 | } |
103 | | |
104 | 0 | Edits::~Edits() { |
105 | 0 | releaseArray(); |
106 | 0 | } |
107 | | |
108 | 0 | void Edits::reset() U_NOEXCEPT { |
109 | 0 | length = delta = numChanges = 0; |
110 | 0 | errorCode_ = U_ZERO_ERROR; |
111 | 0 | } |
112 | | |
113 | 0 | void Edits::addUnchanged(int32_t unchangedLength) { |
114 | 0 | if(U_FAILURE(errorCode_) || unchangedLength == 0) { return; } |
115 | 0 | if(unchangedLength < 0) { |
116 | 0 | errorCode_ = U_ILLEGAL_ARGUMENT_ERROR; |
117 | 0 | return; |
118 | 0 | } |
119 | 0 | // Merge into previous unchanged-text record, if any. |
120 | 0 | int32_t last = lastUnit(); |
121 | 0 | if(last < MAX_UNCHANGED) { |
122 | 0 | int32_t remaining = MAX_UNCHANGED - last; |
123 | 0 | if (remaining >= unchangedLength) { |
124 | 0 | setLastUnit(last + unchangedLength); |
125 | 0 | return; |
126 | 0 | } |
127 | 0 | setLastUnit(MAX_UNCHANGED); |
128 | 0 | unchangedLength -= remaining; |
129 | 0 | } |
130 | 0 | // Split large lengths into multiple units. |
131 | 0 | while(unchangedLength >= MAX_UNCHANGED_LENGTH) { |
132 | 0 | append(MAX_UNCHANGED); |
133 | 0 | unchangedLength -= MAX_UNCHANGED_LENGTH; |
134 | 0 | } |
135 | 0 | // Write a small (remaining) length. |
136 | 0 | if(unchangedLength > 0) { |
137 | 0 | append(unchangedLength - 1); |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | 0 | void Edits::addReplace(int32_t oldLength, int32_t newLength) { |
142 | 0 | if(U_FAILURE(errorCode_)) { return; } |
143 | 0 | if(oldLength < 0 || newLength < 0) { |
144 | 0 | errorCode_ = U_ILLEGAL_ARGUMENT_ERROR; |
145 | 0 | return; |
146 | 0 | } |
147 | 0 | if (oldLength == 0 && newLength == 0) { |
148 | 0 | return; |
149 | 0 | } |
150 | 0 | ++numChanges; |
151 | 0 | int32_t newDelta = newLength - oldLength; |
152 | 0 | if (newDelta != 0) { |
153 | 0 | if ((newDelta > 0 && delta >= 0 && newDelta > (INT32_MAX - delta)) || |
154 | 0 | (newDelta < 0 && delta < 0 && newDelta < (INT32_MIN - delta))) { |
155 | 0 | // Integer overflow or underflow. |
156 | 0 | errorCode_ = U_INDEX_OUTOFBOUNDS_ERROR; |
157 | 0 | return; |
158 | 0 | } |
159 | 0 | delta += newDelta; |
160 | 0 | } |
161 | 0 |
|
162 | 0 | if(0 < oldLength && oldLength <= MAX_SHORT_CHANGE_OLD_LENGTH && |
163 | 0 | newLength <= MAX_SHORT_CHANGE_NEW_LENGTH) { |
164 | 0 | // Merge into previous same-lengths short-replacement record, if any. |
165 | 0 | int32_t u = (oldLength << 12) | (newLength << 9); |
166 | 0 | int32_t last = lastUnit(); |
167 | 0 | if(MAX_UNCHANGED < last && last < MAX_SHORT_CHANGE && |
168 | 0 | (last & ~SHORT_CHANGE_NUM_MASK) == u && |
169 | 0 | (last & SHORT_CHANGE_NUM_MASK) < SHORT_CHANGE_NUM_MASK) { |
170 | 0 | setLastUnit(last + 1); |
171 | 0 | return; |
172 | 0 | } |
173 | 0 | append(u); |
174 | 0 | return; |
175 | 0 | } |
176 | 0 | |
177 | 0 | int32_t head = 0x7000; |
178 | 0 | if (oldLength < LENGTH_IN_1TRAIL && newLength < LENGTH_IN_1TRAIL) { |
179 | 0 | head |= oldLength << 6; |
180 | 0 | head |= newLength; |
181 | 0 | append(head); |
182 | 0 | } else if ((capacity - length) >= 5 || growArray()) { |
183 | 0 | int32_t limit = length + 1; |
184 | 0 | if(oldLength < LENGTH_IN_1TRAIL) { |
185 | 0 | head |= oldLength << 6; |
186 | 0 | } else if(oldLength <= 0x7fff) { |
187 | 0 | head |= LENGTH_IN_1TRAIL << 6; |
188 | 0 | array[limit++] = (uint16_t)(0x8000 | oldLength); |
189 | 0 | } else { |
190 | 0 | head |= (LENGTH_IN_2TRAIL + (oldLength >> 30)) << 6; |
191 | 0 | array[limit++] = (uint16_t)(0x8000 | (oldLength >> 15)); |
192 | 0 | array[limit++] = (uint16_t)(0x8000 | oldLength); |
193 | 0 | } |
194 | 0 | if(newLength < LENGTH_IN_1TRAIL) { |
195 | 0 | head |= newLength; |
196 | 0 | } else if(newLength <= 0x7fff) { |
197 | 0 | head |= LENGTH_IN_1TRAIL; |
198 | 0 | array[limit++] = (uint16_t)(0x8000 | newLength); |
199 | 0 | } else { |
200 | 0 | head |= LENGTH_IN_2TRAIL + (newLength >> 30); |
201 | 0 | array[limit++] = (uint16_t)(0x8000 | (newLength >> 15)); |
202 | 0 | array[limit++] = (uint16_t)(0x8000 | newLength); |
203 | 0 | } |
204 | 0 | array[length] = (uint16_t)head; |
205 | 0 | length = limit; |
206 | 0 | } |
207 | 0 | } |
208 | | |
209 | 0 | void Edits::append(int32_t r) { |
210 | 0 | if(length < capacity || growArray()) { |
211 | 0 | array[length++] = (uint16_t)r; |
212 | 0 | } |
213 | 0 | } |
214 | | |
215 | 0 | UBool Edits::growArray() { |
216 | 0 | int32_t newCapacity; |
217 | 0 | if (array == stackArray) { |
218 | 0 | newCapacity = 2000; |
219 | 0 | } else if (capacity == INT32_MAX) { |
220 | 0 | // Not U_BUFFER_OVERFLOW_ERROR because that could be confused on a string transform API |
221 | 0 | // with a result-string-buffer overflow. |
222 | 0 | errorCode_ = U_INDEX_OUTOFBOUNDS_ERROR; |
223 | 0 | return FALSE; |
224 | 0 | } else if (capacity >= (INT32_MAX / 2)) { |
225 | 0 | newCapacity = INT32_MAX; |
226 | 0 | } else { |
227 | 0 | newCapacity = 2 * capacity; |
228 | 0 | } |
229 | 0 | // Grow by at least 5 units so that a maximal change record will fit. |
230 | 0 | if ((newCapacity - capacity) < 5) { |
231 | 0 | errorCode_ = U_INDEX_OUTOFBOUNDS_ERROR; |
232 | 0 | return FALSE; |
233 | 0 | } |
234 | 0 | uint16_t *newArray = (uint16_t *)uprv_malloc((size_t)newCapacity * 2); |
235 | 0 | if (newArray == NULL) { |
236 | 0 | errorCode_ = U_MEMORY_ALLOCATION_ERROR; |
237 | 0 | return FALSE; |
238 | 0 | } |
239 | 0 | uprv_memcpy(newArray, array, (size_t)length * 2); |
240 | 0 | releaseArray(); |
241 | 0 | array = newArray; |
242 | 0 | capacity = newCapacity; |
243 | 0 | return TRUE; |
244 | 0 | } |
245 | | |
246 | 0 | UBool Edits::copyErrorTo(UErrorCode &outErrorCode) { |
247 | 0 | if (U_FAILURE(outErrorCode)) { return TRUE; } |
248 | 0 | if (U_SUCCESS(errorCode_)) { return FALSE; } |
249 | 0 | outErrorCode = errorCode_; |
250 | 0 | return TRUE; |
251 | 0 | } |
252 | | |
253 | 0 | Edits &Edits::mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode) { |
254 | 0 | if (copyErrorTo(errorCode)) { return *this; } |
255 | 0 | // Picture string a --(Edits ab)--> string b --(Edits bc)--> string c. |
256 | 0 | // Parallel iteration over both Edits. |
257 | 0 | Iterator abIter = ab.getFineIterator(); |
258 | 0 | Iterator bcIter = bc.getFineIterator(); |
259 | 0 | UBool abHasNext = TRUE, bcHasNext = TRUE; |
260 | 0 | // Copy iterator state into local variables, so that we can modify and subdivide spans. |
261 | 0 | // ab old & new length, bc old & new length |
262 | 0 | int32_t aLength = 0, ab_bLength = 0, bc_bLength = 0, cLength = 0; |
263 | 0 | // When we have different-intermediate-length changes, we accumulate a larger change. |
264 | 0 | int32_t pending_aLength = 0, pending_cLength = 0; |
265 | 0 | for (;;) { |
266 | 0 | // At this point, for each of the two iterators: |
267 | 0 | // Either we are done with the locally cached current edit, |
268 | 0 | // and its intermediate-string length has been reset, |
269 | 0 | // or we will continue to work with a truncated remainder of this edit. |
270 | 0 | // |
271 | 0 | // If the current edit is done, and the iterator has not yet reached the end, |
272 | 0 | // then we fetch the next edit. This is true for at least one of the iterators. |
273 | 0 | // |
274 | 0 | // Normally it does not matter whether we fetch from ab and then bc or vice versa. |
275 | 0 | // However, the result is observably different when |
276 | 0 | // ab deletions meet bc insertions at the same intermediate-string index. |
277 | 0 | // Some users expect the bc insertions to come first, so we fetch from bc first. |
278 | 0 | if (bc_bLength == 0) { |
279 | 0 | if (bcHasNext && (bcHasNext = bcIter.next(errorCode))) { |
280 | 0 | bc_bLength = bcIter.oldLength(); |
281 | 0 | cLength = bcIter.newLength(); |
282 | 0 | if (bc_bLength == 0) { |
283 | 0 | // insertion |
284 | 0 | if (ab_bLength == 0 || !abIter.hasChange()) { |
285 | 0 | addReplace(pending_aLength, pending_cLength + cLength); |
286 | 0 | pending_aLength = pending_cLength = 0; |
287 | 0 | } else { |
288 | 0 | pending_cLength += cLength; |
289 | 0 | } |
290 | 0 | continue; |
291 | 0 | } |
292 | 0 | } |
293 | 0 | // else see if the other iterator is done, too. |
294 | 0 | } |
295 | 0 | if (ab_bLength == 0) { |
296 | 0 | if (abHasNext && (abHasNext = abIter.next(errorCode))) { |
297 | 0 | aLength = abIter.oldLength(); |
298 | 0 | ab_bLength = abIter.newLength(); |
299 | 0 | if (ab_bLength == 0) { |
300 | 0 | // deletion |
301 | 0 | if (bc_bLength == bcIter.oldLength() || !bcIter.hasChange()) { |
302 | 0 | addReplace(pending_aLength + aLength, pending_cLength); |
303 | 0 | pending_aLength = pending_cLength = 0; |
304 | 0 | } else { |
305 | 0 | pending_aLength += aLength; |
306 | 0 | } |
307 | 0 | continue; |
308 | 0 | } |
309 | 0 | } else if (bc_bLength == 0) { |
310 | 0 | // Both iterators are done at the same time: |
311 | 0 | // The intermediate-string lengths match. |
312 | 0 | break; |
313 | 0 | } else { |
314 | 0 | // The ab output string is shorter than the bc input string. |
315 | 0 | if (!copyErrorTo(errorCode)) { |
316 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
317 | 0 | } |
318 | 0 | return *this; |
319 | 0 | } |
320 | 0 | } |
321 | 0 | if (bc_bLength == 0) { |
322 | 0 | // The bc input string is shorter than the ab output string. |
323 | 0 | if (!copyErrorTo(errorCode)) { |
324 | 0 | errorCode = U_ILLEGAL_ARGUMENT_ERROR; |
325 | 0 | } |
326 | 0 | return *this; |
327 | 0 | } |
328 | 0 | // Done fetching: ab_bLength > 0 && bc_bLength > 0 |
329 | 0 |
|
330 | 0 | // The current state has two parts: |
331 | 0 | // - Past: We accumulate a longer ac edit in the "pending" variables. |
332 | 0 | // - Current: We have copies of the current ab/bc edits in local variables. |
333 | 0 | // At least one side is newly fetched. |
334 | 0 | // One side might be a truncated remainder of an edit we fetched earlier. |
335 | 0 |
|
336 | 0 | if (!abIter.hasChange() && !bcIter.hasChange()) { |
337 | 0 | // An unchanged span all the way from string a to string c. |
338 | 0 | if (pending_aLength != 0 || pending_cLength != 0) { |
339 | 0 | addReplace(pending_aLength, pending_cLength); |
340 | 0 | pending_aLength = pending_cLength = 0; |
341 | 0 | } |
342 | 0 | int32_t unchangedLength = aLength <= cLength ? aLength : cLength; |
343 | 0 | addUnchanged(unchangedLength); |
344 | 0 | ab_bLength = aLength -= unchangedLength; |
345 | 0 | bc_bLength = cLength -= unchangedLength; |
346 | 0 | // At least one of the unchanged spans is now empty. |
347 | 0 | continue; |
348 | 0 | } |
349 | 0 | if (!abIter.hasChange() && bcIter.hasChange()) { |
350 | 0 | // Unchanged a->b but changed b->c. |
351 | 0 | if (ab_bLength >= bc_bLength) { |
352 | 0 | // Split the longer unchanged span into change + remainder. |
353 | 0 | addReplace(pending_aLength + bc_bLength, pending_cLength + cLength); |
354 | 0 | pending_aLength = pending_cLength = 0; |
355 | 0 | aLength = ab_bLength -= bc_bLength; |
356 | 0 | bc_bLength = 0; |
357 | 0 | continue; |
358 | 0 | } |
359 | 0 | // Handle the shorter unchanged span below like a change. |
360 | 0 | } else if (abIter.hasChange() && !bcIter.hasChange()) { |
361 | 0 | // Changed a->b and then unchanged b->c. |
362 | 0 | if (ab_bLength <= bc_bLength) { |
363 | 0 | // Split the longer unchanged span into change + remainder. |
364 | 0 | addReplace(pending_aLength + aLength, pending_cLength + ab_bLength); |
365 | 0 | pending_aLength = pending_cLength = 0; |
366 | 0 | cLength = bc_bLength -= ab_bLength; |
367 | 0 | ab_bLength = 0; |
368 | 0 | continue; |
369 | 0 | } |
370 | 0 | // Handle the shorter unchanged span below like a change. |
371 | 0 | } else { // both abIter.hasChange() && bcIter.hasChange() |
372 | 0 | if (ab_bLength == bc_bLength) { |
373 | 0 | // Changes on both sides up to the same position. Emit & reset. |
374 | 0 | addReplace(pending_aLength + aLength, pending_cLength + cLength); |
375 | 0 | pending_aLength = pending_cLength = 0; |
376 | 0 | ab_bLength = bc_bLength = 0; |
377 | 0 | continue; |
378 | 0 | } |
379 | 0 | } |
380 | 0 | // Accumulate the a->c change, reset the shorter side, |
381 | 0 | // keep a remainder of the longer one. |
382 | 0 | pending_aLength += aLength; |
383 | 0 | pending_cLength += cLength; |
384 | 0 | if (ab_bLength < bc_bLength) { |
385 | 0 | bc_bLength -= ab_bLength; |
386 | 0 | cLength = ab_bLength = 0; |
387 | 0 | } else { // ab_bLength > bc_bLength |
388 | 0 | ab_bLength -= bc_bLength; |
389 | 0 | aLength = bc_bLength = 0; |
390 | 0 | } |
391 | 0 | } |
392 | 0 | if (pending_aLength != 0 || pending_cLength != 0) { |
393 | 0 | addReplace(pending_aLength, pending_cLength); |
394 | 0 | } |
395 | 0 | copyErrorTo(errorCode); |
396 | 0 | return *this; |
397 | 0 | } |
398 | | |
399 | | Edits::Iterator::Iterator(const uint16_t *a, int32_t len, UBool oc, UBool crs) : |
400 | | array(a), index(0), length(len), remaining(0), |
401 | | onlyChanges_(oc), coarse(crs), |
402 | | dir(0), changed(FALSE), oldLength_(0), newLength_(0), |
403 | 0 | srcIndex(0), replIndex(0), destIndex(0) {} |
404 | | |
405 | 0 | int32_t Edits::Iterator::readLength(int32_t head) { |
406 | 0 | if (head < LENGTH_IN_1TRAIL) { |
407 | 0 | return head; |
408 | 0 | } else if (head < LENGTH_IN_2TRAIL) { |
409 | 0 | U_ASSERT(index < length); |
410 | 0 | U_ASSERT(array[index] >= 0x8000); |
411 | 0 | return array[index++] & 0x7fff; |
412 | 0 | } else { |
413 | 0 | U_ASSERT((index + 2) <= length); |
414 | 0 | U_ASSERT(array[index] >= 0x8000); |
415 | 0 | U_ASSERT(array[index + 1] >= 0x8000); |
416 | 0 | int32_t len = ((head & 1) << 30) | |
417 | 0 | ((int32_t)(array[index] & 0x7fff) << 15) | |
418 | 0 | (array[index + 1] & 0x7fff); |
419 | 0 | index += 2; |
420 | 0 | return len; |
421 | 0 | } |
422 | 0 | } |
423 | | |
424 | 0 | void Edits::Iterator::updateNextIndexes() { |
425 | 0 | srcIndex += oldLength_; |
426 | 0 | if (changed) { |
427 | 0 | replIndex += newLength_; |
428 | 0 | } |
429 | 0 | destIndex += newLength_; |
430 | 0 | } |
431 | | |
432 | 0 | void Edits::Iterator::updatePreviousIndexes() { |
433 | 0 | srcIndex -= oldLength_; |
434 | 0 | if (changed) { |
435 | 0 | replIndex -= newLength_; |
436 | 0 | } |
437 | 0 | destIndex -= newLength_; |
438 | 0 | } |
439 | | |
440 | 0 | UBool Edits::Iterator::noNext() { |
441 | 0 | // No change before or beyond the string. |
442 | 0 | dir = 0; |
443 | 0 | changed = FALSE; |
444 | 0 | oldLength_ = newLength_ = 0; |
445 | 0 | return FALSE; |
446 | 0 | } |
447 | | |
448 | 0 | UBool Edits::Iterator::next(UBool onlyChanges, UErrorCode &errorCode) { |
449 | 0 | // Forward iteration: Update the string indexes to the limit of the current span, |
450 | 0 | // and post-increment-read array units to assemble a new span. |
451 | 0 | // Leaves the array index one after the last unit of that span. |
452 | 0 | if (U_FAILURE(errorCode)) { return FALSE; } |
453 | 0 | // We have an errorCode in case we need to start guarding against integer overflows. |
454 | 0 | // It is also convenient for caller loops if we bail out when an error was set elsewhere. |
455 | 0 | if (dir > 0) { |
456 | 0 | updateNextIndexes(); |
457 | 0 | } else { |
458 | 0 | if (dir < 0) { |
459 | 0 | // Turn around from previous() to next(). |
460 | 0 | // Post-increment-read the same span again. |
461 | 0 | if (remaining > 0) { |
462 | 0 | // Fine-grained iterator: |
463 | 0 | // Stay on the current one of a sequence of compressed changes. |
464 | 0 | ++index; // next() rests on the index after the sequence unit. |
465 | 0 | dir = 1; |
466 | 0 | return TRUE; |
467 | 0 | } |
468 | 0 | } |
469 | 0 | dir = 1; |
470 | 0 | } |
471 | 0 | if (remaining >= 1) { |
472 | 0 | // Fine-grained iterator: Continue a sequence of compressed changes. |
473 | 0 | if (remaining > 1) { |
474 | 0 | --remaining; |
475 | 0 | return TRUE; |
476 | 0 | } |
477 | 0 | remaining = 0; |
478 | 0 | } |
479 | 0 | if (index >= length) { |
480 | 0 | return noNext(); |
481 | 0 | } |
482 | 0 | int32_t u = array[index++]; |
483 | 0 | if (u <= MAX_UNCHANGED) { |
484 | 0 | // Combine adjacent unchanged ranges. |
485 | 0 | changed = FALSE; |
486 | 0 | oldLength_ = u + 1; |
487 | 0 | while (index < length && (u = array[index]) <= MAX_UNCHANGED) { |
488 | 0 | ++index; |
489 | 0 | oldLength_ += u + 1; |
490 | 0 | } |
491 | 0 | newLength_ = oldLength_; |
492 | 0 | if (onlyChanges) { |
493 | 0 | updateNextIndexes(); |
494 | 0 | if (index >= length) { |
495 | 0 | return noNext(); |
496 | 0 | } |
497 | 0 | // already fetched u > MAX_UNCHANGED at index |
498 | 0 | ++index; |
499 | 0 | } else { |
500 | 0 | return TRUE; |
501 | 0 | } |
502 | 0 | } |
503 | 0 | changed = TRUE; |
504 | 0 | if (u <= MAX_SHORT_CHANGE) { |
505 | 0 | int32_t oldLen = u >> 12; |
506 | 0 | int32_t newLen = (u >> 9) & MAX_SHORT_CHANGE_NEW_LENGTH; |
507 | 0 | int32_t num = (u & SHORT_CHANGE_NUM_MASK) + 1; |
508 | 0 | if (coarse) { |
509 | 0 | oldLength_ = num * oldLen; |
510 | 0 | newLength_ = num * newLen; |
511 | 0 | } else { |
512 | 0 | // Split a sequence of changes that was compressed into one unit. |
513 | 0 | oldLength_ = oldLen; |
514 | 0 | newLength_ = newLen; |
515 | 0 | if (num > 1) { |
516 | 0 | remaining = num; // This is the first of two or more changes. |
517 | 0 | } |
518 | 0 | return TRUE; |
519 | 0 | } |
520 | 0 | } else { |
521 | 0 | U_ASSERT(u <= 0x7fff); |
522 | 0 | oldLength_ = readLength((u >> 6) & 0x3f); |
523 | 0 | newLength_ = readLength(u & 0x3f); |
524 | 0 | if (!coarse) { |
525 | 0 | return TRUE; |
526 | 0 | } |
527 | 0 | } |
528 | 0 | // Combine adjacent changes. |
529 | 0 | while (index < length && (u = array[index]) > MAX_UNCHANGED) { |
530 | 0 | ++index; |
531 | 0 | if (u <= MAX_SHORT_CHANGE) { |
532 | 0 | int32_t num = (u & SHORT_CHANGE_NUM_MASK) + 1; |
533 | 0 | oldLength_ += (u >> 12) * num; |
534 | 0 | newLength_ += ((u >> 9) & MAX_SHORT_CHANGE_NEW_LENGTH) * num; |
535 | 0 | } else { |
536 | 0 | U_ASSERT(u <= 0x7fff); |
537 | 0 | oldLength_ += readLength((u >> 6) & 0x3f); |
538 | 0 | newLength_ += readLength(u & 0x3f); |
539 | 0 | } |
540 | 0 | } |
541 | 0 | return TRUE; |
542 | 0 | } |
543 | | |
544 | 0 | UBool Edits::Iterator::previous(UErrorCode &errorCode) { |
545 | 0 | // Backward iteration: Pre-decrement-read array units to assemble a new span, |
546 | 0 | // then update the string indexes to the start of that span. |
547 | 0 | // Leaves the array index on the head unit of that span. |
548 | 0 | if (U_FAILURE(errorCode)) { return FALSE; } |
549 | 0 | // We have an errorCode in case we need to start guarding against integer overflows. |
550 | 0 | // It is also convenient for caller loops if we bail out when an error was set elsewhere. |
551 | 0 | if (dir >= 0) { |
552 | 0 | if (dir > 0) { |
553 | 0 | // Turn around from next() to previous(). |
554 | 0 | // Set the string indexes to the span limit and |
555 | 0 | // pre-decrement-read the same span again. |
556 | 0 | if (remaining > 0) { |
557 | 0 | // Fine-grained iterator: |
558 | 0 | // Stay on the current one of a sequence of compressed changes. |
559 | 0 | --index; // previous() rests on the sequence unit. |
560 | 0 | dir = -1; |
561 | 0 | return TRUE; |
562 | 0 | } |
563 | 0 | updateNextIndexes(); |
564 | 0 | } |
565 | 0 | dir = -1; |
566 | 0 | } |
567 | 0 | if (remaining > 0) { |
568 | 0 | // Fine-grained iterator: Continue a sequence of compressed changes. |
569 | 0 | int32_t u = array[index]; |
570 | 0 | U_ASSERT(MAX_UNCHANGED < u && u <= MAX_SHORT_CHANGE); |
571 | 0 | if (remaining <= (u & SHORT_CHANGE_NUM_MASK)) { |
572 | 0 | ++remaining; |
573 | 0 | updatePreviousIndexes(); |
574 | 0 | return TRUE; |
575 | 0 | } |
576 | 0 | remaining = 0; |
577 | 0 | } |
578 | 0 | if (index <= 0) { |
579 | 0 | return noNext(); |
580 | 0 | } |
581 | 0 | int32_t u = array[--index]; |
582 | 0 | if (u <= MAX_UNCHANGED) { |
583 | 0 | // Combine adjacent unchanged ranges. |
584 | 0 | changed = FALSE; |
585 | 0 | oldLength_ = u + 1; |
586 | 0 | while (index > 0 && (u = array[index - 1]) <= MAX_UNCHANGED) { |
587 | 0 | --index; |
588 | 0 | oldLength_ += u + 1; |
589 | 0 | } |
590 | 0 | newLength_ = oldLength_; |
591 | 0 | // No need to handle onlyChanges as long as previous() is called only from findIndex(). |
592 | 0 | updatePreviousIndexes(); |
593 | 0 | return TRUE; |
594 | 0 | } |
595 | 0 | changed = TRUE; |
596 | 0 | if (u <= MAX_SHORT_CHANGE) { |
597 | 0 | int32_t oldLen = u >> 12; |
598 | 0 | int32_t newLen = (u >> 9) & MAX_SHORT_CHANGE_NEW_LENGTH; |
599 | 0 | int32_t num = (u & SHORT_CHANGE_NUM_MASK) + 1; |
600 | 0 | if (coarse) { |
601 | 0 | oldLength_ = num * oldLen; |
602 | 0 | newLength_ = num * newLen; |
603 | 0 | } else { |
604 | 0 | // Split a sequence of changes that was compressed into one unit. |
605 | 0 | oldLength_ = oldLen; |
606 | 0 | newLength_ = newLen; |
607 | 0 | if (num > 1) { |
608 | 0 | remaining = 1; // This is the last of two or more changes. |
609 | 0 | } |
610 | 0 | updatePreviousIndexes(); |
611 | 0 | return TRUE; |
612 | 0 | } |
613 | 0 | } else { |
614 | 0 | if (u <= 0x7fff) { |
615 | 0 | // The change is encoded in u alone. |
616 | 0 | oldLength_ = readLength((u >> 6) & 0x3f); |
617 | 0 | newLength_ = readLength(u & 0x3f); |
618 | 0 | } else { |
619 | 0 | // Back up to the head of the change, read the lengths, |
620 | 0 | // and reset the index to the head again. |
621 | 0 | U_ASSERT(index > 0); |
622 | 0 | while ((u = array[--index]) > 0x7fff) {} |
623 | 0 | U_ASSERT(u > MAX_SHORT_CHANGE); |
624 | 0 | int32_t headIndex = index++; |
625 | 0 | oldLength_ = readLength((u >> 6) & 0x3f); |
626 | 0 | newLength_ = readLength(u & 0x3f); |
627 | 0 | index = headIndex; |
628 | 0 | } |
629 | 0 | if (!coarse) { |
630 | 0 | updatePreviousIndexes(); |
631 | 0 | return TRUE; |
632 | 0 | } |
633 | 0 | } |
634 | 0 | // Combine adjacent changes. |
635 | 0 | while (index > 0 && (u = array[index - 1]) > MAX_UNCHANGED) { |
636 | 0 | --index; |
637 | 0 | if (u <= MAX_SHORT_CHANGE) { |
638 | 0 | int32_t num = (u & SHORT_CHANGE_NUM_MASK) + 1; |
639 | 0 | oldLength_ += (u >> 12) * num; |
640 | 0 | newLength_ += ((u >> 9) & MAX_SHORT_CHANGE_NEW_LENGTH) * num; |
641 | 0 | } else if (u <= 0x7fff) { |
642 | 0 | // Read the lengths, and reset the index to the head again. |
643 | 0 | int32_t headIndex = index++; |
644 | 0 | oldLength_ += readLength((u >> 6) & 0x3f); |
645 | 0 | newLength_ += readLength(u & 0x3f); |
646 | 0 | index = headIndex; |
647 | 0 | } |
648 | 0 | } |
649 | 0 | updatePreviousIndexes(); |
650 | 0 | return TRUE; |
651 | 0 | } |
652 | | |
653 | 0 | int32_t Edits::Iterator::findIndex(int32_t i, UBool findSource, UErrorCode &errorCode) { |
654 | 0 | if (U_FAILURE(errorCode) || i < 0) { return -1; } |
655 | 0 | int32_t spanStart, spanLength; |
656 | 0 | if (findSource) { // find source index |
657 | 0 | spanStart = srcIndex; |
658 | 0 | spanLength = oldLength_; |
659 | 0 | } else { // find destination index |
660 | 0 | spanStart = destIndex; |
661 | 0 | spanLength = newLength_; |
662 | 0 | } |
663 | 0 | if (i < spanStart) { |
664 | 0 | if (i >= (spanStart / 2)) { |
665 | 0 | // Search backwards. |
666 | 0 | for (;;) { |
667 | 0 | UBool hasPrevious = previous(errorCode); |
668 | 0 | U_ASSERT(hasPrevious); // because i>=0 and the first span starts at 0 |
669 | 0 | (void)hasPrevious; // avoid unused-variable warning |
670 | 0 | spanStart = findSource ? srcIndex : destIndex; |
671 | 0 | if (i >= spanStart) { |
672 | 0 | // The index is in the current span. |
673 | 0 | return 0; |
674 | 0 | } |
675 | 0 | if (remaining > 0) { |
676 | 0 | // Is the index in one of the remaining compressed edits? |
677 | 0 | // spanStart is the start of the current span, first of the remaining ones. |
678 | 0 | spanLength = findSource ? oldLength_ : newLength_; |
679 | 0 | int32_t u = array[index]; |
680 | 0 | U_ASSERT(MAX_UNCHANGED < u && u <= MAX_SHORT_CHANGE); |
681 | 0 | int32_t num = (u & SHORT_CHANGE_NUM_MASK) + 1 - remaining; |
682 | 0 | int32_t len = num * spanLength; |
683 | 0 | if (i >= (spanStart - len)) { |
684 | 0 | int32_t n = ((spanStart - i - 1) / spanLength) + 1; |
685 | 0 | // 1 <= n <= num |
686 | 0 | srcIndex -= n * oldLength_; |
687 | 0 | replIndex -= n * newLength_; |
688 | 0 | destIndex -= n * newLength_; |
689 | 0 | remaining += n; |
690 | 0 | return 0; |
691 | 0 | } |
692 | 0 | // Skip all of these edits at once. |
693 | 0 | srcIndex -= num * oldLength_; |
694 | 0 | replIndex -= num * newLength_; |
695 | 0 | destIndex -= num * newLength_; |
696 | 0 | remaining = 0; |
697 | 0 | } |
698 | 0 | } |
699 | 0 | } |
700 | 0 | // Reset the iterator to the start. |
701 | 0 | dir = 0; |
702 | 0 | index = remaining = oldLength_ = newLength_ = srcIndex = replIndex = destIndex = 0; |
703 | 0 | } else if (i < (spanStart + spanLength)) { |
704 | 0 | // The index is in the current span. |
705 | 0 | return 0; |
706 | 0 | } |
707 | 0 | while (next(FALSE, errorCode)) { |
708 | 0 | if (findSource) { |
709 | 0 | spanStart = srcIndex; |
710 | 0 | spanLength = oldLength_; |
711 | 0 | } else { |
712 | 0 | spanStart = destIndex; |
713 | 0 | spanLength = newLength_; |
714 | 0 | } |
715 | 0 | if (i < (spanStart + spanLength)) { |
716 | 0 | // The index is in the current span. |
717 | 0 | return 0; |
718 | 0 | } |
719 | 0 | if (remaining > 1) { |
720 | 0 | // Is the index in one of the remaining compressed edits? |
721 | 0 | // spanStart is the start of the current span, first of the remaining ones. |
722 | 0 | int32_t len = remaining * spanLength; |
723 | 0 | if (i < (spanStart + len)) { |
724 | 0 | int32_t n = (i - spanStart) / spanLength; // 1 <= n <= remaining - 1 |
725 | 0 | srcIndex += n * oldLength_; |
726 | 0 | replIndex += n * newLength_; |
727 | 0 | destIndex += n * newLength_; |
728 | 0 | remaining -= n; |
729 | 0 | return 0; |
730 | 0 | } |
731 | 0 | // Make next() skip all of these edits at once. |
732 | 0 | oldLength_ *= remaining; |
733 | 0 | newLength_ *= remaining; |
734 | 0 | remaining = 0; |
735 | 0 | } |
736 | 0 | } |
737 | 0 | return 1; |
738 | 0 | } |
739 | | |
740 | 0 | int32_t Edits::Iterator::destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode) { |
741 | 0 | int32_t where = findIndex(i, TRUE, errorCode); |
742 | 0 | if (where < 0) { |
743 | 0 | // Error or before the string. |
744 | 0 | return 0; |
745 | 0 | } |
746 | 0 | if (where > 0 || i == srcIndex) { |
747 | 0 | // At or after string length, or at start of the found span. |
748 | 0 | return destIndex; |
749 | 0 | } |
750 | 0 | if (changed) { |
751 | 0 | // In a change span, map to its end. |
752 | 0 | return destIndex + newLength_; |
753 | 0 | } else { |
754 | 0 | // In an unchanged span, offset 1:1 within it. |
755 | 0 | return destIndex + (i - srcIndex); |
756 | 0 | } |
757 | 0 | } |
758 | | |
759 | 0 | int32_t Edits::Iterator::sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode) { |
760 | 0 | int32_t where = findIndex(i, FALSE, errorCode); |
761 | 0 | if (where < 0) { |
762 | 0 | // Error or before the string. |
763 | 0 | return 0; |
764 | 0 | } |
765 | 0 | if (where > 0 || i == destIndex) { |
766 | 0 | // At or after string length, or at start of the found span. |
767 | 0 | return srcIndex; |
768 | 0 | } |
769 | 0 | if (changed) { |
770 | 0 | // In a change span, map to its end. |
771 | 0 | return srcIndex + oldLength_; |
772 | 0 | } else { |
773 | 0 | // In an unchanged span, offset within it. |
774 | 0 | return srcIndex + (i - destIndex); |
775 | 0 | } |
776 | 0 | } |
777 | | |
778 | 0 | UnicodeString& Edits::Iterator::toString(UnicodeString& sb) const { |
779 | 0 | sb.append(u"{ src[", -1); |
780 | 0 | ICU_Utility::appendNumber(sb, srcIndex); |
781 | 0 | sb.append(u"..", -1); |
782 | 0 | ICU_Utility::appendNumber(sb, srcIndex + oldLength_); |
783 | 0 | if (changed) { |
784 | 0 | sb.append(u"] ⇝ dest[", -1); |
785 | 0 | } else { |
786 | 0 | sb.append(u"] ≡ dest[", -1); |
787 | 0 | } |
788 | 0 | ICU_Utility::appendNumber(sb, destIndex); |
789 | 0 | sb.append(u"..", -1); |
790 | 0 | ICU_Utility::appendNumber(sb, destIndex + newLength_); |
791 | 0 | if (changed) { |
792 | 0 | sb.append(u"], repl[", -1); |
793 | 0 | ICU_Utility::appendNumber(sb, replIndex); |
794 | 0 | sb.append(u"..", -1); |
795 | 0 | ICU_Utility::appendNumber(sb, replIndex + newLength_); |
796 | 0 | sb.append(u"] }", -1); |
797 | 0 | } else { |
798 | 0 | sb.append(u"] (no-change) }", -1); |
799 | 0 | } |
800 | 0 | return sb; |
801 | 0 | } |
802 | | |
803 | | U_NAMESPACE_END |