2001-09-26 04:40:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1999-02-09 19:01:48 +03:00
|
|
|
|
2021-12-06 21:15:50 +03:00
|
|
|
#include "mozilla/intl/UnicodeProperties.h"
|
2017-12-14 07:17:38 +03:00
|
|
|
#include "mozilla/intl/WordBreaker.h"
|
2020-07-11 00:21:30 +03:00
|
|
|
#include "mozilla/StaticPrefs_layout.h"
|
2020-04-23 17:18:08 +03:00
|
|
|
#include "nsComplexBreaker.h"
|
2021-08-31 19:01:32 +03:00
|
|
|
#include "nsTArray.h"
|
1999-02-09 19:01:48 +03:00
|
|
|
|
2021-12-06 21:15:50 +03:00
|
|
|
using mozilla::intl::Script;
|
|
|
|
using mozilla::intl::UnicodeProperties;
|
2017-12-14 07:17:38 +03:00
|
|
|
using mozilla::intl::WordBreaker;
|
|
|
|
using mozilla::intl::WordRange;
|
2021-08-31 19:01:32 +03:00
|
|
|
using mozilla::unicode::GetGenCategory;
|
1999-02-09 19:01:48 +03:00
|
|
|
|
1999-08-24 10:35:32 +04:00
|
|
|
#define IS_ASCII(c) (0 == (0xFF80 & (c)))
|
1999-02-19 04:38:12 +03:00
|
|
|
#define ASCII_IS_ALPHA(c) \
|
|
|
|
((('a' <= (c)) && ((c) <= 'z')) || (('A' <= (c)) && ((c) <= 'Z')))
|
|
|
|
#define ASCII_IS_DIGIT(c) (('0' <= (c)) && ((c) <= '9'))
|
|
|
|
#define ASCII_IS_SPACE(c) \
|
|
|
|
((' ' == (c)) || ('\t' == (c)) || ('\r' == (c)) || ('\n' == (c)))
|
1999-08-24 10:35:32 +04:00
|
|
|
#define IS_ALPHABETICAL_SCRIPT(c) ((c) < 0x2E80)
|
1999-02-19 04:38:12 +03:00
|
|
|
|
1999-08-24 10:35:32 +04:00
|
|
|
// we change the beginning of IS_HAN from 0x4e00 to 0x3400 to relfect
|
|
|
|
// Unicode 3.0
|
|
|
|
#define IS_HAN(c) \
|
|
|
|
((0x3400 <= (c)) && ((c) <= 0x9fff)) || ((0xf900 <= (c)) && ((c) <= 0xfaff))
|
1999-02-19 04:38:12 +03:00
|
|
|
#define IS_KATAKANA(c) ((0x30A0 <= (c)) && ((c) <= 0x30FF))
|
|
|
|
#define IS_HIRAGANA(c) ((0x3040 <= (c)) && ((c) <= 0x309F))
|
1999-08-24 10:35:32 +04:00
|
|
|
#define IS_HALFWIDTHKATAKANA(c) ((0xFF60 <= (c)) && ((c) <= 0xFF9F))
|
2020-04-23 17:18:08 +03:00
|
|
|
|
|
|
|
// Return true if aChar belongs to a SEAsian script that is written without
|
|
|
|
// word spaces, so we need to use the "complex breaker" to find possible word
|
|
|
|
// boundaries. (https://en.wikipedia.org/wiki/Scriptio_continua)
|
|
|
|
// (How well this works depends on the level of platform support for finding
|
|
|
|
// possible line breaks - or possible word boundaries - in the particular
|
|
|
|
// script. Thai, at least, works pretty well on the major desktop OSes. If
|
|
|
|
// the script is not supported by the platform, we just won't find any useful
|
|
|
|
// boundaries.)
|
|
|
|
static bool IsScriptioContinua(char16_t aChar) {
|
2021-12-06 21:15:50 +03:00
|
|
|
Script sc = UnicodeProperties::GetScriptCode(aChar);
|
2020-04-23 17:18:08 +03:00
|
|
|
return sc == Script::THAI || sc == Script::MYANMAR || sc == Script::KHMER ||
|
|
|
|
sc == Script::JAVANESE || sc == Script::BALINESE ||
|
|
|
|
sc == Script::SUNDANESE || sc == Script::LAO;
|
|
|
|
}
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2019-02-26 01:08:21 +03:00
|
|
|
/* static */
|
2021-09-08 07:19:36 +03:00
|
|
|
WordBreaker::WordBreakClass WordBreaker::GetClass(char16_t c) {
|
1999-02-19 04:38:12 +03:00
|
|
|
// begin of the hack
|
|
|
|
|
1999-08-24 10:35:32 +04:00
|
|
|
if (IS_ALPHABETICAL_SCRIPT(c)) {
|
|
|
|
if (IS_ASCII(c)) {
|
|
|
|
if (ASCII_IS_SPACE(c)) {
|
|
|
|
return kWbClassSpace;
|
2020-04-23 17:18:08 +03:00
|
|
|
}
|
|
|
|
if (ASCII_IS_ALPHA(c) || ASCII_IS_DIGIT(c) ||
|
2020-07-11 00:21:30 +03:00
|
|
|
(c == '_' && !StaticPrefs::layout_word_select_stop_at_underscore())) {
|
1999-08-24 10:35:32 +04:00
|
|
|
return kWbClassAlphaLetter;
|
|
|
|
}
|
2020-04-23 17:18:08 +03:00
|
|
|
return kWbClassPunct;
|
|
|
|
}
|
|
|
|
if (c == 0x00A0 /*NBSP*/) {
|
2007-06-15 06:33:54 +04:00
|
|
|
return kWbClassSpace;
|
1999-08-24 10:35:32 +04:00
|
|
|
}
|
2020-06-03 18:24:29 +03:00
|
|
|
if (GetGenCategory(c) == nsUGenCategory::kPunctuation) {
|
|
|
|
return kWbClassPunct;
|
|
|
|
}
|
2020-04-23 17:18:08 +03:00
|
|
|
if (IsScriptioContinua(c)) {
|
|
|
|
return kWbClassScriptioContinua;
|
1999-08-24 10:35:32 +04:00
|
|
|
}
|
2020-04-23 17:18:08 +03:00
|
|
|
return kWbClassAlphaLetter;
|
|
|
|
}
|
|
|
|
if (IS_HAN(c)) {
|
|
|
|
return kWbClassHanLetter;
|
1999-08-24 10:35:32 +04:00
|
|
|
}
|
2020-04-23 17:18:08 +03:00
|
|
|
if (IS_KATAKANA(c)) {
|
|
|
|
return kWbClassKatakanaLetter;
|
|
|
|
}
|
|
|
|
if (IS_HIRAGANA(c)) {
|
|
|
|
return kWbClassHiraganaLetter;
|
|
|
|
}
|
|
|
|
if (IS_HALFWIDTHKATAKANA(c)) {
|
|
|
|
return kWbClassHWKatakanaLetter;
|
|
|
|
}
|
2020-06-03 18:24:29 +03:00
|
|
|
if (GetGenCategory(c) == nsUGenCategory::kPunctuation) {
|
|
|
|
return kWbClassPunct;
|
|
|
|
}
|
2020-04-23 17:18:08 +03:00
|
|
|
if (IsScriptioContinua(c)) {
|
|
|
|
return kWbClassScriptioContinua;
|
|
|
|
}
|
|
|
|
return kWbClassAlphaLetter;
|
1999-02-19 04:38:12 +03:00
|
|
|
}
|
|
|
|
|
2021-09-14 02:55:33 +03:00
|
|
|
WordRange WordBreaker::FindWord(const char16_t* aText, uint32_t aLen,
|
|
|
|
uint32_t aPos) {
|
|
|
|
MOZ_ASSERT(aText);
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2021-09-14 02:55:33 +03:00
|
|
|
if (aPos >= aLen) {
|
|
|
|
return {aLen, aLen};
|
|
|
|
}
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2021-09-14 02:55:33 +03:00
|
|
|
WordBreakClass c = GetClass(aText[aPos]);
|
|
|
|
WordRange range{0, aLen};
|
1999-02-19 04:38:12 +03:00
|
|
|
|
|
|
|
// Scan forward
|
2021-09-14 02:55:33 +03:00
|
|
|
for (uint32_t i = aPos + 1; i <= aLen; i++) {
|
2017-06-01 10:02:54 +03:00
|
|
|
if (c != GetClass(aText[i])) {
|
2005-08-22 07:00:06 +04:00
|
|
|
range.mEnd = i;
|
1999-02-19 04:38:12 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan backward
|
2021-09-14 02:55:33 +03:00
|
|
|
for (uint32_t i = aPos; i > 0; i--) {
|
2017-06-01 10:02:54 +03:00
|
|
|
if (c != GetClass(aText[i - 1])) {
|
2005-08-22 07:00:06 +04:00
|
|
|
range.mBegin = i;
|
1999-02-19 04:38:12 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 17:18:08 +03:00
|
|
|
|
|
|
|
if (kWbClassScriptioContinua == c) {
|
|
|
|
// we pass the whole text segment to the complex word breaker to find a
|
1999-08-24 10:35:32 +04:00
|
|
|
// shorter answer
|
2020-04-23 17:18:08 +03:00
|
|
|
AutoTArray<uint8_t, 256> breakBefore;
|
|
|
|
breakBefore.SetLength(range.mEnd - range.mBegin);
|
2021-10-26 12:58:51 +03:00
|
|
|
ComplexBreaker::GetBreaks(aText + range.mBegin, range.mEnd - range.mBegin,
|
|
|
|
breakBefore.Elements());
|
2020-04-23 17:18:08 +03:00
|
|
|
|
|
|
|
// Scan forward
|
2021-09-14 02:55:33 +03:00
|
|
|
for (uint32_t i = aPos + 1; i < range.mEnd; i++) {
|
2020-04-23 17:18:08 +03:00
|
|
|
if (breakBefore[i - range.mBegin]) {
|
|
|
|
range.mEnd = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan backward
|
2021-09-14 02:55:33 +03:00
|
|
|
for (uint32_t i = aPos; i > range.mBegin; i--) {
|
2020-04-23 17:18:08 +03:00
|
|
|
if (breakBefore[i - range.mBegin]) {
|
|
|
|
range.mBegin = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-08-24 10:35:32 +04:00
|
|
|
}
|
2005-08-22 07:00:06 +04:00
|
|
|
return range;
|
1999-02-09 19:01:48 +03:00
|
|
|
}
|
|
|
|
|
2021-09-08 07:19:37 +03:00
|
|
|
int32_t WordBreaker::Next(const char16_t* aText, uint32_t aLen, uint32_t aPos) {
|
2021-09-08 07:19:38 +03:00
|
|
|
MOZ_ASSERT(aText);
|
|
|
|
|
|
|
|
if (aPos >= aLen) {
|
2020-04-23 17:18:08 +03:00
|
|
|
return NS_WORDBREAKER_NEED_MORE_TEXT;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2021-09-08 07:19:38 +03:00
|
|
|
const WordBreakClass posClass = GetClass(aText[aPos]);
|
|
|
|
uint32_t nextBreakPos;
|
|
|
|
for (nextBreakPos = aPos + 1; nextBreakPos < aLen; ++nextBreakPos) {
|
|
|
|
if (posClass != GetClass(aText[nextBreakPos])) {
|
2020-04-23 17:18:08 +03:00
|
|
|
break;
|
|
|
|
}
|
1999-02-16 21:11:14 +03:00
|
|
|
}
|
2020-04-23 17:18:08 +03:00
|
|
|
|
2021-09-08 07:19:38 +03:00
|
|
|
if (kWbClassScriptioContinua == posClass) {
|
|
|
|
// We pass the whole text segment to the complex word breaker to find a
|
|
|
|
// shorter answer.
|
|
|
|
const char16_t* segStart = aText + aPos;
|
|
|
|
const uint32_t segLen = nextBreakPos - aPos + 1;
|
2020-04-23 17:18:08 +03:00
|
|
|
AutoTArray<uint8_t, 256> breakBefore;
|
2021-09-08 07:19:38 +03:00
|
|
|
breakBefore.SetLength(segLen);
|
2021-10-26 12:58:51 +03:00
|
|
|
ComplexBreaker::GetBreaks(segStart, segLen, breakBefore.Elements());
|
2020-04-23 17:18:08 +03:00
|
|
|
|
2021-09-08 07:19:38 +03:00
|
|
|
for (uint32_t i = aPos + 1; i < nextBreakPos; ++i) {
|
|
|
|
if (breakBefore[i - aPos]) {
|
|
|
|
nextBreakPos = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-23 17:18:08 +03:00
|
|
|
}
|
|
|
|
|
2021-09-08 07:19:38 +03:00
|
|
|
MOZ_ASSERT(nextBreakPos != aPos);
|
|
|
|
return nextBreakPos;
|
1999-02-09 19:01:48 +03:00
|
|
|
}
|