ICU 75.1 75.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
80 class U_COMMON_API Edits final : public UMemory {
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 }
106 Edits(Edits &&src) noexcept :
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) noexcept;
136
141 void reset() 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
203 struct U_COMMON_API Iterator final : public UMemory {
208 Iterator() :
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
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
438 Iterator getCoarseChangesIterator() const {
439 return Iterator(array, length, true, true);
440 }
441
450 Iterator getCoarseIterator() const {
451 return Iterator(array, length, false, true);
452 }
453
462 Iterator getFineChangesIterator() const {
463 return Iterator(array, length, true, false);
464 }
465
473 Iterator getFineIterator() const {
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() noexcept;
508 Edits &copyArray(const Edits &other);
509 Edits &moveArray(Edits &src) 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__
icu::Edits
Records lengths of string edits but not replacement text.
Definition: edits.h:80
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:473
icu::Edits::Edits
Edits(const Edits &other)
Copy constructor.
Definition: edits.h:94
icu::Edits::operator=
Edits & operator=(const Edits &other)
Assignment operator.
icu::Edits::getCoarseChangesIterator
Iterator getCoarseChangesIterator() const
Returns an Iterator for coarse-grained change edits (adjacent change edits are treated as one).
Definition: edits.h:438
icu::Edits::operator=
Edits & operator=(Edits &&src) noexcept
Move assignment operator, might leave src empty.
icu::Edits::reset
void reset() noexcept
Resets the data but may not release memory.
icu::Edits::Edits
Edits(Edits &&src) noexcept
Move constructor, might leave src empty.
Definition: edits.h:106
icu::Edits::Edits
Edits()
Constructs an empty object.
Definition: edits.h:86
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:450
icu::Edits::~Edits
~Edits()
Destructor.
icu::Edits::hasChanges
UBool hasChanges() const
Definition: edits.h:177
icu::Edits::getFineChangesIterator
Iterator getFineChangesIterator() const
Returns an Iterator for fine-grained change edits (full granularity of change edits is retained).
Definition: edits.h:462
icu::Edits::numberOfChanges
int32_t numberOfChanges() const
Definition: edits.h:183
icu::Edits::mergeAndAppend
Edits & mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode)
Merges the two input Edits and appends the result to this object.
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
icu::Edits::Iterator
Access to the list of edits.
Definition: edits.h:203
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:392
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::Iterator::Iterator
Iterator(const Iterator &other)=default
Copy constructor.
icu::Edits::Iterator::destinationIndexFromSourceIndex
int32_t destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode)
Computes the destination index corresponding to the given source index.
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:354
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:363
icu::Edits::Iterator::operator=
Iterator & operator=(const Iterator &other)=default
Assignment operator.
icu::Edits::Iterator::toString
UnicodeString & toString(UnicodeString &appendTo) const
A string representation of the current edit represented by the iterator for debugging.
icu::Edits::Iterator::next
UBool next(UErrorCode &errorCode)
Advances the iterator to the next edit.
Definition: edits.h:232
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:343
icu::Edits::Iterator::Iterator
Iterator()
Default constructor, empty iterator.
Definition: edits.h:208
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:253
icu::Edits::Iterator::sourceIndexFromDestinationIndex
int32_t sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode)
Computes the source index corresponding to the given destination index.
icu::Edits::Iterator::hasChange
UBool hasChange() const
Returns whether the edit currently represented by the iterator is a change edit.
Definition: edits.h:335
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:380
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
uobject.h
C++ API: Common ICU base class UObject.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:450
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:300