#ifndef UTextProvider_h
#define UTextProvider_h
#include <unicode/utext.h>
namespace WTF {
enum class UTextProviderContext {
NoContext,
PriorContext,
PrimaryContext
};
inline UTextProviderContext uTextProviderContext(const UText* text, int64_t nativeIndex, UBool forward)
{
if (!text->b || nativeIndex > text->b)
return UTextProviderContext::PrimaryContext;
if (nativeIndex == text->b)
return forward ? UTextProviderContext::PrimaryContext : UTextProviderContext::PriorContext;
return UTextProviderContext::PriorContext;
}
inline void initializeContextAwareUTextProvider(UText* text, const UTextFuncs* funcs, const void* string, unsigned length, const UChar* priorContext, int priorContextLength)
{
text->pFuncs = funcs;
text->providerProperties = 1 << UTEXT_PROVIDER_STABLE_CHUNKS;
text->context = string;
text->p = string;
text->a = length;
text->q = priorContext;
text->b = priorContextLength;
}
UText* uTextCloneImpl(UText* destination, const UText* source, UBool deep, UErrorCode* status);
inline int64_t uTextAccessPinIndex(int64_t& index, int64_t limit)
{
if (index < 0)
index = 0;
else if (index > limit)
index = limit;
return index;
}
inline bool uTextAccessInChunkOrOutOfRange(UText* text, int64_t nativeIndex, int64_t nativeLength, UBool forward, UBool& isAccessible)
{
if (forward) {
if (nativeIndex >= text->chunkNativeStart && nativeIndex < text->chunkNativeLimit) {
int64_t offset = nativeIndex - text->chunkNativeStart;
ASSERT(offset < std::numeric_limits<int32_t>::max());
text->chunkOffset = offset < std::numeric_limits<int32_t>::max() ? static_cast<int32_t>(offset) : 0;
isAccessible = TRUE;
return true;
}
if (nativeIndex >= nativeLength && text->chunkNativeLimit == nativeLength) {
text->chunkOffset = text->chunkLength;
isAccessible = FALSE;
return true;
}
} else {
if (nativeIndex > text->chunkNativeStart && nativeIndex <= text->chunkNativeLimit) {
int64_t offset = nativeIndex - text->chunkNativeStart;
ASSERT(offset < std::numeric_limits<int32_t>::max());
text->chunkOffset = offset < std::numeric_limits<int32_t>::max() ? static_cast<int32_t>(offset) : 0;
isAccessible = TRUE;
return true;
}
if (nativeIndex <= 0 && !text->chunkNativeStart) {
text->chunkOffset = 0;
isAccessible = FALSE;
return true;
}
}
return false;
}
}
#endif // UTextProvider_h