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 : #include "unotools/unotoolsdllapi.h"
20 :
21 : #ifndef _UNOTOOLS_CHARCLASS_HXX
22 : #define _UNOTOOLS_CHARCLASS_HXX
23 :
24 : #include <boost/noncopyable.hpp>
25 : #include <i18nlangtag/languagetag.hxx>
26 : #include <tools/string.hxx>
27 : #include <tools/solar.h>
28 : #include <com/sun/star/i18n/KCharacterType.hpp>
29 : #include <com/sun/star/i18n/KParseTokens.hpp>
30 : #include <com/sun/star/i18n/KParseType.hpp>
31 : #include <com/sun/star/i18n/ParseResult.hpp>
32 : #include <com/sun/star/i18n/XCharacterClassification.hpp>
33 : #include <osl/mutex.hxx>
34 : #include <rtl/character.hxx>
35 :
36 :
37 : namespace com { namespace sun { namespace star {
38 : namespace uno {
39 : class XComponentContext;
40 : }
41 : }}}
42 :
43 : const sal_Int32 nCharClassAlphaType =
44 : ::com::sun::star::i18n::KCharacterType::UPPER |
45 : ::com::sun::star::i18n::KCharacterType::LOWER |
46 : ::com::sun::star::i18n::KCharacterType::TITLE_CASE;
47 :
48 : const sal_Int32 nCharClassAlphaTypeMask =
49 : nCharClassAlphaType |
50 : ::com::sun::star::i18n::KCharacterType::PRINTABLE |
51 : ::com::sun::star::i18n::KCharacterType::BASE_FORM;
52 :
53 : const sal_Int32 nCharClassLetterType =
54 : nCharClassAlphaType |
55 : ::com::sun::star::i18n::KCharacterType::LETTER;
56 :
57 : const sal_Int32 nCharClassLetterTypeMask =
58 : nCharClassAlphaTypeMask |
59 : ::com::sun::star::i18n::KCharacterType::LETTER;
60 :
61 : const sal_Int32 nCharClassNumericType =
62 : ::com::sun::star::i18n::KCharacterType::DIGIT;
63 :
64 : const sal_Int32 nCharClassNumericTypeMask =
65 : nCharClassNumericType |
66 : ::com::sun::star::i18n::KCharacterType::PRINTABLE |
67 : ::com::sun::star::i18n::KCharacterType::BASE_FORM;
68 :
69 :
70 : class UNOTOOLS_DLLPUBLIC CharClass : private boost::noncopyable
71 : {
72 : LanguageTag maLanguageTag;
73 : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCharacterClassification > xCC;
74 : mutable ::osl::Mutex aMutex;
75 :
76 : public:
77 : /// Preferred ctor with service manager specified
78 : CharClass(
79 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > & rxContext,
80 : const LanguageTag& rLanguageTag );
81 :
82 : /// Depricated ctor, tries to get a process service manager or to load the
83 : /// library directly.
84 : CharClass( const LanguageTag& rLanguageTag );
85 :
86 : ~CharClass();
87 :
88 : /// set a new Locale
89 : void setLanguageTag( const LanguageTag& rLanguageTag );
90 :
91 : /// get current Locale
92 : const LanguageTag& getLanguageTag() const;
93 :
94 :
95 : /// isdigit() on ascii values
96 57824 : static inline bool isAsciiDigit( sal_Unicode c )
97 : {
98 57824 : return rtl::isAsciiDigit( c );
99 : }
100 :
101 : /// isalpha() on ascii values
102 465402 : static inline bool isAsciiAlpha( sal_Unicode c )
103 : {
104 465402 : return rtl::isAsciiAlpha( c );
105 : }
106 :
107 : /// isalnum() on ascii values
108 23437 : static inline bool isAsciiAlphaNumeric( sal_Unicode c )
109 : {
110 23437 : return rtl::isAsciiAlphanumeric( c );
111 : }
112 :
113 : /// isdigit() on ascii values of entire string
114 : static bool isAsciiNumeric( const OUString& rStr );
115 :
116 : /// isalpha() on ascii values of entire string
117 : static bool isAsciiAlpha( const OUString& rStr );
118 :
119 : /// isalnum() on ascii values of entire string
120 : static bool isAsciiAlphaNumeric( const OUString& rStr );
121 :
122 : /// whether type is pure alpha or not, e.g. return of getStringType
123 : static inline bool isAlphaType( sal_Int32 nType )
124 : {
125 : return ((nType & nCharClassAlphaType) != 0) &&
126 : ((nType & ~(nCharClassAlphaTypeMask)) == 0);
127 : }
128 :
129 : /// whether type is pure numeric or not, e.g. return of getStringType
130 40 : static inline bool isNumericType( sal_Int32 nType )
131 : {
132 40 : return ((nType & nCharClassNumericType) != 0) &&
133 40 : ((nType & ~(nCharClassNumericTypeMask)) == 0);
134 : }
135 :
136 : /// whether type is pure alphanumeric or not, e.g. return of getStringType
137 0 : static inline bool isAlphaNumericType( sal_Int32 nType )
138 : {
139 0 : return ((nType & (nCharClassAlphaType |
140 0 : nCharClassNumericType)) != 0) &&
141 0 : ((nType & ~(nCharClassAlphaTypeMask |
142 0 : nCharClassNumericTypeMask)) == 0);
143 : }
144 :
145 : /// whether type is pure letter or not, e.g. return of getStringType
146 14 : static inline bool isLetterType( sal_Int32 nType )
147 : {
148 28 : return ((nType & nCharClassLetterType) != 0) &&
149 28 : ((nType & ~(nCharClassLetterTypeMask)) == 0);
150 : }
151 :
152 : /// whether type is pure letternumeric or not, e.g. return of getStringType
153 10966 : static inline bool isLetterNumericType( sal_Int32 nType )
154 : {
155 10966 : return ((nType & (nCharClassLetterType |
156 20373 : nCharClassNumericType)) != 0) &&
157 9407 : ((nType & ~(nCharClassLetterTypeMask |
158 10966 : nCharClassNumericTypeMask)) == 0);
159 : }
160 :
161 :
162 : // Wrapper implementations of class CharacterClassification
163 :
164 : OUString uppercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
165 : OUString lowercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
166 : OUString titlecase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
167 :
168 316217 : OUString uppercase( const OUString& _rStr ) const
169 : {
170 316217 : return uppercase(_rStr, 0, _rStr.getLength());
171 : }
172 27743 : OUString lowercase( const OUString& _rStr ) const
173 : {
174 27743 : return lowercase(_rStr, 0, _rStr.getLength());
175 : }
176 0 : OUString titlecase( const OUString& _rStr ) const
177 : {
178 0 : return titlecase(_rStr, 0, _rStr.getLength());
179 : }
180 :
181 : sal_Int16 getType( const OUString& rStr, sal_Int32 nPos ) const;
182 : sal_Int16 getCharacterDirection( const OUString& rStr, sal_Int32 nPos ) const;
183 : sal_Int16 getScript( const OUString& rStr, sal_Int32 nPos ) const;
184 : sal_Int32 getCharacterType( const OUString& rStr, sal_Int32 nPos ) const;
185 : sal_Int32 getStringType( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
186 :
187 : ::com::sun::star::i18n::ParseResult parseAnyToken(
188 : const OUString& rStr,
189 : sal_Int32 nPos,
190 : sal_Int32 nStartCharFlags,
191 : const OUString& userDefinedCharactersStart,
192 : sal_Int32 nContCharFlags,
193 : const OUString& userDefinedCharactersCont ) const;
194 :
195 : ::com::sun::star::i18n::ParseResult parsePredefinedToken(
196 : sal_Int32 nTokenType,
197 : const OUString& rStr,
198 : sal_Int32 nPos,
199 : sal_Int32 nStartCharFlags,
200 : const OUString& userDefinedCharactersStart,
201 : sal_Int32 nContCharFlags,
202 : const OUString& userDefinedCharactersCont ) const;
203 :
204 :
205 : // Functionality of class International methods
206 :
207 : bool isAlpha( const OUString& rStr, sal_Int32 nPos ) const;
208 : bool isLetter( const OUString& rStr, sal_Int32 nPos ) const;
209 : bool isDigit( const OUString& rStr, sal_Int32 nPos ) const;
210 : bool isAlphaNumeric( const OUString& rStr, sal_Int32 nPos ) const;
211 : bool isLetterNumeric( const OUString& rStr, sal_Int32 nPos ) const;
212 : bool isAlpha( const OUString& rStr ) const;
213 : bool isLetter( const OUString& rStr ) const;
214 : bool isNumeric( const OUString& rStr ) const;
215 : bool isAlphaNumeric( const OUString& rStr ) const;
216 : bool isLetterNumeric( const OUString& rStr ) const;
217 :
218 : private:
219 :
220 : const ::com::sun::star::lang::Locale & getMyLocale() const;
221 : };
222 :
223 : #endif // _UNOTOOLS_CHARCLASS_HXX
224 :
225 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|