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