Home · All Classes · Main Classes · Deprecated
Public Member Functions

MStringSearch Class Reference

implements language-sensitive text searching More...

List of all members.

Public Member Functions

 MStringSearch (const QString &pattern, const QString &text, const MLocale &locale, MBreakIterator::Type breakIteratorType=MBreakIterator::CharacterIterator)
virtual ~MStringSearch ()
QString errorString () const
void setLocale (const MLocale &locale)
void setText (const QString &text)
QString text () const
void setPattern (const QString &pattern)
QString pattern () const
void setCollatorStrength (MLocale::CollatorStrength collatorStrength)
MLocale::CollatorStrength collatorStrength () const
void setAlternateHandlingShifted (bool isShifted)
bool alternateHandlingShifted () const
int first ()
int last ()
int next ()
int previous ()
int offset () const
void setOffset (int offset)
int matchedStart () const
int matchedLength () const
QString matchedText () const

Detailed Description

implements language-sensitive text searching

By default, MStringSearch tries to be very lenient, i.e. by default it ignores accent differences, case differences, white space and punctuation and matches substrings anywhere in the text. But there are options to select stricter matching as well.

Examples:

In English locale, the search pattern “aa” matches by default all uppercase and lowercase variants and accent variants of “aa”, i.e. it matches “Aa”, “aÁ”, ...:

 QString pattern("aa");
 QString text("Aaland Åland Ááland");
 MLocale locale("en_US");
 int start, length;
 QString match;
 MStringSearch stringSearch(pattern, text, locale);
 if (stringSearch.first() != -1) {
    start = stringSearch.matchedStart();   // now contains “0”
    length = stringSearch.matchedLength(); // now contains “2”
    match = stringSearch.matchedText();    // now contains “Aa”
 }
 if (stringSearch.last() != -1) {
    start = stringSearch.matchedStart();   // now contains “13”
    length = stringSearch.matchedLength(); // now contains “2”
    match = stringSearch.matchedText();    // now contains “Áá”
 }

In Danish locale, “aa” is considered as a character on its own with only a tertiary difference to “å”, therefore searching for “aa” matches “aa”, “Aa”, “aA”, “å”, and “Å”. But it does not match “Áá”

 QString pattern("aa");
 QString text("Aaland Åland Ááland");
 MLocale locale("da_DK");
 int start, length;
 QString match;
 MStringSearch stringSearch(pattern, text, locale);
 if (stringSearch.first() != -1) {
    start = stringSearch.matchedStart();   // now contains “0”
    length = stringSearch.matchedLength(); // now contains “2”
    match = stringSearch.matchedText();    // now contains “Aa”
 }
 if (stringSearch.last() != -1) {
    start = stringSearch.matchedStart();   // now contains “7”
    length = stringSearch.matchedLength(); // now contains “1”
    match = stringSearch.matchedText();    // now contains “Å”
 }

In a Chinese locale which uses pinyin sorting, e.g. “zh_CN”, “zh_SG”, “zh_CN=pinyin”, “zh_TW=pinyin”, ... Chinese characters can be matched via their pinyin transcription.

For example “liu” will match “liu”, “líu”, “liǔ”, “刘”, “柳”, .. and all other Chinese characters which have a pinyin transcription of “liu” (any tone). But if the pattern contains Chinese, it will only match Chinese characters exactly, e.g. the pattern “刘” will only match “刘”, it will not match “líu”, “liǔ”, “柳”, ...

Mixed input will only match the Chinese exactly, i.e. the pattern “liu 刘” will match “liu 刘”, “liu刘”, “líu刘”, ... but will not match “liu 柳”.


Constructor & Destructor Documentation

MStringSearch::MStringSearch ( const QString pattern,
const QString text,
const MLocale locale,
MBreakIterator::Type  breakIteratorType = MBreakIterator::CharacterIterator 
)

constructs a MStringSearch

Parameters:
pattern,: the search string to search for
text,: the text in which the search string is searched
locale,: the locale which determines the language-specific rules
breakIteratorType,: the break iterator type to use

The default for the break iterator type is MBreakIterator::CharacterIterator. Possible choices for the break iterator type are:

See also:
setPattern(const QString &pattern)
setText(const QString &text)
setLocale(const MLocale &locale)
setCollatorStrength(MLocale::CollatorStrength collatorStrength)
setAlternateHandlingShifted(bool isShifted)
MStringSearch::~MStringSearch (  )  [virtual]

destructor for MStringSearch


Member Function Documentation

bool MStringSearch::alternateHandlingShifted (  )  const

