1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 #include "nsSampleWordBreaker.h"
42 #include "nsLWBRKDll.h"
43 nsSampleWordBreaker::nsSampleWordBreaker()
46 nsSampleWordBreaker::~nsSampleWordBreaker()
50 NS_IMPL_ISUPPORTS1(nsSampleWordBreaker
, nsIWordBreaker
)
52 PRBool
nsSampleWordBreaker::BreakInBetween(
53 const PRUnichar
* aText1
, PRUint32 aTextLen1
,
54 const PRUnichar
* aText2
, PRUint32 aTextLen2
)
56 NS_PRECONDITION( nsnull
!= aText1
, "null ptr");
57 NS_PRECONDITION( nsnull
!= aText2
, "null ptr");
59 if(!aText1
|| !aText2
|| (0 == aTextLen1
) || (0 == aTextLen2
))
62 return (this->GetClass(aText1
[aTextLen1
-1]) != this->GetClass(aText2
[0]));
66 #define IS_ASCII(c) (0 == ( 0xFF80 & (c)))
67 #define ASCII_IS_ALPHA(c) ((( 'a' <= (c)) && ((c) <= 'z')) || (( 'A' <= (c)) && ((c) <= 'Z')))
68 #define ASCII_IS_DIGIT(c) (( '0' <= (c)) && ((c) <= '9'))
69 #define ASCII_IS_SPACE(c) (( ' ' == (c)) || ( '\t' == (c)) || ( '\r' == (c)) || ( '\n' == (c)))
70 #define IS_ALPHABETICAL_SCRIPT(c) ((c) < 0x2E80)
72 // we change the beginning of IS_HAN from 0x4e00 to 0x3400 to relfect Unicode 3.0
73 #define IS_HAN(c) (( 0x3400 <= (c)) && ((c) <= 0x9fff))||(( 0xf900 <= (c)) && ((c) <= 0xfaff))
74 #define IS_KATAKANA(c) (( 0x30A0 <= (c)) && ((c) <= 0x30FF))
75 #define IS_HIRAGANA(c) (( 0x3040 <= (c)) && ((c) <= 0x309F))
76 #define IS_HALFWIDTHKATAKANA(c) (( 0xFF60 <= (c)) && ((c) <= 0xFF9F))
77 #define IS_THAI(c) (0x0E00 == (0xFF80 & (c) )) // Look at the higest 9 bits
79 PRUint8
nsSampleWordBreaker::GetClass(PRUnichar c
)
83 if (IS_ALPHABETICAL_SCRIPT(c
)) {
85 if(ASCII_IS_SPACE(c
)) {
87 } else if(ASCII_IS_ALPHA(c
) || ASCII_IS_DIGIT(c
)) {
88 return kWbClassAlphaLetter
;
92 } else if(IS_THAI(c
)) {
93 return kWbClassThaiLetter
;
94 } else if (c
== 0x00A0/*NBSP*/) {
97 return kWbClassAlphaLetter
;
101 return kWbClassHanLetter
;
102 } else if(IS_KATAKANA(c
)) {
103 return kWbClassKatakanaLetter
;
104 } else if(IS_HIRAGANA(c
)) {
105 return kWbClassHiraganaLetter
;
106 } else if(IS_HALFWIDTHKATAKANA(c
)) {
107 return kWbClassHWKatakanaLetter
;
109 return kWbClassAlphaLetter
;
115 nsWordRange
nsSampleWordBreaker::FindWord(
116 const PRUnichar
* aText
, PRUint32 aTextLen
,
120 NS_PRECONDITION( nsnull
!= aText
, "null ptr");
121 NS_PRECONDITION( 0 != aTextLen
, "len = 0");
122 NS_PRECONDITION( aOffset
<= aTextLen
, "aOffset > aTextLen");
124 range
.mBegin
= aTextLen
+ 1;
125 range
.mEnd
= aTextLen
+ 1;
127 if(!aText
|| aOffset
> aTextLen
)
130 PRUint8 c
= this->GetClass(aText
[aOffset
]);
134 for(i
= aOffset
+1;i
<= aTextLen
; i
++)
136 if( c
!= this->GetClass(aText
[i
]))
145 for(i
= aOffset
;i
> 0; i
--)
147 if( c
!= this->GetClass(aText
[i
-1]))
153 if(kWbClassThaiLetter
== c
)
155 // need to call Thai word breaker from here
156 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
161 PRInt32
nsSampleWordBreaker::NextWord(
162 const PRUnichar
* aText
, PRUint32 aLen
, PRUint32 aPos
)
167 return NS_WORDBREAKER_NEED_MORE_TEXT
;
168 c1
= this->GetClass(aText
[cur
]);
170 for(cur
++; cur
<aLen
; cur
++)
172 c2
= this->GetClass(aText
[cur
]);
176 if(kWbClassThaiLetter
== c1
)
178 // need to call Thai word breaker from here
179 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
182 return NS_WORDBREAKER_NEED_MORE_TEXT
;
186 PRInt32
nsSampleWordBreaker::PrevWord(
187 const PRUnichar
* aText
, PRUint32 aLen
, PRUint32 aPos
)
193 return NS_WORDBREAKER_NEED_MORE_TEXT
;
196 c1
= this->GetClass(aText
[cur
]);
198 for(; cur
> 0; cur
--)
200 c2
= this->GetClass(aText
[cur
-1]);
204 if(kWbClassThaiLetter
== c1
)
206 // need to call Thai word breaker from here
207 // we should pass the whole Thai segment to the thai word breaker to find a shorter answer
210 return NS_WORDBREAKER_NEED_MORE_TEXT
;