ICU 60.2  60.2
edits.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 // edits.h
5 // created: 2016dec30 Markus W. Scherer
6 
7 #ifndef __EDITS_H__
8 #define __EDITS_H__
9 
10 #include "unicode/utypes.h"
11 #include "unicode/uobject.h"
12 
19 
20 #ifndef U_HIDE_DRAFT_API
21 
32 class U_COMMON_API Edits U_FINAL : public UMemory {
33 public:
38  Edits() :
39  array(stackArray), capacity(STACK_CAPACITY), length(0), delta(0), numChanges(0),
40  errorCode_(U_ZERO_ERROR) {}
46  Edits(const Edits &other) :
47  array(stackArray), capacity(STACK_CAPACITY), length(other.length),
48  delta(other.delta), numChanges(other.numChanges),
49  errorCode_(other.errorCode_) {
50  copyArray(other);
51  }
58  Edits(Edits &&src) U_NOEXCEPT :
59  array(stackArray), capacity(STACK_CAPACITY), length(src.length),
60  delta(src.delta), numChanges(src.numChanges),
61  errorCode_(src.errorCode_) {
62  moveArray(src);
63  }
64 
69  ~Edits();
70 
77  Edits &operator=(const Edits &other);
78 
87  Edits &operator=(Edits &&src) U_NOEXCEPT;
88 
93  void reset() U_NOEXCEPT;
94 
100  void addUnchanged(int32_t unchangedLength);
106  void addReplace(int32_t oldLength, int32_t newLength);
117  UBool copyErrorTo(UErrorCode &outErrorCode);
118 
124  int32_t lengthDelta() const { return delta; }
129  UBool hasChanges() const { return numChanges != 0; }
130 
135  int32_t numberOfChanges() const { return numChanges; }
136 
143  struct U_COMMON_API Iterator U_FINAL : public UMemory {
149  array(nullptr), index(0), length(0),
150  remaining(0), onlyChanges_(FALSE), coarse(FALSE),
151  dir(0), changed(FALSE), oldLength_(0), newLength_(0),
152  srcIndex(0), replIndex(0), destIndex(0) {}
157  Iterator(const Iterator &other) = default;
162  Iterator &operator=(const Iterator &other) = default;
163 
172  UBool next(UErrorCode &errorCode) { return next(onlyChanges_, errorCode); }
173 
193  UBool findSourceIndex(int32_t i, UErrorCode &errorCode) {
194  return findIndex(i, TRUE, errorCode) == 0;
195  }
196 
216  UBool findDestinationIndex(int32_t i, UErrorCode &errorCode) {
217  return findIndex(i, FALSE, errorCode) == 0;
218  }
219 
242  int32_t destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode);
243 
266  int32_t sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode);
267 
273  UBool hasChange() const { return changed; }
278  int32_t oldLength() const { return oldLength_; }
284  int32_t newLength() const { return newLength_; }
285 
290  int32_t sourceIndex() const { return srcIndex; }
296  int32_t replacementIndex() const { return replIndex; }
301  int32_t destinationIndex() const { return destIndex; }
302 
303  private:
304  friend class Edits;
305 
306  Iterator(const uint16_t *a, int32_t len, UBool oc, UBool crs);
307 
308  int32_t readLength(int32_t head);
309  void updateNextIndexes();
310  void updatePreviousIndexes();
311  UBool noNext();
312  UBool next(UBool onlyChanges, UErrorCode &errorCode);
313  UBool previous(UErrorCode &errorCode);
315  int32_t findIndex(int32_t i, UBool findSource, UErrorCode &errorCode);
316 
317  const uint16_t *array;
318  int32_t index, length;
319  // 0 if we are not within compressed equal-length changes.
320  // Otherwise the number of remaining changes, including the current one.
321  int32_t remaining;
322  UBool onlyChanges_, coarse;
323 
324  int8_t dir; // iteration direction: back(<0), initial(0), forward(>0)
325  UBool changed;
326  int32_t oldLength_, newLength_;
327  int32_t srcIndex, replIndex, destIndex;
328  };
329 
337  return Iterator(array, length, TRUE, TRUE);
338  }
339 
346  return Iterator(array, length, FALSE, TRUE);
347  }
348 
356  return Iterator(array, length, TRUE, FALSE);
357  }
358 
365  return Iterator(array, length, FALSE, FALSE);
366  }
367 
395  Edits &mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode);
396 
397 private:
398  void releaseArray() U_NOEXCEPT;
399  Edits &copyArray(const Edits &other);
400  Edits &moveArray(Edits &src) U_NOEXCEPT;
401 
402  void setLastUnit(int32_t last) { array[length - 1] = (uint16_t)last; }
403  int32_t lastUnit() const { return length > 0 ? array[length - 1] : 0xffff; }
404 
405  void append(int32_t r);
406  UBool growArray();
407 
408  static const int32_t STACK_CAPACITY = 100;
409  uint16_t *array;
410  int32_t capacity;
411  int32_t length;
412  int32_t delta;
413  int32_t numChanges;
414  UErrorCode errorCode_;
415  uint16_t stackArray[STACK_CAPACITY];
416 };
417 
418 #endif // U_HIDE_DRAFT_API
419 
421 
422 #endif // __EDITS_H__
Iterator getCoarseChangesIterator() const
Returns an Iterator for coarse-grained changes for simple string updates.
Definition: edits.h:336
int32_t replacementIndex() const
Definition: edits.h:296
UBool findSourceIndex(int32_t i, UErrorCode &errorCode)
Finds the edit that contains the source index.
Definition: edits.h:193
int32_t newLength() const
Definition: edits.h:284
int32_t lengthDelta() const
How much longer is the new text compared with the old text?
Definition: edits.h:124
Access to the list of edits.
Definition: edits.h:143
No error, no warning.
Definition: utypes.h:430
int32_t sourceIndex() const
Definition: edits.h:290
int32_t oldLength() const
Definition: edits.h:278
Iterator getFineChangesIterator() const
Returns an Iterator for fine-grained changes for modifying styled text.
Definition: edits.h:355
int32_t numberOfChanges() const
Definition: edits.h:135
UBool hasChanges() const
Definition: edits.h:129
Records lengths of string edits but not replacement text.
Definition: edits.h:32
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
Iterator getFineIterator() const
Returns an Iterator for fine-grained changes and non-changes for modifying styled text...
Definition: edits.h:364
Edits(Edits &&src)
Move constructor, might leave src empty.
Definition: edits.h:58
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:240
C++ API: Common ICU base class UObject.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
Edits(const Edits &other)
Copy constructor.
Definition: edits.h:46
Iterator()
Default constructor, empty iterator.
Definition: edits.h:148
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:396
UBool hasChange() const
Definition: edits.h:273
UBool findDestinationIndex(int32_t i, UErrorCode &errorCode)
Finds the edit that contains the destination index.
Definition: edits.h:216
int32_t destinationIndex() const
Definition: edits.h:301
Basic definitions for ICU, for both C and C++ APIs.
Edits()
Constructs an empty object.
Definition: edits.h:38
#define FALSE
The FALSE value of a UBool.
Definition: umachine.h:244
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:359
Iterator getCoarseIterator() const
Returns an Iterator for coarse-grained changes and non-changes for simple string updates.
Definition: edits.h:345
UBool next(UErrorCode &errorCode)
Advances to the next edit.
Definition: edits.h:172
UMemory is the common ICU base class.
Definition: uobject.h:112
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236