ICU 63.1 63.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
18 U_NAMESPACE_BEGIN
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
180 int32_t numberOfChanges() const { return numChanges; }
181
200 struct U_COMMON_API Iterator U_FINAL : public UMemory {
205 Iterator() :
206 array(nullptr), index(0), length(0),
207 remaining(0), onlyChanges_(FALSE), coarse(FALSE),
208 dir(0), changed(FALSE), oldLength_(0), newLength_(0),
209 srcIndex(0), replIndex(0), destIndex(0) {}
214 Iterator(const Iterator &other) = default;
219 Iterator &operator=(const Iterator &other) = default;
220
229 UBool next(UErrorCode &errorCode) { return next(onlyChanges_, errorCode); }
230
250 UBool findSourceIndex(int32_t i, UErrorCode &errorCode) {
251 return findIndex(i, TRUE, errorCode) == 0;
252 }
253
273 UBool findDestinationIndex(int32_t i, UErrorCode &errorCode) {
274 return findIndex(i, FALSE, errorCode) == 0;
275 }
276
299 int32_t destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode);
300
323 int32_t sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode);
324
332 UBool hasChange() const { return changed; }
333
340 int32_t oldLength() const { return oldLength_; }
341
351 int32_t newLength() const { return newLength_; }
352
360 int32_t sourceIndex() const { return srcIndex; }
361
377 int32_t replacementIndex() const {
378 // TODO: Throw an exception if we aren't in a change edit?
379 return replIndex;
380 }
381
389 int32_t destinationIndex() const { return destIndex; }
390
391 #ifndef U_HIDE_INTERNAL_API
392
397 UnicodeString& toString(UnicodeString& appendTo) const;
398 #endif // U_HIDE_INTERNAL_API
399
400 private:
401 friend class Edits;
402
403 Iterator(const uint16_t *a, int32_t len, UBool oc, UBool crs);
404
405 int32_t readLength(int32_t head);
406 void updateNextIndexes();
407 void updatePreviousIndexes();
408 UBool noNext();
409 UBool next(UBool onlyChanges, UErrorCode &errorCode);
410 UBool previous(UErrorCode &errorCode);
412 int32_t findIndex(int32_t i, UBool findSource, UErrorCode &errorCode);
413
414 const uint16_t *array;
415 int32_t index, length;
416 // 0 if we are not within compressed equal-length changes.
417 // Otherwise the number of remaining changes, including the current one.
418 int32_t remaining;
419 UBool onlyChanges_, coarse;
420
421 int8_t dir; // iteration direction: back(<0), initial(0), forward(>0)
422 UBool changed;
423 int32_t oldLength_, newLength_;
424 int32_t srcIndex, replIndex, destIndex;
425 };
426
435 Iterator getCoarseChangesIterator() const {
436 return Iterator(array, length, TRUE, TRUE);
437 }
438
447 Iterator getCoarseIterator() const {
448 return Iterator(array, length, FALSE, TRUE);
449 }
450
459 Iterator getFineChangesIterator() const {
460 return Iterator(array, length, TRUE, FALSE);
461 }
462
470 Iterator getFineIterator() const {
471 return Iterator(array, length, FALSE, FALSE);
472 }
473
501 Edits &mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode);
502
503 private:
504 void releaseArray() U_NOEXCEPT;
505 Edits &copyArray(const Edits &other);
506 Edits &moveArray(Edits &src) U_NOEXCEPT;
507
508 void setLastUnit(int32_t last) { array[length - 1] = (uint16_t)last; }
509 int32_t lastUnit() const { return length > 0 ? array[length - 1] : 0xffff; }
510
511 void append(int32_t r);
512 UBool growArray();
513
514 static const int32_t STACK_CAPACITY = 100;
515 uint16_t *array;
516 int32_t capacity;
517 int32_t length;
518 int32_t delta;
519 int32_t numChanges;
520 UErrorCode errorCode_;
521 uint16_t stackArray[STACK_CAPACITY];
522 };
523
524 U_NAMESPACE_END
525
526 #endif // __EDITS_H__
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:340
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:250
icu::Edits::getFineChangesIterator
Iterator getFineChangesIterator() const
Returns an Iterator for fine-grained change edits (full granularity of change edits is retained)...
Definition: edits.h:459
icu::Edits::Iterator
Access to the list of edits.
Definition: edits.h:200
U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:435
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:447
icu::Edits::getCoarseChangesIterator
Iterator getCoarseChangesIterator() const
Returns an Iterator for coarse-grained change edits (adjacent change edits are treated as one)...
Definition: edits.h:435
icu::Edits::Iterator::newLength
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:351
icu::Edits
Records lengths of string edits but not replacement text.
Definition: edits.h:77
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
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:360
icu::Edits::Edits
Edits(Edits &&src) U_NOEXCEPT
Move constructor, might leave src empty.
Definition: edits.h:103
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:377
icu::Edits::hasChanges
UBool hasChanges() const
Definition: edits.h:174
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:389
TRUE
#define TRUE
The TRUE value of a UBool.
Definition: umachine.h:229
uobject.h
C++ API: Common ICU base class UObject.
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:470
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:503
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
icu::Edits::Edits
Edits(const Edits &other)
Copy constructor.
Definition: edits.h:91
icu::Edits::Iterator::Iterator
Iterator()
Default constructor, empty iterator.
Definition: edits.h:205
icu::Edits::Iterator::hasChange
UBool hasChange() const
Returns whether the edit currently represented by the iterator is a change edit.
Definition: edits.h:332
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
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:273
U_FINAL
#define U_FINAL
Defined to the C++11 "final" keyword if available.
Definition: umachine.h:140
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::Edits::Edits
Edits()
Constructs an empty object.
Definition: edits.h:83
FALSE
#define FALSE
The FALSE value of a UBool.
Definition: umachine.h:233
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
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:289
icu::Edits::numberOfChanges
int32_t numberOfChanges() const
Definition: edits.h:180
icu::Edits::Iterator::next
UBool next(UErrorCode &errorCode)
Advances the iterator to the next edit.
Definition: edits.h:229
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:109
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225