ICU 62.1  62.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 
20 class UnicodeString;
21 
77 class U_COMMON_API Edits U_FINAL : public UMemory {
78 public:
83  Edits() :
84  array(stackArray), capacity(STACK_CAPACITY), length(0), delta(0), numChanges(0),
85  errorCode_(U_ZERO_ERROR) {}
91  Edits(const Edits &other) :
92  array(stackArray), capacity(STACK_CAPACITY), length(other.length),
93  delta(other.delta), numChanges(other.numChanges),
94  errorCode_(other.errorCode_) {
95  copyArray(other);
96  }
103  Edits(Edits &&src) U_NOEXCEPT :
104  array(stackArray), capacity(STACK_CAPACITY), length(src.length),
105  delta(src.delta), numChanges(src.numChanges),
106  errorCode_(src.errorCode_) {
107  moveArray(src);
108  }
109 
114  ~Edits();
115 
122  Edits &operator=(const Edits &other);
123 
132  Edits &operator=(Edits &&src) U_NOEXCEPT;
133 
138  void reset() U_NOEXCEPT;
139 
145  void addUnchanged(int32_t unchangedLength);
151  void addReplace(int32_t oldLength, int32_t newLength);
162  UBool copyErrorTo(UErrorCode &outErrorCode);
163 
169  int32_t lengthDelta() const { return delta; }
174  UBool hasChanges() const { return numChanges != 0; }
175 
176 #ifndef U_HIDE_DRAFT_API
177 
181  int32_t numberOfChanges() const { return numChanges; }
182 #endif // U_HIDE_DRAFT_API
183 
202  struct U_COMMON_API Iterator U_FINAL : public UMemory {
208  array(nullptr), index(0), length(0),
209  remaining(0), onlyChanges_(FALSE), coarse(FALSE),
210  dir(0), changed(FALSE), oldLength_(0), newLength_(0),
211  srcIndex(0), replIndex(0), destIndex(0) {}
216  Iterator(const Iterator &other) = default;
221  Iterator &operator=(const Iterator &other) = default;
222 
231  UBool next(UErrorCode &errorCode) { return next(onlyChanges_, errorCode); }
232 
252  UBool findSourceIndex(int32_t i, UErrorCode &errorCode) {
253  return findIndex(i, TRUE, errorCode) == 0;
254  }
255 
256 #ifndef U_HIDE_DRAFT_API
257 
276  UBool findDestinationIndex(int32_t i, UErrorCode &errorCode) {
277  return findIndex(i, FALSE, errorCode) == 0;
278  }
279 
302  int32_t destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode);
303 
326  int32_t sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode);
327 #endif // U_HIDE_DRAFT_API
328 
336  UBool hasChange() const { return changed; }
337 
344  int32_t oldLength() const { return oldLength_; }
345 
355  int32_t newLength() const { return newLength_; }
356 
364  int32_t sourceIndex() const { return srcIndex; }
365 
381  int32_t replacementIndex() const {
382  // TODO: Throw an exception if we aren't in a change edit?
383  return replIndex;
384  }
385 
393  int32_t destinationIndex() const { return destIndex; }
394 
395 #ifndef U_HIDE_INTERNAL_API
396 
401  UnicodeString& toString(UnicodeString& appendTo) const;
402 #endif // U_HIDE_INTERNAL_API
403 
404  private:
405  friend class Edits;
406 
407  Iterator(const uint16_t *a, int32_t len, UBool oc, UBool crs);
408 
409  int32_t readLength(int32_t head);
410  void updateNextIndexes();
411  void updatePreviousIndexes();
412  UBool noNext();
413  UBool next(UBool onlyChanges, UErrorCode &errorCode);
414  UBool previous(UErrorCode &errorCode);
416  int32_t findIndex(int32_t i, UBool findSource, UErrorCode &errorCode);
417 
418  const uint16_t *array;
419  int32_t index, length;
420  // 0 if we are not within compressed equal-length changes.
421  // Otherwise the number of remaining changes, including the current one.
422  int32_t remaining;
423  UBool onlyChanges_, coarse;
424 
425  int8_t dir; // iteration direction: back(<0), initial(0), forward(>0)
426  UBool changed;
427  int32_t oldLength_, newLength_;
428  int32_t srcIndex, replIndex, destIndex;
429  };
430 
440  return Iterator(array, length, TRUE, TRUE);
441  }
442 
452  return Iterator(array, length, FALSE, TRUE);
453  }
454 
464  return Iterator(array, length, TRUE, FALSE);
465  }
466 
475  return Iterator(array, length, FALSE, FALSE);
476  }
477 
478 #ifndef U_HIDE_DRAFT_API
479 
506  Edits &mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode);
507 #endif // U_HIDE_DRAFT_API
508 
509 private:
510  void releaseArray() U_NOEXCEPT;
511  Edits &copyArray(const Edits &other);
512  Edits &moveArray(Edits &src) U_NOEXCEPT;
513 
514  void setLastUnit(int32_t last) { array[length - 1] = (uint16_t)last; }
515  int32_t lastUnit() const { return length > 0 ? array[length - 1] : 0xffff; }
516 
517  void append(int32_t r);
518  UBool growArray();
519 
520  static const int32_t STACK_CAPACITY = 100;
521  uint16_t *array;
522  int32_t capacity;
523  int32_t length;
524  int32_t delta;
525  int32_t numChanges;
526  UErrorCode errorCode_;
527  uint16_t stackArray[STACK_CAPACITY];
528 };
529 
531 
532 #endif // __EDITS_H__
int32_t oldLength() const
The length of the current span in the source string, which starts at sourceIndex. ...
Definition: edits.h:344
UBool findSourceIndex(int32_t i, UErrorCode &errorCode)
Moves the iterator to the edit that contains the source index.
Definition: edits.h:252
Iterator getFineChangesIterator() const
Returns an Iterator for fine-grained change edits (full granularity of change edits is retained)...
Definition: edits.h:463
Access to the list of edits.
Definition: edits.h:202
No error, no warning.
Definition: utypes.h:430
Iterator getCoarseIterator() const
Returns an Iterator for coarse-grained change and no-change edits (adjacent change edits are treated ...
Definition: edits.h:451
Iterator getCoarseChangesIterator() const
Returns an Iterator for coarse-grained change edits (adjacent change edits are treated as one)...
Definition: edits.h:439
int32_t newLength() const
The length of the current span in the destination string, which starts at destinationIndex, or in the replacement string, which starts at replacementIndex.
Definition: edits.h:355
Records lengths of string edits but not replacement text.
Definition: edits.h:77
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
int32_t sourceIndex() const
The start index of the current span in the source string; the span has length oldLength.
Definition: edits.h:364
int32_t lengthDelta() const
How much longer is the new text compared with the old text?
Definition: edits.h:169
Edits(Edits &&src)
Move constructor, might leave src empty.
Definition: edits.h:103
int32_t replacementIndex() const
The start index of the current span in the replacement string; the span has length newLength...
Definition: edits.h:381
UBool hasChanges() const
Definition: edits.h:174
int32_t destinationIndex() const
The start index of the current span in the destination string; the span has length newLength...
Definition: edits.h:393
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:240
C++ API: Common ICU base class UObject.
Iterator getFineIterator() const
Returns an Iterator for fine-grained change and no-change edits (full granularity of change edits is ...
Definition: edits.h:474
#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:91
Iterator()
Default constructor, empty iterator.
Definition: edits.h:207
UBool hasChange() const
Returns whether the edit currently represented by the iterator is a change edit.
Definition: edits.h:336
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:396
UBool findDestinationIndex(int32_t i, UErrorCode &errorCode)
Moves the iterator to the edit that contains the destination index.
Definition: edits.h:276
Basic definitions for ICU, for both C and C++ APIs.
Edits()
Constructs an empty object.
Definition: edits.h:83
#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
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:286
int32_t numberOfChanges() const
Definition: edits.h:181
UBool next(UErrorCode &errorCode)
Advances the iterator to the next edit.
Definition: edits.h:231
UMemory is the common ICU base class.
Definition: uobject.h:112
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236