1/* 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. 3 * Copyright (C) 2009 Dominik Röttsches <dominik.roettsches@access-company.com> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "config.h" 28#include "TextBoundaries.h" 29 30#include "TextBreakIterator.h" 31#include <wtf/text/StringImpl.h> 32 33namespace WebCore { 34 35unsigned endOfFirstWordBoundaryContext(StringView text) 36{ 37 unsigned length = text.length(); 38 for (unsigned i = 0; i < length; ) { 39 unsigned first = i; 40 UChar32 ch; 41 U16_NEXT(text, i, length, ch); 42 if (!requiresContextForWordBoundary(ch)) 43 return first; 44 } 45 return length; 46} 47 48unsigned startOfLastWordBoundaryContext(StringView text) 49{ 50 unsigned length = text.length(); 51 for (unsigned i = length; i > 0; ) { 52 unsigned last = i; 53 UChar32 ch; 54 U16_PREV(text, 0, i, ch); 55 if (!requiresContextForWordBoundary(ch)) 56 return last; 57 } 58 return 0; 59} 60 61#if !PLATFORM(COCOA) 62 63int findNextWordFromIndex(StringView text, int position, bool forward) 64{ 65 TextBreakIterator* it = wordBreakIterator(text); 66 67 if (forward) { 68 position = textBreakFollowing(it, position); 69 while (position != TextBreakDone) { 70 // We stop searching when the character preceeding the break is alphanumeric. 71 if (static_cast<unsigned>(position) < text.length() && u_isalnum(text[position - 1])) 72 return position; 73 74 position = textBreakFollowing(it, position); 75 } 76 77 return text.length(); 78 } else { 79 position = textBreakPreceding(it, position); 80 while (position != TextBreakDone) { 81 // We stop searching when the character following the break is alphanumeric. 82 if (position && u_isalnum(text[position])) 83 return position; 84 85 position = textBreakPreceding(it, position); 86 } 87 88 return 0; 89 } 90} 91 92void findWordBoundary(StringView text, int position, int* start, int* end) 93{ 94 TextBreakIterator* it = wordBreakIterator(text); 95 *end = textBreakFollowing(it, position); 96 if (*end < 0) 97 *end = textBreakLast(it); 98 *start = textBreakPrevious(it); 99} 100 101void findEndWordBoundary(StringView text, int position, int* end) 102{ 103 TextBreakIterator* it = wordBreakIterator(text); 104 *end = textBreakFollowing(it, position); 105 if (*end < 0) 106 *end = textBreakLast(it); 107} 108 109#endif // !PLATFORM(COCOA) 110 111} // namespace WebCore 112