#include "config.h"
#include "Hyphenation.h"
#include <wtf/Language.h>
#include <wtf/RetainPtr.h>
#include <wtf/TinyLRUCache.h>
#include <wtf/text/StringView.h>
#include <wtf/text/TextBreakIteratorInternalICU.h>
namespace WTF {
template<>
class TinyLRUCachePolicy<AtomString, RetainPtr<CFLocaleRef>>
{
public:
static TinyLRUCache<AtomString, RetainPtr<CFLocaleRef>>& cache()
{
static NeverDestroyed<TinyLRUCache<AtomString, RetainPtr<CFLocaleRef>>> cache;
return cache;
}
static bool isKeyNull(const AtomString& localeIdentifier)
{
return localeIdentifier.isNull();
}
static RetainPtr<CFLocaleRef> createValueForNullKey()
{
RetainPtr<CFLocaleRef> locale = adoptCF(CFLocaleCreate(kCFAllocatorDefault, defaultLanguage().createCFString().get()));
return CFStringIsHyphenationAvailableForLocale(locale.get()) ? locale : nullptr;
}
static RetainPtr<CFLocaleRef> createValueForKey(const AtomString& localeIdentifier)
{
RetainPtr<CFLocaleRef> locale = adoptCF(CFLocaleCreate(kCFAllocatorDefault, localeIdentifier.string().createCFString().get()));
return CFStringIsHyphenationAvailableForLocale(locale.get()) ? locale : nullptr;
}
};
}
namespace WebCore {
bool canHyphenate(const AtomString& localeIdentifier)
{
return TinyLRUCachePolicy<AtomString, RetainPtr<CFLocaleRef>>::cache().get(localeIdentifier);
}
size_t lastHyphenLocation(StringView text, size_t beforeIndex, const AtomString& localeIdentifier)
{
RetainPtr<CFLocaleRef> locale = TinyLRUCachePolicy<AtomString, RetainPtr<CFLocaleRef>>::cache().get(localeIdentifier);
CFOptionFlags searchAcrossWordBoundaries = 1;
CFIndex result = CFStringGetHyphenationLocationBeforeIndex(text.createCFStringWithoutCopying().get(), beforeIndex, CFRangeMake(0, text.length()), searchAcrossWordBoundaries, locale.get(), nullptr);
return result == kCFNotFound ? 0 : result;
}
}