LineBreakIteratorPoolICU.h [plain text]
#pragma once
#include <unicode/uloc.h>
#include <wtf/HashMap.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/ThreadSpecific.h>
#include <wtf/text/AtomString.h>
#include <wtf/text/TextBreakIterator.h>
#include <wtf/unicode/icu/ICUHelpers.h>
namespace WTF {
class LineBreakIteratorPool {
WTF_MAKE_NONCOPYABLE(LineBreakIteratorPool);
WTF_MAKE_FAST_ALLOCATED;
public:
LineBreakIteratorPool() = default;
WTF_EXPORT_PRIVATE static LineBreakIteratorPool& sharedPool();
static AtomString makeLocaleWithBreakKeyword(const AtomString& locale, LineBreakIteratorMode mode)
{
auto utf8Locale = locale.string().utf8();
if (!utf8Locale.length())
return locale;
Vector<char> scratchBuffer(utf8Locale.length() + 11, 0);
memcpy(scratchBuffer.data(), utf8Locale.data(), utf8Locale.length());
const char* keywordValue = nullptr;
switch (mode) {
case LineBreakIteratorMode::Default:
break;
case LineBreakIteratorMode::Loose:
keywordValue = "loose";
break;
case LineBreakIteratorMode::Normal:
keywordValue = "normal";
break;
case LineBreakIteratorMode::Strict:
keywordValue = "strict";
break;
}
UErrorCode status = U_ZERO_ERROR;
int32_t lengthNeeded = uloc_setKeywordValue("lb", keywordValue, scratchBuffer.data(), scratchBuffer.size(), &status);
if (U_SUCCESS(status))
return AtomString::fromUTF8(scratchBuffer.data(), lengthNeeded);
if (needsToGrowToProduceBuffer(status)) {
scratchBuffer.grow(lengthNeeded + 1);
memset(scratchBuffer.data() + utf8Locale.length(), 0, scratchBuffer.size() - utf8Locale.length());
status = U_ZERO_ERROR;
int32_t lengthNeeded2 = uloc_setKeywordValue("lb", keywordValue, scratchBuffer.data(), scratchBuffer.size(), &status);
if (!U_SUCCESS(status) || lengthNeeded != lengthNeeded2)
return locale;
return AtomString::fromUTF8(scratchBuffer.data(), lengthNeeded);
}
return locale;
}
UBreakIterator* take(const AtomString& locale, LineBreakIteratorMode mode)
{
auto localeWithOptionalBreakKeyword = makeLocaleWithBreakKeyword(locale, mode);
UBreakIterator* iterator = nullptr;
for (size_t i = 0; i < m_pool.size(); ++i) {
if (m_pool[i].first == localeWithOptionalBreakKeyword) {
iterator = m_pool[i].second;
m_pool.remove(i);
break;
}
}
if (!iterator) {
iterator = openLineBreakIterator(localeWithOptionalBreakKeyword);
if (!iterator)
return nullptr;
}
ASSERT(!m_vendedIterators.contains(iterator));
m_vendedIterators.add(iterator, localeWithOptionalBreakKeyword);
return iterator;
}
void put(UBreakIterator* iterator)
{
ASSERT(m_vendedIterators.contains(iterator));
if (m_pool.size() == capacity) {
closeLineBreakIterator(m_pool[0].second);
m_pool.remove(0);
}
m_pool.uncheckedAppend({ m_vendedIterators.take(iterator), iterator });
}
private:
static constexpr size_t capacity = 4;
Vector<std::pair<AtomString, UBreakIterator*>, capacity> m_pool;
HashMap<UBreakIterator*, AtomString> m_vendedIterators;
friend WTF::ThreadSpecific<LineBreakIteratorPool>::operator LineBreakIteratorPool*();
};
}