Qt 4.8
Public Types | Public Functions | Properties | List of all members
QTextBoundaryFinder Class Reference

The QTextBoundaryFinder class provides a way of finding Unicode text boundaries in a string. More...

#include <qtextboundaryfinder.h>

Public Types

enum  BoundaryReason { NotAtBoundary = 0, StartWord = 1, EndWord = 2 }
 
enum  BoundaryType { Grapheme, Word, Line, Sentence }
 

Public Functions

BoundaryReasons boundaryReasons () const
 Returns the reasons for the boundary finder to have chosen the current position as a boundary. More...
 
bool isAtBoundary () const
 Returns true if the object's position() is currently at a valid text boundary. More...
 
bool isValid () const
 Returns true if the text boundary finder is valid; otherwise returns false. More...
 
QTextBoundaryFinderoperator= (const QTextBoundaryFinder &other)
 Assigns the object, other, to another QTextBoundaryFinder object. More...
 
int position () const
 Returns the current position of the QTextBoundaryFinder. More...
 
 QTextBoundaryFinder ()
 Constructs an invalid QTextBoundaryFinder object. More...
 
 QTextBoundaryFinder (const QTextBoundaryFinder &other)
 Copies the QTextBoundaryFinder object, other. More...
 
 QTextBoundaryFinder (BoundaryType type, const QString &string)
 Creates a QTextBoundaryFinder object of type operating on string. More...
 
 QTextBoundaryFinder (BoundaryType type, const QChar *chars, int length, unsigned char *buffer=0, int bufferSize=0)
 Creates a QTextBoundaryFinder object of type operating on chars with length. More...
 
void setPosition (int position)
 Sets the current position of the QTextBoundaryFinder to position. More...
 
QString string () const
 Returns the string the QTextBoundaryFinder object operates on. More...
 
void toEnd ()
 Moves the finder to the end of the string. More...
 
int toNextBoundary ()
 Moves the QTextBoundaryFinder to the next boundary position and returns that position. More...
 
int toPreviousBoundary ()
 Moves the QTextBoundaryFinder to the previous boundary position and returns that position. More...
 
void toStart ()
 Moves the finder to the start of the string. More...
 
BoundaryType type () const
 Returns the type of the QTextBoundaryFinder. More...
 
 ~QTextBoundaryFinder ()
 Destructs the QTextBoundaryFinder object. More...
 

Properties

const QCharchars
 
QTextBoundaryFinderPrivated
 
uint freePrivate: 1
 
int length
 
int pos
 
QString s
 
BoundaryType t
 
uint unused: 31
 

Detailed Description

The QTextBoundaryFinder class provides a way of finding Unicode text boundaries in a string.

Since
4.4
Note
This class or function is reentrant.

