ICU 61.1  61.1
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 
30 class U_COMMON_API Edits U_FINAL : public UMemory {
31 public:
36  Edits() :
37  array(stackArray), capacity(STACK_CAPACITY), length(0), delta(0), numChanges(0),
38  errorCode_(U_ZERO_ERROR) {}
44  Edits(const Edits &other) :
45  array(stackArray), capacity(STACK_CAPACITY), length(other.length),
46  delta(other.delta), numChanges(other.numChanges),
47  errorCode_(other.errorCode_) {
48  copyArray(other);
49  }
56  Edits(Edits &&src) U_NOEXCEPT :
57  array(stackArray), capacity(STACK_CAPACITY), length(src.length),
58  delta(src.delta), numChanges(src.numChanges),
59  errorCode_(src.errorCode_) {
60  moveArray(src);
61  }
62 
67  ~Edits();
68 
75  Edits &operator=(const Edits &other);
76 
85  Edits &operator=(Edits &&src) U_NOEXCEPT;
86 
91  void reset() U_NOEXCEPT;
92 
98  void addUnchanged(int32_t unchangedLength);
104  void addReplace(int32_t oldLength, int32_t newLength);
115  UBool copyErrorTo(UErrorCode &outErrorCode);
116 
122  int32_t lengthDelta() const { return delta; }
127  UBool hasChanges() const { return numChanges != 0; }
128 
129 #ifndef U_HIDE_DRAFT_API
130 
134  int32_t numberOfChanges() const { return numChanges; }
135 #endif // U_HIDE_DRAFT_API
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 
197 #ifndef U_HIDE_DRAFT_API
198 
217  UBool findDestinationIndex(int32_t i, UErrorCode &errorCode) {
218  return findIndex(i, FALSE, errorCode) == 0;
219  }
220 
243  int32_t destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode);
244 
267  int32_t sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode);
268 #endif // U_HIDE_DRAFT_API
269 
275  UBool hasChange() const { return changed; }
280  int32_t oldLength() const { return oldLength_; }
286  int32_t newLength() const { return newLength_; }
287 
292  int32_t sourceIndex() const { return srcIndex; }
298  int32_t replacementIndex() const { return replIndex; }
303  int32_t destinationIndex() const { return destIndex; }
304 
305  private:
306  friend class Edits;
307 
308  Iterator(const uint16_t *a, int32_t len, UBool oc, UBool crs);
309 
310  int32_t readLength(int32_t head);
311  void updateNextIndexes();
312  void updatePreviousIndexes();
313  UBool noNext();
314  UBool next(UBool onlyChanges, UErrorCode &errorCode);
315  UBool previous(UErrorCode &errorCode);
317  int32_t findIndex(int32_t i, UBool findSource, UErrorCode &errorCode);
318 
319  const uint16_t *array;
320  int32_t index, length;
321  // 0 if we are not within compressed equal-length changes.
322  // Otherwise the number of remaining changes, including the current one.
323  int32_t remaining;
324  UBool onlyChanges_, coarse;
325 
326  int8_t dir; // iteration direction: back(<0), initial(0), forward(>0)
327  UBool changed;
328  int32_t oldLength_, newLength_;
329  int32_t srcIndex, replIndex, destIndex;
330  };
331 
339  return Iterator(array, length, TRUE, TRUE);
340  }
341 
348  return Iterator(array, length, FALSE, TRUE);
349  }
350 
358  return Iterator(array, length, TRUE, FALSE);
359  }
360 
367  return Iterator(array, length, FALSE, FALSE);
368  }
369 
370 #ifndef U_HIDE_DRAFT_API
371 
398  Edits &mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode);
399 #endif // U_HIDE_DRAFT_API
400 
401 private:
402  void releaseArray() U_NOEXCEPT;
403  Edits &copyArray(const Edits &other);
404  Edits &moveArray(Edits &src) U_NOEXCEPT;
405 
406  void setLastUnit(int32_t last) { array[length - 1] = (uint16_t)last; }
407  int32_t lastUnit() const { return length > 0 ? array[length - 1] : 0xffff; }
408 
409  void append(int32_t r);
410  UBool growArray();
411 
412  static const int32_t STACK_CAPACITY = 100;
413  uint16_t *array;
414  int32_t capacity;
415  int32_t length;
416  int32_t delta;
417  int32_t numChanges;
418  UErrorCode errorCode_;
419  uint16_t stackArray[STACK_CAPACITY];
420 };
421 
423 
424 #endif // __EDITS_H__
Iterator getCoarseChangesIterator() const
Returns an Iterator for coarse-grained changes for simple string updates.
Definition: edits.h:338
int32_t replacementIndex() const
Definition: edits.h:298
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:286
int32_t lengthDelta() const
How much longer is the new text compared with the old text?
Definition: edits.h:122
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:292
int32_t oldLength() const
Definition: edits.h:280
Iterator getFineChangesIterator() const
Returns an Iterator for fine-grained changes for modifying styled text.
Definition: edits.h:357
int32_t numberOfChanges() const
Definition: edits.h:134
UBool hasChanges() const
Definition: edits.h:127
Records lengths of string edits but not replacement text.
Definition: edits.h:30
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
Iterator getFineIterator() const
Returns an Iterator for fine-grained changes and non-changes for modifying styled text...
Definition: edits.h:366
Edits(Edits &&src)
Move constructor, might leave src empty.
Definition: edits.h:56
#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:138
Edits(const Edits &other)
Copy constructor.
Definition: edits.h:44
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:275
UBool findDestinationIndex(int32_t i, UErrorCode &errorCode)
Finds the edit that contains the destination index.
Definition: edits.h:217
int32_t destinationIndex() const
Definition: edits.h:303
Basic definitions for ICU, for both C and C++ APIs.
Edits()
Constructs an empty object.
Definition: edits.h:36
#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:347
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