1/*
2 * Copyright (C) 2004, 2006, 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "config.h"
27#import "TextBoundaries.h"
28
29#import <wtf/RetainPtr.h>
30
31namespace WebCore {
32
33void findWordBoundary(const UChar* chars, int len, int position, int* start, int* end)
34{
35    NSString* string = [[NSString alloc] initWithCharactersNoCopy:const_cast<unichar*>(chars)
36        length:len freeWhenDone:NO];
37    NSAttributedString* attr = [[NSAttributedString alloc] initWithString:string];
38    NSRange range = [attr doubleClickAtIndex:(position >= len) ? len - 1 : position];
39    [attr release];
40    [string release];
41    *start = range.location;
42    *end = range.location + range.length;
43}
44
45static CFStringTokenizerRef wordStringTokenizer(CFStringRef string)
46{
47    static CFStringTokenizerRef stringTokenizer;
48    if (stringTokenizer)
49        CFStringTokenizerSetString(stringTokenizer, string, CFRangeMake(0, CFStringGetLength(string)));
50    else
51        stringTokenizer = CFStringTokenizerCreate(kCFAllocatorDefault, string, CFRangeMake(0, CFStringGetLength(string)), kCFStringTokenizerUnitWord, 0);
52
53    return stringTokenizer;
54}
55
56int findNextWordFromIndex(const UChar* chars, int len, int position, bool forward)
57{
58    RetainPtr<CFStringRef> string(AdoptCF, CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, chars, len, kCFAllocatorNull));
59    CFStringTokenizerRef stringTokenizer = wordStringTokenizer(string.get());
60
61    CFIndex lastTokenStart = 0;
62    while (true) {
63        CFStringTokenizerTokenType tokenType = CFStringTokenizerAdvanceToNextToken(stringTokenizer);
64        if (tokenType == kCFStringTokenizerTokenNone)
65            return forward ? len : lastTokenStart;
66
67        CFRange tokenRange = CFStringTokenizerGetCurrentTokenRange(stringTokenizer);
68
69        if (!forward && tokenRange.location >= position)
70            return lastTokenStart;
71        if (forward && tokenRange.location + tokenRange.length > position)
72            return tokenRange.location + tokenRange.length;
73
74        lastTokenStart = tokenRange.location;
75    }
76}
77
78}
79