Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public final Class Edits

extends Object
Class Inheritance
Imports
java.nio.BufferOverflowException, java.util.Arrays

Records lengths of string edits but not replacement text. Supports replacements, insertions, deletions in linear progression. Does not support moving/reordering of text.

There are two types of edits: change edits and no-change edits. Add edits to instances of this class using addReplace(int, int) (for change edits) and addUnchanged(int) (for no-change edits). Change edits are retained with full granularity, whereas adjacent no-change edits are always merged together. In no-change edits, there is a one-to-one mapping between code points in the source and destination strings.

After all edits have been added, instances of this class should be considered immutable, and an Edits.Iterator can be used for queries.

There are four flavors of Edits.Iterator:

For example, consider the string "abcßDeF", which case-folds to "abcssdef". This string has the following fine edits:

and the following coarse edits (note how adjacent change edits get merged together):

The "fine changes" and "coarse changes" iterators will step through only the change edits when their Edits.Iterator#next() methods are called. They are identical to the non-change iterators when their Edits.Iterator#findSourceIndex(int) or Edits.Iterator#findDestinationIndex(int) methods are used to walk through the string.

For examples of how to use this class, see the test TestCaseMapEditsIteratorDocs in UCharacterCaseTest.java.

ICU Status
Stable since ICU 59.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Edits.Iterator

Access to the list of edits.

Field Summary

Modifier and TypeField and Description
private char[]
private int
private int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private int
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
public
Edits()

Constructs an empty object.

Method Summary

Modifier and TypeMethod and Description
public void
addReplace(int oldLength, int newLength)

Adds a change edit: a record for a text replacement/insertion/deletion.

public void
addUnchanged(int unchangedLength)

Adds a no-change edit: a record for an unchanged segment of text.

private void
append(int r)

public Edits.Iterator

Returns:

an Iterator that merges adjacent changes.
getCoarseChangesIterator
()

Returns an Iterator for coarse-grained change edits (adjacent change edits are treated as one).

public Edits.Iterator

Returns:

an Iterator that merges adjacent changes.
getCoarseIterator
()

Returns an Iterator for coarse-grained change and no-change edits (adjacent change edits are treated as one).

public Edits.Iterator

Returns:

an Iterator that separates adjacent changes.
getFineChangesIterator
()

Returns an Iterator for fine-grained change edits (full granularity of change edits is retained).

public Edits.Iterator

Returns:

an Iterator that separates adjacent changes.
getFineIterator
()

Returns an Iterator for fine-grained change and no-change edits (full granularity of change edits is retained).

private boolean
public boolean

Returns:

true if there are any change edits
hasChanges
()

private int
public int

Returns:

new length minus old length
lengthDelta
()

How much longer is the new text compared with the old text?

public Edits

Returns:

this, with the merged edits appended
mergeAndAppend
(Edits
reflects how substrings of input string a map to substrings of intermediate string b.
ab
,
Edits
reflects how substrings of intermediate string b map to substrings of output string c.
bc
)

Merges the two input Edits and appends the result to this object.

public int

Returns:

the number of change edits
numberOfChanges
()

public void
reset()

Resets the data but may not release memory.

private void
setLastUnit(int last)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

arrayback to summary
private char[] array
deltaback to summary
private int delta
lengthback to summary
private int length
LENGTH_IN_1TRAILback to summary
private static final int LENGTH_IN_1TRAIL
LENGTH_IN_2TRAILback to summary
private static final int LENGTH_IN_2TRAIL
MAX_SHORT_CHANGEback to summary
private static final int MAX_SHORT_CHANGE
MAX_SHORT_CHANGE_NEW_LENGTHback to summary
private static final int MAX_SHORT_CHANGE_NEW_LENGTH
MAX_SHORT_CHANGE_OLD_LENGTHback to summary
private static final int MAX_SHORT_CHANGE_OLD_LENGTH
MAX_UNCHANGEDback to summary
private static final int MAX_UNCHANGED
MAX_UNCHANGED_LENGTHback to summary
private static final int MAX_UNCHANGED_LENGTH
numChangesback to summary
private int numChanges
SHORT_CHANGE_NUM_MASKback to summary
private static final int SHORT_CHANGE_NUM_MASK
STACK_CAPACITYback to summary
private static final int STACK_CAPACITY

Constructor Detail

Editsback to summary
public Edits()

Constructs an empty object.

ICU Status
Stable since ICU 59.

Method Detail

addReplaceback to summary
public void addReplace(int oldLength, int newLength)

Adds a change edit: a record for a text replacement/insertion/deletion. Normally called from inside ICU string transformation functions, not user code.

ICU Status
Stable since ICU 59.
addUnchangedback to summary
public void addUnchanged(int unchangedLength)

Adds a no-change edit: a record for an unchanged segment of text. Normally called from inside ICU string transformation functions, not user code.

ICU Status
Stable since ICU 59.
appendback to summary
private void append(int r)
getCoarseChangesIteratorback to summary
public Edits.Iterator getCoarseChangesIterator()

Returns an Iterator for coarse-grained change edits (adjacent change edits are treated as one). Can be used to perform simple string updates. Skips no-change edits.

Returns:Edits.Iterator

an Iterator that merges adjacent changes.

ICU Status
Stable since ICU 59.
getCoarseIteratorback to summary
public Edits.Iterator getCoarseIterator()

Returns an Iterator for coarse-grained change and no-change edits (adjacent change edits are treated as one). Can be used to perform simple string updates. Adjacent change edits are treated as one edit.

Returns:Edits.Iterator

an Iterator that merges adjacent changes.

ICU Status
Stable since ICU 59.
getFineChangesIteratorback to summary
public Edits.Iterator getFineChangesIterator()

Returns an Iterator for fine-grained change edits (full granularity of change edits is retained). Can be used for modifying styled text. Skips no-change edits.

Returns:Edits.Iterator

an Iterator that separates adjacent changes.

ICU Status
Stable since ICU 59.
getFineIteratorback to summary
public Edits.Iterator getFineIterator()

Returns an Iterator for fine-grained change and no-change edits (full granularity of change edits is retained). Can be used for modifying styled text.

Returns:Edits.Iterator

an Iterator that separates adjacent changes.

ICU Status
Stable since ICU 59.
growArrayback to summary
private boolean growArray()
hasChangesback to summary
public boolean hasChanges()
Returns:boolean

true if there are any change edits

ICU Status
Stable since ICU 59.
lastUnitback to summary
private int lastUnit()
lengthDeltaback to summary
public int lengthDelta()

How much longer is the new text compared with the old text?

Returns:int

new length minus old length

ICU Status
Stable since ICU 59.
mergeAndAppendback to summary
public Edits mergeAndAppend(Edits ab, Edits bc)

Merges the two input Edits and appends the result to this object.

Consider two string transformations (for example, normalization and case mapping) where each records Edits in addition to writing an output string.
Edits ab reflect how substrings of input string a map to substrings of intermediate string b.
Edits bc reflect how substrings of intermediate string b map to substrings of output string c.
This function merges ab and bc such that the additional edits recorded in this object reflect how substrings of input string a map to substrings of output string c.

If unrelated Edits are passed in where the output string of the first has a different length than the input string of the second, then an IllegalArgumentException is thrown.

Parameters
ab:Edits

reflects how substrings of input string a map to substrings of intermediate string b.

bc:Edits

reflects how substrings of intermediate string b map to substrings of output string c.

Returns:Edits

this, with the merged edits appended

ICU Status
Stable since ICU 60.
numberOfChangesback to summary
public int numberOfChanges()
Returns:int

the number of change edits

ICU Status
Stable since ICU 60.
resetback to summary
public void reset()

Resets the data but may not release memory.

ICU Status
Stable since ICU 59.
setLastUnitback to summary
private void setLastUnit(int last)
com.ibm.icu.text back to summary

public final Class Edits.Iterator

extends Object
Class Inheritance

Access to the list of edits.

At any moment in time, an instance of this class points to a single edit: a "window" into a span of the source string and the corresponding span of the destination string. The source string span starts at sourceIndex() and runs for oldLength() chars; the destination string span starts at destinationIndex() and runs for newLength() chars.