QTextBoundaryFinder allows to find Unicode text boundaries in a string, similar to the Unicode text boundary specification (see http://www.unicode.org/reports/tr29/tr29-11.html).

QTextBoundaryFinder can operate on a QString in four possible modes depending on the value of BoundaryType.

Units of Unicode characters that make up what the user thinks of as a character or basic unit of the language are here called Grapheme clusters. The two unicode characters 'A' + diaeresis do for example form one grapheme cluster as the user thinks of them as one character, yet it is in this case represented by two unicode code points.

Word boundaries are there to locate the start and end of what a language considers to be a word.

Line break boundaries give possible places where a line break might happen and sentence boundaries will show the beginning and end of whole sentences.

The first position in a string is always a valid boundary and refers to the position before the first character. The last position at the length of the string is also valid and refers to the position after the last character.

Definition at line 56 of file qtextboundaryfinder.h.

Enumerations

◆ BoundaryReason

  • NotAtBoundary The boundary finder is not at a boundary position.
  • StartWord The boundary finder is at the start of a word.
  • EndWord The boundary finder is at the end of a word.
Enumerator
NotAtBoundary 
StartWord 
EndWord 

Definition at line 71 of file qtextboundaryfinder.h.

◆ BoundaryType

  • Grapheme Finds a grapheme which is the smallest boundary. It including letters, punctation marks, numerals and more.
  • Word Finds a word.
  • Line Finds possible positions for breaking the text into multiple lines.
  • Sentence Finds sentence boundaries. These include periods, question marks etc.
Enumerator
Grapheme 
Word 
Line 
Sentence 

Definition at line 64 of file qtextboundaryfinder.h.

Constructors and Destructors

◆ QTextBoundaryFinder() [1/4]

QTextBoundaryFinder::QTextBoundaryFinder ( )

Constructs an invalid QTextBoundaryFinder object.

Definition at line 167 of file qtextboundaryfinder.cpp.

168  : t(Grapheme)
169  , chars(0)
170  , length(0)
171  , freePrivate(true)
172  , d(0)
173 {
174 }
QTextBoundaryFinderPrivate * d

◆ QTextBoundaryFinder() [2/4]

QTextBoundaryFinder::QTextBoundaryFinder ( const QTextBoundaryFinder other)

Copies the QTextBoundaryFinder object, other.

Definition at line 179 of file qtextboundaryfinder.cpp.

180  : t(other.t)
181  , s(other.s)
182  , chars(other.chars)
183  , length(other.length)
184  , pos(other.pos)
185  , freePrivate(true)
186 {
187  d = (QTextBoundaryFinderPrivate *) malloc(length*sizeof(HB_CharAttributes));
188  Q_CHECK_PTR(d);
189  memcpy(d, other.d, length*sizeof(HB_CharAttributes));
190 }
QTextBoundaryFinderPrivate * d
#define Q_CHECK_PTR(p)
Definition: qglobal.h:1853

◆ ~QTextBoundaryFinder()

QTextBoundaryFinder::~QTextBoundaryFinder ( )

Destructs the QTextBoundaryFinder object.

Definition at line 219 of file qtextboundaryfinder.cpp.

220 {
221  if (freePrivate)
222  free(d);
223 }
QTextBoundaryFinderPrivate * d

◆ QTextBoundaryFinder() [3/4]

QTextBoundaryFinder::QTextBoundaryFinder ( BoundaryType  type,
const QString string 
)

Creates a QTextBoundaryFinder object of type operating on string.

Definition at line 228 of file qtextboundaryfinder.cpp.

229  : t(type)
230  , s(string)
231  , chars(string.unicode())
232  , length(string.length())
233  , pos(0)
234  , freePrivate(true)
235 {
236  d = (QTextBoundaryFinderPrivate *) malloc(length*sizeof(HB_CharAttributes));
237  Q_CHECK_PTR(d);
238  init(t, chars, length, d->attributes);
239 }
QTextBoundaryFinderPrivate * d
static void init(QTextBoundaryFinder::BoundaryType type, const QChar *chars, int length, HB_CharAttributes *attributes)
BoundaryType type() const
Returns the type of the QTextBoundaryFinder.
#define Q_CHECK_PTR(p)
Definition: qglobal.h:1853
HB_CharAttributes attributes[1]

◆ QTextBoundaryFinder() [4/4]

QTextBoundaryFinder::QTextBoundaryFinder ( BoundaryType  type,
const QChar chars,
int  length,
unsigned char *  buffer = 0,
int  bufferSize = 0 
)

Creates a QTextBoundaryFinder object of type operating on chars with length.

buffer is an optional working buffer of size bufferSize you can pass to the QTextBoundaryFinder. If the buffer is large enough to hold the working data required, it will use this instead of allocating its own buffer.

Warning
QTextBoundaryFinder does not create a copy of chars. It is the application programmer's responsibility to ensure the array is allocated for as long as the QTextBoundaryFinder object stays alive. The same applies to buffer.

Definition at line 254 of file qtextboundaryfinder.cpp.

255  : t(type)
256  , chars(chars)
257  , length(length)
258  , pos(0)
259 {
260  if (buffer && (uint)bufferSize >= length*sizeof(HB_CharAttributes)) {
261  d = (QTextBoundaryFinderPrivate *)buffer;
262  freePrivate = false;
263  } else {
264  d = (QTextBoundaryFinderPrivate *) malloc(length*sizeof(HB_CharAttributes));
265  Q_CHECK_PTR(d);
266  freePrivate = true;
267  }
268  init(t, chars, length, d->attributes);
269 }
QTextBoundaryFinderPrivate * d
static void init(QTextBoundaryFinder::BoundaryType type, const QChar *chars, int length, HB_CharAttributes *attributes)
unsigned int uint
Definition: qglobal.h:996
BoundaryType type() const
Returns the type of the QTextBoundaryFinder.
#define Q_CHECK_PTR(p)
Definition: qglobal.h:1853
HB_CharAttributes attributes[1]

Functions

◆ boundaryReasons()

QTextBoundaryFinder::BoundaryReasons QTextBoundaryFinder::boundaryReasons ( ) const

Returns the reasons for the boundary finder to have chosen the current position as a boundary.

Definition at line 458 of file qtextboundaryfinder.cpp.

Referenced by QDeclarativeTextInput::moveCursorSelection().

459 {
460  if (!d)
461  return NotAtBoundary;
462  if (! isAtBoundary())
463  return NotAtBoundary;
464  if (pos == 0) {
465  if (d->attributes[pos].whiteSpace)
466  return NotAtBoundary;
467  return StartWord;
468  }
469  if (pos == length) {
470  if (d->attributes[length-1].whiteSpace)
471  return NotAtBoundary;
472  return EndWord;
473  }
474 
475  const bool nextIsSpace = d->attributes[pos].whiteSpace;
476  const bool prevIsSpace = d->attributes[pos - 1].whiteSpace;
477 
478  if (prevIsSpace && !nextIsSpace)
479  return StartWord;
480  else if (!prevIsSpace && nextIsSpace)
481  return EndWord;
482  else if (!prevIsSpace && !nextIsSpace)
483  return BoundaryReasons(StartWord | EndWord);
484  else
485  return NotAtBoundary;
486 }
QTextBoundaryFinderPrivate * d
bool isAtBoundary() const
Returns true if the object&#39;s position() is currently at a valid text boundary.
HB_CharAttributes attributes[1]

◆ isAtBoundary()

bool QTextBoundaryFinder::isAtBoundary ( ) const

Returns true if the object's position() is currently at a valid text boundary.

Definition at line 434 of file qtextboundaryfinder.cpp.

Referenced by boundaryReasons(), qTextAtOffsetFromString(), and qTextBeforeOffsetFromString().

435 {
436  if (!d || pos < 0)
437  return false;
438 
439  if (pos == length)
440  return true;
441 
442  switch(t) {
443  case Grapheme:
444  return d->attributes[pos].charStop;
445  case Word:
446  return d->attributes[pos].wordBoundary;
447  case Line:
448  return (pos > 0) ? d->attributes[pos-1].lineBreakType >= HB_Break : true;
449  case Sentence:
450  return d->attributes[pos].sentenceBoundary;
451  }
452  return false;
453 }
QTextBoundaryFinderPrivate * d
HB_CharAttributes attributes[1]

◆ isValid()

bool QTextBoundaryFinder::isValid ( ) const
inline

Returns true if the text boundary finder is valid; otherwise returns false.

A default QTextBoundaryFinder is invalid.

Definition at line 82 of file qtextboundaryfinder.h.

82 { return d; }
QTextBoundaryFinderPrivate * d

◆ operator=()

QTextBoundaryFinder & QTextBoundaryFinder::operator= ( const QTextBoundaryFinder other)

Assigns the object, other, to another QTextBoundaryFinder object.

Definition at line 195 of file qtextboundaryfinder.cpp.

196 {
197  if (&other == this)
198  return *this;
199 
200  t = other.t;
201  s = other.s;
202  chars = other.chars;
203  length = other.length;
204  pos = other.pos;
205 
207  realloc(freePrivate ? d : 0, length*sizeof(HB_CharAttributes));
208  Q_CHECK_PTR(newD);
209  freePrivate = true;
210  d = newD;
211  memcpy(d, other.d, length*sizeof(HB_CharAttributes));
212 
213  return *this;
214 }
QTextBoundaryFinderPrivate * d
#define Q_CHECK_PTR(p)
Definition: qglobal.h:1853

◆ position()

int QTextBoundaryFinder::position ( ) const

Returns the current position of the QTextBoundaryFinder.

The range is from 0 (the beginning of the string) to the length of the string inclusive.

See also
setPosition()

Definition at line 299 of file qtextboundaryfinder.cpp.

Referenced by QDeclarativeTextInput::moveCursorSelection(), qTextAfterOffsetFromString(), qTextAtOffsetFromString(), and qTextBeforeOffsetFromString().

300 {
301  return pos;
302 }

◆ setPosition()

void QTextBoundaryFinder::setPosition ( int  position)

Sets the current position of the QTextBoundaryFinder to position.

If position is out of bounds, it will be bound to only valid positions. In this case, valid positions are from 0 to the length of the string inclusive.

See also
position()

Definition at line 313 of file qtextboundaryfinder.cpp.

Referenced by QDeclarativeTextInput::moveCursorSelection(), qTextAfterOffsetFromString(), qTextAtOffsetFromString(), and qTextBeforeOffsetFromString().

314 {
315  pos = qBound(0, position, length);
316 }
int position() const
Returns the current position of the QTextBoundaryFinder.
Q_DECL_CONSTEXPR const T & qBound(const T &min, const T &val, const T &max)
Definition: qglobal.h:1219

◆ string()

QString QTextBoundaryFinder::string ( ) const

Returns the string the QTextBoundaryFinder object operates on.

Definition at line 338 of file qtextboundaryfinder.cpp.

339 {
340  if (chars == s.unicode() && length == s.length())
341  return s;
342  return QString(chars, length);
343 }
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
The QString class provides a Unicode character string.
Definition: qstring.h:83
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706

◆ toEnd()

void QTextBoundaryFinder::toEnd ( )

Moves the finder to the end of the string.

This is equivalent to setPosition(string.length()).

See also
setPosition(), position()

Definition at line 286 of file qtextboundaryfinder.cpp.

◆ toNextBoundary()

int QTextBoundaryFinder::toNextBoundary ( )

Moves the QTextBoundaryFinder to the next boundary position and returns that position.

Returns -1 if there is no next boundary.

Definition at line 351 of file qtextboundaryfinder.cpp.

Referenced by QDeclarativeTextInput::moveCursorSelection(), qTextAfterOffsetFromString(), qTextAtOffsetFromString(), and qTextBeforeOffsetFromString().

352 {
353  if (!d) {
354  pos = -1;
355  return pos;
356  }
357 
358  if (pos < 0 || pos >= length) {
359  pos = -1;
360  return pos;
361  }
362  ++pos;
363  if (pos == length)
364  return pos;
365 
366  switch(t) {
367  case Grapheme:
368  while (pos < length && !d->attributes[pos].charStop)
369  ++pos;
370  break;
371  case Word:
372  while (pos < length && !d->attributes[pos].wordBoundary)
373  ++pos;
374  break;
375  case Sentence:
376  while (pos < length && !d->attributes[pos].sentenceBoundary)
377  ++pos;
378  break;
379  case Line:
380  Q_ASSERT(pos);
381  while (pos < length && d->attributes[pos-1].lineBreakType < HB_Break)
382  ++pos;
383  break;
384  }
385 
386  return pos;
387 }
QTextBoundaryFinderPrivate * d
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

◆ toPreviousBoundary()

int QTextBoundaryFinder::toPreviousBoundary ( )

Moves the QTextBoundaryFinder to the previous boundary position and returns that position.

Returns -1 if there is no previous boundary.

Definition at line 394 of file qtextboundaryfinder.cpp.

Referenced by QDeclarativeTextInput::moveCursorSelection(), qTextAtOffsetFromString(), and qTextBeforeOffsetFromString().

395 {
396  if (!d) {
397  pos = -1;
398  return pos;
399  }
400 
401  if (pos <= 0 || pos > length) {
402  pos = -1;
403  return pos;
404  }
405  --pos;
406  if (pos == 0)
407  return pos;
408 
409  switch(t) {
410  case Grapheme:
411  while (pos > 0 && !d->attributes[pos].charStop)
412  --pos;
413  break;
414  case Word:
415  while (pos > 0 && !d->attributes[pos].wordBoundary)
416  --pos;
417  break;
418  case Sentence:
419  while (pos > 0 && !d->attributes[pos].sentenceBoundary)
420  --pos;
421  break;
422  case Line:
423  while (pos > 0 && d->attributes[pos-1].lineBreakType < HB_Break)
424  --pos;
425  break;
426  }
427 
428  return pos;
429 }
QTextBoundaryFinderPrivate * d
HB_CharAttributes attributes[1]

◆ toStart()

void QTextBoundaryFinder::toStart ( )

Moves the finder to the start of the string.

This is equivalent to setPosition(0).

See also
setPosition(), position()

Definition at line 276 of file qtextboundaryfinder.cpp.

277 {
278  pos = 0;
279 }

◆ type()

QTextBoundaryFinder::BoundaryType QTextBoundaryFinder::type ( ) const
inline

Returns the type of the QTextBoundaryFinder.

Definition at line 84 of file qtextboundaryfinder.h.

84 { return t; }

Properties

◆ chars

const QChar* QTextBoundaryFinder::chars
private

Definition at line 101 of file qtextboundaryfinder.h.

Referenced by operator=(), QTextBoundaryFinder(), and string().

◆ d

QTextBoundaryFinderPrivate* QTextBoundaryFinder::d
private

◆ freePrivate

uint QTextBoundaryFinder::freePrivate
private

Definition at line 104 of file qtextboundaryfinder.h.

Referenced by operator=(), QTextBoundaryFinder(), and ~QTextBoundaryFinder().

◆ length

int QTextBoundaryFinder::length
private

◆ pos

int QTextBoundaryFinder::pos
private

◆ s

QString QTextBoundaryFinder::s
private

Definition at line 100 of file qtextboundaryfinder.h.

Referenced by operator=(), and string().

◆ t

BoundaryType QTextBoundaryFinder::t
private

◆ unused

uint QTextBoundaryFinder::unused
private

Definition at line 105 of file qtextboundaryfinder.h.


The documentation for this class was generated from the following files: