Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : :
21 : : #include <unotools/charclass.hxx>
22 : : #include <tools/string.hxx>
23 : : #include <tools/debug.hxx>
24 : :
25 : : #include "instance.hxx"
26 : :
27 : : #define CHARCLASS_SERVICENAME "com.sun.star.i18n.CharacterClassification"
28 : :
29 : : using namespace ::com::sun::star;
30 : : using namespace ::com::sun::star::i18n;
31 : : using namespace ::com::sun::star::uno;
32 : :
33 : :
34 : 2824 : CharClass::CharClass(
35 : : const Reference< lang::XMultiServiceFactory > & xSF,
36 : : const lang::Locale& rLocale
37 : : )
38 : : :
39 [ + - ]: 2824 : xSMgr( xSF )
40 : : {
41 [ + - ]: 2824 : setLocale( rLocale );
42 [ + - ][ + - ]: 2824 : xCC = Reference< XCharacterClassification > ( intl_createInstance( xSMgr, CHARCLASS_SERVICENAME, "CharClass" ), uno::UNO_QUERY );
[ + - ]
43 : 2824 : }
44 : :
45 : :
46 : 32017 : CharClass::CharClass(
47 [ + - ]: 32017 : const ::com::sun::star::lang::Locale& rLocale )
48 : : {
49 [ + - ]: 32017 : setLocale( rLocale );
50 : 32017 : Reference< lang::XMultiServiceFactory > xNil;
51 [ + - ][ + - ]: 32017 : xCC = Reference< XCharacterClassification > ( intl_createInstance( xNil, CHARCLASS_SERVICENAME, "CharClass" ), uno::UNO_QUERY );
[ + - ]
52 : 32017 : }
53 : :
54 : :
55 [ + - ]: 34581 : CharClass::~CharClass()
56 : : {
57 : 34581 : }
58 : :
59 : :
60 : 191622 : void CharClass::setLocale( const ::com::sun::star::lang::Locale& rLocale )
61 : : {
62 [ + - ]: 191622 : ::osl::MutexGuard aGuard( aMutex );
63 : 191622 : aLocale.Language = rLocale.Language;
64 : 191622 : aLocale.Country = rLocale.Country;
65 [ + - ]: 191622 : aLocale.Variant = rLocale.Variant;
66 : 191622 : }
67 : :
68 : :
69 : 1311040 : const ::com::sun::star::lang::Locale& CharClass::getLocale() const
70 : : {
71 [ + - ]: 1311040 : ::osl::MutexGuard aGuard( aMutex );
72 [ + - ]: 1311040 : return aLocale;
73 : : }
74 : :
75 : :
76 : : // static
77 : 30961 : sal_Bool CharClass::isAsciiNumeric( const String& rStr )
78 : : {
79 [ + + ]: 30961 : if ( !rStr.Len() )
80 : 1275 : return sal_False;
81 : 29686 : register const sal_Unicode* p = rStr.GetBuffer();
82 : 29686 : register const sal_Unicode* const pStop = p + rStr.Len();
83 [ - + ]: 1284 : do
84 : : {
85 [ + + ]: 29686 : if ( !isAsciiDigit( *p ) )
86 : 28402 : return sal_False;
87 : : } while ( ++p < pStop );
88 : 30961 : return sal_True;
89 : : }
90 : :
91 : :
92 : : // static
93 : 0 : sal_Bool CharClass::isAsciiAlpha( const String& rStr )
94 : : {
95 [ # # ]: 0 : if ( !rStr.Len() )
96 : 0 : return sal_False;
97 : 0 : register const sal_Unicode* p = rStr.GetBuffer();
98 : 0 : register const sal_Unicode* const pStop = p + rStr.Len();
99 [ # # ]: 0 : do
100 : : {
101 [ # # ]: 0 : if ( !isAsciiAlpha( *p ) )
102 : 0 : return sal_False;
103 : : } while ( ++p < pStop );
104 : 0 : return sal_True;
105 : : }
106 : :
107 : :
108 : :
109 : 0 : sal_Bool CharClass::isAlpha( const String& rStr, xub_StrLen nPos ) const
110 : : {
111 : 0 : sal_Unicode c = rStr.GetChar( nPos );
112 [ # # ]: 0 : if ( c < 128 )
113 : 0 : return isAsciiAlpha( c );
114 : :
115 : : try
116 : : {
117 [ # # ]: 0 : if ( xCC.is() )
118 [ # # ][ # # ]: 0 : return (xCC->getCharacterType( rStr, nPos, getLocale() ) &
[ # # ][ # # ]
[ # # ]
119 : 0 : nCharClassAlphaType) != 0;
120 : : else
121 : 0 : return sal_False;
122 : : }
123 : 0 : catch ( const Exception& )
124 : : {
125 : : SAL_WARN( "unotools.i18n", "isAlpha: Exception caught!" );
126 : 0 : return sal_False;
127 : : }
128 : : }
129 : :
130 : :
131 : :
132 : 545630 : sal_Bool CharClass::isLetter( const String& rStr, xub_StrLen nPos ) const
133 : : {
134 : 545630 : sal_Unicode c = rStr.GetChar( nPos );
135 [ + + ]: 545630 : if ( c < 128 )
136 : 545540 : return isAsciiAlpha( c );
137 : :
138 : : try
139 : : {
140 [ + - ]: 90 : if ( xCC.is() )
141 [ + - ][ + - ]: 90 : return (xCC->getCharacterType( rStr, nPos, getLocale() ) &
[ + - ][ + - ]
[ # # ]
142 : 90 : nCharClassLetterType) != 0;
143 : : else
144 : 545630 : return sal_False;
145 : : }
146 : 0 : catch ( const Exception& )
147 : : {
148 : : SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
149 : 0 : return sal_False;
150 : : }
151 : : }
152 : :
153 : :
154 : 0 : sal_Bool CharClass::isLetter( const String& rStr ) const
155 : : {
156 : : try
157 : : {
158 [ # # ]: 0 : if ( xCC.is() )
159 [ # # ][ # # ]: 0 : return isLetterType( xCC->getStringType( rStr, 0, rStr.Len(), getLocale() ) );
[ # # ][ # # ]
[ # # ]
160 : : else
161 : 0 : return sal_False;
162 : : }
163 : 0 : catch ( const Exception& )
164 : : {
165 : : SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
166 : 0 : return sal_False;
167 : : }
168 : : }
169 : :
170 : :
171 : 0 : sal_Bool CharClass::isDigit( const String& rStr, xub_StrLen nPos ) const
172 : : {
173 : 0 : sal_Unicode c = rStr.GetChar( nPos );
174 [ # # ]: 0 : if ( c < 128 )
175 : 0 : return isAsciiDigit( c );
176 : :
177 : : try
178 : : {
179 [ # # ]: 0 : if ( xCC.is() )
180 [ # # ][ # # ]: 0 : return (xCC->getCharacterType( rStr, nPos, getLocale() ) &
[ # # ][ # # ]
[ # # ]
181 : 0 : KCharacterType::DIGIT) != 0;
182 : : else
183 : 0 : return sal_False;
184 : : }
185 : 0 : catch ( const Exception& )
186 : : {
187 : : SAL_WARN( "unotools.i18n", "isDigit: Exception caught!" );
188 : 0 : return sal_False;
189 : : }
190 : : }
191 : :
192 : :
193 : 0 : sal_Bool CharClass::isNumeric( const String& rStr ) const
194 : : {
195 : : try
196 : : {
197 [ # # ]: 0 : if ( xCC.is() )
198 [ # # ][ # # ]: 0 : return isNumericType( xCC->getStringType( rStr, 0, rStr.Len(), getLocale() ) );
[ # # ][ # # ]
[ # # ]
199 : : else
200 : 0 : return sal_False;
201 : : }
202 : 0 : catch ( const Exception& )
203 : : {
204 : : SAL_WARN( "unotools.i18n", "isNumeric: Exception caught!" );
205 : 0 : return sal_False;
206 : : }
207 : : }
208 : :
209 : :
210 : 0 : sal_Bool CharClass::isAlphaNumeric( const String& rStr, xub_StrLen nPos ) const
211 : : {
212 : 0 : sal_Unicode c = rStr.GetChar( nPos );
213 [ # # ]: 0 : if ( c < 128 )
214 : 0 : return isAsciiAlphaNumeric( c );
215 : :
216 : : try
217 : : {
218 [ # # ]: 0 : if ( xCC.is() )
219 [ # # ][ # # ]: 0 : return (xCC->getCharacterType( rStr, nPos, getLocale() ) &
[ # # ][ # # ]
[ # # ]
220 : 0 : (nCharClassAlphaType | KCharacterType::DIGIT)) != 0;
221 : : else
222 : 0 : return sal_False;
223 : : }
224 : 0 : catch ( const Exception& )
225 : : {
226 : : SAL_WARN( "unotools.i18n", "isAlphaNumeric: Exception caught!" );
227 : 0 : return sal_False;
228 : : }
229 : : }
230 : :
231 : :
232 : 53854 : sal_Bool CharClass::isLetterNumeric( const String& rStr, xub_StrLen nPos ) const
233 : : {
234 : 53854 : sal_Unicode c = rStr.GetChar( nPos );
235 [ + + ]: 53854 : if ( c < 128 )
236 : 53758 : return isAsciiAlphaNumeric( c );
237 : :
238 : : try
239 : : {
240 [ + - ]: 96 : if ( xCC.is() )
241 [ + - ][ + - ]: 96 : return (xCC->getCharacterType( rStr, nPos, getLocale() ) &
[ + - ][ + - ]
[ # # ]
242 : 96 : (nCharClassLetterType | KCharacterType::DIGIT)) != 0;
243 : : else
244 : 53854 : return sal_False;
245 : : }
246 : 0 : catch ( const Exception& )
247 : : {
248 : : SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
249 : 0 : return sal_False;
250 : : }
251 : : }
252 : :
253 : :
254 : 33961 : sal_Bool CharClass::isLetterNumeric( const String& rStr ) const
255 : : {
256 : : try
257 : : {
258 [ + - ]: 33961 : if ( xCC.is() )
259 [ + - ][ + - ]: 33961 : return isLetterNumericType( xCC->getStringType( rStr, 0, rStr.Len(), getLocale() ) );
[ + - ][ + - ]
[ # # ]
260 : : else
261 : 33961 : return sal_False;
262 : : }
263 : 0 : catch ( const Exception& )
264 : : {
265 : : SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
266 : 0 : return sal_False;
267 : : }
268 : : }
269 : :
270 : 0 : rtl::OUString CharClass::titlecase(const rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount) const
271 : : {
272 : : try
273 : : {
274 [ # # ]: 0 : if ( xCC.is() )
275 [ # # ][ # # ]: 0 : return xCC->toTitle( rStr, nPos, nCount, getLocale() );
[ # # ]
276 : : else
277 [ # # ]: 0 : return rStr.copy( nPos, nCount );
278 : : }
279 : 0 : catch ( const Exception& )
280 : : {
281 : : SAL_WARN( "unotools.i18n", "titlecase: Exception caught!" );
282 : 0 : return rStr.copy( nPos, nCount );
283 : : }
284 : : }
285 : :
286 : 859541 : ::rtl::OUString CharClass::uppercase( const ::rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
287 : : {
288 : : try
289 : : {
290 [ + + ]: 859541 : if ( xCC.is() )
291 [ + - ][ + - ]: 859028 : return xCC->toUpper( rStr, nPos, nCount, getLocale() );
[ + - ]
292 : : else
293 [ # # ]: 859541 : return rStr.copy( nPos, nCount );
294 : : }
295 : 0 : catch ( const Exception& )
296 : : {
297 : : SAL_WARN( "unotools.i18n", "uppercase: Exception caught!" );
298 : 0 : return rStr.copy( nPos, nCount );
299 : : }
300 : : }
301 : :
302 : 34313 : ::rtl::OUString CharClass::lowercase( const ::rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
303 : : {
304 : : try
305 : : {
306 [ + - ]: 34313 : if ( xCC.is() )
307 [ + - ][ + - ]: 34313 : return xCC->toLower( rStr, nPos, nCount, getLocale() );
[ + - ]
308 : : else
309 [ # # ]: 34313 : return rStr.copy( nPos, nCount );
310 : : }
311 : 0 : catch ( const Exception& )
312 : : {
313 : : SAL_WARN( "unotools.i18n", "lowercase: Exception caught!" );
314 : 0 : return rStr.copy( nPos, nCount );
315 : : }
316 : : }
317 : :
318 : 40278 : sal_Int16 CharClass::getType( const String& rStr, xub_StrLen nPos ) const
319 : : {
320 : : try
321 : : {
322 [ + - ]: 40278 : if ( xCC.is() )
323 [ + - ][ + - ]: 40278 : return xCC->getType( rStr, nPos );
[ + - ][ # # ]
324 : : else
325 : 40278 : return 0;
326 : : }
327 : 0 : catch ( const Exception& )
328 : : {
329 : : SAL_WARN( "unotools.i18n", "getType: Exception caught!" );
330 : 0 : return 0;
331 : : }
332 : : }
333 : :
334 : :
335 : 10931 : sal_Int16 CharClass::getCharacterDirection( const String& rStr, xub_StrLen nPos ) const
336 : : {
337 : : try
338 : : {
339 [ + - ]: 10931 : if ( xCC.is() )
340 [ + - ][ + - ]: 10931 : return xCC->getCharacterDirection( rStr, nPos );
[ + - ][ # # ]
341 : : else
342 : 10931 : return 0;
343 : : }
344 : 0 : catch ( const Exception& )
345 : : {
346 : : SAL_WARN( "unotools.i18n", "getCharacterDirection: Exception caught!" );
347 : 0 : return 0;
348 : : }
349 : : }
350 : :
351 : :
352 : 0 : sal_Int16 CharClass::getScript( const String& rStr, xub_StrLen nPos ) const
353 : : {
354 : : try
355 : : {
356 [ # # ]: 0 : if ( xCC.is() )
357 [ # # ][ # # ]: 0 : return xCC->getScript( rStr, nPos );
[ # # ][ # # ]
358 : : else
359 : 0 : return 0;
360 : : }
361 : 0 : catch ( const Exception& )
362 : : {
363 : : SAL_WARN( "unotools.i18n", "getScript: Exception caught!" );
364 : 0 : return 0;
365 : : }
366 : : }
367 : :
368 : :
369 : 181970 : sal_Int32 CharClass::getCharacterType( const String& rStr, xub_StrLen nPos ) const
370 : : {
371 : : try
372 : : {
373 [ + - ]: 181970 : if ( xCC.is() )
374 [ + - ][ + - ]: 181970 : return xCC->getCharacterType( rStr, nPos, getLocale() );
[ + - ][ + - ]
[ # # ]
375 : : else
376 : 181970 : return 0;
377 : : }
378 : 0 : catch ( const Exception& )
379 : : {
380 : : SAL_WARN( "unotools.i18n", "getCharacterType: Exception caught!" );
381 : 0 : return 0;
382 : : }
383 : : }
384 : :
385 : :
386 : 12613 : sal_Int32 CharClass::getStringType( const String& rStr, xub_StrLen nPos, xub_StrLen nCount ) const
387 : : {
388 : : try
389 : : {
390 [ + - ]: 12613 : if ( xCC.is() )
391 [ + - ][ + - ]: 12613 : return xCC->getStringType( rStr, nPos, nCount, getLocale() );
[ + - ][ + - ]
[ # # ]
392 : : else
393 : 12613 : return 0;
394 : : }
395 : 0 : catch ( const Exception& )
396 : : {
397 : : SAL_WARN( "unotools.i18n", "getStringType: Exception caught!" );
398 : 0 : return 0;
399 : : }
400 : : }
401 : :
402 : :
403 : 26969 : ::com::sun::star::i18n::ParseResult CharClass::parseAnyToken(
404 : : const String& rStr,
405 : : sal_Int32 nPos,
406 : : sal_Int32 nStartCharFlags,
407 : : const String& userDefinedCharactersStart,
408 : : sal_Int32 nContCharFlags,
409 : : const String& userDefinedCharactersCont ) const
410 : : {
411 : : try
412 : : {
413 [ + - ]: 26969 : if ( xCC.is() )
414 [ + - ][ + - ]: 53938 : return xCC->parseAnyToken( rStr, nPos, getLocale(),
415 : : nStartCharFlags, userDefinedCharactersStart,
416 [ + - ][ + - ]: 53938 : nContCharFlags, userDefinedCharactersCont );
[ + - ][ + - ]
[ # # ]
417 : : else
418 : 26969 : return ParseResult();
419 : : }
420 : 0 : catch ( const Exception& e )
421 : : {
422 : : SAL_WARN( "unotools.i18n", "parseAnyToken: Exception caught " << e.Message );
423 : 0 : return ParseResult();
424 : : }
425 : : }
426 : :
427 : :
428 : 27337 : ::com::sun::star::i18n::ParseResult CharClass::parsePredefinedToken(
429 : : sal_Int32 nTokenType,
430 : : const String& rStr,
431 : : sal_Int32 nPos,
432 : : sal_Int32 nStartCharFlags,
433 : : const String& userDefinedCharactersStart,
434 : : sal_Int32 nContCharFlags,
435 : : const String& userDefinedCharactersCont ) const
436 : : {
437 : : try
438 : : {
439 [ + - ]: 27337 : if ( xCC.is() )
440 [ + - ][ + - ]: 54674 : return xCC->parsePredefinedToken( nTokenType, rStr, nPos, getLocale(),
441 : : nStartCharFlags, userDefinedCharactersStart,
442 [ + - ][ + - ]: 54674 : nContCharFlags, userDefinedCharactersCont );
[ + - ][ + - ]
[ # # ]
443 : : else
444 : 27337 : return ParseResult();
445 : : }
446 : 0 : catch ( const Exception& e )
447 : : {
448 : : SAL_WARN( "unotools.i18n", "parsePredefinedToken: Exception caught " << e.Message );
449 : 0 : return ParseResult();
450 : : }
451 : : }
452 : :
453 : :
454 : :
455 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|