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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 :
39 : #include "nsSampleWordBreaker.h"
40 :
41 : #include "pratom.h"
42 : #include "nsLWBRKDll.h"
43 1404 : nsSampleWordBreaker::nsSampleWordBreaker()
44 : {
45 1404 : }
46 2806 : nsSampleWordBreaker::~nsSampleWordBreaker()
47 : {
48 5612 : }
49 :
50 12635 : NS_IMPL_ISUPPORTS1(nsSampleWordBreaker, nsIWordBreaker)
51 :
52 0 : bool nsSampleWordBreaker::BreakInBetween(
53 : const PRUnichar* aText1 , PRUint32 aTextLen1,
54 : const PRUnichar* aText2 , PRUint32 aTextLen2)
55 : {
56 0 : NS_PRECONDITION( nsnull != aText1, "null ptr");
57 0 : NS_PRECONDITION( nsnull != aText2, "null ptr");
58 :
59 0 : if(!aText1 || !aText2 || (0 == aTextLen1) || (0 == aTextLen2))
60 0 : return false;
61 :
62 0 : return (this->GetClass(aText1[aTextLen1-1]) != this->GetClass(aText2[0]));
63 : }
64 :
65 :
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)
71 :
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
78 :
79 0 : PRUint8 nsSampleWordBreaker::GetClass(PRUnichar c)
80 : {
81 : // begin of the hack
82 :
83 0 : if (IS_ALPHABETICAL_SCRIPT(c)) {
84 0 : if(IS_ASCII(c)) {
85 0 : if(ASCII_IS_SPACE(c)) {
86 0 : return kWbClassSpace;
87 0 : } else if(ASCII_IS_ALPHA(c) || ASCII_IS_DIGIT(c)) {
88 0 : return kWbClassAlphaLetter;
89 : } else {
90 0 : return kWbClassPunct;
91 : }
92 0 : } else if(IS_THAI(c)) {
93 0 : return kWbClassThaiLetter;
94 0 : } else if (c == 0x00A0/*NBSP*/) {
95 0 : return kWbClassSpace;
96 : } else {
97 0 : return kWbClassAlphaLetter;
98 : }
99 : } else {
100 0 : if(IS_HAN(c)) {
101 0 : return kWbClassHanLetter;
102 0 : } else if(IS_KATAKANA(c)) {
103 0 : return kWbClassKatakanaLetter;
104 0 : } else if(IS_HIRAGANA(c)) {
105 0 : return kWbClassHiraganaLetter;
106 0 : } else if(IS_HALFWIDTHKATAKANA(c)) {
107 0 : return kWbClassHWKatakanaLetter;
108 : } else {
109 0 : return kWbClassAlphaLetter;
110 : }
111 : }
112 : return 0;
113 : }
114 :
115 0 : nsWordRange nsSampleWordBreaker::FindWord(
116 : const PRUnichar* aText , PRUint32 aTextLen,
117 : PRUint32 aOffset)
118 : {
119 : nsWordRange range;
120 0 : NS_PRECONDITION( nsnull != aText, "null ptr");
121 0 : NS_PRECONDITION( 0 != aTextLen, "len = 0");
122 0 : NS_PRECONDITION( aOffset <= aTextLen, "aOffset > aTextLen");
123 :
124 0 : range.mBegin = aTextLen + 1;
125 0 : range.mEnd = aTextLen + 1;
126 :
127 0 : if(!aText || aOffset > aTextLen)
128 0 : return range;
129 :
130 0 : PRUint8 c = this->GetClass(aText[aOffset]);
131 : PRUint32 i;
132 : // Scan forward
133 0 : range.mEnd--;
134 0 : for(i = aOffset +1;i <= aTextLen; i++)
135 : {
136 0 : if( c != this->GetClass(aText[i]))
137 : {
138 0 : range.mEnd = i;
139 0 : break;
140 : }
141 : }
142 :
143 : // Scan backward
144 0 : range.mBegin = 0;
145 0 : for(i = aOffset ;i > 0; i--)
146 : {
147 0 : if( c != this->GetClass(aText[i-1]))
148 : {
149 0 : range.mBegin = i;
150 0 : break;
151 : }
152 : }
153 : if(kWbClassThaiLetter == c)
154 : {
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
157 : }
158 0 : return range;
159 : }
160 :
161 0 : PRInt32 nsSampleWordBreaker::NextWord(
162 : const PRUnichar* aText, PRUint32 aLen, PRUint32 aPos)
163 : {
164 : PRInt8 c1, c2;
165 0 : PRUint32 cur = aPos;
166 0 : if (cur == aLen)
167 0 : return NS_WORDBREAKER_NEED_MORE_TEXT;
168 0 : c1 = this->GetClass(aText[cur]);
169 :
170 0 : for(cur++; cur <aLen; cur++)
171 : {
172 0 : c2 = this->GetClass(aText[cur]);
173 0 : if(c2 != c1)
174 0 : break;
175 : }
176 : if(kWbClassThaiLetter == c1)
177 : {
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
180 : }
181 0 : if (cur == aLen)
182 0 : return NS_WORDBREAKER_NEED_MORE_TEXT;
183 0 : return cur;
184 : }
|