update credits
[LibreOffice.git] / unotools / source / i18n / charclass.cxx
blobcd8d036ab601ad4b0d0a7f58aedae410138e8808
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <comphelper/processfactory.hxx>
22 #include <unotools/charclass.hxx>
23 #include <tools/string.hxx>
24 #include <tools/debug.hxx>
26 #include <com/sun/star/i18n/CharacterClassification.hpp>
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::i18n;
30 using namespace ::com::sun::star::uno;
33 CharClass::CharClass(
34 const Reference< uno::XComponentContext > & rxContext,
35 const LanguageTag& rLanguageTag
38 maLanguageTag( rLanguageTag)
40 xCC = CharacterClassification::create( rxContext );
44 CharClass::CharClass(
45 const LanguageTag& rLanguageTag )
47 maLanguageTag( rLanguageTag)
49 xCC = CharacterClassification::create( comphelper::getProcessComponentContext() );
53 CharClass::~CharClass()
58 void CharClass::setLanguageTag( const LanguageTag& rLanguageTag )
60 ::osl::MutexGuard aGuard( aMutex );
61 maLanguageTag = rLanguageTag;
65 const LanguageTag& CharClass::getLanguageTag() const
67 ::osl::MutexGuard aGuard( aMutex );
68 return maLanguageTag;
72 const ::com::sun::star::lang::Locale& CharClass::getMyLocale() const
74 ::osl::MutexGuard aGuard( aMutex );
75 return maLanguageTag.getLocale();
79 // static
80 sal_Bool CharClass::isAsciiNumeric( const String& rStr )
82 if ( !rStr.Len() )
83 return sal_False;
84 register const sal_Unicode* p = rStr.GetBuffer();
85 register const sal_Unicode* const pStop = p + rStr.Len();
88 if ( !isAsciiDigit( *p ) )
89 return sal_False;
90 } while ( ++p < pStop );
91 return sal_True;
95 // static
96 sal_Bool CharClass::isAsciiAlpha( const String& rStr )
98 if ( !rStr.Len() )
99 return sal_False;
100 register const sal_Unicode* p = rStr.GetBuffer();
101 register const sal_Unicode* const pStop = p + rStr.Len();
104 if ( !isAsciiAlpha( *p ) )
105 return sal_False;
106 } while ( ++p < pStop );
107 return sal_True;
112 sal_Bool CharClass::isAlpha( const String& rStr, xub_StrLen nPos ) const
114 sal_Unicode c = rStr.GetChar( nPos );
115 if ( c < 128 )
116 return isAsciiAlpha( c );
120 if ( xCC.is() )
121 return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
122 nCharClassAlphaType) != 0;
123 else
124 return sal_False;
126 catch ( const Exception& )
128 SAL_WARN( "unotools.i18n", "isAlpha: Exception caught!" );
129 return sal_False;
135 sal_Bool CharClass::isLetter( const String& rStr, xub_StrLen nPos ) const
137 sal_Unicode c = rStr.GetChar( nPos );
138 if ( c < 128 )
139 return isAsciiAlpha( c );
143 if ( xCC.is() )
144 return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
145 nCharClassLetterType) != 0;
146 else
147 return sal_False;
149 catch ( const Exception& )
151 SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
152 return sal_False;
157 sal_Bool CharClass::isLetter( const String& rStr ) const
161 if ( xCC.is() )
162 return isLetterType( xCC->getStringType( rStr, 0, rStr.Len(), getMyLocale() ) );
163 else
164 return sal_False;
166 catch ( const Exception& )
168 SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
169 return sal_False;
174 sal_Bool CharClass::isDigit( const String& rStr, xub_StrLen nPos ) const
176 sal_Unicode c = rStr.GetChar( nPos );
177 if ( c < 128 )
178 return isAsciiDigit( c );
182 if ( xCC.is() )
183 return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
184 KCharacterType::DIGIT) != 0;
185 else
186 return sal_False;
188 catch ( const Exception& )
190 SAL_WARN( "unotools.i18n", "isDigit: Exception caught!" );
191 return sal_False;
196 sal_Bool CharClass::isNumeric( const String& rStr ) const
200 if ( xCC.is() )
201 return isNumericType( xCC->getStringType( rStr, 0, rStr.Len(), getMyLocale() ) );
202 else
203 return sal_False;
205 catch ( const Exception& )
207 SAL_WARN( "unotools.i18n", "isNumeric: Exception caught!" );
208 return sal_False;
213 sal_Bool CharClass::isAlphaNumeric( const String& rStr, xub_StrLen nPos ) const
215 sal_Unicode c = rStr.GetChar( nPos );
216 if ( c < 128 )
217 return isAsciiAlphaNumeric( c );
221 if ( xCC.is() )
222 return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
223 (nCharClassAlphaType | KCharacterType::DIGIT)) != 0;
224 else
225 return sal_False;
227 catch ( const Exception& )
229 SAL_WARN( "unotools.i18n", "isAlphaNumeric: Exception caught!" );
230 return sal_False;
235 sal_Bool CharClass::isLetterNumeric( const String& rStr, xub_StrLen nPos ) const
237 sal_Unicode c = rStr.GetChar( nPos );
238 if ( c < 128 )
239 return isAsciiAlphaNumeric( c );
243 if ( xCC.is() )
244 return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
245 (nCharClassLetterType | KCharacterType::DIGIT)) != 0;
246 else
247 return sal_False;
249 catch ( const Exception& )
251 SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
252 return sal_False;
257 sal_Bool CharClass::isLetterNumeric( const String& rStr ) const
261 if ( xCC.is() )
262 return isLetterNumericType( xCC->getStringType( rStr, 0, rStr.Len(), getMyLocale() ) );
263 else
264 return sal_False;
266 catch ( const Exception& )
268 SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
269 return sal_False;
273 OUString CharClass::titlecase(const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount) const
277 if ( xCC.is() )
278 return xCC->toTitle( rStr, nPos, nCount, getMyLocale() );
279 else
280 return rStr.copy( nPos, nCount );
282 catch ( const Exception& )
284 SAL_WARN( "unotools.i18n", "titlecase: Exception caught!" );
285 return rStr.copy( nPos, nCount );
289 OUString CharClass::uppercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
293 if ( xCC.is() )
294 return xCC->toUpper( rStr, nPos, nCount, getMyLocale() );
295 else
296 return rStr.copy( nPos, nCount );
298 catch ( const Exception& )
300 SAL_WARN( "unotools.i18n", "uppercase: Exception caught!" );
301 return rStr.copy( nPos, nCount );
305 OUString CharClass::lowercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
309 if ( xCC.is() )
310 return xCC->toLower( rStr, nPos, nCount, getMyLocale() );
311 else
312 return rStr.copy( nPos, nCount );
314 catch ( const Exception& )
316 SAL_WARN( "unotools.i18n", "lowercase: Exception caught!" );
317 return rStr.copy( nPos, nCount );
321 sal_Int16 CharClass::getType( const String& rStr, xub_StrLen nPos ) const
325 if ( xCC.is() )
326 return xCC->getType( rStr, nPos );
327 else
328 return 0;
330 catch ( const Exception& )
332 SAL_WARN( "unotools.i18n", "getType: Exception caught!" );
333 return 0;
338 sal_Int16 CharClass::getCharacterDirection( const String& rStr, xub_StrLen nPos ) const
342 if ( xCC.is() )
343 return xCC->getCharacterDirection( rStr, nPos );
344 else
345 return 0;
347 catch ( const Exception& )
349 SAL_WARN( "unotools.i18n", "getCharacterDirection: Exception caught!" );
350 return 0;
355 sal_Int16 CharClass::getScript( const String& rStr, xub_StrLen nPos ) const
359 if ( xCC.is() )
360 return xCC->getScript( rStr, nPos );
361 else
362 return 0;
364 catch ( const Exception& )
366 SAL_WARN( "unotools.i18n", "getScript: Exception caught!" );
367 return 0;
372 sal_Int32 CharClass::getCharacterType( const String& rStr, xub_StrLen nPos ) const
376 if ( xCC.is() )
377 return xCC->getCharacterType( rStr, nPos, getMyLocale() );
378 else
379 return 0;
381 catch ( const Exception& )
383 SAL_WARN( "unotools.i18n", "getCharacterType: Exception caught!" );
384 return 0;
389 sal_Int32 CharClass::getStringType( const String& rStr, xub_StrLen nPos, xub_StrLen nCount ) const
393 if ( xCC.is() )
394 return xCC->getStringType( rStr, nPos, nCount, getMyLocale() );
395 else
396 return 0;
398 catch ( const Exception& )
400 SAL_WARN( "unotools.i18n", "getStringType: Exception caught!" );
401 return 0;
406 ::com::sun::star::i18n::ParseResult CharClass::parseAnyToken(
407 const String& rStr,
408 sal_Int32 nPos,
409 sal_Int32 nStartCharFlags,
410 const String& userDefinedCharactersStart,
411 sal_Int32 nContCharFlags,
412 const String& userDefinedCharactersCont ) const
416 if ( xCC.is() )
417 return xCC->parseAnyToken( rStr, nPos, getMyLocale(),
418 nStartCharFlags, userDefinedCharactersStart,
419 nContCharFlags, userDefinedCharactersCont );
420 else
421 return ParseResult();
423 catch ( const Exception& e )
425 SAL_WARN( "unotools.i18n", "parseAnyToken: Exception caught " << e.Message );
426 return ParseResult();
431 ::com::sun::star::i18n::ParseResult CharClass::parsePredefinedToken(
432 sal_Int32 nTokenType,
433 const String& rStr,
434 sal_Int32 nPos,
435 sal_Int32 nStartCharFlags,
436 const String& userDefinedCharactersStart,
437 sal_Int32 nContCharFlags,
438 const String& userDefinedCharactersCont ) const
442 if ( xCC.is() )
443 return xCC->parsePredefinedToken( nTokenType, rStr, nPos, getMyLocale(),
444 nStartCharFlags, userDefinedCharactersStart,
445 nContCharFlags, userDefinedCharactersCont );
446 else
447 return ParseResult();
449 catch ( const Exception& e )
451 SAL_WARN( "unotools.i18n", "parsePredefinedToken: Exception caught " << e.Message );
452 return ParseResult();
458 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */