TextBoundaries.cpp [plain text]
#include "config.h"
#include "TextBoundaries.h"
#include "TextBreakIterator.h"
#include <wtf/text/StringImpl.h>
namespace WebCore {
unsigned endOfFirstWordBoundaryContext(StringView text)
{
unsigned length = text.length();
for (unsigned i = 0; i < length; ) {
unsigned first = i;
UChar32 ch;
U16_NEXT(text, i, length, ch);
if (!requiresContextForWordBoundary(ch))
return first;
}
return length;
}
unsigned startOfLastWordBoundaryContext(StringView text)
{
unsigned length = text.length();
for (unsigned i = length; i > 0; ) {
unsigned last = i;
UChar32 ch;
U16_PREV(text, 0, i, ch);
if (!requiresContextForWordBoundary(ch))
return last;
}
return 0;
}
#if !PLATFORM(COCOA)
int findNextWordFromIndex(StringView text, int position, bool forward)
{
TextBreakIterator* it = wordBreakIterator(text);
if (forward) {
position = textBreakFollowing(it, position);
while (position != TextBreakDone) {
if (static_cast<unsigned>(position) < text.length() && u_isalnum(text[position - 1]))
return position;
position = textBreakFollowing(it, position);
}
return text.length();
} else {
position = textBreakPreceding(it, position);
while (position != TextBreakDone) {
if (position && u_isalnum(text[position]))
return position;
position = textBreakPreceding(it, position);
}
return 0;
}
}
void findWordBoundary(StringView text, int position, int* start, int* end)
{
TextBreakIterator* it = wordBreakIterator(text);
*end = textBreakFollowing(it, position);
if (*end < 0)
*end = textBreakLast(it);
*start = textBreakPrevious(it);
}
void findEndWordBoundary(StringView text, int position, int* end)
{
TextBreakIterator* it = wordBreakIterator(text);
*end = textBreakFollowing(it, position);
if (*end < 0)
*end = textBreakLast(it);
}
#endif // !PLATFORM(COCOA)
}