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