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 .
20 #include <comphelper/processfactory.hxx>
21 #include <unotools/charclass.hxx>
22 #include <rtl/character.hxx>
23 #include <comphelper/diagnose_ex.hxx>
25 #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 const Reference
< uno::XComponentContext
> & rxContext
,
34 LanguageTag aLanguageTag
36 : maLanguageTag(std::move( aLanguageTag
))
38 xCC
= CharacterClassification::create( rxContext
);
41 CharClass::CharClass( LanguageTag aLanguageTag
)
42 : maLanguageTag(std::move( aLanguageTag
))
44 xCC
= CharacterClassification::create( comphelper::getProcessComponentContext() );
47 CharClass::~CharClass()
51 const LanguageTag
& CharClass::getLanguageTag() const
56 const css::lang::Locale
& CharClass::getMyLocale() const
58 return maLanguageTag
.getLocale();
62 bool CharClass::isAsciiNumeric( std::u16string_view rStr
)
66 const sal_Unicode
* p
= rStr
.data();
67 const sal_Unicode
* const pStop
= p
+ rStr
.size();
71 if ( !rtl::isAsciiDigit( *p
) )
74 while ( ++p
< pStop
);
80 bool CharClass::isAsciiAlpha( std::u16string_view rStr
)
84 const sal_Unicode
* p
= rStr
.data();
85 const sal_Unicode
* const pStop
= p
+ rStr
.size();
89 if ( !rtl::isAsciiAlpha( *p
) )
92 while ( ++p
< pStop
);
97 bool CharClass::isAlpha( const OUString
& rStr
, sal_Int32 nPos
) const
99 sal_Unicode c
= rStr
[nPos
];
101 return rtl::isAsciiAlpha( c
);
105 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
106 nCharClassAlphaType
) != 0;
108 catch ( const Exception
& )
110 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
115 bool CharClass::isLetter( const OUString
& rStr
, sal_Int32 nPos
) const
117 sal_Unicode c
= rStr
[nPos
];
119 return rtl::isAsciiAlpha( c
);
123 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
124 nCharClassLetterType
) != 0;
126 catch ( const Exception
& )
128 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
133 bool CharClass::isLetter( const OUString
& rStr
) const
141 while (nPos
< rStr
.getLength())
143 if (!isLetter( rStr
, nPos
))
145 rStr
.iterateCodePoints( &nPos
);
149 catch ( const Exception
& )
151 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
156 bool CharClass::isDigit( const OUString
& rStr
, sal_Int32 nPos
) const
158 sal_Unicode c
= rStr
[ nPos
];
160 return rtl::isAsciiDigit( c
);
164 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
165 KCharacterType::DIGIT
) != 0;
167 catch ( const Exception
& )
169 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
174 bool CharClass::isNumeric( const OUString
& rStr
) const
182 while (nPos
< rStr
.getLength())
184 if (!isDigit( rStr
, nPos
))
186 rStr
.iterateCodePoints( &nPos
);
190 catch ( const Exception
& )
192 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
197 bool CharClass::isAlphaNumeric( const OUString
& rStr
, sal_Int32 nPos
) const
199 sal_Unicode c
= rStr
[nPos
];
201 return rtl::isAsciiAlphanumeric( c
);
205 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
206 (nCharClassAlphaType
| nCharClassNumericType
)) != 0;
208 catch ( const Exception
& )
210 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
215 bool CharClass::isLetterNumeric( const OUString
& rStr
, sal_Int32 nPos
) const
217 sal_Unicode c
= rStr
[nPos
];
219 return rtl::isAsciiAlphanumeric( c
);
223 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) &
224 (nCharClassLetterType
| nCharClassNumericType
)) != 0;
226 catch ( const Exception
& )
228 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
233 bool CharClass::isLetterNumeric( const OUString
& rStr
) const
241 while (nPos
< rStr
.getLength())
243 if (!isLetterNumeric( rStr
, nPos
))
245 rStr
.iterateCodePoints( &nPos
);
249 catch ( const Exception
& )
251 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
256 bool CharClass::isBase( const OUString
& rStr
, sal_Int32 nPos
) const
258 sal_Unicode c
= rStr
[nPos
];
260 return rtl::isAsciiAlphanumeric( c
);
264 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale() ) & nCharClassBaseType
) != 0;
266 catch ( const Exception
& )
268 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
273 bool CharClass::isUpper( const OUString
& rStr
, sal_Int32 nPos
) const
275 sal_Unicode c
= rStr
[nPos
];
277 return rtl::isAsciiUpperCase(c
);
281 return (xCC
->getCharacterType( rStr
, nPos
, getMyLocale()) &
282 KCharacterType::UPPER
) != 0;
284 catch ( const Exception
& )
286 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
291 bool CharClass::isUpper( const OUString
& rStr
, sal_Int32 nPos
, sal_Int32 nCount
) const
296 assert(nPos
>= 0 && nPos
< rStr
.getLength() && nCount
> 0);
297 if (nPos
< 0 || nPos
>= rStr
.getLength() || nCount
== 0)
302 const sal_Int32 nLen
= std::min( nPos
+ nCount
, rStr
.getLength());
305 if (!isUpper( rStr
, nPos
))
307 rStr
.iterateCodePoints( &nPos
);
311 catch ( const Exception
& )
313 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
318 OUString
CharClass::titlecase(const OUString
& rStr
, sal_Int32 nPos
, sal_Int32 nCount
) const
322 return xCC
->toTitle( rStr
, nPos
, nCount
, getMyLocale() );
324 catch ( const Exception
& )
326 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
328 return rStr
.copy( nPos
, nCount
);
331 OUString
CharClass::uppercase( const OUString
& rStr
, sal_Int32 nPos
, sal_Int32 nCount
) const
335 return xCC
->toUpper( rStr
, nPos
, nCount
, getMyLocale() );
337 catch ( const Exception
& )
339 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
341 return rStr
.copy( nPos
, nCount
);
344 OUString
CharClass::lowercase( const OUString
& rStr
, sal_Int32 nPos
, sal_Int32 nCount
) const
348 return xCC
->toLower( rStr
, nPos
, nCount
, getMyLocale() );
350 catch ( const Exception
& )
352 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
354 return rStr
.copy( nPos
, nCount
);
357 sal_Int16
CharClass::getType( const OUString
& rStr
, sal_Int32 nPos
) const
361 return xCC
->getType( rStr
, nPos
);
363 catch ( const Exception
& )
365 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
370 css::i18n::DirectionProperty
CharClass::getCharacterDirection( const OUString
& rStr
, sal_Int32 nPos
) const
374 return static_cast<css::i18n::DirectionProperty
>(xCC
->getCharacterDirection( rStr
, nPos
));
376 catch ( const Exception
& )
378 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
380 return css::i18n::DirectionProperty_LEFT_TO_RIGHT
;
383 css::i18n::UnicodeScript
CharClass::getScript( const OUString
& rStr
, sal_Int32 nPos
) const
387 return static_cast<css::i18n::UnicodeScript
>(xCC
->getScript( rStr
, nPos
));
389 catch ( const Exception
& )
391 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
393 return UnicodeScript_kBasicLatin
;
396 sal_Int32
CharClass::getCharacterType( const OUString
& rStr
, sal_Int32 nPos
) const
400 return xCC
->getCharacterType( rStr
, nPos
, getMyLocale() );
402 catch ( const Exception
& )
404 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
409 css::i18n::ParseResult
CharClass::parseAnyToken(
410 const OUString
& rStr
,
412 sal_Int32 nStartCharFlags
,
413 const OUString
& userDefinedCharactersStart
,
414 sal_Int32 nContCharFlags
,
415 const OUString
& userDefinedCharactersCont
) const
419 return xCC
->parseAnyToken( rStr
, nPos
, getMyLocale(),
420 nStartCharFlags
, userDefinedCharactersStart
,
421 nContCharFlags
, userDefinedCharactersCont
);
423 catch ( const Exception
& )
425 TOOLS_WARN_EXCEPTION( "unotools.i18n", "parseAnyToken" );
427 return ParseResult();
430 css::i18n::ParseResult
CharClass::parsePredefinedToken(
431 sal_Int32 nTokenType
,
432 const OUString
& rStr
,
434 sal_Int32 nStartCharFlags
,
435 const OUString
& userDefinedCharactersStart
,
436 sal_Int32 nContCharFlags
,
437 const OUString
& userDefinedCharactersCont
) const
441 return xCC
->parsePredefinedToken( nTokenType
, rStr
, nPos
, getMyLocale(),
442 nStartCharFlags
, userDefinedCharactersStart
,
443 nContCharFlags
, userDefinedCharactersCont
);
445 catch ( const Exception
& )
447 TOOLS_WARN_EXCEPTION( "unotools.i18n", "parsePredefinedToken" );
449 return ParseResult();
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */