TextBoundaries.cpp [plain text]
#include "config.h"
#include "TextBoundaries.h"
#include "NotImplemented.h"
#include <QString>
#include <QChar>
#include <QDebug>
#include <stdio.h>
#if QT_VERSION >= 0x040400
#include <qtextboundaryfinder.h>
namespace WebCore
{
int findNextWordFromIndex(UChar const* buffer, int len, int position, bool forward)
{
QString str(reinterpret_cast<QChar const*>(buffer), len);
QTextBoundaryFinder iterator(QTextBoundaryFinder::Word, str);
iterator.setPosition(position >= len ? len - 1 : position);
if (forward) {
int pos = iterator.toNextBoundary();
while (pos > 0) {
if (QChar(buffer[pos-1]).isLetterOrNumber())
return pos;
pos = iterator.toNextBoundary();
}
return len;
} else {
int pos = iterator.toPreviousBoundary();
while (pos > 0) {
if (QChar(buffer[pos]).isLetterOrNumber())
return pos;
pos = iterator.toPreviousBoundary();
}
return 0;
}
}
void findWordBoundary(UChar const* buffer, int len, int position, int* start, int* end)
{
QString str(reinterpret_cast<QChar const*>(buffer), len);
QTextBoundaryFinder iterator(QTextBoundaryFinder::Word, str);
iterator.setPosition(position);
*start = position > 0 ? iterator.toPreviousBoundary() : 0;
*end = position == len ? len : iterator.toNextBoundary();
}
}
#else
namespace WebCore
{
int findNextWordFromIndex(UChar const* buffer, int len, int position, bool forward)
{
QString str(reinterpret_cast<QChar const*>(buffer), len);
notImplemented();
return 0;
}
void findWordBoundary(UChar const* buffer, int len, int position, int* start, int* end)
{
QString str(reinterpret_cast<QChar const*>(buffer), len);
if (position > str.length()) {
*start = 0;
*end = 0;
return;
}
int currentPosition = position - 1;
QString foundWord;
while (currentPosition >= 0 &&
str[currentPosition].isLetter()) {
foundWord.prepend(str[currentPosition]);
--currentPosition;
}
int startPos = (currentPosition < 0) ? 0 : ++currentPosition;
currentPosition = position;
if (str[currentPosition].isLetter()) {
while (str[currentPosition].isLetter()) {
foundWord.append(str[currentPosition]);
++currentPosition;
}
}
*start = startPos;
*end = currentPosition;
}
}
#endif