Qt 4.8
Classes | Public Functions | Properties | List of all members
QStringMatcher Class Reference

The QStringMatcher class holds a sequence of characters that can be quickly matched in a Unicode string. More...

#include <qstringmatcher.h>

Classes

struct  Data
 

Public Functions

Qt::CaseSensitivity caseSensitivity () const
 Returns the case sensitivity setting for this string matcher. More...
 
int indexIn (const QString &str, int from=0) const
 Searches the string str from character position from (default 0, i.e. More...
 
int indexIn (const QChar *str, int length, int from=0) const
 Searches the string starting at str (of length length) from character position from (default 0, i. More...
 
QStringMatcheroperator= (const QStringMatcher &other)
 Assigns the other string matcher to this string matcher. More...
 
QString pattern () const
 Returns the string pattern that this string matcher will search for. More...
 
 QStringMatcher ()
 Constructs an empty string matcher that won't match anything. More...
 
 QStringMatcher (const QString &pattern, Qt::CaseSensitivity cs=Qt::CaseSensitive)
 Constructs a string matcher that will search for pattern, with case sensitivity cs. More...
 
 QStringMatcher (const QChar *uc, int len, Qt::CaseSensitivity cs=Qt::CaseSensitive)
 
 QStringMatcher (const QStringMatcher &other)
 Copies the other string matcher to this string matcher. More...
 
void setCaseSensitivity (Qt::CaseSensitivity cs)
 Sets the case sensitivity setting of this string matcher to cs. More...
 
void setPattern (const QString &pattern)
 Sets the string that this string matcher will search for to pattern. More...
 
 ~QStringMatcher ()
 Destroys the string matcher. More...
 

Properties

union {
   Data   p
 
   uint   q_data [256]
 
}; 
 
QStringMatcherPrivate * d_ptr
 
Qt::CaseSensitivity q_cs
 
QString q_pattern
 

Detailed Description

The QStringMatcher class holds a sequence of characters that can be quickly matched in a Unicode string.

This class is useful when you have a sequence of QChar that you want to repeatedly match against some strings (perhaps in a loop), or when you want to search for the same sequence of characters multiple times in the same string. Using a matcher object and indexIn() is faster than matching a plain QString with QString::indexOf() if repeated matching takes place. This class offers no benefit if you are doing one-off string matches.

Create the QStringMatcher with the QString you want to search for. Then call indexIn() on the QString that you want to search.

See also
QString, QByteArrayMatcher, QRegExp

Definition at line 55 of file qstringmatcher.h.

Constructors and Destructors

◆ QStringMatcher() [1/4]

QStringMatcher::QStringMatcher ( )

Constructs an empty string matcher that won't match anything.

Call setPattern() to give it a pattern to match.

Definition at line 154 of file qstringmatcher.cpp.

156 {
157  qMemSet(q_data, 0, sizeof(q_data));
158 }
uint q_data[256]
Qt::CaseSensitivity q_cs
QStringMatcherPrivate * d_ptr
void * qMemSet(void *dest, int c, size_t n)
Definition: qglobal.cpp:2509

◆ QStringMatcher() [2/4]

QStringMatcher::QStringMatcher ( const QString pattern,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
)

Constructs a string matcher that will search for pattern, with case sensitivity cs.

Call indexIn() to perform a search.

Definition at line 166 of file qstringmatcher.cpp.

167  : d_ptr(0), q_pattern(pattern), q_cs(cs)
168 {
169  p.uc = pattern.unicode();
170  p.len = pattern.size();
171  bm_init_skiptable((const ushort *)p.uc, p.len, p.q_skiptable, cs);
172 }
static void bm_init_skiptable(const ushort *uc, int len, uchar *skiptable, Qt::CaseSensitivity cs)
Qt::CaseSensitivity q_cs
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
QStringMatcherPrivate * d_ptr
unsigned short ushort
Definition: qglobal.h:995

◆ QStringMatcher() [3/4]

QStringMatcher::QStringMatcher ( const QChar uc,
int  length,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
)
Since
4.5

Constructs a string matcher that will search for the pattern referred to by uc with the given length and case sensitivity specified by cs.

Definition at line 181 of file qstringmatcher.cpp.

182  : d_ptr(0), q_cs(cs)
183 {
184  p.uc = uc;
185  p.len = len;
186  bm_init_skiptable((const ushort *)p.uc, len, p.q_skiptable, cs);
187 }
static void bm_init_skiptable(const ushort *uc, int len, uchar *skiptable, Qt::CaseSensitivity cs)
Qt::CaseSensitivity q_cs
QStringMatcherPrivate * d_ptr
unsigned short ushort
Definition: qglobal.h:995

◆ QStringMatcher() [4/4]

QStringMatcher::QStringMatcher ( const QStringMatcher other)

Copies the other string matcher to this string matcher.

Definition at line 192 of file qstringmatcher.cpp.

193  : d_ptr(0)
194 {
195  operator=(other);
196 }
QStringMatcherPrivate * d_ptr
QStringMatcher & operator=(const QStringMatcher &other)
Assigns the other string matcher to this string matcher.

◆ ~QStringMatcher()

QStringMatcher::~QStringMatcher ( )

Destroys the string matcher.

Definition at line 201 of file qstringmatcher.cpp.

202 {
203 }

Functions

◆ caseSensitivity()

Qt::CaseSensitivity QStringMatcher::caseSensitivity ( ) const
inline

Returns the case sensitivity setting for this string matcher.

See also
setCaseSensitivity()

Definition at line 74 of file qstringmatcher.h.

74 { return q_cs; }
Qt::CaseSensitivity q_cs

◆ indexIn() [1/2]

int QStringMatcher::indexIn ( const QString str,
int  from = 0 
) const

Searches the string str from character position from (default 0, i.e.

from the first character), for the string pattern() that was set in the constructor or in the most recent call to setPattern(). Returns the position where the pattern() matched in str, or -1 if no match was found.

See also
setPattern(), setCaseSensitivity()

Definition at line 274 of file qstringmatcher.cpp.

Referenced by QtPrivate::QStringList_filter(), qt_string_count(), and QString::replace().

275 {
276  if (from < 0)
277  from = 0;
278  return bm_find((const ushort *)str.unicode(), str.size(), from,
279  (const ushort *)p.uc, p.len,
280  p.q_skiptable, q_cs);
281 }
Qt::CaseSensitivity q_cs
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
static int bm_find(const ushort *uc, uint l, int index, const ushort *puc, uint pl, const uchar *skiptable, Qt::CaseSensitivity cs)
unsigned short ushort
Definition: qglobal.h:995

◆ indexIn() [2/2]

int QStringMatcher::indexIn ( const QChar str,
int  length,
int  from = 0 
) const

Searches the string starting at str (of length length) from character position from (default 0, i.

Since
4.5

e. from the first character), for the string pattern() that was set in the constructor or in the most recent call to setPattern(). Returns the position where the pattern() matched in str, or -1 if no match was found.

See also
setPattern(), setCaseSensitivity()

Definition at line 298 of file qstringmatcher.cpp.

299 {
300  if (from < 0)
301  from = 0;
302  return bm_find((const ushort *)str, length, from,
303  (const ushort *)p.uc, p.len,
304  p.q_skiptable, q_cs);
305 }
Qt::CaseSensitivity q_cs
static int bm_find(const ushort *uc, uint l, int index, const ushort *puc, uint pl, const uchar *skiptable, Qt::CaseSensitivity cs)
unsigned short ushort
Definition: qglobal.h:995

◆ operator=()

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

Assigns the other string matcher to this string matcher.

Definition at line 208 of file qstringmatcher.cpp.

Referenced by QStringMatcher().

209 {
210  if (this != &other) {
211  q_pattern = other.q_pattern;
212  q_cs = other.q_cs;
213  memcpy(q_data, other.q_data, sizeof(q_data));
214  }
215  return *this;
216 }
uint q_data[256]
Qt::CaseSensitivity q_cs

◆ pattern()

QString QStringMatcher::pattern ( ) const

Returns the string pattern that this string matcher will search for.

See also
setPattern()

Definition at line 244 of file qstringmatcher.cpp.

Referenced by setPattern().

245 {
246  if (!q_pattern.isEmpty())
247  return q_pattern;
248  return QString(p.uc, p.len);
249 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704

◆ setCaseSensitivity()

void QStringMatcher::setCaseSensitivity ( Qt::CaseSensitivity  cs)

Sets the case sensitivity setting of this string matcher to cs.

See also
caseSensitivity(), setPattern(), indexIn()

Definition at line 257 of file qstringmatcher.cpp.

258 {
259  if (cs == q_cs)
260  return;
262  q_cs = cs;
263 }
static void bm_init_skiptable(const ushort *uc, int len, uchar *skiptable, Qt::CaseSensitivity cs)
Qt::CaseSensitivity q_cs
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
unsigned short ushort
Definition: qglobal.h:995

◆ setPattern()

void QStringMatcher::setPattern ( const QString pattern)

Sets the string that this string matcher will search for to pattern.

See also
pattern(), setCaseSensitivity(), indexIn()

Definition at line 224 of file qstringmatcher.cpp.

225 {
226  q_pattern = pattern;
227  p.uc = pattern.unicode();
228  p.len = pattern.size();
229  bm_init_skiptable((const ushort *)pattern.unicode(), pattern.size(), p.q_skiptable, q_cs);
230 }
static void bm_init_skiptable(const ushort *uc, int len, uchar *skiptable, Qt::CaseSensitivity cs)
Qt::CaseSensitivity q_cs
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
QString pattern() const
Returns the string pattern that this string matcher will search for.
unsigned short ushort
Definition: qglobal.h:995

Properties

◆ @86

union { ... }

◆ d_ptr

QStringMatcherPrivate* QStringMatcher::d_ptr
private

Definition at line 77 of file qstringmatcher.h.

◆ p

Data QStringMatcher::p

Definition at line 92 of file qstringmatcher.h.

Referenced by indexIn(), pattern(), QStringMatcher(), setCaseSensitivity(), and setPattern().

◆ q_cs

Qt::CaseSensitivity QStringMatcher::q_cs
private

Definition at line 79 of file qstringmatcher.h.

Referenced by indexIn(), operator=(), setCaseSensitivity(), and setPattern().

◆ q_data

uint QStringMatcher::q_data[256]

Definition at line 91 of file qstringmatcher.h.

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

◆ q_pattern

QString QStringMatcher::q_pattern
private

Definition at line 78 of file qstringmatcher.h.

Referenced by operator=(), pattern(), setCaseSensitivity(), and setPattern().


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