Text ranges and regions

Refer to a sequence of contiguous characters in a text object by defining a text range. Conceptually, a TTextRange instance consists of a set of zero or more contiguous text indexes, bounded by a beginning and an ending text offset. For example, the range describing the text object containing the string abc :

Within TTextRange, a range is defined by the beginning offset and the length. Member functions give you access to information about the indexes contained by the range and the ending offset.

To define a text range, you specify the beginning offset and either the ending offset or the length of the range. Creating an empty text range results in a range of length zero (equivalent to an insertion point) placed at offset zero.

TTextRange defines a default maximum range that you can retrieve with the function TTextRange::GetMaximumRange. Use the GetMaximumRange function to specify all the text in a text object without having to first determine its length.

To simultaneously reference multiple text ranges, use a TTextRegion object. A text region object describes an arbitrary one-dimensional text region, consisting of one or more text ranges that might or might not be contiguous.

NOTE You cannot add a zero-length text range to a TTextRegion instance.


You create text range and text region objects independently of a text object, and then use them in conjunction with a text object to operate on specific text data. Text ranges and regions represent only the bounds of substrings. They do not contain any text data themselves.

TTextRegion geometry functions

Unless you copy an existing text region object, you construct a text region object that contains a single text range. (Following the protocol of the text range class, creating an empty text region results in a single empty text range placed at offset zero.) You can then use the TTextRegion region geometry functions to produce a new region from various combinations of two regions. Discontiguous regions are created using these geometry functions.

This table shows the results of the text region geometry functions operating on the text region this, containing the range bounded by offsets 0 and 21 (0, 21), and the text region that, containing the range bounded by offsets 15 and 35
(15, 35).

TTextRegion function Result
Add that to this this contains the range (0, 35)
Subtract that from this this contains the range (0, 14)
Intersect this and that this contains the range (15, 21)
ExclusiveOr this and that this contains the ranges (0, 15) and (21, 35)
Invert this this contains the range from offset 21 to the last offset in the current domain

NOTE Although the text region geometry functions are similar to graphics display region geometry functions, they differ in that text regions map into one-dimensional character arrays and graphics regions map into two- or three-dimensional arrays of display space.

The geometry functions are useful for text editing applications that often need to simultaneously represent multiple discontiguous text ranges--for example, to create discontiguous selections. Use the TTextRegionIterator class to sequentially iterate through the nonzero ranges within a text region object. This iterator is read-only.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker