00001
00002
00003
00004
00005
00006
00007 #ifndef __EDITS_H__
00008 #define __EDITS_H__
00009
00010 #include "unicode/utypes.h"
00011 #include "unicode/uobject.h"
00012
00018 U_NAMESPACE_BEGIN
00019
00020 class UnicodeString;
00021
00077 class U_COMMON_API Edits U_FINAL : public UMemory {
00078 public:
00083 Edits() :
00084 array(stackArray), capacity(STACK_CAPACITY), length(0), delta(0), numChanges(0),
00085 errorCode_(U_ZERO_ERROR) {}
00091 Edits(const Edits &other) :
00092 array(stackArray), capacity(STACK_CAPACITY), length(other.length),
00093 delta(other.delta), numChanges(other.numChanges),
00094 errorCode_(other.errorCode_) {
00095 copyArray(other);
00096 }
00103 Edits(Edits &&src) U_NOEXCEPT :
00104 array(stackArray), capacity(STACK_CAPACITY), length(src.length),
00105 delta(src.delta), numChanges(src.numChanges),
00106 errorCode_(src.errorCode_) {
00107 moveArray(src);
00108 }
00109
00114 ~Edits();
00115
00122 Edits &operator=(const Edits &other);
00123
00132 Edits &operator=(Edits &&src) U_NOEXCEPT;
00133
00138 void reset() U_NOEXCEPT;
00139
00145 void addUnchanged(int32_t unchangedLength);
00151 void addReplace(int32_t oldLength, int32_t newLength);
00162 UBool copyErrorTo(UErrorCode &outErrorCode);
00163
00169 int32_t lengthDelta() const { return delta; }
00174 UBool hasChanges() const { return numChanges != 0; }
00175
00180 int32_t numberOfChanges() const { return numChanges; }
00181
00200 struct U_COMMON_API Iterator U_FINAL : public UMemory {
00205 Iterator() :
00206 array(nullptr), index(0), length(0),
00207 remaining(0), onlyChanges_(FALSE), coarse(FALSE),
00208 dir(0), changed(FALSE), oldLength_(0), newLength_(0),
00209 srcIndex(0), replIndex(0), destIndex(0) {}
00214 Iterator(const Iterator &other) = default;
00219 Iterator &operator=(const Iterator &other) = default;
00220
00229 UBool next(UErrorCode &errorCode) { return next(onlyChanges_, errorCode); }
00230
00250 UBool findSourceIndex(int32_t i, UErrorCode &errorCode) {
00251 return findIndex(i, TRUE, errorCode) == 0;
00252 }
00253
00273 UBool findDestinationIndex(int32_t i, UErrorCode &errorCode) {
00274 return findIndex(i, FALSE, errorCode) == 0;
00275 }
00276
00299 int32_t destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode);
00300
00323 int32_t sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode);
00324
00332 UBool hasChange() const { return changed; }
00333
00340 int32_t oldLength() const { return oldLength_; }
00341
00351 int32_t newLength() const { return newLength_; }
00352
00360 int32_t sourceIndex() const { return srcIndex; }
00361
00377 int32_t replacementIndex() const {
00378
00379 return replIndex;
00380 }
00381
00389 int32_t destinationIndex() const { return destIndex; }
00390
00391 #ifndef U_HIDE_INTERNAL_API
00392
00397 UnicodeString& toString(UnicodeString& appendTo) const;
00398 #endif // U_HIDE_INTERNAL_API
00399
00400 private:
00401 friend class Edits;
00402
00403 Iterator(const uint16_t *a, int32_t len, UBool oc, UBool crs);
00404
00405 int32_t readLength(int32_t head);
00406 void updateNextIndexes();
00407 void updatePreviousIndexes();
00408 UBool noNext();
00409 UBool next(UBool onlyChanges, UErrorCode &errorCode);
00410 UBool previous(UErrorCode &errorCode);
00412 int32_t findIndex(int32_t i, UBool findSource, UErrorCode &errorCode);
00413
00414 const uint16_t *array;
00415 int32_t index, length;
00416
00417
00418 int32_t remaining;
00419 UBool onlyChanges_, coarse;
00420
00421 int8_t dir;
00422 UBool changed;
00423 int32_t oldLength_, newLength_;
00424 int32_t srcIndex, replIndex, destIndex;
00425 };
00426
00435 Iterator getCoarseChangesIterator() const {
00436 return Iterator(array, length, TRUE, TRUE);
00437 }
00438
00447 Iterator getCoarseIterator() const {
00448 return Iterator(array, length, FALSE, TRUE);
00449 }
00450
00459 Iterator getFineChangesIterator() const {
00460 return Iterator(array, length, TRUE, FALSE);
00461 }
00462
00470 Iterator getFineIterator() const {
00471 return Iterator(array, length, FALSE, FALSE);
00472 }
00473
00501 Edits &mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode);
00502
00503 private:
00504 void releaseArray() U_NOEXCEPT;
00505 Edits ©Array(const Edits &other);
00506 Edits &moveArray(Edits &src) U_NOEXCEPT;
00507
00508 void setLastUnit(int32_t last) { array[length - 1] = (uint16_t)last; }
00509 int32_t lastUnit() const { return length > 0 ? array[length - 1] : 0xffff; }
00510
00511 void append(int32_t r);
00512 UBool growArray();
00513
00514 static const int32_t STACK_CAPACITY = 100;
00515 uint16_t *array;
00516 int32_t capacity;
00517 int32_t length;
00518 int32_t delta;
00519 int32_t numChanges;
00520 UErrorCode errorCode_;
00521 uint16_t stackArray[STACK_CAPACITY];
00522 };
00523
00524 U_NAMESPACE_END
00525
00526 #endif // __EDITS_H__