edits.h
Go to the documentation of this file.
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
// edits.h
// created: 2016dec30 Markus W. Scherer
#ifndef __EDITS_H__
#define __EDITS_H__
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#include "unicode/uobject.h"
U_NAMESPACE_BEGIN
class UnicodeString;
class U_COMMON_API Edits final : public UMemory {
public:
Edits() :
array(stackArray), capacity(STACK_CAPACITY), length(0), delta(0), numChanges(0),
errorCode_(U_ZERO_ERROR) {}
Edits(const Edits &other) :
array(stackArray), capacity(STACK_CAPACITY), length(other.length),
delta(other.delta), numChanges(other.numChanges),
errorCode_(other.errorCode_) {
copyArray(other);
}
Edits(Edits &&src) noexcept :
array(stackArray), capacity(STACK_CAPACITY), length(src.length),
delta(src.delta), numChanges(src.numChanges),
errorCode_(src.errorCode_) {
moveArray(src);
}
~Edits();
Edits &operator=(const Edits &other);
Edits &operator=(Edits &&src) noexcept;
void reset() noexcept;
void addUnchanged(int32_t unchangedLength);
void addReplace(int32_t oldLength, int32_t newLength);
UBool copyErrorTo(UErrorCode &outErrorCode) const;
int32_t lengthDelta() const { return delta; }
UBool hasChanges() const { return numChanges != 0; }
int32_t numberOfChanges() const { return numChanges; }
struct U_COMMON_API Iterator final : public UMemory {
Iterator() :
array(nullptr), index(0), length(0),
remaining(0), onlyChanges_(false), coarse(false),
dir(0), changed(false), oldLength_(0), newLength_(0),
srcIndex(0), replIndex(0), destIndex(0) {}
Iterator(const Iterator &other) = default;
Iterator &operator=(const Iterator &other) = default;
UBool next(UErrorCode &errorCode) { return next(onlyChanges_, errorCode); }
UBool findSourceIndex(int32_t i, UErrorCode &errorCode) {
return findIndex(i, true, errorCode) == 0;
}
UBool findDestinationIndex(int32_t i, UErrorCode &errorCode) {
return findIndex(i, false, errorCode) == 0;
}
int32_t destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode);
int32_t sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode);
UBool hasChange() const { return changed; }
int32_t oldLength() const { return oldLength_; }
int32_t newLength() const { return newLength_; }
int32_t sourceIndex() const { return srcIndex; }
int32_t replacementIndex() const {
// TODO: Throw an exception if we aren't in a change edit?
return replIndex;
}
int32_t destinationIndex() const { return destIndex; }
#ifndef U_HIDE_INTERNAL_API
UnicodeString& toString(UnicodeString& appendTo) const;
#endif // U_HIDE_INTERNAL_API
private:
friend class Edits;
Iterator(const uint16_t *a, int32_t len, UBool oc, UBool crs);
int32_t readLength(int32_t head);
void updateNextIndexes();
void updatePreviousIndexes();
UBool noNext();
UBool next(UBool onlyChanges, UErrorCode &errorCode);
UBool previous(UErrorCode &errorCode);
int32_t findIndex(int32_t i, UBool findSource, UErrorCode &errorCode);
const uint16_t *array;
int32_t index, length;
// 0 if we are not within compressed equal-length changes.
// Otherwise the number of remaining changes, including the current one.
int32_t remaining;
UBool onlyChanges_, coarse;
int8_t dir; // iteration direction: back(<0), initial(0), forward(>0)
UBool changed;
int32_t oldLength_, newLength_;
int32_t srcIndex, replIndex, destIndex;
};
Iterator getCoarseChangesIterator() const {
return Iterator(array, length, true, true);
}
Iterator getCoarseIterator() const {
return Iterator(array, length, false, true);
}
Iterator getFineChangesIterator() const {
return Iterator(array, length, true, false);
}
Iterator getFineIterator() const {
return Iterator(array, length, false, false);
}
Edits &mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode);
private:
void releaseArray() noexcept;
Edits &copyArray(const Edits &other);
Edits &moveArray(Edits &src) noexcept;
void setLastUnit(int32_t last) { array[length - 1] = (uint16_t)last; }
int32_t lastUnit() const { return length > 0 ? array[length - 1] : 0xffff; }
void append(int32_t r);
UBool growArray();
static const int32_t STACK_CAPACITY = 100;
uint16_t *array;
int32_t capacity;
int32_t length;
int32_t delta;
int32_t numChanges;
UErrorCode errorCode_;
uint16_t stackArray[STACK_CAPACITY];
};
U_NAMESPACE_END
#endif /* U_SHOW_CPLUSPLUS_API */
#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