ICU 76.1 76.1
Loading...
Searching...
No Matches
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
21U_NAMESPACE_BEGIN
22
23class UnicodeString;
24
80class U_COMMON_API Edits final : public UMemory {
81public:
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
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 {
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
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
506private:
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] = static_cast<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
527U_NAMESPACE_END
528
529#endif /* U_SHOW_CPLUSPLUS_API */
530
531#endif // __EDITS_H__
Records lengths of string edits but not replacement text.
Definition edits.h:80
Iterator getFineIterator() const
Returns an Iterator for fine-grained change and no-change edits (full granularity of change edits is ...
Definition edits.h:473
Edits(const Edits &other)
Copy constructor.
Definition edits.h:94
Edits & mergeAndAppend(const Edits &ab, const Edits &bc, UErrorCode &errorCode)
Merges the two input Edits and appends the result to this object.
Iterator getCoarseChangesIterator() const
Returns an Iterator for coarse-grained change edits (adjacent change edits are treated as one).
Definition edits.h:438
void reset() noexcept
Resets the data but may not release memory.
Edits & operator=(Edits &&src) noexcept
Move assignment operator, might leave src empty.
Edits(Edits &&src) noexcept
Move constructor, might leave src empty.
Definition edits.h:106
Edits()
Constructs an empty object.
Definition edits.h:86
Iterator getCoarseIterator() const
Returns an Iterator for coarse-grained change and no-change edits (adjacent change edits are treated ...
Definition edits.h:450
~Edits()
Destructor.
UBool hasChanges() const
Definition edits.h:177
Iterator getFineChangesIterator() const
Returns an Iterator for fine-grained change edits (full granularity of change edits is retained).
Definition edits.h:462
int32_t numberOfChanges() const
Definition edits.h:183
Edits & operator=(const Edits &other)
Assignment operator.
UMemory is the common ICU base class.
Definition uobject.h:115
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:296
Access to the list of edits.
Definition edits.h:203
int32_t destinationIndex() const
The start index of the current span in the destination string; the span has length newLength.
Definition edits.h:392
UBool findDestinationIndex(int32_t i, UErrorCode &errorCode)
Moves the iterator to the edit that contains the destination index.
Definition edits.h:276
Iterator(const Iterator &other)=default
Copy constructor.
Iterator & operator=(const Iterator &other)=default
Assignment operator.
UnicodeString & toString(UnicodeString &appendTo) const
A string representation of the current edit represented by the iterator for debugging.
int32_t destinationIndexFromSourceIndex(int32_t i, UErrorCode &errorCode)
Computes the destination index corresponding to the given source index.
int32_t newLength() const
The length of the current span in the destination string, which starts at destinationIndex,...
Definition edits.h:354
int32_t sourceIndex() const
The start index of the current span in the source string; the span has length oldLength.
Definition edits.h:363
UBool next(UErrorCode &errorCode)
Advances the iterator to the next edit.
Definition edits.h:232
int32_t oldLength() const
The length of the current span in the source string, which starts at sourceIndex.
Definition edits.h:343
Iterator()
Default constructor, empty iterator.
Definition edits.h:208
UBool findSourceIndex(int32_t i, UErrorCode &errorCode)
Moves the iterator to the edit that contains the source index.
Definition edits.h:253
int32_t sourceIndexFromDestinationIndex(int32_t i, UErrorCode &errorCode)
Computes the source index corresponding to the given destination index.
UBool hasChange() const
Returns whether the edit currently represented by the iterator is a change edit.
Definition edits.h:335
int32_t replacementIndex() const
The start index of the current span in the replacement string; the span has length newLength.
Definition edits.h:380
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:247
C++ API: Common ICU base class UObject.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:430
@ U_ZERO_ERROR
No error, no warning.
Definition utypes.h:465
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition utypes.h:315