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(int, int)} (for change edits) and
29 * {@link #addUnchanged(int)} (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 * {@link Edits::Iterator#next()} methods are called. They are identical to the non-change iterators when
67 * their {@link Edits::Iterator#findSourceIndex(int)} or {@link Edits::Iterator#findDestinationIndex(int)}
68 * methods are used to walk through the string.
70 * For examples of how to use this class, see the test <code>TestCaseMapEditsIteratorDocs</code> 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; }
177 #ifndef U_HIDE_DRAFT_API
179 * @return the number of change edits
182 int32_t numberOfChanges() const { return numChanges
; }
183 #endif // U_HIDE_DRAFT_API
186 * Access to the list of edits.
188 * At any moment in time, an instance of this class points to a single edit: a "window" into a span
189 * of the source string and the corresponding span of the destination string. The source string span
190 * starts at {@link #sourceIndex()} and runs for {@link #oldLength()} chars; the destination string
191 * span starts at {@link #destinationIndex()} and runs for {@link #newLength()} chars.
193 * The iterator can be moved between edits using the {@link #next()}, {@link #findSourceIndex(int)},
194 * and {@link #findDestinationIndex(int)} methods. Calling any of these methods mutates the iterator
195 * to make it point to the corresponding edit.
197 * For more information, see the documentation for {@link Edits}.
199 * @see getCoarseIterator
200 * @see getFineIterator
203 struct U_COMMON_API Iterator U_FINAL
: public UMemory
{
205 * Default constructor, empty iterator.
209 array(nullptr), index(0), length(0),
210 remaining(0), onlyChanges_(FALSE
), coarse(FALSE
),
211 dir(0), changed(FALSE
), oldLength_(0), newLength_(0),
212 srcIndex(0), replIndex(0), destIndex(0) {}
217 Iterator(const Iterator
&other
) = default;
219 * Assignment operator.
222 Iterator
&operator=(const Iterator
&other
) = default;
225 * Advances the iterator to the next edit.
226 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
227 * or else the function returns immediately. Check for U_FAILURE()
228 * on output or use with function chaining. (See User Guide for details.)
229 * @return TRUE if there is another edit
232 UBool
next(UErrorCode
&errorCode
) { return next(onlyChanges_
, errorCode
); }
235 * Moves the iterator to the edit that contains the source index.
236 * The source index may be found in a no-change edit
237 * even if normal iteration would skip no-change edits.
238 * Normal iteration can continue from a found edit.
240 * The iterator state before this search logically does not matter.
241 * (It may affect the performance of the search.)
243 * The iterator state after this search is undefined
244 * if the source index is out of bounds for the source string.
246 * @param i source index
247 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
248 * or else the function returns immediately. Check for U_FAILURE()
249 * on output or use with function chaining. (See User Guide for details.)
250 * @return TRUE if the edit for the source index was found
253 UBool
findSourceIndex(int32_t i
, UErrorCode
&errorCode
) {
254 return findIndex(i
, TRUE
, errorCode
) == 0;
257 #ifndef U_HIDE_DRAFT_API
259 * Moves the iterator to the edit that contains the destination index.
260 * The destination index may be found in a no-change edit
261 * even if normal iteration would skip no-change edits.
262 * Normal iteration can continue from a found edit.
264 * The iterator state before this search logically does not matter.
265 * (It may affect the performance of the search.)
267 * The iterator state after this search is undefined
268 * if the source index is out of bounds for the source string.
270 * @param i destination index
271 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
272 * or else the function returns immediately. Check for U_FAILURE()
273 * on output or use with function chaining. (See User Guide for details.)
274 * @return TRUE if the edit for the destination index was found
277 UBool
findDestinationIndex(int32_t i
, UErrorCode
&errorCode
) {
278 return findIndex(i
, FALSE
, errorCode
) == 0;
282 * Computes the destination index corresponding to the given source index.
283 * If the source index is inside a change edit (not at its start),
284 * then the destination index at the end of that edit is returned,
285 * since there is no information about index mapping inside a change edit.
287 * (This means that indexes to the start and middle of an edit,
288 * for example around a grapheme cluster, are mapped to indexes
289 * encompassing the entire edit.
290 * The alternative, mapping an interior index to the start,
291 * would map such an interval to an empty one.)
293 * This operation will usually but not always modify this object.
294 * The iterator state after this search is undefined.
296 * @param i source index
297 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
298 * or else the function returns immediately. Check for U_FAILURE()
299 * on output or use with function chaining. (See User Guide for details.)
300 * @return destination index; undefined if i is not 0..string length
303 int32_t destinationIndexFromSourceIndex(int32_t i
, UErrorCode
&errorCode
);
306 * Computes the source index corresponding to the given destination index.
307 * If the destination index is inside a change edit (not at its start),
308 * then the source index at the end of that edit is returned,
309 * since there is no information about index mapping inside a change edit.
311 * (This means that indexes to the start and middle of an edit,
312 * for example around a grapheme cluster, are mapped to indexes
313 * encompassing the entire edit.
314 * The alternative, mapping an interior index to the start,
315 * would map such an interval to an empty one.)
317 * This operation will usually but not always modify this object.
318 * The iterator state after this search is undefined.
320 * @param i destination index
321 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
322 * or else the function returns immediately. Check for U_FAILURE()
323 * on output or use with function chaining. (See User Guide for details.)
324 * @return source index; undefined if i is not 0..string length
327 int32_t sourceIndexFromDestinationIndex(int32_t i
, UErrorCode
&errorCode
);
328 #endif // U_HIDE_DRAFT_API
331 * Returns whether the edit currently represented by the iterator is a change edit.
333 * @return TRUE if this edit replaces oldLength() units with newLength() different ones.
334 * FALSE if oldLength units remain unchanged.
337 UBool
hasChange() const { return changed
; }
340 * The length of the current span in the source string, which starts at {@link #sourceIndex}.
342 * @return the number of units in the original string which are replaced or remain unchanged.
345 int32_t oldLength() const { return oldLength_
; }
348 * The length of the current span in the destination string, which starts at
349 * {@link #destinationIndex}, or in the replacement string, which starts at
350 * {@link #replacementIndex}.
352 * @return the number of units in the modified string, if hasChange() is TRUE.
353 * Same as oldLength if hasChange() is FALSE.
356 int32_t newLength() const { return newLength_
; }
359 * The start index of the current span in the source string; the span has length
360 * {@link #oldLength}.
362 * @return the current index into the source string
365 int32_t sourceIndex() const { return srcIndex
; }
368 * The start index of the current span in the replacement string; the span has length
369 * {@link #newLength}. Well-defined only if the current edit is a change edit.
371 * The <em>replacement string</em> is the concatenation of all substrings of the destination
372 * string corresponding to change edits.
374 * This method is intended to be used together with operations that write only replacement
375 * characters (e.g., {@link CaseMap#omitUnchangedText()}). The source string can then be modified
378 * @return the current index into the replacement-characters-only string,
379 * not counting unchanged spans
382 int32_t replacementIndex() const {
383 // TODO: Throw an exception if we aren't in a change edit?
388 * The start index of the current span in the destination string; the span has length
389 * {@link #newLength}.
391 * @return the current index into the full destination string
394 int32_t destinationIndex() const { return destIndex
; }
396 #ifndef U_HIDE_INTERNAL_API
398 * A string representation of the current edit represented by the iterator for debugging. You
399 * should not depend on the contents of the return string.
402 UnicodeString
& toString(UnicodeString
& appendTo
) const;
403 #endif // U_HIDE_INTERNAL_API
408 Iterator(const uint16_t *a
, int32_t len
, UBool oc
, UBool crs
);
410 int32_t readLength(int32_t head
);
411 void updateNextIndexes();
412 void updatePreviousIndexes();
414 UBool
next(UBool onlyChanges
, UErrorCode
&errorCode
);
415 UBool
previous(UErrorCode
&errorCode
);
416 /** @return -1: error or i<0; 0: found; 1: i>=string length */
417 int32_t findIndex(int32_t i
, UBool findSource
, UErrorCode
&errorCode
);
419 const uint16_t *array
;
420 int32_t index
, length
;
421 // 0 if we are not within compressed equal-length changes.
422 // Otherwise the number of remaining changes, including the current one.
424 UBool onlyChanges_
, coarse
;
426 int8_t dir
; // iteration direction: back(<0), initial(0), forward(>0)
428 int32_t oldLength_
, newLength_
;
429 int32_t srcIndex
, replIndex
, destIndex
;
433 * Returns an Iterator for coarse-grained change edits
434 * (adjacent change edits are treated as one).
435 * Can be used to perform simple string updates.
436 * Skips no-change edits.
437 * @return an Iterator that merges adjacent changes.
440 Iterator
getCoarseChangesIterator() const {
441 return Iterator(array
, length
, TRUE
, TRUE
);
445 * Returns an Iterator for coarse-grained change and no-change edits
446 * (adjacent change edits are treated as one).
447 * Can be used to perform simple string updates.
448 * Adjacent change edits are treated as one edit.
449 * @return an Iterator that merges adjacent changes.
452 Iterator
getCoarseIterator() const {
453 return Iterator(array
, length
, FALSE
, TRUE
);
457 * Returns an Iterator for fine-grained change edits
458 * (full granularity of change edits is retained).
459 * Can be used for modifying styled text.
460 * Skips no-change edits.
461 * @return an Iterator that separates adjacent changes.
464 Iterator
getFineChangesIterator() const {
465 return Iterator(array
, length
, TRUE
, FALSE
);
469 * Returns an Iterator for fine-grained change and no-change edits
470 * (full granularity of change edits is retained).
471 * Can be used for modifying styled text.
472 * @return an Iterator that separates adjacent changes.
475 Iterator
getFineIterator() const {
476 return Iterator(array
, length
, FALSE
, FALSE
);
479 #ifndef U_HIDE_DRAFT_API
481 * Merges the two input Edits and appends the result to this object.
483 * Consider two string transformations (for example, normalization and case mapping)
484 * where each records Edits in addition to writing an output string.<br>
485 * Edits ab reflect how substrings of input string a
486 * map to substrings of intermediate string b.<br>
487 * Edits bc reflect how substrings of intermediate string b
488 * map to substrings of output string c.<br>
489 * This function merges ab and bc such that the additional edits
490 * recorded in this object reflect how substrings of input string a
491 * map to substrings of output string c.
493 * If unrelated Edits are passed in where the output string of the first
494 * has a different length than the input string of the second,
495 * then a U_ILLEGAL_ARGUMENT_ERROR is reported.
497 * @param ab reflects how substrings of input string a
498 * map to substrings of intermediate string b.
499 * @param bc reflects how substrings of intermediate string b
500 * map to substrings of output string c.
501 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
502 * or else the function returns immediately. Check for U_FAILURE()
503 * on output or use with function chaining. (See User Guide for details.)
504 * @return *this, with the merged edits appended
507 Edits
&mergeAndAppend(const Edits
&ab
, const Edits
&bc
, UErrorCode
&errorCode
);
508 #endif // U_HIDE_DRAFT_API
511 void releaseArray() U_NOEXCEPT
;
512 Edits
©Array(const Edits
&other
);
513 Edits
&moveArray(Edits
&src
) U_NOEXCEPT
;
515 void setLastUnit(int32_t last
) { array
[length
- 1] = (uint16_t)last
; }
516 int32_t lastUnit() const { return length
> 0 ? array
[length
- 1] : 0xffff; }
518 void append(int32_t r
);
521 static const int32_t STACK_CAPACITY
= 100;
527 UErrorCode errorCode_
;
528 uint16_t stackArray
[STACK_CAPACITY
];
532 #endif // U_SHOW_CPLUSPLUS_API
534 #endif // __EDITS_H__