1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
34 const Reference
< uno::XComponentContext
> & rxContext
,
35 const LanguageTag
& rLanguageTag
38 maLanguageTag( rLanguageTag
)
40 xCC
= CharacterClassification::create( rxContext
);
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
);
72 const ::com::sun::star::lang::Locale
& CharClass::getMyLocale() const
74 ::osl::MutexGuard
aGuard( aMutex
);
75 return maLanguageTag
.getLocale();
80 sal_Bool
CharClass::isAsciiNumeric( const String
& rStr
)
84 register const sal_Unicode
* p
= rStr
.GetBuffer();
85 register const sal_Unicode
* const pStop
= p
+ rStr
.Len();
88 if ( !isAsciiDigit( *p
) )
90 } while ( ++p
< pStop
);
96 sal_Bool
CharClass::isAsciiAlpha( const String
& rStr
)
100 register const sal_Unicode
* p
= rStr
.GetBuffer();
101 register const sal_Unicode
* const pStop
= p
+ rStr
.Len();
104 if ( !isAsciiAlpha( *p
) )
106 } while ( ++p
< pStop
);
112 sal_Bool
CharClass::isAlpha( const String
& rStr
, xub_StrLen nPos
) const
114 sal_Unicode c
= rStr
.GetChar( nPos
);
116 return isAsciiAlpha( c
);
121 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
122 nCharClassAlphaType
) != 0;
126 catch ( const Exception
& )
128 SAL_WARN( "unotools.i18n", "isAlpha: Exception caught!" );
135 sal_Bool
CharClass::isLetter( const String
& rStr
, xub_StrLen nPos
) const
137 sal_Unicode c
= rStr
.GetChar( nPos
);
139 return isAsciiAlpha( c
);
144 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
145 nCharClassLetterType
) != 0;
149 catch ( const Exception
& )
151 SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
157 sal_Bool
CharClass::isLetter( const String
& rStr
) const
162 return isLetterType( xCC
->getStringType( rStr
, 0, rStr
.Len(), getMyLocale() ) );
166 catch ( const Exception
& )
168 SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
174 sal_Bool
CharClass::isDigit( const String
& rStr
, xub_StrLen nPos
) const
176 sal_Unicode c
= rStr
.GetChar( nPos
);
178 return isAsciiDigit( c
);
183 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
184 KCharacterType::DIGIT
) != 0;
188 catch ( const Exception
& )
190 SAL_WARN( "unotools.i18n", "isDigit: Exception caught!" );
196 sal_Bool
CharClass::isNumeric( const String
& rStr
) const
201 return isNumericType( xCC
->getStringType( rStr
, 0, rStr
.Len(), getMyLocale() ) );
205 catch ( const Exception
& )
207 SAL_WARN( "unotools.i18n", "isNumeric: Exception caught!" );
213 sal_Bool
CharClass::isAlphaNumeric( const String
& rStr
, xub_StrLen nPos
) const
215 sal_Unicode c
= rStr
.GetChar( nPos
);
217 return isAsciiAlphaNumeric( c
);
222 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
223 (nCharClassAlphaType
| KCharacterType::DIGIT
)) != 0;
227 catch ( const Exception
& )
229 SAL_WARN( "unotools.i18n", "isAlphaNumeric: Exception caught!" );
235 sal_Bool
CharClass::isLetterNumeric( const String
& rStr
, xub_StrLen nPos
) const
237 sal_Unicode c
= rStr
.GetChar( nPos
);
239 return isAsciiAlphaNumeric( c
);
244 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
245 (nCharClassLetterType
| KCharacterType::DIGIT
)) != 0;
249 catch ( const Exception
& )
251 SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
257 sal_Bool
CharClass::isLetterNumeric( const String
& rStr
) const
262 return isLetterNumericType( xCC
->getStringType( rStr
, 0, rStr
.Len(), getMyLocale() ) );
266 catch ( const Exception
& )
268 SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
273 OUString
CharClass::titlecase(const OUString
& rStr
, sal_Int32 nPos
, sal_Int32 nCount
) const
278 return xCC
->toTitle( rStr
, nPos
, nCount
, getMyLocale() );
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
294 return xCC
->toUpper( rStr
, nPos
, nCount
, getMyLocale() );
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
310 return xCC
->toLower( rStr
, nPos
, nCount
, getMyLocale() );
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
326 return xCC
->getType( rStr
, nPos
);
330 catch ( const Exception
& )
332 SAL_WARN( "unotools.i18n", "getType: Exception caught!" );
338 sal_Int16
CharClass::getCharacterDirection( const String
& rStr
, xub_StrLen nPos
) const
343 return xCC
->getCharacterDirection( rStr
, nPos
);
347 catch ( const Exception
& )
349 SAL_WARN( "unotools.i18n", "getCharacterDirection: Exception caught!" );
355 sal_Int16
CharClass::getScript( const String
& rStr
, xub_StrLen nPos
) const
360 return xCC
->getScript( rStr
, nPos
);
364 catch ( const Exception
& )
366 SAL_WARN( "unotools.i18n", "getScript: Exception caught!" );
372 sal_Int32
CharClass::getCharacterType( const String
& rStr
, xub_StrLen nPos
) const
377 return xCC
->getCharacterType( rStr
, nPos
, getMyLocale() );
381 catch ( const Exception
& )
383 SAL_WARN( "unotools.i18n", "getCharacterType: Exception caught!" );
389 sal_Int32
CharClass::getStringType( const String
& rStr
, xub_StrLen nPos
, xub_StrLen nCount
) const
394 return xCC
->getStringType( rStr
, nPos
, nCount
, getMyLocale() );
398 catch ( const Exception
& )
400 SAL_WARN( "unotools.i18n", "getStringType: Exception caught!" );
406 ::com::sun::star::i18n::ParseResult
CharClass::parseAnyToken(
409 sal_Int32 nStartCharFlags
,
410 const String
& userDefinedCharactersStart
,
411 sal_Int32 nContCharFlags
,
412 const String
& userDefinedCharactersCont
) const
417 return xCC
->parseAnyToken( rStr
, nPos
, getMyLocale(),
418 nStartCharFlags
, userDefinedCharactersStart
,
419 nContCharFlags
, userDefinedCharactersCont
);
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
,
435 sal_Int32 nStartCharFlags
,
436 const String
& userDefinedCharactersStart
,
437 sal_Int32 nContCharFlags
,
438 const String
& userDefinedCharactersCont
) const
443 return xCC
->parsePredefinedToken( nTokenType
, rStr
, nPos
, getMyLocale(),
444 nStartCharFlags
, userDefinedCharactersStart
,
445 nContCharFlags
, userDefinedCharactersCont
);
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: */