1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
5 // created: 2016dec30 Markus W. Scherer
10 #include "unicode/utypes.h"
11 #include "unicode/uobject.h"
15 * \brief C++ API: C++ class Edits for low-level string transformations on styled text.
18 #if U_SHOW_CPLUSPLUS_API
24 * Records lengths of string edits but not replacement text. Supports replacements, insertions, deletions
25 * in linear progression. Does not support moving/reordering of text.
27 * There are two types of edits: <em>change edits</em> and <em>no-change edits</em>. Add edits to
28 * instances of this class using {@link #addReplace(int32_t, int32_t)} (for change edits) and
29 * {@link #addUnchanged(int32_t)} (for no-change edits). Change edits are retained with full granularity,
30 * whereas adjacent no-change edits are always merged together. In no-change edits, there is a one-to-one
31 * mapping between code points in the source and destination strings.
33 * After all edits have been added, instances of this class should be considered immutable, and an
34 * {@link Edits::Iterator} can be used for queries.
36 * There are four flavors of Edits::Iterator:
39 * <li>{@link #getFineIterator()} retains full granularity of change edits.
40 * <li>{@link #getFineChangesIterator()} retains full granularity of change edits, and when calling
41 * next() on the iterator, skips over no-change edits (unchanged regions).
42 * <li>{@link #getCoarseIterator()} treats adjacent change edits as a single edit. (Adjacent no-change
43 * edits are automatically merged during the construction phase.)
44 * <li>{@link #getCoarseChangesIterator()} treats adjacent change edits as a single edit, and when
45 * calling next() on the iterator, skips over no-change edits (unchanged regions).
48 * For example, consider the string "abcßDeF", which case-folds to "abcssdef". This string has the
49 * following fine edits:
51 * <li>abc ⇨ abc (no-change)
54 * <li>e ⇨ e (no-change)
57 * and the following coarse edits (note how adjacent change edits get merged together):
59 * <li>abc ⇨ abc (no-change)
60 * <li>ßD ⇨ ssd (change)
61 * <li>e ⇨ e (no-change)
65 * The "fine changes" and "coarse changes" iterators will step through only the change edits when their
66 * `Edits::Iterator::next()` methods are called. They are identical to the non-change iterators when
67 * their `Edits::Iterator::findSourceIndex()` or `Edits::Iterator::findDestinationIndex()`
68 * methods are used to walk through the string.
70 * For examples of how to use this class, see the test `TestCaseMapEditsIteratorDocs` in
71 * UCharacterCaseTest.java.
73 * An Edits object tracks a separate UErrorCode, but ICU string transformation functions
74 * (e.g., case mapping functions) merge any such errors into their API's UErrorCode.
78 class U_COMMON_API Edits U_FINAL
: public UMemory
{
81 * Constructs an empty object.
85 array(stackArray
), capacity(STACK_CAPACITY
), length(0), delta(0), numChanges(0),
86 errorCode_(U_ZERO_ERROR
) {}
89 * @param other source edits
92 Edits(const Edits
&other
) :
93 array(stackArray
), capacity(STACK_CAPACITY
), length(other
.length
),
94 delta(other
.delta
), numChanges(other
.numChanges
),
95 errorCode_(other
.errorCode_
) {
99 * Move constructor, might leave src empty.
100 * This object will have the same contents that the source object had.
101 * @param src source edits
104 Edits(Edits
&&src
) U_NOEXCEPT
:
105 array(stackArray
), capacity(STACK_CAPACITY
), length(src
.length
),
106 delta(src
.delta
), numChanges(src
.numChanges
),
107 errorCode_(src
.errorCode_
) {
118 * Assignment operator.
119 * @param other source edits
123 Edits
&operator=(const Edits
&other
);
126 * Move assignment operator, might leave src empty.
127 * This object will have the same contents that the source object had.
128 * The behavior is undefined if *this and src are the same object.
129 * @param src source edits
133 Edits
&operator=(Edits
&&src
) U_NOEXCEPT
;
136 * Resets the data but may not release memory.
139 void reset() U_NOEXCEPT
;
142 * Adds a no-change edit: a record for an unchanged segment of text.
143 * Normally called from inside ICU string transformation functions, not user code.
146 void addUnchanged(int32_t unchangedLength
);
148 * Adds a change edit: a record for a text replacement/insertion/deletion.
149 * Normally called from inside ICU string transformation functions, not user code.
152 void addReplace(int32_t oldLength
, int32_t newLength
);
154 * Sets the UErrorCode if an error occurred while recording edits.
155 * Preserves older error codes in the outErrorCode.
156 * Normally called from inside ICU string transformation functions, not user code.
157 * @param outErrorCode Set to an error code if it does not contain one already
158 * and an error occurred while recording edits.
159 * Otherwise unchanged.
160 * @return TRUE if U_FAILURE(outErrorCode)
163 UBool
copyErrorTo(UErrorCode
&outErrorCode
);
166 * How much longer is the new text compared with the old text?
167 * @return new length minus old length
170 int32_t lengthDelta() const { return delta
; }
172 * @return TRUE if there are any change edits
175 UBool
hasChanges() const { return numChanges
!= 0; }
178 * @return the number of change edits
181 int32_t numberOfChanges() const { return numChanges
; }
184 * Access to the list of edits.
186 * At any moment in time, an instance of this class points to a single edit: a "window" into a span
187 * of the source string and the corresponding span of the destination string. The source string span
188 * starts at {@link #sourceIndex()} and runs for {@link #oldLength()} chars; the destination string
189 * span starts at {@link #destinationIndex()} and runs for {@link #newLength()} chars.
191 * The iterator can be moved between edits using the `next()`, `findSourceIndex(int32_t, UErrorCode &)`,
192 * and `findDestinationIndex(int32_t, UErrorCode &)` methods.
193 * Calling any of these methods mutates the iterator to make it point to the corresponding edit.
195 * For more information, see the documentation for {@link Edits}.
197 * @see getCoarseIterator
198 * @see getFineIterator
201 struct U_COMMON_API Iterator U_FINAL
: public UMemory
{
203 * Default constructor, empty iterator.
207 array(nullptr), index(0), length(0),
208 remaining(0), onlyChanges_(FALSE
), coarse(FALSE
),
209 dir(0), changed(FALSE
), oldLength_(0), newLength_(0),
210 srcIndex(0), replIndex(0), destIndex(0) {}
215 Iterator(const Iterator
&other
) = default;
217 * Assignment operator.
220 Iterator
&operator=(const Iterator
&other
) = default;
223 * Advances the iterator to the next edit.
224 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
225 * or else the function returns immediately. Check for U_FAILURE()
226 * on output or use with function chaining. (See User Guide for details.)
227 * @return TRUE if there is another edit
230 UBool
next(UErrorCode
&errorCode
) { return next(onlyChanges_
, errorCode
); }
233 * Moves the iterator to the edit that contains the source index.
234 * The source index may be found in a no-change edit
235 * even if normal iteration would skip no-change edits.
236 * Normal iteration can continue from a found edit.
238 * The iterator state before this search logically does not matter.
239 * (It may affect the performance of the search.)
241 * The iterator state after this search is undefined
242 * if the source index is out of bounds for the source string.
244 * @param i source index
245 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
246 * or else the function returns immediately. Check for U_FAILURE()
247 * on output or use with function chaining. (See User Guide for details.)
248 * @return TRUE if the edit for the source index was found
251 UBool
findSourceIndex(int32_t i
, UErrorCode
&errorCode
) {
252 return findIndex(i
, TRUE
, errorCode
) == 0;
256 * Moves the iterator to the edit that contains the destination index.
257 * The destination index may be found in a no-change edit
258 * even if normal iteration would skip no-change edits.
259 * Normal iteration can continue from a found edit.
261 * The iterator state before this search logically does not matter.
262 * (It may affect the performance of the search.)
264 * The iterator state after this search is undefined
265 * if the source index is out of bounds for the source string.
267 * @param i destination index
268 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
269 * or else the function returns immediately. Check for U_FAILURE()
270 * on output or use with function chaining. (See User Guide for details.)
271 * @return TRUE if the edit for the destination index was found
274 UBool
findDestinationIndex(int32_t i
, UErrorCode
&errorCode
) {
275 return findIndex(i
, FALSE
, errorCode
) == 0;
279 * Computes the destination index corresponding to the given source index.
280 * If the source index is inside a change edit (not at its start),
281 * then the destination index at the end of that edit is returned,
282 * since there is no information about index mapping inside a change edit.
284 * (This means that indexes to the start and middle of an edit,
285 * for example around a grapheme cluster, are mapped to indexes
286 * encompassing the entire edit.
287 * The alternative, mapping an interior index to the start,
288 * would map such an interval to an empty one.)
290 * This operation will usually but not always modify this object.
291 * The iterator state after this search is undefined.
293 * @param i source index
294 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
295 * or else the function returns immediately. Check for U_FAILURE()
296 * on output or use with function chaining. (See User Guide for details.)
297 * @return destination index; undefined if i is not 0..string length
300 int32_t destinationIndexFromSourceIndex(int32_t i
, UErrorCode
&errorCode
);
303 * Computes the source index corresponding to the given destination index.
304 * If the destination index is inside a change edit (not at its start),
305 * then the source index at the end of that edit is returned,
306 * since there is no information about index mapping inside a change edit.
308 * (This means that indexes to the start and middle of an edit,
309 * for example around a grapheme cluster, are mapped to indexes
310 * encompassing the entire edit.
311 * The alternative, mapping an interior index to the start,
312 * would map such an interval to an empty one.)
314 * This operation will usually but not always modify this object.
315 * The iterator state after this search is undefined.
317 * @param i destination index
318 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
319 * or else the function returns immediately. Check for U_FAILURE()
320 * on output or use with function chaining. (See User Guide for details.)
321 * @return source index; undefined if i is not 0..string length
324 int32_t sourceIndexFromDestinationIndex(int32_t i
, UErrorCode
&errorCode
);
327 * Returns whether the edit currently represented by the iterator is a change edit.
329 * @return TRUE if this edit replaces oldLength() units with newLength() different ones.
330 * FALSE if oldLength units remain unchanged.
333 UBool
hasChange() const { return changed
; }
336 * The length of the current span in the source string, which starts at {@link #sourceIndex}.
338 * @return the number of units in the original string which are replaced or remain unchanged.
341 int32_t oldLength() const { return oldLength_
; }
344 * The length of the current span in the destination string, which starts at
345 * {@link #destinationIndex}, or in the replacement string, which starts at
346 * {@link #replacementIndex}.
348 * @return the number of units in the modified string, if hasChange() is TRUE.
349 * Same as oldLength if hasChange() is FALSE.
352 int32_t newLength() const { return newLength_
; }
355 * The start index of the current span in the source string; the span has length
356 * {@link #oldLength}.
358 * @return the current index into the source string
361 int32_t sourceIndex() const { return srcIndex
; }
364 * The start index of the current span in the replacement string; the span has length
365 * {@link #newLength}. Well-defined only if the current edit is a change edit.
367 * The *replacement string* is the concatenation of all substrings of the destination
368 * string corresponding to change edits.
370 * This method is intended to be used together with operations that write only replacement
371 * characters (e.g. operations specifying the \ref U_OMIT_UNCHANGED_TEXT option).
372 * The source string can then be modified in-place.
374 * @return the current index into the replacement-characters-only string,
375 * not counting unchanged spans
378 int32_t replacementIndex() const {
379 // TODO: Throw an exception if we aren't in a change edit?
384 * The start index of the current span in the destination string; the span has length
385 * {@link #newLength}.
387 * @return the current index into the full destination string
390 int32_t destinationIndex() const { return destIndex
; }
392 #ifndef U_HIDE_INTERNAL_API
394 * A string representation of the current edit represented by the iterator for debugging. You
395 * should not depend on the contents of the return string.
398 UnicodeString
& toString(UnicodeString
& appendTo
) const;
399 #endif // U_HIDE_INTERNAL_API
404 Iterator(const uint16_t *a
, int32_t len
, UBool oc
, UBool crs
);
406 int32_t readLength(int32_t head
);
407 void updateNextIndexes();
408 void updatePreviousIndexes();
410 UBool
next(UBool onlyChanges
, UErrorCode
&errorCode
);
411 UBool
previous(UErrorCode
&errorCode
);
412 /** @return -1: error or i<0; 0: found; 1: i>=string length */
413 int32_t findIndex(int32_t i
, UBool findSource
, UErrorCode
&errorCode
);
415 const uint16_t *array
;
416 int32_t index
, length
;
417 // 0 if we are not within compressed equal-length changes.
418 // Otherwise the number of remaining changes, including the current one.
420 UBool onlyChanges_
, coarse
;
422 int8_t dir
; // iteration direction: back(<0), initial(0), forward(>0)
424 int32_t oldLength_
, newLength_
;
425 int32_t srcIndex
, replIndex
, destIndex
;
429 * Returns an Iterator for coarse-grained change edits
430 * (adjacent change edits are treated as one).
431 * Can be used to perform simple string updates.
432 * Skips no-change edits.
433 * @return an Iterator that merges adjacent changes.
436 Iterator
getCoarseChangesIterator() const {
437 return Iterator(array
, length
, TRUE
, TRUE
);
441 * Returns an Iterator for coarse-grained change and no-change edits
442 * (adjacent change edits are treated as one).
443 * Can be used to perform simple string updates.
444 * Adjacent change edits are treated as one edit.
445 * @return an Iterator that merges adjacent changes.
448 Iterator
getCoarseIterator() const {
449 return Iterator(array
, length
, FALSE
, TRUE
);
453 * Returns an Iterator for fine-grained change edits
454 * (full granularity of change edits is retained).
455 * Can be used for modifying styled text.
456 * Skips no-change edits.
457 * @return an Iterator that separates adjacent changes.
460 Iterator
getFineChangesIterator() const {
461 return Iterator(array
, length
, TRUE
, FALSE
);
465 * Returns an Iterator for fine-grained change and no-change edits
466 * (full granularity of change edits is retained).
467 * Can be used for modifying styled text.
468 * @return an Iterator that separates adjacent changes.
471 Iterator
getFineIterator() const {
472 return Iterator(array
, length
, FALSE
, FALSE
);
476 * Merges the two input Edits and appends the result to this object.
478 * Consider two string transformations (for example, normalization and case mapping)
479 * where each records Edits in addition to writing an output string.<br>
480 * Edits ab reflect how substrings of input string a
481 * map to substrings of intermediate string b.<br>
482 * Edits bc reflect how substrings of intermediate string b
483 * map to substrings of output string c.<br>
484 * This function merges ab and bc such that the additional edits
485 * recorded in this object reflect how substrings of input string a
486 * map to substrings of output string c.
488 * If unrelated Edits are passed in where the output string of the first
489 * has a different length than the input string of the second,
490 * then a U_ILLEGAL_ARGUMENT_ERROR is reported.
492 * @param ab reflects how substrings of input string a
493 * map to substrings of intermediate string b.
494 * @param bc reflects how substrings of intermediate string b
495 * map to substrings of output string c.
496 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
497 * or else the function returns immediately. Check for U_FAILURE()
498 * on output or use with function chaining. (See User Guide for details.)
499 * @return *this, with the merged edits appended
502 Edits
&mergeAndAppend(const Edits
&ab
, const Edits
&bc
, UErrorCode
&errorCode
);
505 void releaseArray() U_NOEXCEPT
;
506 Edits
©Array(const Edits
&other
);
507 Edits
&moveArray(Edits
&src
) U_NOEXCEPT
;
509 void setLastUnit(int32_t last
) { array
[length
- 1] = (uint16_t)last
; }
510 int32_t lastUnit() const { return length
> 0 ? array
[length
- 1] : 0xffff; }
512 void append(int32_t r
);
515 static const int32_t STACK_CAPACITY
= 100;
521 UErrorCode errorCode_
;
522 uint16_t stackArray
[STACK_CAPACITY
];
526 #endif // U_SHOW_CPLUSPLUS_API
528 #endif // __EDITS_H__