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__
FALSE
#define FALSE
The FALSE value of a UBool.
Definition: umachine.h:244
icu::Edits::getFineChangesIterator
Iterator getFineChangesIterator() const
Returns an Iterator for fine-grained change edits (full granularity of change edits is retained).
Definition: edits.h:463
icu::Edits::Iterator::newLength
int32_t newLength() const
The length of the current span in the destination string, which starts at destinationIndex,...
Definition: edits.h:355
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::Edits::numberOfChanges
int32_t numberOfChanges() const
Definition: edits.h:181
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236
icu::Edits::Iterator::next
UBool next(UErrorCode &errorCode)
Advances the iterator to the next edit.
Definition: edits.h:231
U_COMMON_API
#define U_COMMON_API
Definition: utypes.h:359
icu::Edits::Iterator::findDestinationIndex
UBool findDestinationIndex(int32_t i, UErrorCode &errorCode)
Moves the iterator to the edit that contains the destination index.
Definition: edits.h:276
icu::Edits::Edits
Edits(Edits &&src)
Move constructor, might leave src empty.
Definition: edits.h:103
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:286
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition: utypes.h:396
icu::Edits
Records lengths of string edits but not replacement text.
Definition: edits.h:77
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:240
icu::Edits::getFineIterator
Iterator getFineIterator() const
Returns an Iterator for fine-grained change and no-change edits (full granularity of change edits is ...
Definition: edits.h:474
icu::Edits::Iterator::hasChange
UBool hasChange() const
Returns whether the edit currently represented by the iterator is a change edit.
Definition: edits.h:336
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:112
icu::Edits::Iterator::oldLength
int32_t oldLength() const
The length of the current span in the source string, which starts at sourceIndex.
Definition: edits.h:344
icu::Edits::getCoarseIterator
Iterator getCoarseIterator() const
Returns an Iterator for coarse-grained change and no-change edits (adjacent change edits are treated ...
Definition: edits.h:451
icu::Edits::getCoarseChangesIterator
Iterator getCoarseChangesIterator() const
Returns an Iterator for coarse-grained change edits (adjacent change edits are treated as one).
Definition: edits.h:439
icu::Edits::Iterator::findSourceIndex
UBool findSourceIndex(int32_t i, UErrorCode &errorCode)
Moves the iterator to the edit that contains the source index.
Definition: edits.h:252
icu::Edits::Iterator::sourceIndex
int32_t sourceIndex() const
The start index of the current span in the source string; the span has length oldLength.
Definition: edits.h:364
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:430
icu::Edits::Iterator::Iterator
Iterator()
Default constructor, empty iterator.
Definition: edits.h:207
icu::Edits::Iterator::replacementIndex
int32_t replacementIndex() const
The start index of the current span in the replacement string; the span has length newLength.
Definition: edits.h:381
icu::Edits::Iterator::destinationIndex
int32_t destinationIndex() const
The start index of the current span in the destination string; the span has length newLength.
Definition: edits.h:393
icu::Edits::Edits
Edits()
Constructs an empty object.
Definition: edits.h:83
icu::Edits::hasChanges
UBool hasChanges() const
Definition: edits.h:174
icu::Edits::Iterator
Access to the list of edits.
Definition: edits.h:202
uobject.h
C++ API: Common ICU base class UObject.
icu::Edits::Edits
Edits(const Edits &other)
Copy constructor.
Definition: edits.h:91
U_NAMESPACE_END
#define U_NAMESPACE_END
Definition: uversion.h:138
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
Definition: uversion.h:137