The iterator can be moved between edits using the next(), findSourceIndex(int), and findDestinationIndex(int) methods. Calling any of these methods mutates the iterator to make it point to the corresponding edit.

For more information, see the documentation for Edits.

Note

Although this class is called "Iterator", it does not implement java.util.Iterator.

ICU Status
Stable since ICU 59.
See Also
getCoarseIterator, getFineIterator

Field Summary

Modifier and TypeField and Description
private final char[]
private boolean
private final boolean
private int
private int
private int
private final int
private int
private int
private final boolean
private int
remaining

0 if we are not within compressed equal-length changes.

private int
private int

Constructor Summary

AccessConstructor and Description
private
Iterator(char[] a, int len, boolean oc, boolean crs)

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the current index into the full destination string
destinationIndex
()

The start index of the current span in the destination string; the span has length newLength.

public int

Returns:

destination index; undefined if i is not 0..string length
destinationIndexFromSourceIndex
(int
source index
i
)

Computes the destination index corresponding to the given source index.

public boolean

Returns:

true if the edit for the destination index was found
findDestinationIndex
(int
destination index
i
)

Moves the iterator to the edit that contains the destination index.

private int

Returns:

-1: error or i<0; 0: found; 1: i>=string length
findIndex
(int i, boolean findSource)

public boolean

Returns:

true if the edit for the source index was found
findSourceIndex
(int
source index
i
)

Moves the iterator to the edit that contains the source index.

public boolean

Returns:

true if this edit replaces oldLength() units with newLength() different ones. false if oldLength units remain unchanged.
hasChange
()

Returns whether the edit currently represented by the iterator is a change edit.

public int

Returns:

the number of units in the destination string, if hasChange() is true. Same as oldLength if hasChange() is false.
newLength
()

The length of the current span in the destination string, which starts at destinationIndex, or in the replacement string, which starts at replacementIndex.

public boolean

Returns:

true if there is another edit
next
()

Advances the iterator to the next edit.

private boolean
next(boolean onlyChanges)

private boolean
public int

Returns:

the number of units in the source string which are replaced or remain unchanged.
oldLength
()

The length of the current span in the source string, which starts at sourceIndex.

private boolean
private int
readLength(int head)

public int

Returns:

the current index into the replacement-characters-only string, not counting unchanged spans
replacementIndex
()

The start index of the current span in the replacement string; the span has length newLength.

public int

Returns:

the current index into the source string
sourceIndex
()

The start index of the current span in the source string; the span has length oldLength.

public int

Returns:

source index; undefined if i is not 0..string length
sourceIndexFromDestinationIndex
(int
destination index
i
)

Computes the source index corresponding to the given destination index.

public String

Returns:

a string representation of the object.
toString
()

Overrides java.lang.Object.toString.

A string representation of the current edit represented by the iterator for debugging.
private void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

arrayback to summary
private final char[] array
changedback to summary
private boolean changed
coarseback to summary
private final boolean coarse
destIndexback to summary
private int destIndex
dirback to summary
private int dir
indexback to summary
private int index
lengthback to summary
private final int length
newLength_back to summary
private int newLength_
oldLength_back to summary
private int oldLength_
onlyChanges_back to summary
private final boolean onlyChanges_
remainingback to summary
private int remaining

0 if we are not within compressed equal-length changes. Otherwise the number of remaining changes, including the current one.

replIndexback to summary
private int replIndex
srcIndexback to summary
private int srcIndex

Constructor Detail

Iteratorback to summary
private Iterator(char[] a, int len, boolean oc, boolean crs)

Method Detail

destinationIndexback to summary
public int destinationIndex()

The start index of the current span in the destination string; the span has length newLength.

Returns:int

the current index into the full destination string

ICU Status
Stable since ICU 59.
destinationIndexFromSourceIndexback to summary
public int destinationIndexFromSourceIndex(int i)

Computes the destination index corresponding to the given source index. If the source index is inside a change edit (not at its start), then the destination index at the end of that edit is returned, since there is no information about index mapping inside a change edit.

(This means that indexes to the start and middle of an edit, for example around a grapheme cluster, are mapped to indexes encompassing the entire edit. The alternative, mapping an interior index to the start, would map such an interval to an empty one.)

