1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsSampleWordBreaker.h"
9 nsSampleWordBreaker::nsSampleWordBreaker()
12 nsSampleWordBreaker::~nsSampleWordBreaker()
16 NS_IMPL_ISUPPORTS(nsSampleWordBreaker
, nsIWordBreaker
)
18 bool nsSampleWordBreaker::BreakInBetween(
19 const char16_t
* aText1
, uint32_t aTextLen1
,
20 const char16_t
* aText2
, uint32_t aTextLen2
)
22 NS_PRECONDITION( nullptr != aText1
, "null ptr");
23 NS_PRECONDITION( nullptr != aText2
, "null ptr");
25 if(!aText1
|| !aText2
|| (0 == aTextLen1
) || (0 == aTextLen2
))
28 return (this->GetClass(aText1
[aTextLen1
-1]) != this->GetClass(aText2
[0]));
32 #define IS_ASCII(c) (0 == ( 0xFF80 & (c)))
33 #define ASCII_IS_ALPHA(c) ((( 'a' <= (c)) && ((c) <= 'z')) || (( 'A' <= (c)) && ((c) <= 'Z')))
34 #define ASCII_IS_DIGIT(c) (( '0' <= (c)) && ((c) <= '9'))
35 #define ASCII_IS_SPACE(c) (( ' ' == (c)) || ( '\t' == (c)) || ( '\r' == (c)) || ( '\n' == (c)))
36 #define IS_ALPHABETICAL_SCRIPT(c) ((c) < 0x2E80)
38 // we change the beginning of IS_HAN from 0x4e00 to 0x3400 to relfect Unicode 3.0
39 #define IS_HAN(c) (( 0x3400 <= (c)) && ((c) <= 0x9fff))||(( 0xf900 <= (c)) && ((c) <= 0xfaff))
40 #define IS_KATAKANA(c) (( 0x30A0 <= (c)) && ((c) <= 0x30FF))
41 #define IS_HIRAGANA(c) (( 0x3040 <= (c)) && ((c) <= 0x309F))
42 #define IS_HALFWIDTHKATAKANA(c) (( 0xFF60 <= (c)) && ((c) <= 0xFF9F))
43 #define IS_THAI(c) (0x0E00 == (0xFF80 & (c) )) // Look at the higest 9 bits
45 uint8_t nsSampleWordBreaker::GetClass(char16_t c
)
49 if (IS_ALPHABETICAL_SCRIPT(c
)) {
51 if(ASCII_IS_SPACE(c
)) {
53 } else if(ASCII_IS_ALPHA(c
) || ASCII_IS_DIGIT(c
)) {
54 return kWbClassAlphaLetter
;
58 } else if(IS_THAI(c
)) {
59 return kWbClassThaiLetter
;
60 } else if (c
== 0x00A0/*NBSP*/) {
63 return kWbClassAlphaLetter
;
67 return kWbClassHanLetter
;
68 } else if(IS_KATAKANA(c
)) {
69 return kWbClassKatakanaLetter
;
70 } else if(IS_HIRAGANA(c
)) {
71 return kWbClassHiraganaLetter
;
72 } else if(IS_HALFWIDTHKATAKANA(c
)) {
73 return kWbClassHWKatakanaLetter
;
75 return kWbClassAlphaLetter
;
81 nsWordRange
nsSampleWordBreaker::FindWord(
82 const char16_t
* aText
, uint32_t aTextLen
,
86 NS_PRECONDITION( nullptr != aText
, "null ptr");
87 NS_PRECONDITION( 0 != aTextLen
, "len = 0");
88 NS_PRECONDITION( aOffset
<= aTextLen
, "aOffset > aTextLen");
90 range
.mBegin
= aTextLen
+ 1;
91 range
.mEnd
= aTextLen
+ 1;
93 if(!aText
|| aOffset
> aTextLen
)
96 uint8_t c
= this->GetClass(aText
[aOffset
]);
100 for(i
= aOffset
+1;i
<= aTextLen
; i
++)
102 if( c
!= this->GetClass(aText
[i
]))
111 for(i
= aOffset
;i
> 0; i
--)
113 if( c
!= this->GetClass(aText
[i
-1]))
119 if(kWbClassThaiLetter
== c
)
121 // need to call Thai word breaker from here
122 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
127 int32_t nsSampleWordBreaker::NextWord(
128 const char16_t
* aText
, uint32_t aLen
, uint32_t aPos
)
133 return NS_WORDBREAKER_NEED_MORE_TEXT
;
134 c1
= this->GetClass(aText
[cur
]);
136 for(cur
++; cur
<aLen
; cur
++)
138 c2
= this->GetClass(aText
[cur
]);
142 if(kWbClassThaiLetter
== c1
)
144 // need to call Thai word breaker from here
145 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
148 return NS_WORDBREAKER_NEED_MORE_TEXT
;