Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unotools / source / i18n / charclass.cxx
blob423f9530f2cb440075f7d84741897e2e56d74a2a
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 .
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>
26 #include <utility>
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::i18n;
30 using namespace ::com::sun::star::uno;
32 CharClass::CharClass(
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
53 return maLanguageTag;
56 const css::lang::Locale& CharClass::getMyLocale() const
58 return maLanguageTag.getLocale();
61 // static
62 bool CharClass::isAsciiNumeric( std::u16string_view rStr )
64 if ( rStr.empty() )
65 return false;
66 const sal_Unicode* p = rStr.data();
67 const sal_Unicode* const pStop = p + rStr.size();
71 if ( !rtl::isAsciiDigit( *p ) )
72 return false;
74 while ( ++p < pStop );
76 return true;
79 // static
80 bool CharClass::isAsciiAlpha( std::u16string_view rStr )
82 if ( rStr.empty() )
83 return false;
84 const sal_Unicode* p = rStr.data();
85 const sal_Unicode* const pStop = p + rStr.size();
89 if ( !rtl::isAsciiAlpha( *p ) )
90 return false;
92 while ( ++p < pStop );
94 return true;
97 bool CharClass::isAlpha( const OUString& rStr, sal_Int32 nPos ) const
99 sal_Unicode c = rStr[nPos];
100 if ( c < 128 )
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", "" );
112 return false;
115 bool CharClass::isLetter( const OUString& rStr, sal_Int32 nPos ) const
117 sal_Unicode c = rStr[nPos];
118 if ( c < 128 )
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", "" );
130 return false;
133 bool CharClass::isLetter( const OUString& rStr ) const
135 if (rStr.isEmpty())
136 return false;
140 sal_Int32 nPos = 0;
141 while (nPos < rStr.getLength())
143 if (!isLetter( rStr, nPos))
144 return false;
145 rStr.iterateCodePoints( &nPos);
147 return true;
149 catch ( const Exception& )
151 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
153 return false;
156 bool CharClass::isDigit( const OUString& rStr, sal_Int32 nPos ) const
158 sal_Unicode c = rStr[ nPos ];
159 if ( c < 128 )
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", "" );
171 return false;
174 bool CharClass::isNumeric( const OUString& rStr ) const
176 if (rStr.isEmpty())
177 return false;
181 sal_Int32 nPos = 0;
182 while (nPos < rStr.getLength())
184 if (!isDigit( rStr, nPos))
185 return false;
186 rStr.iterateCodePoints( &nPos);
188 return true;
190 catch ( const Exception& )
192 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
194 return false;
197 bool CharClass::isAlphaNumeric( const OUString& rStr, sal_Int32 nPos ) const
199 sal_Unicode c = rStr[nPos];
200 if ( c < 128 )
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", "" );
212 return false;
215 bool CharClass::isLetterNumeric( const OUString& rStr, sal_Int32 nPos ) const
217 sal_Unicode c = rStr[nPos];
218 if ( c < 128 )
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", "" );
230 return false;
233 bool CharClass::isLetterNumeric( const OUString& rStr ) const
235 if (rStr.isEmpty())
236 return false;
240 sal_Int32 nPos = 0;
241 while (nPos < rStr.getLength())
243 if (!isLetterNumeric( rStr, nPos))
244 return false;
245 rStr.iterateCodePoints( &nPos);
247 return true;
249 catch ( const Exception& )
251 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
253 return false;
256 bool CharClass::isBase( const OUString& rStr, sal_Int32 nPos ) const
258 sal_Unicode c = rStr[nPos];
259 if ( c < 128 )
260 return rtl::isAsciiAlphanumeric( c );
264 return (xCC->getCharacterType( rStr, nPos, getMyLocale() ) & nCharClassBaseType ) != 0;
266 catch ( const Exception& )
268 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
270 return false;
273 bool CharClass::isUpper( const OUString& rStr, sal_Int32 nPos ) const
275 sal_Unicode c = rStr[nPos];
276 if ( c < 128 )
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", "" );
288 return false;
291 bool CharClass::isUpper( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
293 if (rStr.isEmpty())
294 return false;
296 assert(nPos >= 0 && nPos < rStr.getLength() && nCount > 0);
297 if (nPos < 0 || nPos >= rStr.getLength() || nCount == 0)
298 return false;
302 const sal_Int32 nLen = std::min( nPos + nCount, rStr.getLength());
303 while (nPos < nLen)
305 if (!isUpper( rStr, nPos))
306 return false;
307 rStr.iterateCodePoints( &nPos);
309 return true;
311 catch ( const Exception& )
313 TOOLS_WARN_EXCEPTION("unotools.i18n", "" );
315 return false;
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", "" );
367 return 0;
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", "" );
406 return 0;
409 css::i18n::ParseResult CharClass::parseAnyToken(
410 const OUString& rStr,
411 sal_Int32 nPos,
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,
433 sal_Int32 nPos,
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: */