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
|
|
|
|
2017-12-14 07:17:38 +03:00
|
|
|
#include "mozilla/intl/WordBreaker.h"
|
2018-01-22 13:19:30 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
1999-02-09 19:01:48 +03:00
|
|
|
|
2017-12-14 07:17:38 +03:00
|
|
|
using mozilla::intl::WordBreakClass;
|
|
|
|
using mozilla::intl::WordBreaker;
|
|
|
|
using mozilla::intl::WordRange;
|
1999-02-09 19:01:48 +03:00
|
|
|
|
2017-12-14 07:17:38 +03:00
|
|
|
/*static*/
|
|
|
|
already_AddRefed<WordBreaker> WordBreaker::Create() {
|
|
|
|
return RefPtr<WordBreaker>(new WordBreaker()).forget();
|
1999-02-10 02:56:05 +03:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
bool WordBreaker::BreakInBetween(const char16_t* aText1, uint32_t aTextLen1,
|
|
|
|
const char16_t* aText2, uint32_t aTextLen2) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(nullptr != aText1, "null ptr");
|
|
|
|
MOZ_ASSERT(nullptr != aText2, "null ptr");
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2005-08-22 07:00:06 +04:00
|
|
|
if (!aText1 || !aText2 || (0 == aTextLen1) || (0 == aTextLen2)) return false;
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2017-06-01 10:02:54 +03:00
|
|
|
return GetClass(aText1[aTextLen1 - 1]) != GetClass(aText2[0]);
|
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)))
|
2017-07-06 15:00:35 +03:00
|
|
|
#define IS_ALPHABETICAL_SCRIPT(c) ((c) < 0x2E80)
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
// we change the beginning of IS_HAN from 0x4e00 to 0x3400 to relfect
|
|
|
|
// Unicode 3.0
|
1999-08-24 10:35:32 +04:00
|
|
|
#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))
|
|
|
|
#define IS_THAI(c) (0x0E00 == (0xFF80 & (c))) // Look at the higest 9 bits
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2019-02-26 01:08:21 +03:00
|
|
|
/* static */
|
|
|
|
WordBreakClass WordBreaker::GetClass(char16_t c) {
|
2018-01-22 13:19:30 +03:00
|
|
|
// The pref is cached on first call; changes will require a browser restart.
|
|
|
|
static bool sStopAtUnderscore =
|
|
|
|
Preferences::GetBool("layout.word_select.stop_at_underscore", false);
|
|
|
|
|
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;
|
2018-01-22 13:19:30 +03:00
|
|
|
} else if (ASCII_IS_ALPHA(c) || ASCII_IS_DIGIT(c) ||
|
|
|
|
(c == '_' && !sStopAtUnderscore)) {
|
1999-08-24 10:35:32 +04:00
|
|
|
return kWbClassAlphaLetter;
|
|
|
|
} else {
|
|
|
|
return kWbClassPunct;
|
|
|
|
}
|
|
|
|
} else if (IS_THAI(c)) {
|
|
|
|
return kWbClassThaiLetter;
|
2007-06-15 06:33:54 +04:00
|
|
|
} else if (c == 0x00A0 /*NBSP*/) {
|
|
|
|
return kWbClassSpace;
|
|
|
|
} else {
|
1999-08-24 10:35:32 +04:00
|
|
|
return kWbClassAlphaLetter;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (IS_HAN(c)) {
|
|
|
|
return kWbClassHanLetter;
|
|
|
|
} else if (IS_KATAKANA(c)) {
|
|
|
|
return kWbClassKatakanaLetter;
|
|
|
|
} else if (IS_HIRAGANA(c)) {
|
|
|
|
return kWbClassHiraganaLetter;
|
|
|
|
} else if (IS_HALFWIDTHKATAKANA(c)) {
|
|
|
|
return kWbClassHWKatakanaLetter;
|
|
|
|
} else {
|
|
|
|
return kWbClassAlphaLetter;
|
|
|
|
}
|
|
|
|
}
|
2017-12-14 07:17:38 +03:00
|
|
|
return static_cast<WordBreakClass>(0);
|
1999-02-19 04:38:12 +03:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
WordRange WordBreaker::FindWord(const char16_t* aText, uint32_t aTextLen,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aOffset) {
|
2017-12-14 07:17:38 +03:00
|
|
|
WordRange range;
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(nullptr != aText, "null ptr");
|
|
|
|
MOZ_ASSERT(0 != aTextLen, "len = 0");
|
|
|
|
MOZ_ASSERT(aOffset <= aTextLen, "aOffset > aTextLen");
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2005-08-22 07:00:06 +04:00
|
|
|
range.mBegin = aTextLen + 1;
|
|
|
|
range.mEnd = aTextLen + 1;
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2005-08-22 07:00:06 +04:00
|
|
|
if (!aText || aOffset > aTextLen) return range;
|
1999-02-19 04:38:12 +03:00
|
|
|
|
2017-12-14 07:17:38 +03:00
|
|
|
WordBreakClass c = GetClass(aText[aOffset]);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
1999-02-19 04:38:12 +03:00
|
|
|
// Scan forward
|
2005-08-22 07:00:06 +04:00
|
|
|
range.mEnd--;
|
1999-02-19 04:38:12 +03:00
|
|
|
for (i = aOffset + 1; i <= aTextLen; 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
|
2005-08-22 07:00:06 +04:00
|
|
|
range.mBegin = 0;
|
1999-02-20 04:45:51 +03:00
|
|
|
for (i = aOffset; 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;
|
|
|
|
}
|
|
|
|
}
|
1999-08-24 10:35:32 +04:00
|
|
|
if (kWbClassThaiLetter == c) {
|
|
|
|
// need to call Thai word breaker from here
|
|
|
|
// we should pass the whole Thai segment to the thai word breaker to find a
|
|
|
|
// shorter answer
|
|
|
|
}
|
2005-08-22 07:00:06 +04:00
|
|
|
return range;
|
1999-02-09 19:01:48 +03:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
int32_t WordBreaker::NextWord(const char16_t* aText, uint32_t aLen,
|
|
|
|
uint32_t aPos) {
|
2017-12-14 07:17:38 +03:00
|
|
|
WordBreakClass c1, c2;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t cur = aPos;
|
2007-09-11 03:03:18 +04:00
|
|
|
if (cur == aLen) return NS_WORDBREAKER_NEED_MORE_TEXT;
|
2017-06-01 10:02:54 +03:00
|
|
|
c1 = GetClass(aText[cur]);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
1999-02-19 04:38:12 +03:00
|
|
|
for (cur++; cur < aLen; cur++) {
|
2017-06-01 10:02:54 +03:00
|
|
|
c2 = GetClass(aText[cur]);
|
2017-07-06 15:00:35 +03:00
|
|
|
if (c2 != c1) break;
|
1999-02-16 21:11:14 +03:00
|
|
|
}
|
1999-08-24 10:35:32 +04:00
|
|
|
if (kWbClassThaiLetter == c1) {
|
|
|
|
// need to call Thai word breaker from here
|
|
|
|
// we should pass the whole Thai segment to the thai word breaker to find a
|
|
|
|
// shorter answer
|
|
|
|
}
|
2005-08-22 07:00:06 +04:00
|
|
|
if (cur == aLen) return NS_WORDBREAKER_NEED_MORE_TEXT;
|
|
|
|
return cur;
|
1999-02-09 19:01:48 +03:00
|
|
|
}
|