1/*
2 * Copyright (C) 2014 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#include "config.h"
27#include "UTextProviderUTF16.h"
28
29#include "UTextProvider.h"
30
31namespace WebCore {
32
33// UTF16ContextAware provider
34
35static UText* uTextUTF16ContextAwareClone(UText*, const UText*, UBool, UErrorCode*);
36static int64_t uTextUTF16ContextAwareNativeLength(UText*);
37static UBool uTextUTF16ContextAwareAccess(UText*, int64_t, UBool);
38static int32_t uTextUTF16ContextAwareExtract(UText*, int64_t, int64_t, UChar*, int32_t, UErrorCode*);
39static void uTextUTF16ContextAwareClose(UText*);
40
41static const struct UTextFuncs textUTF16ContextAwareFuncs = {
42    sizeof(UTextFuncs),
43    0,
44    0,
45    0,
46    uTextUTF16ContextAwareClone,
47    uTextUTF16ContextAwareNativeLength,
48    uTextUTF16ContextAwareAccess,
49    uTextUTF16ContextAwareExtract,
50    nullptr,
51    nullptr,
52    nullptr,
53    nullptr,
54    uTextUTF16ContextAwareClose,
55    nullptr,
56    nullptr,
57    nullptr
58};
59
60static inline UTextProviderContext textUTF16ContextAwareGetCurrentContext(const UText* text)
61{
62    if (!text->chunkContents)
63        return UTextProviderContext::NoContext;
64    return text->chunkContents == text->p ? UTextProviderContext::PrimaryContext : UTextProviderContext::PriorContext;
65}
66
67static void textUTF16ContextAwareMoveInPrimaryContext(UText* text, int64_t nativeIndex, int64_t nativeLength, UBool forward)
68{
69    ASSERT(text->chunkContents == text->p);
70    ASSERT_UNUSED(forward, forward ? nativeIndex >= text->b : nativeIndex > text->b);
71    ASSERT_UNUSED(forward, forward ? nativeIndex < nativeLength : nativeIndex <= nativeLength);
72    text->chunkNativeStart = text->b;
73    text->chunkNativeLimit = nativeLength;
74    int64_t length = text->chunkNativeLimit - text->chunkNativeStart;
75    // Ensure chunk length is well defined if computed length exceeds int32_t range.
76    ASSERT(length < std::numeric_limits<int32_t>::max());
77    text->chunkLength = length < std::numeric_limits<int32_t>::max() ? static_cast<int32_t>(length) : 0;
78    text->nativeIndexingLimit = text->chunkLength;
79    int64_t offset = nativeIndex - text->chunkNativeStart;
80    // Ensure chunk offset is well defined if computed offset exceeds int32_t range or chunk length.
81    ASSERT(offset < std::numeric_limits<int32_t>::max());
82    text->chunkOffset = std::min(offset < std::numeric_limits<int32_t>::max() ? static_cast<int32_t>(offset) : 0, text->chunkLength);
83}
84
85static void textUTF16ContextAwareSwitchToPrimaryContext(UText* text, int64_t nativeIndex, int64_t nativeLength, UBool forward)
86{
87    ASSERT(!text->chunkContents || text->chunkContents == text->q);
88    text->chunkContents = static_cast<const UChar*>(text->p);
89    textUTF16ContextAwareMoveInPrimaryContext(text, nativeIndex, nativeLength, forward);
90}
91
92static void textUTF16ContextAwareMoveInPriorContext(UText* text, int64_t nativeIndex, int64_t nativeLength, UBool forward)
93{
94    ASSERT(text->chunkContents == text->q);
95    ASSERT(forward ? nativeIndex < text->b : nativeIndex <= text->b);
96    ASSERT_UNUSED(nativeLength, forward ? nativeIndex < nativeLength : nativeIndex <= nativeLength);
97    ASSERT_UNUSED(forward, forward ? nativeIndex < nativeLength : nativeIndex <= nativeLength);
98    text->chunkNativeStart = 0;
99    text->chunkNativeLimit = text->b;
100    text->chunkLength = text->b;
101    text->nativeIndexingLimit = text->chunkLength;
102    int64_t offset = nativeIndex - text->chunkNativeStart;
103    // Ensure chunk offset is well defined if computed offset exceeds int32_t range or chunk length.
104    ASSERT(offset < std::numeric_limits<int32_t>::max());
105    text->chunkOffset = std::min(offset < std::numeric_limits<int32_t>::max() ? static_cast<int32_t>(offset) : 0, text->chunkLength);
106}
107
108static void textUTF16ContextAwareSwitchToPriorContext(UText* text, int64_t nativeIndex, int64_t nativeLength, UBool forward)
109{
110    ASSERT(!text->chunkContents || text->chunkContents == text->p);
111    text->chunkContents = static_cast<const UChar*>(text->q);
112    textUTF16ContextAwareMoveInPriorContext(text, nativeIndex, nativeLength, forward);
113}
114
115static UText* uTextUTF16ContextAwareClone(UText* destination, const UText* source, UBool deep, UErrorCode* status)
116{
117    return uTextCloneImpl(destination, source, deep, status);
118}
119
120static inline int64_t uTextUTF16ContextAwareNativeLength(UText* text)
121{
122    return text->a + text->b;
123}
124
125static UBool uTextUTF16ContextAwareAccess(UText* text, int64_t nativeIndex, UBool forward)
126{
127    if (!text->context)
128        return FALSE;
129    int64_t nativeLength = uTextUTF16ContextAwareNativeLength(text);
130    UBool isAccessible;
131    if (uTextAccessInChunkOrOutOfRange(text, nativeIndex, nativeLength, forward, isAccessible))
132        return isAccessible;
133    nativeIndex = uTextAccessPinIndex(nativeIndex, nativeLength);
134    UTextProviderContext currentContext = textUTF16ContextAwareGetCurrentContext(text);
135    UTextProviderContext newContext = uTextProviderContext(text, nativeIndex, forward);
136    ASSERT(newContext != UTextProviderContext::NoContext);
137    if (newContext == currentContext) {
138        if (currentContext == UTextProviderContext::PrimaryContext)
139            textUTF16ContextAwareMoveInPrimaryContext(text, nativeIndex, nativeLength, forward);
140        else
141            textUTF16ContextAwareMoveInPriorContext(text, nativeIndex, nativeLength, forward);
142    } else if (newContext == UTextProviderContext::PrimaryContext)
143        textUTF16ContextAwareSwitchToPrimaryContext(text, nativeIndex, nativeLength, forward);
144    else {
145        ASSERT(newContext == UTextProviderContext::PriorContext);
146        textUTF16ContextAwareSwitchToPriorContext(text, nativeIndex, nativeLength, forward);
147    }
148    return TRUE;
149}
150
151static int32_t uTextUTF16ContextAwareExtract(UText*, int64_t, int64_t, UChar*, int32_t, UErrorCode* errorCode)
152{
153    // In the present context, this text provider is used only with ICU functions
154    // that do not perform an extract operation.
155    ASSERT_NOT_REACHED();
156    *errorCode = U_UNSUPPORTED_ERROR;
157    return 0;
158}
159
160static void uTextUTF16ContextAwareClose(UText* text)
161{
162    text->context = nullptr;
163}
164
165UText* openUTF16ContextAwareUTextProvider(UText* text, const UChar* string, unsigned length, const UChar* priorContext, int priorContextLength, UErrorCode* status)
166{
167    if (U_FAILURE(*status))
168        return 0;
169    if (!string || length > static_cast<unsigned>(std::numeric_limits<int32_t>::max())) {
170        *status = U_ILLEGAL_ARGUMENT_ERROR;
171        return 0;
172    }
173    text = utext_setup(text, 0, status);
174    if (U_FAILURE(*status)) {
175        ASSERT(!text);
176        return 0;
177    }
178
179    initializeContextAwareUTextProvider(text, &textUTF16ContextAwareFuncs, string, length, priorContext, priorContextLength);
180    return text;
181}
182
183} // namespace WebCore
184