gets whether alternate characters are handled shifted or not

See also:
setAlternateHandlingShifted(bool isShifted)
MLocale::CollatorStrength MStringSearch::collatorStrength (  )  const

gets the strength of the collator currently used for searching

See also:
setCollatorStrength(MLocale::CollatorStrength collatorStrength)
QString MStringSearch::errorString (  )  const

text describing the error which occurred during the last action

Example:

 QString pattern("World");
 QString text("Hello world!");
 MLocale locale("en_US");
 MStringSearch stringSearch(pattern, text, locale);
 if(!stringSearch.errorString().isEmpty())
   qWarning() << stringSearch.errorString();
int MStringSearch::first (  ) 

returns the first index at which the search pattern matches in the text returns “-1” if there is no match.

If there is a match, matchedStart(), matchedLength(), and matchedText() can be used to find out what was matched.

See also:
last()
matchedStart()
matchedLength()
matchedText()
int MStringSearch::last (  ) 

returns the last index at which the search pattern matches in the text returns “-1” if there is no match.

If there is a match, matchedStart(), matchedLength(), and matchedText() can be used to find out what was matched.

See also:
first()
matchedStart()
matchedLength()
matchedText()
int MStringSearch::matchedLength (  )  const

returns the start index of a match found by a previous command

Returns the length of the text that was matched by the most recent call to first(), last(), next(), previous().

See also:
first()
last()
next()
previous()
int MStringSearch::matchedStart (  )  const

returns the start index of a match found by a previous command

Returns the start index of the text that was matched by the most recent call to first(), last(), next(), previous().

See also:
first()
last()
next()
previous()
QString MStringSearch::matchedText (  )  const

returns the start index of a match found by a previous command

Returns the text that was matched by the most recent call to first(), last(), next(), previous().

See also:
first()
last()
next()
previous()
int MStringSearch::next (  ) 

moves to the next match

Returns the index of the next point at which the text matches the search pattern, starting from the current position.

The iterator is adjusted so that its current index (as returned by offset()) is the match position if one was found.

Returns “-1” if there no next match is found.

If there is a match, matchedStart(), matchedLength(), and matchedText() can be used to find out what was matched.

See also:
previous()
offset()
setOffset(int offset)
matchedStart()
matchedLength()
matchedText()
int MStringSearch::offset (  )  const

returns the current search index position

See also:
setOffset(int offset)
QString MStringSearch::pattern (  )  const

get the string which is searched for

See also:
setText(const QString &text)
text()
setPattern(const QString &pattern)
int MStringSearch::previous (  ) 

moves to the previous < match

Returns the index of the previous point at which the string text matches the search pattern, starting at the current position.

The iterator is adjusted so that its current index (as returned by offset()) is the match position if one was found.

Returns “-1” if there no previous match is found.

If there is a match, matchedStart(), matchedLength(), and matchedText() can be used to find out what was matched.

See also:
previous()
offset()
setOffset(int offset)
matchedStart()
matchedLength()
matchedText()
void MStringSearch::setAlternateHandlingShifted ( bool  isShifted  ) 

sets whether the alternate characters are handled shifted or not

The default is true.

“true”, i.e. “shifted” basically means that white space and punctuation is ignore when matching.

See also:
alternateHandlingShifted()
void MStringSearch::setCollatorStrength ( MLocale::CollatorStrength  collatorStrength  ) 

set the strength of the collator used for searching

The default strength of MStringSearch is MLocale::CollatorStrengthPrimary. Therefore, accent differences and case differences are ignored by default.

Accent differences usually have secondary strength and case differences usually have tertiary strength. Therefore, setting secondary strengths would not ignore accent differences anymore but still ignore case differences, setting tertiary strength would neither ignore accent differences nor case differences.

See also:
collatorStrength()
void MStringSearch::setLocale ( const MLocale locale  ) 

sets the locale used for the language-sensitive text searching

void MStringSearch::setOffset ( int  offset  ) 

sets the current search index position

See also:
offset()
void MStringSearch::setPattern ( const QString pattern  ) 

sets the string to search in the text

See also:
text()
setText(const QString &text)
pattern()
void MStringSearch::setText ( const QString text  ) 

sets text in which the pattern is searched

See also:
text()
setPattern(const QString &pattern)
pattern()
QString MStringSearch::text (  )  const

get the text in which the pattern is searched

See also:
setText(const QString &text)
setPattern(const QString &pattern)
pattern()

Copyright © 2010 Nokia Corporation
MeeGo Touch