2021-11-09 01:14:14 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* Classes to iterate over grapheme, word, sentence, or line. */
|
|
|
|
|
|
|
|
#include "mozilla/intl/Segmenter.h"
|
|
|
|
|
|
|
|
#include "mozilla/intl/LineBreaker.h"
|
|
|
|
#include "mozilla/intl/WordBreaker.h"
|
2022-01-13 18:36:03 +00:00
|
|
|
#include "mozilla/intl/UnicodeProperties.h"
|
2023-08-07 06:23:48 +00:00
|
|
|
#include "mozilla/StaticPrefs_intl.h"
|
2022-01-13 18:36:03 +00:00
|
|
|
#include "nsUnicodeProperties.h"
|
|
|
|
#include "nsCharTraits.h"
|
|
|
|
|
2023-08-07 06:23:48 +00:00
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
# include "ICU4XDataProvider.h"
|
|
|
|
# include "ICU4XGraphemeClusterSegmenter.h"
|
|
|
|
# include "ICU4XLineSegmenter.h"
|
|
|
|
# include "ICU4XSentenceSegmenter.h"
|
|
|
|
# include "ICU4XWordSegmenter.h"
|
2023-10-13 12:49:08 +00:00
|
|
|
# include "mozilla/ClearOnShutdown.h"
|
2023-08-07 06:23:48 +00:00
|
|
|
# include "mozilla/intl/ICU4XGeckoDataProvider.h"
|
2023-10-13 12:49:08 +00:00
|
|
|
# include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
# include <mutex>
|
2023-08-07 06:23:48 +00:00
|
|
|
#endif
|
|
|
|
|
2022-01-13 18:36:03 +00:00
|
|
|
using namespace mozilla::unicode;
|
2021-11-09 01:14:14 +00:00
|
|
|
|
|
|
|
namespace mozilla::intl {
|
|
|
|
|
|
|
|
SegmentIteratorUtf16::SegmentIteratorUtf16(Span<const char16_t> aText)
|
|
|
|
: mText(aText) {}
|
|
|
|
|
|
|
|
Maybe<uint32_t> SegmentIteratorUtf16::Seek(uint32_t aPos) {
|
|
|
|
if (mPos < aPos) {
|
|
|
|
mPos = aPos;
|
|
|
|
}
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
LineBreakIteratorUtf16::LineBreakIteratorUtf16(Span<const char16_t> aText,
|
|
|
|
const LineBreakOptions& aOptions)
|
2023-08-07 06:23:48 +00:00
|
|
|
: SegmentIteratorUtf16(aText), mOptions(aOptions) {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (!StaticPrefs::intl_icu4x_segmenter_enabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto result =
|
|
|
|
capi::ICU4XLineSegmenter_create_auto(mozilla::intl::GetDataProvider());
|
|
|
|
MOZ_RELEASE_ASSERT(result.is_ok);
|
|
|
|
mSegmenter = result.ok;
|
|
|
|
mIterator = capi::ICU4XLineSegmenter_segment_utf16(
|
2024-06-17 02:03:32 +00:00
|
|
|
mSegmenter, mText.Elements(), mText.Length());
|
2023-08-07 06:23:48 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
LineBreakIteratorUtf16::~LineBreakIteratorUtf16() {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (mIterator) {
|
|
|
|
capi::ICU4XLineBreakIteratorUtf16_destroy(mIterator);
|
|
|
|
}
|
|
|
|
if (mSegmenter) {
|
|
|
|
capi::ICU4XLineSegmenter_destroy(mSegmenter);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-11-09 01:14:14 +00:00
|
|
|
|
|
|
|
Maybe<uint32_t> LineBreakIteratorUtf16::Next() {
|
2023-08-07 06:23:48 +00:00
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (mIterator) {
|
|
|
|
const int32_t nextPos = capi::ICU4XLineBreakIteratorUtf16_next(mIterator);
|
|
|
|
if (nextPos < 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
if (!nextPos) {
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
mPos = nextPos;
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-09 01:14:14 +00:00
|
|
|
const int32_t nextPos =
|
|
|
|
LineBreaker::Next(mText.Elements(), mText.Length(), mPos);
|
|
|
|
if (nextPos == NS_LINEBREAKER_NEED_MORE_TEXT) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
mPos = nextPos;
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:23:48 +00:00
|
|
|
Maybe<uint32_t> LineBreakIteratorUtf16::Seek(uint32_t aPos) {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (mIterator) {
|
|
|
|
if (mPos >= aPos) {
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mPos < aPos) {
|
|
|
|
const int32_t nextPos = capi::ICU4XLineBreakIteratorUtf16_next(mIterator);
|
|
|
|
if (nextPos < 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
mPos = static_cast<uint32_t>(nextPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aPos < mPos) {
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return SegmentIteratorUtf16::Seek(aPos);
|
|
|
|
}
|
|
|
|
|
2021-11-09 01:14:14 +00:00
|
|
|
WordBreakIteratorUtf16::WordBreakIteratorUtf16(Span<const char16_t> aText)
|
2023-08-07 06:23:48 +00:00
|
|
|
: SegmentIteratorUtf16(aText) {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (!StaticPrefs::intl_icu4x_segmenter_enabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto result =
|
|
|
|
capi::ICU4XWordSegmenter_create_auto(mozilla::intl::GetDataProvider());
|
|
|
|
MOZ_RELEASE_ASSERT(result.is_ok);
|
|
|
|
mSegmenter = result.ok;
|
|
|
|
mIterator = capi::ICU4XWordSegmenter_segment_utf16(
|
2024-06-17 02:03:32 +00:00
|
|
|
mSegmenter, mText.Elements(), mText.Length());
|
2023-08-07 06:23:48 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
WordBreakIteratorUtf16::~WordBreakIteratorUtf16() {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (mIterator) {
|
|
|
|
capi::ICU4XWordBreakIteratorUtf16_destroy(mIterator);
|
|
|
|
}
|
|
|
|
if (mSegmenter) {
|
|
|
|
capi::ICU4XWordSegmenter_destroy(mSegmenter);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2021-11-09 01:14:14 +00:00
|
|
|
|
2024-11-04 12:44:49 +00:00
|
|
|
void WordBreakIteratorUtf16::Reset(Span<const char16_t> aText) {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
mText = aText;
|
|
|
|
if (mIterator) {
|
|
|
|
capi::ICU4XWordBreakIteratorUtf16_destroy(mIterator);
|
|
|
|
}
|
|
|
|
mIterator = capi::ICU4XWordSegmenter_segment_utf16(
|
|
|
|
mSegmenter, mText.Elements(), mText.Length());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-11-09 01:14:14 +00:00
|
|
|
Maybe<uint32_t> WordBreakIteratorUtf16::Next() {
|
2023-08-07 06:23:48 +00:00
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (mIterator) {
|
|
|
|
const int32_t nextPos = capi::ICU4XWordBreakIteratorUtf16_next(mIterator);
|
|
|
|
if (nextPos < 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
if (!nextPos) {
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
mPos = nextPos;
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-09 01:14:14 +00:00
|
|
|
const int32_t nextPos =
|
|
|
|
WordBreaker::Next(mText.Elements(), mText.Length(), mPos);
|
|
|
|
if (nextPos == NS_WORDBREAKER_NEED_MORE_TEXT) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
mPos = nextPos;
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:23:48 +00:00
|
|
|
Maybe<uint32_t> WordBreakIteratorUtf16::Seek(uint32_t aPos) {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (mIterator) {
|
|
|
|
if (mPos >= aPos) {
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mPos < aPos) {
|
|
|
|
const int32_t nextPos = capi::ICU4XWordBreakIteratorUtf16_next(mIterator);
|
|
|
|
if (nextPos < 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
mPos = static_cast<uint32_t>(nextPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aPos < mPos) {
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return SegmentIteratorUtf16::Seek(aPos);
|
|
|
|
}
|
|
|
|
|
2023-10-13 12:49:08 +00:00
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
capi::ICU4XGraphemeClusterSegmenter*
|
|
|
|
GraphemeClusterBreakIteratorUtf16::sSegmenter = nullptr;
|
|
|
|
#endif
|
|
|
|
|
2022-01-13 18:36:04 +00:00
|
|
|
GraphemeClusterBreakIteratorUtf16::GraphemeClusterBreakIteratorUtf16(
|
|
|
|
Span<const char16_t> aText)
|
2023-08-07 06:23:48 +00:00
|
|
|
: SegmentIteratorUtf16(aText) {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (!StaticPrefs::intl_icu4x_segmenter_enabled()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-13 12:49:08 +00:00
|
|
|
static std::once_flag sOnce;
|
|
|
|
|
|
|
|
std::call_once(sOnce, [] {
|
|
|
|
auto result = capi::ICU4XGraphemeClusterSegmenter_create(
|
|
|
|
mozilla::intl::GetDataProvider());
|
|
|
|
MOZ_RELEASE_ASSERT(result.is_ok);
|
|
|
|
sSegmenter = result.ok;
|
|
|
|
|
|
|
|
NS_DispatchToMainThread(
|
|
|
|
NS_NewRunnableFunction("GraphemeClusterBreakIteratorUtf16", [] {
|
|
|
|
RunOnShutdown([] {
|
|
|
|
capi::ICU4XGraphemeClusterSegmenter_destroy(sSegmenter);
|
|
|
|
sSegmenter = nullptr;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(sSegmenter);
|
2023-08-07 06:23:48 +00:00
|
|
|
mIterator = capi::ICU4XGraphemeClusterSegmenter_segment_utf16(
|
2024-06-17 02:03:32 +00:00
|
|
|
sSegmenter, mText.Elements(), mText.Length());
|
2023-08-07 06:23:48 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphemeClusterBreakIteratorUtf16::~GraphemeClusterBreakIteratorUtf16() {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (mIterator) {
|
|
|
|
capi::ICU4XGraphemeClusterBreakIteratorUtf16_destroy(mIterator);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2022-01-13 18:36:04 +00:00
|
|
|
|
2022-01-13 18:36:03 +00:00
|
|
|
enum HSType {
|
|
|
|
HST_NONE = U_HST_NOT_APPLICABLE,
|
|
|
|
HST_L = U_HST_LEADING_JAMO,
|
|
|
|
HST_V = U_HST_VOWEL_JAMO,
|
|
|
|
HST_T = U_HST_TRAILING_JAMO,
|
|
|
|
HST_LV = U_HST_LV_SYLLABLE,
|
|
|
|
HST_LVT = U_HST_LVT_SYLLABLE
|
|
|
|
};
|
|
|
|
|
|
|
|
static HSType GetHangulSyllableType(uint32_t aCh) {
|
|
|
|
return HSType(UnicodeProperties::GetIntPropertyValue(
|
|
|
|
aCh, UnicodeProperties::IntProperty::HangulSyllableType));
|
|
|
|
}
|
|
|
|
|
2022-01-13 18:36:04 +00:00
|
|
|
Maybe<uint32_t> GraphemeClusterBreakIteratorUtf16::Next() {
|
|
|
|
const auto len = mText.Length();
|
2023-08-07 06:23:48 +00:00
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (mIterator) {
|
|
|
|
const int32_t nextPos =
|
|
|
|
capi::ICU4XGraphemeClusterBreakIteratorUtf16_next(mIterator);
|
|
|
|
if (nextPos < 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
if (!nextPos) {
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
mPos = nextPos;
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
#endif
|
2022-01-13 18:36:04 +00:00
|
|
|
if (mPos >= len) {
|
|
|
|
// The iterator has already reached the end.
|
|
|
|
return Nothing();
|
2022-01-13 18:36:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 18:36:04 +00:00
|
|
|
uint32_t ch = mText[mPos++];
|
2022-01-13 18:36:03 +00:00
|
|
|
|
2022-01-13 18:36:04 +00:00
|
|
|
if (mPos < len && NS_IS_SURROGATE_PAIR(ch, mText[mPos])) {
|
|
|
|
ch = SURROGATE_TO_UCS4(ch, mText[mPos++]);
|
2022-01-13 18:36:03 +00:00
|
|
|
} else if ((ch & ~0xff) == 0x1100 || (ch >= 0xa960 && ch <= 0xa97f) ||
|
|
|
|
(ch >= 0xac00 && ch <= 0xd7ff)) {
|
|
|
|
// Handle conjoining Jamo that make Hangul syllables
|
|
|
|
HSType hangulState = GetHangulSyllableType(ch);
|
2022-01-13 18:36:04 +00:00
|
|
|
while (mPos < len) {
|
|
|
|
ch = mText[mPos];
|
2022-01-13 18:36:03 +00:00
|
|
|
HSType hangulType = GetHangulSyllableType(ch);
|
|
|
|
switch (hangulType) {
|
|
|
|
case HST_L:
|
|
|
|
case HST_LV:
|
|
|
|
case HST_LVT:
|
|
|
|
if (hangulState == HST_L) {
|
|
|
|
hangulState = hangulType;
|
|
|
|
mPos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HST_V:
|
|
|
|
if ((hangulState != HST_NONE) && (hangulState != HST_T) &&
|
|
|
|
(hangulState != HST_LVT)) {
|
|
|
|
hangulState = hangulType;
|
|
|
|
mPos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HST_T:
|
|
|
|
if (hangulState != HST_NONE && hangulState != HST_L) {
|
|
|
|
hangulState = hangulType;
|
|
|
|
mPos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t kVS16 = 0xfe0f;
|
|
|
|
const uint32_t kZWJ = 0x200d;
|
|
|
|
// UTF-16 surrogate values for Fitzpatrick type modifiers
|
|
|
|
const uint32_t kFitzpatrickHigh = 0xD83C;
|
|
|
|
const uint32_t kFitzpatrickLowFirst = 0xDFFB;
|
|
|
|
const uint32_t kFitzpatrickLowLast = 0xDFFF;
|
|
|
|
|
2023-03-07 09:51:12 +00:00
|
|
|
// Checking the emoji-presentation property of the base character is a bit
|
|
|
|
// expensive, so we do it lazily.
|
|
|
|
enum class EmojiStatus : uint8_t {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
Unknown,
|
|
|
|
} baseIsEmojiStatus = EmojiStatus::Unknown;
|
|
|
|
|
|
|
|
// Remember the base character and the position of the next, in case we need
|
|
|
|
// to evaluate its emoji status.
|
|
|
|
uint32_t baseCh = ch;
|
|
|
|
uint32_t afterBase = mPos;
|
|
|
|
|
|
|
|
auto isFitzpatrickModifierAt = [&](uint32_t aPos) -> bool {
|
|
|
|
return aPos + 1 < len && mText[aPos] == kFitzpatrickHigh &&
|
|
|
|
mText[aPos + 1] >= kFitzpatrickLowFirst &&
|
|
|
|
mText[aPos + 1] <= kFitzpatrickLowLast;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto baseIsEmoji = [&]() -> bool {
|
|
|
|
if (baseIsEmojiStatus == EmojiStatus::Unknown) {
|
|
|
|
auto basePresentation = GetEmojiPresentation(baseCh);
|
|
|
|
baseIsEmojiStatus =
|
|
|
|
basePresentation == EmojiDefault ||
|
|
|
|
(basePresentation == TextDefault &&
|
|
|
|
((afterBase < len && mText[afterBase] == kVS16) ||
|
|
|
|
isFitzpatrickModifierAt(afterBase)))
|
|
|
|
? EmojiStatus::Yes
|
|
|
|
: EmojiStatus::No;
|
|
|
|
}
|
|
|
|
return baseIsEmojiStatus == EmojiStatus::Yes;
|
|
|
|
};
|
|
|
|
|
2022-01-13 18:36:03 +00:00
|
|
|
bool prevWasZwj = false;
|
|
|
|
|
2022-01-13 18:36:04 +00:00
|
|
|
while (mPos < len) {
|
|
|
|
ch = mText[mPos];
|
2022-01-13 18:36:03 +00:00
|
|
|
size_t chLen = 1;
|
|
|
|
|
|
|
|
// Check for surrogate pairs; note that isolated surrogates will just
|
|
|
|
// be treated as generic (non-cluster-extending) characters here,
|
|
|
|
// which is fine for cluster-iterating purposes
|
2022-01-13 18:36:04 +00:00
|
|
|
if (mPos < len - 1 && NS_IS_SURROGATE_PAIR(ch, mText[mPos + 1])) {
|
|
|
|
ch = SURROGATE_TO_UCS4(ch, mText[mPos + 1]);
|
2022-01-13 18:36:03 +00:00
|
|
|
chLen = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool extendCluster =
|
|
|
|
IsClusterExtender(ch) ||
|
2023-03-07 09:51:12 +00:00
|
|
|
(prevWasZwj && baseIsEmoji() &&
|
2022-01-13 18:36:03 +00:00
|
|
|
((GetEmojiPresentation(ch) == EmojiDefault) ||
|
2022-01-13 18:36:04 +00:00
|
|
|
(GetEmojiPresentation(ch) == TextDefault && mPos + chLen < len &&
|
|
|
|
mText[mPos + chLen] == kVS16)));
|
2022-01-13 18:36:03 +00:00
|
|
|
if (!extendCluster) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevWasZwj = (ch == kZWJ);
|
|
|
|
mPos += chLen;
|
|
|
|
}
|
|
|
|
|
2022-01-13 18:36:04 +00:00
|
|
|
MOZ_ASSERT(mPos <= len, "Next() has overshot the string!");
|
|
|
|
return Some(mPos);
|
2022-01-13 18:36:03 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 06:23:48 +00:00
|
|
|
Maybe<uint32_t> GraphemeClusterBreakIteratorUtf16::Seek(uint32_t aPos) {
|
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (mIterator) {
|
|
|
|
if (mPos >= aPos) {
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mPos < aPos) {
|
|
|
|
const int32_t nextPos =
|
|
|
|
capi::ICU4XGraphemeClusterBreakIteratorUtf16_next(mIterator);
|
|
|
|
if (nextPos < 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
mPos = static_cast<uint32_t>(nextPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aPos < mPos) {
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return SegmentIteratorUtf16::Seek(aPos);
|
|
|
|
}
|
|
|
|
|
2022-01-13 18:36:04 +00:00
|
|
|
GraphemeClusterBreakReverseIteratorUtf16::
|
|
|
|
GraphemeClusterBreakReverseIteratorUtf16(Span<const char16_t> aText)
|
|
|
|
: SegmentIteratorUtf16(aText) {
|
|
|
|
mPos = mText.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<uint32_t> GraphemeClusterBreakReverseIteratorUtf16::Next() {
|
|
|
|
if (mPos == 0) {
|
|
|
|
return Nothing();
|
2022-01-13 18:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ch;
|
|
|
|
do {
|
2022-01-13 18:36:04 +00:00
|
|
|
ch = mText[--mPos];
|
2022-01-13 18:36:03 +00:00
|
|
|
|
2022-01-13 18:36:04 +00:00
|
|
|
if (mPos > 0 && NS_IS_SURROGATE_PAIR(mText[mPos - 1], ch)) {
|
|
|
|
ch = SURROGATE_TO_UCS4(mText[--mPos], ch);
|
2022-01-13 18:36:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsClusterExtender(ch)) {
|
|
|
|
break;
|
|
|
|
}
|
2022-01-13 18:36:04 +00:00
|
|
|
} while (mPos > 0);
|
2022-01-13 18:36:03 +00:00
|
|
|
|
|
|
|
// XXX May need to handle conjoining Jamo
|
|
|
|
|
2022-01-13 18:36:04 +00:00
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<uint32_t> GraphemeClusterBreakReverseIteratorUtf16::Seek(uint32_t aPos) {
|
|
|
|
if (mPos > aPos) {
|
|
|
|
mPos = aPos;
|
|
|
|
}
|
|
|
|
return Next();
|
2022-01-13 18:36:03 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 06:23:48 +00:00
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
SentenceBreakIteratorUtf16::SentenceBreakIteratorUtf16(
|
|
|
|
Span<const char16_t> aText)
|
|
|
|
: SegmentIteratorUtf16(aText) {
|
|
|
|
auto result =
|
|
|
|
capi::ICU4XSentenceSegmenter_create(mozilla::intl::GetDataProvider());
|
|
|
|
MOZ_RELEASE_ASSERT(result.is_ok);
|
|
|
|
mSegmenter = result.ok;
|
|
|
|
mIterator = capi::ICU4XSentenceSegmenter_segment_utf16(
|
2024-06-17 02:03:32 +00:00
|
|
|
mSegmenter, mText.Elements(), mText.Length());
|
2023-08-07 06:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SentenceBreakIteratorUtf16::~SentenceBreakIteratorUtf16() {
|
|
|
|
if (mIterator) {
|
|
|
|
capi::ICU4XSentenceBreakIteratorUtf16_destroy(mIterator);
|
|
|
|
}
|
|
|
|
if (mSegmenter) {
|
|
|
|
capi::ICU4XSentenceSegmenter_destroy(mSegmenter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<uint32_t> SentenceBreakIteratorUtf16::Seek(uint32_t aPos) {
|
|
|
|
if (!mIterator) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPos >= aPos) {
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mPos < aPos) {
|
|
|
|
const int32_t nextPos =
|
|
|
|
capi::ICU4XSentenceBreakIteratorUtf16_next(mIterator);
|
|
|
|
if (nextPos < 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
mPos = static_cast<uint32_t>(nextPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aPos < mPos) {
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<uint32_t> SentenceBreakIteratorUtf16::Next() {
|
|
|
|
if (!mIterator) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
const int32_t nextPos = capi::ICU4XSentenceBreakIteratorUtf16_next(mIterator);
|
|
|
|
if (nextPos < 0) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
if (!nextPos) {
|
|
|
|
return Next();
|
|
|
|
}
|
|
|
|
mPos = nextPos;
|
|
|
|
return Some(mPos);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-11-09 01:14:14 +00:00
|
|
|
Result<UniquePtr<Segmenter>, ICUError> Segmenter::TryCreate(
|
|
|
|
Span<const char> aLocale, const SegmenterOptions& aOptions) {
|
2023-08-07 06:23:48 +00:00
|
|
|
#if !defined(MOZ_ICU4X) || !defined(JS_HAS_INTL_API)
|
2022-01-13 18:36:04 +00:00
|
|
|
if (aOptions.mGranularity == SegmenterGranularity::Sentence) {
|
2021-11-09 01:14:14 +00:00
|
|
|
// Grapheme and Sentence iterator are not yet implemented.
|
|
|
|
return Err(ICUError::InternalError);
|
|
|
|
}
|
2023-08-07 06:23:48 +00:00
|
|
|
#endif
|
2021-11-09 01:14:14 +00:00
|
|
|
return MakeUnique<Segmenter>(aLocale, aOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<SegmentIteratorUtf16> Segmenter::Segment(
|
|
|
|
Span<const char16_t> aText) const {
|
|
|
|
switch (mOptions.mGranularity) {
|
|
|
|
case SegmenterGranularity::Grapheme:
|
2022-01-13 18:36:04 +00:00
|
|
|
return MakeUnique<GraphemeClusterBreakIteratorUtf16>(aText);
|
2021-11-09 01:14:14 +00:00
|
|
|
case SegmenterGranularity::Sentence:
|
2023-08-07 06:23:48 +00:00
|
|
|
#if defined(MOZ_ICU4X) && defined(JS_HAS_INTL_API)
|
|
|
|
if (StaticPrefs::intl_icu4x_segmenter_enabled()) {
|
|
|
|
return MakeUnique<SentenceBreakIteratorUtf16>(aText);
|
|
|
|
}
|
|
|
|
#endif
|
2021-11-09 01:14:14 +00:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Unimplemented yet!");
|
|
|
|
return nullptr;
|
|
|
|
case SegmenterGranularity::Word:
|
|
|
|
return MakeUnique<WordBreakIteratorUtf16>(aText);
|
|
|
|
case SegmenterGranularity::Line:
|
|
|
|
return MakeUnique<LineBreakIteratorUtf16>(aText);
|
|
|
|
}
|
|
|
|
MOZ_ASSERT_UNREACHABLE("All granularities must be handled!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla::intl
|