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