jpayne@69
|
1 // © 2016 and later: Unicode, Inc. and others.
|
jpayne@69
|
2 // License & terms of use: http://www.unicode.org/copyright.html
|
jpayne@69
|
3
|
jpayne@69
|
4 // edits.h
|
jpayne@69
|
5 // created: 2016dec30 Markus W. Scherer
|
jpayne@69
|
6
|
jpayne@69
|
7 #ifndef __EDITS_H__
|
jpayne@69
|
8 #define __EDITS_H__
|
jpayne@69
|
9
|
jpayne@69
|
10 #include "unicode/utypes.h"
|
jpayne@69
|
11
|
jpayne@69
|
12 #if U_SHOW_CPLUSPLUS_API
|
jpayne@69
|
13
|
jpayne@69
|
14 #include "unicode/uobject.h"
|
jpayne@69
|
15
|
jpayne@69
|
16 /**
|
jpayne@69
|
17 * \file
|
jpayne@69
|
18 * \brief C++ API: C++ class Edits for low-level string transformations on styled text.
|
jpayne@69
|
19 */
|
jpayne@69
|
20
|
jpayne@69
|
21 U_NAMESPACE_BEGIN
|
jpayne@69
|
22
|
jpayne@69
|
23 class UnicodeString;
|
jpayne@69
|
24
|
jpayne@69
|
25 /**
|
jpayne@69
|
26 * Records lengths of string edits but not replacement text. Supports replacements, insertions, deletions
|
jpayne@69
|
27 * in linear progression. Does not support moving/reordering of text.
|
jpayne@69
|
28 *
|
jpayne@69
|
29 * There are two types of edits: <em>change edits</em> and <em>no-change edits</em>. Add edits to
|
jpayne@69
|
30 * instances of this class using {@link #addReplace(int32_t, int32_t)} (for change edits) and
|
jpayne@69
|
31 * {@link #addUnchanged(int32_t)} (for no-change edits). Change edits are retained with full granularity,
|
jpayne@69
|
32 * whereas adjacent no-change edits are always merged together. In no-change edits, there is a one-to-one
|
jpayne@69
|
33 * mapping between code points in the source and destination strings.
|
jpayne@69
|
34 *
|
jpayne@69
|
35 * After all edits have been added, instances of this class should be considered immutable, and an
|
jpayne@69
|
36 * {@link Edits::Iterator} can be used for queries.
|
jpayne@69
|
37 *
|
jpayne@69
|
38 * There are four flavors of Edits::Iterator:
|
jpayne@69
|
39 *
|
jpayne@69
|
40 * <ul>
|
jpayne@69
|
41 * <li>{@link #getFineIterator()} retains full granularity of change edits.
|
jpayne@69
|
42 * <li>{@link #getFineChangesIterator()} retains full granularity of change edits, and when calling
|
jpayne@69
|
43 * next() on the iterator, skips over no-change edits (unchanged regions).
|
jpayne@69
|
44 * <li>{@link #getCoarseIterator()} treats adjacent change edits as a single edit. (Adjacent no-change
|
jpayne@69
|
45 * edits are automatically merged during the construction phase.)
|
jpayne@69
|
46 * <li>{@link #getCoarseChangesIterator()} treats adjacent change edits as a single edit, and when
|
jpayne@69
|
47 * calling next() on the iterator, skips over no-change edits (unchanged regions).
|
jpayne@69
|
48 * </ul>
|
jpayne@69
|
49 *
|
jpayne@69
|
50 * For example, consider the string "abcßDeF", which case-folds to "abcssdef". This string has the
|
jpayne@69
|
51 * following fine edits:
|
jpayne@69
|
52 * <ul>
|
jpayne@69
|
53 * <li>abc ⇨ abc (no-change)
|
jpayne@69
|
54 * <li>ß ⇨ ss (change)
|
jpayne@69
|
55 * <li>D ⇨ d (change)
|
jpayne@69
|
56 * <li>e ⇨ e (no-change)
|
jpayne@69
|
57 * <li>F ⇨ f (change)
|
jpayne@69
|
58 * </ul>
|
jpayne@69
|
59 * and the following coarse edits (note how adjacent change edits get merged together):
|
jpayne@69
|
60 * <ul>
|
jpayne@69
|
61 * <li>abc ⇨ abc (no-change)
|
jpayne@69
|
62 * <li>ßD ⇨ ssd (change)
|
jpayne@69
|
63 * <li>e ⇨ e (no-change)
|
jpayne@69
|
64 * <li>F ⇨ f (change)
|
jpayne@69
|
65 * </ul>
|
jpayne@69
|
66 *
|
jpayne@69
|
67 * The "fine changes" and "coarse changes" iterators will step through only the change edits when their
|
jpayne@69
|
68 * `Edits::Iterator::next()` methods are called. They are identical to the non-change iterators when
|
jpayne@69
|
69 * their `Edits::Iterator::findSourceIndex()` or `Edits::Iterator::findDestinationIndex()`
|
jpayne@69
|
70 * methods are used to walk through the string.
|
jpayne@69
|
71 *
|
jpayne@69
|
72 * For examples of how to use this class, see the test `TestCaseMapEditsIteratorDocs` in
|
jpayne@69
|
73 * UCharacterCaseTest.java.
|
jpayne@69
|
74 *
|
jpayne@69
|
75 * An Edits object tracks a separate UErrorCode, but ICU string transformation functions
|
jpayne@69
|
76 * (e.g., case mapping functions) merge any such errors into their API's UErrorCode.
|
jpayne@69
|
77 *
|
jpayne@69
|
78 * @stable ICU 59
|
jpayne@69
|
79 */
|
jpayne@69
|
80 class U_COMMON_API Edits U_FINAL : public UMemory {
|
jpayne@69
|
81 public:
|
jpayne@69
|
82 /**
|
jpayne@69
|
83 * Constructs an empty object.
|
jpayne@69
|
84 * @stable ICU 59
|
jpayne@69
|
85 */
|
jpayne@69
|
86 Edits() :
|
jpayne@69
|
87 array(stackArray), capacity(STACK_CAPACITY), length(0), delta(0), numChanges(0),
|
jpayne@69
|
88 errorCode_(U_ZERO_ERROR) {}
|
jpayne@69
|
89 /**
|
jpayne@69
|
90 * Copy constructor.
|
jpayne@69
|
91 * @param other source edits
|
jpayne@69
|
92 * @stable ICU 60
|
jpayne@69
|
93 */
|
jpayne@69
|
94 Edits(const Edits &other) :
|
jpayne@69
|
95 array(stackArray), capacity(STACK_CAPACITY), length(other.length),
|
jpayne@69
|
96 delta(other.delta), numChanges(other.numChanges),
|
jpayne@69
|
97 errorCode_(other.errorCode_) {
|
jpayne@69
|
98 copyArray(other);
|
jpayne@69
|
99 }
|
jpayne@69
|
100 /**
|
jpayne@69
|
101 * Move constructor, might leave src empty.
|
jpayne@69
|
102 * This object will have the same contents that the source object had.
|
jpayne@69
|
103 * @param src source edits
|
jpayne@69
|
104 * @stable ICU 60
|
jpayne@69
|
105 */
|
jpayne@69
|
106 Edits(Edits &&src) U_NOEXCEPT :
|
jpayne@69
|
107 array(stackArray), capacity(STACK_CAPACITY), length(src.length),
|
jpayne@69
|
108 delta(src.delta), numChanges(src.numChanges),
|
jpayne@69
|
109 errorCode_(src.errorCode_) {
|
jpayne@69
|
110 moveArray(src);
|
jpayne@69
|
111 }
|
jpayne@69
|
112
|
jpayne@69
|
113 /**
|
jpayne@69
|
114 * Destructor.
|
jpayne@69
|
115 * @stable ICU 59
|
jpayne@69
|
116 */
|
jpayne@69
|
117 ~Edits();
|
jpayne@69
|
118
|
jpayne@69
|
119 /**
|
jpayne@69
|
120 * Assignment operator.
|
jpayne@69
|
121 * @param other source edits
|
jpayne@69
|
122 * @return *this
|
jpayne@69
|
123 * @stable ICU 60
|
jpayne@69
|
124 */
|
jpayne@69
|
125 Edits &operator=(const Edits &other);
|
jpayne@69
|
126
|
jpayne@69
|
127 /**
|
jpayne@69
|
128 * Move assignment operator, might leave src empty.
|
jpayne@69
|
129 * This object will have the same contents that the source object had.
|
jpayne@69
|
130 * The behavior is undefined if *this and src are the same object.
|
jpayne@69
|
131 * @param src source edits
|
jpayne@69
|
132 * @return *this
|
jpayne@69
|
133 * @stable ICU 60
|
jpayne@69
|
134 */
|
jpayne@69
|
135 Edits &operator=(Edits &&src) U_NOEXCEPT;
|
jpayne@69
|
136
|
jpayne@69
|
137 /**
|
jpayne@69
|
138 * Resets the data but may not release memory.
|
jpayne@69
|
139 * @stable ICU 59
|
jpayne@69
|
140 */
|
jpayne@69
|
141 void reset() U_NOEXCEPT;
|
jpayne@69
|
142
|
jpayne@69
|
143 /**
|
jpayne@69
|
144 * Adds a no-change edit: a record for an unchanged segment of text.
|
jpayne@69
|
145 * Normally called from inside ICU string transformation functions, not user code.
|
jpayne@69
|
146 * @stable ICU 59
|
jpayne@69
|
147 */
|
jpayne@69
|
148 void addUnchanged(int32_t unchangedLength);
|
jpayne@69
|
149 /**
|
jpayne@69
|
150 * Adds a change edit: a record for a text replacement/insertion/deletion.
|
jpayne@69
|
151 * Normally called from inside ICU string transformation functions, not user code.
|
jpayne@69
|
152 * @stable ICU 59
|
jpayne@69
|
153 */
|
jpayne@69
|
154 void addReplace(int32_t oldLength, int32_t newLength);
|
jpayne@69
|
155 /**
|
jpayne@69
|
156 * Sets the UErrorCode if an error occurred while recording edits.
|
jpayne@69
|
157 * Preserves older error codes in the outErrorCode.
|
jpayne@69
|
158 * Normally called from inside ICU string transformation functions, not user code.
|
jpayne@69
|
159 * @param outErrorCode Set to an error code if it does not contain one already
|
jpayne@69
|
160 * and an error occurred while recording edits.
|
jpayne@69
|
161 * Otherwise unchanged.
|
jpayne@69
|
162 * @return TRUE if U_FAILURE(outErrorCode)
|
jpayne@69
|
163 * @stable ICU 59
|
jpayne@69
|
164 */
|
jpayne@69
|
165 UBool copyErrorTo(UErrorCode &outErrorCode) const;
|
jpayne@69
|
166
|
jpayne@69
|
167 /**
|
jpayne@69
|
168 * How much longer is the new text compared with the old text?
|
jpayne@69
|
169 * @return new length minus old length
|
jpayne@69
|
170 * @stable ICU 59
|
jpayne@69
|
171 */
|
jpayne@69
|
172 int32_t lengthDelta() const { return delta; }
|
jpayne@69
|
173 /**
|
jpayne@69
|
174 * @return TRUE if there are any change edits
|
jpayne@69
|
175 * @stable ICU 59
|
jpayne@69
|
176 */
|
jpayne@69
|
177 UBool hasChanges() const { return numChanges != 0; }
|
jpayne@69
|
178
|
jpayne@69
|
179 /**
|
jpayne@69
|
180 * @return the number of change edits
|
jpayne@69
|
181 * @stable ICU 60
|
jpayne@69
|
182 */
|
jpayne@69
|
183 int32_t numberOfChanges() const { return numChanges; }
|
jpayne@69
|
184
|
jpayne@69
|
185 /**
|
jpayne@69
|
186 * Access to the list of edits.
|
jpayne@69
|
187 *
|
jpayne@69
|
188 * At any moment in time, an instance of this class points to a single edit: a "window" into a span
|
jpayne@69
|
189 * of the source string and the corresponding span of the destination string. The source string span
|
jpayne@69
|
190 * starts at {@link #sourceIndex()} and runs for {@link #oldLength()} chars; the destination string
|
jpayne@69
|
191 * span starts at {@link #destinationIndex()} and runs for {@link #newLength()} chars.
|
jpayne@69
|
192 *
|
jpayne@69
|
193 * The iterator can be moved between edits using the `next()`, `findSourceIndex(int32_t, UErrorCode &)`,
|
jpayne@69
|
194 * and `findDestinationIndex(int32_t, UErrorCode &)` methods.
|
jpayne@69
|
195 * Calling any of these methods mutates the iterator to make it point to the corresponding edit.
|
jpayne@69
|
196 *
|
jpayne@69
|
197 * For more information, see the documentation for {@link Edits}.
|
jpayne@69
|
198 *
|
jpayne@69
|
199 * @see getCoarseIterator
|
jpayne@69
|
200 * @see getFineIterator
|
jpayne@69
|
201 * @stable ICU 59
|
jpayne@69
|
202 */
|
jpayne@69
|
203 struct U_COMMON_API Iterator U_FINAL : public UMemory {
|
jpayne@69
|
204 /**
|
jpayne@69
|
205 * Default constructor, empty iterator.
|
jpayne@69
|
206 * @stable ICU 60
|
jpayne@69
|
207 */
|
jpayne@69
|
208 Iterator() :
|
jpayne@69
|
209 array(nullptr), index(0), length(0),
|
jpayne@69
|
210 remaining(0), onlyChanges_(FALSE), coarse(FALSE),
|
jpayne@69
|
211 dir(0), changed(FALSE), oldLength_(0), newLength_(0),
|
jpayne@69
|
212 srcIndex(0), replIndex(0), destIndex(0) {}
|
jpayne@69
|
213 /**
|
jpayne@69
|
214 * Copy constructor.
|
jpayne@69
|
215 * @stable ICU 59
|
jpayne@69
|
216 */
|
jpayne@69
|
217 Iterator(const Iterator &other) = default;
|
jpayne@69
|
218 /**
|
jpayne@69
|
219 * Assignment operator.
|
jpayne@69
|
220 * @stable ICU 59
|
jpayne@69
|
221 */
|
jpayne@69
|
222 Iterator &operator=(const Iterator &other) = default;
|
jpayne@69
|
223
|
jpayne@69
|
224 /**
|
jpayne@69
|
225 * Advances the iterator to the next edit.
|
jpayne@69
|
226 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
|
jpayne@69
|
227 * or else the function returns immediately. Check for U_FAILURE()
|
jpayne@69
|
228 * on output or use with function chaining. (See User Guide for details.)
|
jpayne@69
|
229 * @return TRUE if there is another edit
|
jpayne@69
|
230 * @stable ICU 59
|
jpayne@69
|
231 */
|
jpayne@69
|
232 UBool next(UErrorCode &errorCode) { return next(onlyChanges_, errorCode); }
|
jpayne@69
|
233
|
jpayne@69
|
234 /**
|
jpayne@69
|
235 * Moves the iterator to the edit that contains the source index.
|
jpayne@69
|
236 * The source index may be found in a no-change edit
|
jpayne@69
|
237 * even if normal iteration would skip no-change edits.
|
jpayne@69
|
238 * Normal iteration can continue from a found edit.
|
jpayne@69
|
239 *
|
jpayne@69
|
240 * The iterator state before this search logically does not matter.
|
jpayne@69
|
241 * (It may affect the performance of the search.)
|
jpayne@69
|
242 *
|
jpayne@69
|
243 * The iterator state after this search is undefined
|
jpayne@69
|
244 * if the source index is out of bounds for the source string.
|
jpayne@69
|
245 *
|
jpayne@69
|
246 * @param i source index
|
jpayne@69
|
247 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
|
jpayne@69
|
248 * or else the function returns immediately. Check for U_FAILURE()
|
jpayne@69
|
249 * on output or use with function chaining. (See User Guide for details.)
|
jpayne@69
|
250 * @return TRUE if the edit for the source index was found
|
jpayne@69
|
251 * @stable ICU 59
|
jpayne@69
|
252 */
|
jpayne@69
|
253 UBool findSourceIndex(int32_t i, UErrorCode &errorCode) {
|
jpayne@69
|
254 return findIndex(i, TRUE, errorCode) == 0;
|
jpayne@69
|
255 }
|
jpayne@69
|
256
|
jpayne@69
|
257 /**
|
jpayne@69
|
258 * Moves the iterator to the edit that contains the destination index.
|
jpayne@69
|
259 * The destination index may be found in a no-change edit
|
jpayne@69
|
260 * even if normal iteration would skip no-change edits.
|
jpayne@69
|
261 * Normal iteration can continue from a found edit.
|
jpayne@69
|
262 *
|
jpayne@69
|
263 * The iterator state before this search logically does not matter.
|
jpayne@69
|
264 * (It may affect the performance of the search.)
|
jpayne@69
|
265 *
|
jpayne@69
|
266 * The iterator state after this search is undefined
|
jpayne@69
|
267 * if the source index is out of bounds for the source string.
|
jpayne@69
|
268 *
|
jpayne@69
|
269 * @param i destination index
|
jpayne@69
|
270 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
|
jpayne@69
|
271 * or else the function returns immediately. Check for U_FAILURE()
|
jpayne@69
|
272 * on output or use with function chaining. (See User Guide for details.)
|
jpayne@69
|
273 * @return TRUE if the edit for the destination index was found
|
jpayne@69
|
274 * @stable ICU 60
|
jpayne@69
|
275 */
|
jpayne@69
|
276 UBool findDestinationIndex(int32_t i, UErrorCode &errorCode) {
|
jpayne@69
|
277 return findIndex(i, FALSE, errorCode) == 0;
|
jpayne@69
|
278 }
|
jpayne@69
|
279
|
jpayne@69
|
280 /**
|
jpayne@69
|
281 * Computes the destination index corresponding to the given source index.
|
jpayne@69
|
282 * If the source index is inside a change edit (not at its start),
|
jpayne@69
|
283 * then the destination index at the end of that edit is returned,
|
jpayne@69
|
284 * since there is no information about index mapping inside a change edit.
|
jpayne@69
|
285 *
|
jpayne@69
|
286 * (This means that indexes to the start and middle of an edit,
|
jpayne@69
|
287 * for example around a grapheme cluster, are mapped to indexes
|
jpayne@69
|
288 * encompassing the entire edit.
|
jpayne@69
|
289 * The alternative, mapping an interior index to the start,
|
jpayne@69
|
290 * would map such an interval to an empty one.)
|
jpayne@69
|
291 *
|
jpayne@69
|
292 * This operation will usually but not always modify this object.
|
jpayne@69
|
293 * The iterator state after this search is undefined.
|
jpayne@69
|
294 *
|
jpayne@69
|
295 * @param i source index
|
jpayne@69
|
296 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
|
jpayne@69
|
297 * or else the function returns immediately. Check for U_FAILURE()
|
jpayne@69
|
298 * on output or use with function chaining. (See User Guide for details.)
|
jpayne@69
|
299 * @return destination index; undefined if i is not 0..string length
|
jpayne@69
|
300 * @stable ICU 60
|
jpayne@69
|
301 */
|
jpayne@69
|
302 int32_t destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode);
|
jpayne@69
|
303
|
jpayne@69
|
304 /**
|
jpayne@69
|
305 * Computes the source index corresponding to the given destination index.
|
jpayne@69
|
306 * If the destination index is inside a change edit (not at its start),
|
jpayne@69
|
307 * then the source index at the end of that edit is returned,
|
jpayne@69
|
308 * since there is no information about index mapping inside a change edit.
|
jpayne@69
|
309 *
|
jpayne@69
|
310 * (This means that indexes to the start and middle of an edit,
|
jpayne@69
|
311 * for example around a grapheme cluster, are mapped to indexes
|
jpayne@69
|
312 * encompassing the entire edit.
|
jpayne@69
|
313 * The alternative, mapping an interior index to the start,
|
jpayne@69
|
314 * would map such an interval to an empty one.)
|
jpayne@69
|
315 *
|
jpayne@69
|
316 * This operation will usually but not always modify this object.
|
jpayne@69
|
317 * The iterator state after this search is undefined.
|
jpayne@69
|
318 *
|
jpayne@69
|
319 * @param i destination index
|
jpayne@69
|
320 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
|
jpayne@69
|
321 * or else the function returns immediately. Check for U_FAILURE()
|
jpayne@69
|
322 * on output or use with function chaining. (See User Guide for details.)
|
jpayne@69
|
323 * @return source index; undefined if i is not 0..string length
|
jpayne@69
|
324 * @stable ICU 60
|
jpayne@69
|
325 */
|
jpayne@69
|
326 int32_t sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode);
|
jpayne@69
|
327
|
jpayne@69
|
328 /**
|
jpayne@69
|
329 * Returns whether the edit currently represented by the iterator is a change edit.
|
jpayne@69
|
330 *
|
jpayne@69
|
331 * @return TRUE if this edit replaces oldLength() units with newLength() different ones.
|
jpayne@69
|
332 * FALSE if oldLength units remain unchanged.
|
jpayne@69
|
333 * @stable ICU 59
|
jpayne@69
|
334 */
|
jpayne@69
|
335 UBool hasChange() const { return changed; }
|
jpayne@69
|
336
|
jpayne@69
|
337 /**
|
jpayne@69
|
338 * The length of the current span in the source string, which starts at {@link #sourceIndex}.
|
jpayne@69
|
339 *
|
jpayne@69
|
340 * @return the number of units in the original string which are replaced or remain unchanged.
|
jpayne@69
|
341 * @stable ICU 59
|
jpayne@69
|
342 */
|
jpayne@69
|
343 int32_t oldLength() const { return oldLength_; }
|
jpayne@69
|
344
|
jpayne@69
|
345 /**
|
jpayne@69
|
346 * The length of the current span in the destination string, which starts at
|
jpayne@69
|
347 * {@link #destinationIndex}, or in the replacement string, which starts at
|
jpayne@69
|
348 * {@link #replacementIndex}.
|
jpayne@69
|
349 *
|
jpayne@69
|
350 * @return the number of units in the modified string, if hasChange() is TRUE.
|
jpayne@69
|
351 * Same as oldLength if hasChange() is FALSE.
|
jpayne@69
|
352 * @stable ICU 59
|
jpayne@69
|
353 */
|
jpayne@69
|
354 int32_t newLength() const { return newLength_; }
|
jpayne@69
|
355
|
jpayne@69
|
356 /**
|
jpayne@69
|
357 * The start index of the current span in the source string; the span has length
|
jpayne@69
|
358 * {@link #oldLength}.
|
jpayne@69
|
359 *
|
jpayne@69
|
360 * @return the current index into the source string
|
jpayne@69
|
361 * @stable ICU 59
|
jpayne@69
|
362 */
|
jpayne@69
|
363 int32_t sourceIndex() const { return srcIndex; }
|
jpayne@69
|
364
|
jpayne@69
|
365 /**
|
jpayne@69
|
366 * The start index of the current span in the replacement string; the span has length
|
jpayne@69
|
367 * {@link #newLength}. Well-defined only if the current edit is a change edit.
|
jpayne@69
|
368 *
|
jpayne@69
|
369 * The *replacement string* is the concatenation of all substrings of the destination
|
jpayne@69
|
370 * string corresponding to change edits.
|
jpayne@69
|
371 *
|
jpayne@69
|
372 * This method is intended to be used together with operations that write only replacement
|
jpayne@69
|
373 * characters (e.g. operations specifying the \ref U_OMIT_UNCHANGED_TEXT option).
|
jpayne@69
|
374 * The source string can then be modified in-place.
|
jpayne@69
|
375 *
|
jpayne@69
|
376 * @return the current index into the replacement-characters-only string,
|
jpayne@69
|
377 * not counting unchanged spans
|
jpayne@69
|
378 * @stable ICU 59
|
jpayne@69
|
379 */
|
jpayne@69
|
380 int32_t replacementIndex() const {
|
jpayne@69
|
381 // TODO: Throw an exception if we aren't in a change edit?
|
jpayne@69
|
382 return replIndex;
|
jpayne@69
|
383 }
|
jpayne@69
|
384
|
jpayne@69
|
385 /**
|
jpayne@69
|
386 * The start index of the current span in the destination string; the span has length
|
jpayne@69
|
387 * {@link #newLength}.
|
jpayne@69
|
388 *
|
jpayne@69
|
389 * @return the current index into the full destination string
|
jpayne@69
|
390 * @stable ICU 59
|
jpayne@69
|
391 */
|
jpayne@69
|
392 int32_t destinationIndex() const { return destIndex; }
|
jpayne@69
|
393
|
jpayne@69
|
394 #ifndef U_HIDE_INTERNAL_API
|
jpayne@69
|
395 /**
|
jpayne@69
|
396 * A string representation of the current edit represented by the iterator for debugging. You
|
jpayne@69
|
397 * should not depend on the contents of the return string.
|
jpayne@69
|
398 * @internal
|
jpayne@69
|
399 */
|
jpayne@69
|
400 UnicodeString& toString(UnicodeString& appendTo) const;
|
jpayne@69
|
401 #endif // U_HIDE_INTERNAL_API
|
jpayne@69
|
402
|
jpayne@69
|
403 private:
|
jpayne@69
|
404 friend class Edits;
|
jpayne@69
|
405
|
jpayne@69
|
406 Iterator(const uint16_t *a, int32_t len, UBool oc, UBool crs);
|
jpayne@69
|
407
|
jpayne@69
|
408 int32_t readLength(int32_t head);
|
jpayne@69
|
409 void updateNextIndexes();
|
jpayne@69
|
410 void updatePreviousIndexes();
|
jpayne@69
|
411 UBool noNext();
|
jpayne@69
|
412 UBool next(UBool onlyChanges, UErrorCode &errorCode);
|
jpayne@69
|
413 UBool previous(UErrorCode &errorCode);
|
jpayne@69
|
414 /** @return -1: error or i<0; 0: found; 1: i>=string length */
|
jpayne@69
|
415 int32_t findIndex(int32_t i, UBool findSource, UErrorCode &errorCode);
|
jpayne@69
|
416
|
jpayne@69
|
417 const uint16_t *array;
|
jpayne@69
|
418 int32_t index, length;
|
jpayne@69
|
419 // 0 if we are not within compressed equal-length changes.
|
jpayne@69
|
420 // Otherwise the number of remaining changes, including the current one.
|
jpayne@69
|
421 int32_t remaining;
|
jpayne@69
|
422 UBool onlyChanges_, coarse;
|
jpayne@69
|
423
|
jpayne@69
|
424 int8_t dir; // iteration direction: back(<0), initial(0), forward(>0)
|
jpayne@69
|
425 UBool changed;
|
jpayne@69
|
426 int32_t oldLength_, newLength_;
|
jpayne@69
|
427 int32_t srcIndex, replIndex, destIndex;
|
jpayne@69
|
428 };
|
jpayne@69
|
429
|
jpayne@69
|
430 /**
|
jpayne@69
|
431 * Returns an Iterator for coarse-grained change edits
|
jpayne@69
|
432 * (adjacent change edits are treated as one).
|
jpayne@69
|
433 * Can be used to perform simple string updates.
|
jpayne@69
|
434 * Skips no-change edits.
|
jpayne@69
|
435 * @return an Iterator that merges adjacent changes.
|
jpayne@69
|
436 * @stable ICU 59
|
jpayne@69
|
437 */
|
jpayne@69
|
438 Iterator getCoarseChangesIterator() const {
|
jpayne@69
|
439 return Iterator(array, length, TRUE, TRUE);
|
jpayne@69
|
440 }
|
jpayne@69
|
441
|
jpayne@69
|
442 /**
|
jpayne@69
|
443 * Returns an Iterator for coarse-grained change and no-change edits
|
jpayne@69
|
444 * (adjacent change edits are treated as one).
|
jpayne@69
|
445 * Can be used to perform simple string updates.
|
jpayne@69
|
446 * Adjacent change edits are treated as one edit.
|
jpayne@69
|
447 * @return an Iterator that merges adjacent changes.
|
jpayne@69
|
448 * @stable ICU 59
|
jpayne@69
|
449 */
|
jpayne@69
|
450 Iterator getCoarseIterator() const {
|
jpayne@69
|
451 return Iterator(array, length, FALSE, TRUE);
|
jpayne@69
|
452 }
|
jpayne@69
|
453
|
jpayne@69
|
454 /**
|
jpayne@69
|
455 * Returns an Iterator for fine-grained change edits
|
jpayne@69
|
456 * (full granularity of change edits is retained).
|
jpayne@69
|
457 * Can be used for modifying styled text.
|
jpayne@69
|
458 * Skips no-change edits.
|
jpayne@69
|
459 * @return an Iterator that separates adjacent changes.
|
jpayne@69
|
460 * @stable ICU 59
|
jpayne@69
|
461 */
|
jpayne@69
|
462 Iterator getFineChangesIterator() const {
|
jpayne@69
|
463 return Iterator(array, length, TRUE, FALSE);
|
jpayne@69
|
464 }
|
jpayne@69
|
465
|
jpayne@69
|
466 /**
|
jpayne@69
|
467 * Returns an Iterator for fine-grained change and no-change edits
|
jpayne@69
|
468 * (full granularity of change edits is retained).
|
jpayne@69
|
469 * Can be used for modifying styled text.
|
jpayne@69
|
470 * @return an Iterator that separates adjacent changes.
|
jpayne@69
|
471 * @stable ICU 59
|
jpayne@69
|
472 */
|
jpayne@69
|
473 Iterator getFineIterator() const {
|
jpayne@69
|
474 return Iterator(array, length, FALSE, FALSE);
|
jpayne@69
|
475 }
|
jpayne@69
|
476
|
jpayne@69
|
477 /**
|
jpayne@69
|
478 * Merges the two input Edits and appends the result to this object.
|
jpayne@69
|
479 *
|
jpayne@69
|
480 * Consider two string transformations (for example, normalization and case mapping)
|
jpayne@69
|
481 * where each records Edits in addition to writing an output string.<br>
|
jpayne@69
|
482 * Edits ab reflect how substrings of input string a
|
jpayne@69
|
483 * map to substrings of intermediate string b.<br>
|
jpayne@69
|
484 * Edits bc reflect how substrings of intermediate string b
|
jpayne@69
|
485 * map to substrings of output string c.<br>
|
jpayne@69
|
486 * This function merges ab and bc such that the additional edits
|
jpayne@69
|
487 * recorded in this object reflect how substrings of input string a
|
jpayne@69
|
488 * map to substrings of output string c.
|
jpayne@69
|
489 *
|
jpayne@69
|
490 * If unrelated Edits are passed in where the output string of the first
|
jpayne@69
|
491 * has a different length than the input string of the second,
|
jpayne@69
|
492 * then a U_ILLEGAL_ARGUMENT_ERROR is reported.
|
jpayne@69
|
493 *
|
jpayne@69
|
494 * @param ab reflects how substrings of input string a
|
jpayne@69
|
495 * map to substrings of intermediate string b.
|
jpayne@69
|
496 * @param bc reflects how substrings of intermediate string b
|
jpayne@69
|
497 * map to substrings of output string c.
|
jpayne@69
|
498 * @param errorCode ICU error code. Its input value must pass the U_SUCCESS() test,
|
jpayne@69
|
499 * or else the function returns immediately. Check for U_FAILURE()
|
jpayne@69
|
500 * on output or use with function chaining. (See User Guide for details.)
|
jpayne@69
|
501 * @return *this, with the merged edits appended
|
jpayne@69
|
502 * @stable ICU 60
|
jpayne@69
|
503 */
|
jpayne@69
|
504 Edits &mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode);
|
jpayne@69
|
505
|
jpayne@69
|
506 private:
|
jpayne@69
|
507 void releaseArray() U_NOEXCEPT;
|
jpayne@69
|
508 Edits ©Array(const Edits &other);
|
jpayne@69
|
509 Edits &moveArray(Edits &src) U_NOEXCEPT;
|
jpayne@69
|
510
|
jpayne@69
|
511 void setLastUnit(int32_t last) { array[length - 1] = (uint16_t)last; }
|
jpayne@69
|
512 int32_t lastUnit() const { return length > 0 ? array[length - 1] : 0xffff; }
|
jpayne@69
|
513
|
jpayne@69
|
514 void append(int32_t r);
|
jpayne@69
|
515 UBool growArray();
|
jpayne@69
|
516
|
jpayne@69
|
517 static const int32_t STACK_CAPACITY = 100;
|
jpayne@69
|
518 uint16_t *array;
|
jpayne@69
|
519 int32_t capacity;
|
jpayne@69
|
520 int32_t length;
|
jpayne@69
|
521 int32_t delta;
|
jpayne@69
|
522 int32_t numChanges;
|
jpayne@69
|
523 UErrorCode errorCode_;
|
jpayne@69
|
524 uint16_t stackArray[STACK_CAPACITY];
|
jpayne@69
|
525 };
|
jpayne@69
|
526
|
jpayne@69
|
527 U_NAMESPACE_END
|
jpayne@69
|
528
|
jpayne@69
|
529 #endif /* U_SHOW_CPLUSPLUS_API */
|
jpayne@69
|
530
|
jpayne@69
|
531 #endif // __EDITS_H__
|