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#ifndef UTextProvider_h
27#define UTextProvider_h
28
29#include <unicode/utext.h>
30
31namespace WebCore {
32
33enum class UTextProviderContext {
34    NoContext,
35    PriorContext,
36    PrimaryContext
37};
38
39inline UTextProviderContext uTextProviderContext(const UText* text, int64_t nativeIndex, UBool forward)
40{
41    if (!text->b || nativeIndex > text->b)
42        return UTextProviderContext::PrimaryContext;
43    if (nativeIndex == text->b)
44        return forward ? UTextProviderContext::PrimaryContext : UTextProviderContext::PriorContext;
45    return UTextProviderContext::PriorContext;
46}
47
48inline void initializeContextAwareUTextProvider(UText* text, const UTextFuncs* funcs, const void* string, unsigned length, const UChar* priorContext, int priorContextLength)
49{
50    text->pFuncs = funcs;
51    text->providerProperties = 1 << UTEXT_PROVIDER_STABLE_CHUNKS;
52    text->context = string;
53    text->p = string;
54    text->a = length;
55    text->q = priorContext;
56    text->b = priorContextLength;
57}
58
59// Shared implementation for the UTextClone function on UTextFuncs.
60
61UText* uTextCloneImpl(UText* destination, const UText* source, UBool deep, UErrorCode* status);
62
63
64// Helpers for the UTextAccess function on UTextFuncs.
65
66inline int64_t uTextAccessPinIndex(int64_t& index, int64_t limit)
67{
68    if (index < 0)
69        index = 0;
70    else if (index > limit)
71        index = limit;
72    return index;
73}
74
75inline bool uTextAccessInChunkOrOutOfRange(UText* text, int64_t nativeIndex, int64_t nativeLength, UBool forward, UBool& isAccessible)
76{
77    if (forward) {
78        if (nativeIndex >= text->chunkNativeStart && nativeIndex < text->chunkNativeLimit) {
79            int64_t offset = nativeIndex - text->chunkNativeStart;
80            // Ensure chunk offset is well formed if computed offset exceeds int32_t range.
81            ASSERT(offset < std::numeric_limits<int32_t>::max());
82            text->chunkOffset = offset < std::numeric_limits<int32_t>::max() ? static_cast<int32_t>(offset) : 0;
83            isAccessible = TRUE;
84            return true;
85        }
86        if (nativeIndex >= nativeLength && text->chunkNativeLimit == nativeLength) {
87            text->chunkOffset = text->chunkLength;
88            isAccessible = FALSE;
89            return true;
90        }
91    } else {
92        if (nativeIndex > text->chunkNativeStart && nativeIndex <= text->chunkNativeLimit) {
93            int64_t offset = nativeIndex - text->chunkNativeStart;
94            // Ensure chunk offset is well formed if computed offset exceeds int32_t range.
95            ASSERT(offset < std::numeric_limits<int32_t>::max());
96            text->chunkOffset = offset < std::numeric_limits<int32_t>::max() ? static_cast<int32_t>(offset) : 0;
97            isAccessible = TRUE;
98            return true;
99        }
100        if (nativeIndex <= 0 && !text->chunkNativeStart) {
101            text->chunkOffset = 0;
102            isAccessible = FALSE;
103            return true;
104        }
105    }
106    return false;
107}
108
109} // namespace WebCore
110
111#endif // UTextProvider_h
112