Example #1
0
static bool
finderNextWord(QTextBoundaryFinder &finder, QString &word, int &bufferStart)
{
    QTextBoundaryFinder::BoundaryReasons boundary = finder.boundaryReasons();
    int start = finder.position(), end = finder.position();
    bool inWord = (boundary & QTextBoundaryFinder::StartWord) != 0;
    while (finder.toNextBoundary() > 0) {
        boundary = finder.boundaryReasons();
        if ((boundary & QTextBoundaryFinder::EndWord) && inWord) {
            end = finder.position();
            QString str = finder.string().mid(start, end - start);
            if (isValidWord(str)) {
                word = str;
                bufferStart = start;
#if 0
                kDebug() << "Word at " << start << " word = '"
                         <<  str << "', len = " << str.length();
#endif
                return true;
            }
            inWord = false;
        }
        if ((boundary & QTextBoundaryFinder::StartWord)) {
            start = finder.position();
            inWord = true;
        }
    }
    return false;
}
Example #2
0
	void SpellcheckerExt::checkSpellingOfString (const QString& word,
			int *misspellingLocation, int *misspellingLength)
	{
		if (!misspellingLength || !misspellingLocation)
			return;

		*misspellingLocation = 0;
		*misspellingLength = 0;

		QTextBoundaryFinder finder { QTextBoundaryFinder::Word, word };
		bool isInWord = false;
		int start = -1;
		int end = -1;
		do
		{
			const auto reasons = finder.boundaryReasons ();

			if (IsWordStart (reasons, finder.type ()))
			{
				start = finder.position ();
				isInWord = true;
			}

			if (isInWord && IsWordEnd (reasons, finder.type ()))
			{
				end = finder.position ();

				const auto& str = word.mid (start, end - start);
				if (std::none_of (Spellcheckers_.begin (), Spellcheckers_.end (),
						[&str] (const ISpellChecker_ptr& sc) { return sc->IsCorrect (str); }))
				{
					*misspellingLocation = start;
					*misspellingLength = end - start;
					return;
				}

				isInWord = false;
			}
		}
		while (finder.toNextBoundary () > 0);
	}
Example #3
0
static bool finderWordAt(QTextBoundaryFinder &finder,
                         int at,
                         QString &word, int &bufferStart)
{
    int oldPosition = finder.position();

    finder.setPosition(at);
    if (!finder.isAtBoundary() || (finder.boundaryReasons() & QTextBoundaryFinder::EndWord)) {
        if (finder.toPreviousBoundary() <= 0) {
            /* QTextBoundaryIterator doesn't consider start of the string
             * a boundary so we need to rewind to the beginning to catch
             * the first word */
            if (at > 0 && finder.string().length() > 0) {
                finder.toStart();
            } else
                return false;
        }
    }
    bool ret = finderNextWord(finder, word, bufferStart);
    finder.setPosition(oldPosition);
    return ret;
}