ICU 65.1  65.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 
12 #if U_SHOW_CPLUSPLUS_API
13 
14 #include "unicode/uobject.h"
15 
21 U_NAMESPACE_BEGIN
22 
23 class UnicodeString;
24 
81 public:
86  Edits() :
87  array(stackArray), capacity(STACK_CAPACITY), length(0), delta(0), numChanges(0),
88  errorCode_(U_ZERO_ERROR) {}
94  Edits(const Edits &other) :
95  array(stackArray), capacity(STACK_CAPACITY), length(other.length),
96  delta(other.delta), numChanges(other.numChanges),
97  errorCode_(other.errorCode_) {
98  copyArray(other);
99  }
107  array(stackArray), capacity(STACK_CAPACITY), length(src.length),
108  delta(src.delta), numChanges(src.numChanges),
109  errorCode_(src.errorCode_) {
110  moveArray(src);
111  }
112 
117  ~Edits();
118 
125  Edits &operator=(const Edits &other);
126 
135  Edits &operator=(Edits &&src) U_NOEXCEPT;
136 
141  void reset() U_NOEXCEPT;
142 
148  void addUnchanged(int32_t unchangedLength);
154  void addReplace(int32_t oldLength, int32_t newLength);
165  UBool copyErrorTo(UErrorCode &outErrorCode) const;
166 
172  int32_t lengthDelta() const { return delta; }
177  UBool hasChanges() const { return numChanges != 0; }
178 
183  int32_t numberOfChanges() const { return numChanges; }
184 
209  array(nullptr), index(0), length(0),
210  remaining(0), onlyChanges_(FALSE), coarse(FALSE),
211  dir(0), changed(FALSE), oldLength_(0), newLength_(0),
212  srcIndex(0), replIndex(0), destIndex(0) {}
217  Iterator(const Iterator &other) = default;
222  Iterator &operator=(const Iterator &other) = default;
223 
232  UBool next(UErrorCode &errorCode) { return next(onlyChanges_, errorCode); }
233 
253  UBool findSourceIndex(int32_t i, UErrorCode &errorCode) {
254  return findIndex(i, TRUE, errorCode) == 0;
255  }
256 
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 
335  UBool hasChange() const { return changed; }
336 
343  int32_t oldLength() const { return oldLength_; }
344 
354  int32_t newLength() const { return newLength_; }
355 
363  int32_t sourceIndex() const { return srcIndex; }
364 
380  int32_t replacementIndex() const {
381  // TODO: Throw an exception if we aren't in a change edit?
382  return replIndex;
383  }
384 
392  int32_t destinationIndex() const { return destIndex; }
393 
394 #ifndef U_HIDE_INTERNAL_API
395 
400  UnicodeString& toString(UnicodeString& appendTo) const;
401 #endif // U_HIDE_INTERNAL_API
402 
403  private:
404  friend class Edits;
405 
406  Iterator(const uint16_t *a, int32_t len, UBool oc, UBool crs);
407 
408  int32_t readLength(int32_t head);
409  void updateNextIndexes();
410  void updatePreviousIndexes();
411  UBool noNext();
412  UBool next(UBool onlyChanges, UErrorCode &errorCode);
413  UBool previous(UErrorCode &errorCode);
415  int32_t findIndex(int32_t i, UBool findSource, UErrorCode &errorCode);
416 
417  const uint16_t *array;
418  int32_t index, length;
419  // 0 if we are not within compressed equal-length changes.
420  // Otherwise the number of remaining changes, including the current one.
421  int32_t remaining;
422  UBool onlyChanges_, coarse;
423 
424  int8_t dir; // iteration direction: back(<0), initial(0), forward(>0)
425  UBool changed;
426  int32_t oldLength_, newLength_;
427  int32_t srcIndex, replIndex, destIndex;
428  };
429 
439  return Iterator(array, length, TRUE, TRUE);
440  }
441 
451  return Iterator(array, length, FALSE, TRUE);
452  }
453 
463  return Iterator(array, length, TRUE, FALSE);
464  }
465 
474  return Iterator(array, length, FALSE, FALSE);
475  }
476 
504  Edits &mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode);
505 
506 private:
507  void releaseArray() U_NOEXCEPT;
508  Edits &copyArray(const Edits &other);
509  Edits &moveArray(Edits &src) U_NOEXCEPT;
510 
511  void setLastUnit(int32_t last) { array[length - 1] = (uint16_t)last; }
512  int32_t lastUnit() const { return length > 0 ? array[length - 1] : 0xffff; }
513 
514  void append(int32_t r);
515  UBool growArray();
516 
517  static const int32_t STACK_CAPACITY = 100;
518  uint16_t *array;
519  int32_t capacity;
520  int32_t length;
521  int32_t delta;
522  int32_t numChanges;
523  UErrorCode errorCode_;
524  uint16_t stackArray[STACK_CAPACITY];
525 };
526 
527 U_NAMESPACE_END
528 
529 #endif /* U_SHOW_CPLUSPLUS_API */
530 
531 #endif // __EDITS_H__
int32_t oldLength() const
The length of the current span in the source string, which starts at sourceIndex. ...
Definition: edits.h:343
UBool findSourceIndex(int32_t i, UErrorCode &errorCode)
Moves the iterator to the edit that contains the source index.
Definition: edits.h:253
Iterator getFineChangesIterator() const
Returns an Iterator for fine-grained change edits (full granularity of change edits is retained)...
Definition: edits.h:462
Access to the list of edits.
Definition: edits.h:203
No error, no warning.
Definition: utypes.h:449
Iterator getCoarseIterator() const
Returns an Iterator for coarse-grained change and no-change edits (adjacent change edits are treated ...
Definition: edits.h:450
Iterator getCoarseChangesIterator() const
Returns an Iterator for coarse-grained change edits (adjacent change edits are treated as one)...
Definition: edits.h:438
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:354
Records lengths of string edits but not replacement text.
Definition: edits.h:80
int32_t sourceIndex() const
The start index of the current span in the source string; the span has length oldLength.
Definition: edits.h:363
int32_t lengthDelta() const
How much longer is the new text compared with the old text?
Definition: edits.h:172
Edits(Edits &&src) U_NOEXCEPT
Move constructor, might leave src empty.
Definition: edits.h:106
int32_t replacementIndex() const
The start index of the current span in the replacement string; the span has length newLength...
Definition: edits.h:380
UBool hasChanges() const
Definition: edits.h:177
int32_t destinationIndex() const
The start index of the current span in the destination string; the span has length newLength...
Definition: edits.h:392
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:265
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:473
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
Edits(const Edits &other)
Copy constructor.
Definition: edits.h:94
Iterator()
Default constructor, empty iterator.
Definition: edits.h:208
UBool hasChange() const
Returns whether the edit currently represented by the iterator is a change edit.
Definition: edits.h:335
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
UBool findDestinationIndex(int32_t i, UErrorCode &errorCode)
Moves the iterator to the edit that contains the destination index.
Definition: edits.h:276
#define U_FINAL
Defined to the C++11 "final" keyword if available.
Definition: umachine.h:140
Basic definitions for ICU, for both C and C++ APIs.
Edits()
Constructs an empty object.
Definition: edits.h:86
#define FALSE
The FALSE value of a UBool.
Definition: umachine.h:269
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:300
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
int32_t numberOfChanges() const
Definition: edits.h:183
UBool next(UErrorCode &errorCode)
Advances the iterator to the next edit.
Definition: edits.h:232
UMemory is the common ICU base class.
Definition: uobject.h:115
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261