This operation will usually but not always modify this object. The iterator state after this search is undefined.

Parameters
i:int

source index

Returns:int

destination index; undefined if i is not 0..string length

ICU Status
Stable since ICU 60.
findDestinationIndexback to summary
public boolean findDestinationIndex(int i)

Moves the iterator to the edit that contains the destination index. The destination index may be found in a no-change edit even if normal iteration would skip no-change edits. Normal iteration can continue from a found edit.

The iterator state before this search logically does not matter. (It may affect the performance of the search.)

The iterator state after this search is undefined if the source index is out of bounds for the source string.

Parameters
i:int

destination index

Returns:boolean

true if the edit for the destination index was found

ICU Status
Stable since ICU 60.
findIndexback to summary
private int findIndex(int i, boolean findSource)
Returns:int

-1: error or i<0; 0: found; 1: i>=string length

findSourceIndexback to summary
public boolean findSourceIndex(int i)

Moves the iterator to the edit that contains the source index. The source index may be found in a no-change edit even if normal iteration would skip no-change edits. Normal iteration can continue from a found edit.

The iterator state before this search logically does not matter. (It may affect the performance of the search.)

The iterator state after this search is undefined if the source index is out of bounds for the source string.

Parameters
i:int

source index

Returns:boolean

true if the edit for the source index was found

ICU Status
Stable since ICU 59.
hasChangeback to summary
public boolean hasChange()

Returns whether the edit currently represented by the iterator is a change edit.

Returns:boolean

true if this edit replaces oldLength() units with newLength() different ones. false if oldLength units remain unchanged.

ICU Status
Stable since ICU 59.
newLengthback to summary
public int newLength()

The length of the current span in the destination string, which starts at destinationIndex, or in the replacement string, which starts at replacementIndex.

Returns:int

the number of units in the destination string, if hasChange() is true. Same as oldLength if hasChange() is false.

ICU Status
Stable since ICU 59.
nextback to summary
public boolean next()

Advances the iterator to the next edit.

Returns:boolean

true if there is another edit

ICU Status
Stable since ICU 59.
nextback to summary
private boolean next(boolean onlyChanges)
noNextback to summary
private boolean noNext()
oldLengthback to summary
public int oldLength()

The length of the current span in the source string, which starts at sourceIndex.

Returns:int

the number of units in the source string which are replaced or remain unchanged.

ICU Status
Stable since ICU 59.
previousback to summary
private boolean previous()
readLengthback to summary
private int readLength(int head)
replacementIndexback to summary
public int replacementIndex()

The start index of the current span in the replacement string; the span has length newLength. Well-defined only if the current edit is a change edit.

The replacement string is the concatenation of all substrings of the destination string corresponding to change edits.

This method is intended to be used together with operations that write only replacement characters (e.g., CaseMap#omitUnchangedText()). The source string can then be modified in-place.

Returns:int

the current index into the replacement-characters-only string, not counting unchanged spans

ICU Status
Stable since ICU 59.
sourceIndexback to summary
public int sourceIndex()

The start index of the current span in the source string; the span has length oldLength.

Returns:int

the current index into the source string

ICU Status
Stable since ICU 59.
sourceIndexFromDestinationIndexback to summary
public int sourceIndexFromDestinationIndex(int i)

Computes the source index corresponding to the given destination index. If the destination index is inside a change edit (not at its start), then the source index at the end of that edit is returned, since there is no information about index mapping inside a change edit.

(This means that indexes to the start and middle of an edit, for example around a grapheme cluster, are mapped to indexes encompassing the entire edit. The alternative, mapping an interior index to the start, would map such an interval to an empty one.)

This operation will usually but not always modify this object. The iterator state after this search is undefined.

Parameters
i:int

destination index

Returns:int

source index; undefined if i is not 0..string length

ICU Status
Stable since ICU 60.
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

A string representation of the current edit represented by the iterator for debugging. You should not depend on the contents of the return string; it may change over time.

Returns:String

a string representation of the object.

Annotations
@Override
ICU Status
Stable since ICU 59.
updateNextIndexesback to summary
private void updateNextIndexes()
updatePreviousIndexesback to summary
private void updatePreviousIndexes()