xref: /trunk/main/unotools/inc/unotools/charclass.hxx (revision bae3752e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "unotools/unotoolsdllapi.h"
24 
25 #ifndef _UNOTOOLS_CHARCLASS_HXX
26 #define _UNOTOOLS_CHARCLASS_HXX
27 
28 #include <ctype.h>		// isdigit(), isalpha()
29 #include <tools/string.hxx>
30 #include <tools/solar.h>
31 #include <com/sun/star/i18n/KCharacterType.hpp>
32 #include <com/sun/star/i18n/KParseTokens.hpp>
33 #include <com/sun/star/i18n/KParseType.hpp>
34 #include <com/sun/star/i18n/ParseResult.hpp>
35 #include <com/sun/star/i18n/XCharacterClassification.hpp>
36 #include <osl/mutex.hxx>
37 
38 class String;
39 namespace com { namespace sun { namespace star {
40 	namespace lang {
41 		class XMultiServiceFactory;
42 	}
43 }}}
44 
45 const sal_Int32 nCharClassAlphaType =
46 	::com::sun::star::i18n::KCharacterType::UPPER |
47 	::com::sun::star::i18n::KCharacterType::LOWER |
48 	::com::sun::star::i18n::KCharacterType::TITLE_CASE;
49 
50 const sal_Int32 nCharClassAlphaTypeMask =
51 	nCharClassAlphaType |
52 	::com::sun::star::i18n::KCharacterType::PRINTABLE |
53 	::com::sun::star::i18n::KCharacterType::BASE_FORM;
54 
55 const sal_Int32 nCharClassLetterType =
56 	nCharClassAlphaType |
57 	::com::sun::star::i18n::KCharacterType::LETTER;
58 
59 const sal_Int32 nCharClassLetterTypeMask =
60 	nCharClassAlphaTypeMask |
61 	::com::sun::star::i18n::KCharacterType::LETTER;
62 
63 const sal_Int32 nCharClassNumericType =
64 	::com::sun::star::i18n::KCharacterType::DIGIT;
65 
66 const sal_Int32 nCharClassNumericTypeMask =
67 	nCharClassNumericType |
68 	::com::sun::star::i18n::KCharacterType::PRINTABLE |
69 	::com::sun::star::i18n::KCharacterType::BASE_FORM;
70 
71 
72 class UNOTOOLS_DLLPUBLIC CharClass
73 {
74 	::com::sun::star::lang::Locale	aLocale;
75 	::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCharacterClassification >	xCC;
76 	::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
77     mutable ::osl::Mutex        aMutex;
78 
79 								// not implemented, prevent usage
80 								CharClass( const CharClass& );
81 			CharClass&			operator=( const CharClass& );
82 
83 								// instantiate component somehow
84 			void				getComponentInstance();
85 
86 public:
87 	/// Preferred ctor with service manager specified
88 								CharClass(
89 									const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > & xSF,
90 									const ::com::sun::star::lang::Locale& rLocale
91 									);
92 
93 	/// Depricated ctor, tries to get a process service manager or to load the
94 	/// library directly.
95 								CharClass(
96 									const ::com::sun::star::lang::Locale& rLocale
97 									);
98 
99 								~CharClass();
100 
101 	/// set a new Locale
102 			void				setLocale( const ::com::sun::star::lang::Locale& rLocale );
103 
104 	/// get current Locale
105     const ::com::sun::star::lang::Locale& getLocale() const;
106 
107 
108 	/// isdigit() on ascii values
isAsciiDigit(sal_Unicode c)109 	static	inline	sal_Bool	isAsciiDigit( sal_Unicode c )
110 		{ return c < 128 ? sal_Bool(isdigit( (unsigned char) c ) != 0) : sal_False; }
111 
112 	/// isalpha() on ascii values
isAsciiAlpha(sal_Unicode c)113 	static	inline	sal_Bool	isAsciiAlpha( sal_Unicode c )
114         { return c < 128 ? sal_Bool(isalpha( (unsigned char) c ) != 0) : sal_False; }
115 
116 	/// isalnum() on ascii values
isAsciiAlphaNumeric(sal_Unicode c)117 	static	inline	sal_Bool	isAsciiAlphaNumeric( sal_Unicode c )
118         { return c < 128 ? sal_Bool(isalnum( (unsigned char) c ) != 0) : sal_False; }
119 
120 	/// isdigit() on ascii values of entire string
121 	static	sal_Bool			isAsciiNumeric( const String& rStr );
122 
123 	/// isalpha() on ascii values of entire string
124 	static	sal_Bool			isAsciiAlpha( const String& rStr );
125 
126 	/// isalnum() on ascii values of entire string
127 	static	sal_Bool			isAsciiAlphaNumeric( const String& rStr );
128 
129 	/// whether type is pure alpha or not, e.g. return of getStringType
isAlphaType(sal_Int32 nType)130 	static	inline	sal_Bool	isAlphaType( sal_Int32 nType )
131 		{
132 			return ((nType & nCharClassAlphaType) != 0) &&
133 				((nType & ~(nCharClassAlphaTypeMask)) == 0);
134 		}
135 
136 	/// whether type is pure numeric or not, e.g. return of getStringType
isNumericType(sal_Int32 nType)137 	static	inline	sal_Bool	isNumericType( sal_Int32 nType )
138 		{
139 			return ((nType & nCharClassNumericType) != 0) &&
140 				((nType & ~(nCharClassNumericTypeMask)) == 0);
141 		}
142 
143 	/// whether type is pure alphanumeric or not, e.g. return of getStringType
isAlphaNumericType(sal_Int32 nType)144 	static	inline	sal_Bool	isAlphaNumericType( sal_Int32 nType )
145 		{
146 			return ((nType & (nCharClassAlphaType |
147 				nCharClassNumericType)) != 0) &&
148 				((nType & ~(nCharClassAlphaTypeMask |
149 				nCharClassNumericTypeMask)) == 0);
150 		}
151 
152 	/// whether type is pure letter or not, e.g. return of getStringType
isLetterType(sal_Int32 nType)153 	static	inline	sal_Bool	isLetterType( sal_Int32 nType )
154 		{
155 			return ((nType & nCharClassLetterType) != 0) &&
156 				((nType & ~(nCharClassLetterTypeMask)) == 0);
157 		}
158 
159 	/// whether type is pure letternumeric or not, e.g. return of getStringType
isLetterNumericType(sal_Int32 nType)160 	static	inline	sal_Bool	isLetterNumericType( sal_Int32 nType )
161 		{
162 			return ((nType & (nCharClassLetterType |
163 				nCharClassNumericType)) != 0) &&
164 				((nType & ~(nCharClassLetterTypeMask |
165 				nCharClassNumericTypeMask)) == 0);
166 		}
167 
168 
169 	// Wrapper implementations of class CharacterClassification
170 
171 			String				toUpper( const String& rStr, xub_StrLen nPos, xub_StrLen nCount ) const;
172 			String				toLower( const String& rStr, xub_StrLen nPos, xub_StrLen nCount ) const;
173 			String				toTitle( const String& rStr, xub_StrLen nPos, xub_StrLen nCount ) const;
174 
175 			::rtl::OUString		toUpper_rtl( const ::rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
176 			::rtl::OUString		toLower_rtl( const ::rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
177 				// use the postfix because compilers could get confused by the both similar versions
178 				// (me thinks they shouldn't, but in fact MSCV 6 does)
179 
180 			sal_Int16			getType( const String& rStr, xub_StrLen nPos ) const;
181 			sal_Int16			getCharacterDirection( const String& rStr, xub_StrLen nPos ) const;
182 			sal_Int16			getScript( const String& rStr, xub_StrLen nPos ) const;
183 			sal_Int32			getCharacterType( const String& rStr, xub_StrLen nPos ) const;
184 			sal_Int32			getStringType( const String& rStr, xub_StrLen nPos, xub_StrLen nCount ) const;
185 
186 	::com::sun::star::i18n::ParseResult parseAnyToken(
187 									const String& rStr,
188 									sal_Int32 nPos,
189 									sal_Int32 nStartCharFlags,
190 									const String& userDefinedCharactersStart,
191 									sal_Int32 nContCharFlags,
192 									const String& userDefinedCharactersCont ) const;
193 
194 	::com::sun::star::i18n::ParseResult parsePredefinedToken(
195 									sal_Int32 nTokenType,
196 									const String& rStr,
197 									sal_Int32 nPos,
198 									sal_Int32 nStartCharFlags,
199 									const String& userDefinedCharactersStart,
200 									sal_Int32 nContCharFlags,
201 									const String& userDefinedCharactersCont ) const;
202 
203 
204 	// Functionality of class International methods
205 
206 			sal_Bool			isAlpha( const String& rStr, xub_StrLen nPos ) const;
207 			sal_Bool			isLetter( const String& rStr, xub_StrLen nPos ) const;
208 			sal_Bool			isDigit( const String& rStr, xub_StrLen nPos ) const;
209 			sal_Bool			isAlphaNumeric( const String& rStr, xub_StrLen nPos ) const;
210 			sal_Bool			isLetterNumeric( const String& rStr, xub_StrLen nPos ) const;
211 			sal_Bool			isAlpha( const String& rStr ) const;
212 			sal_Bool			isLetter( const String& rStr ) const;
213 			sal_Bool			isNumeric( const String& rStr ) const;
214 			sal_Bool			isAlphaNumeric( const String& rStr ) const;
215 			sal_Bool			isLetterNumeric( const String& rStr ) const;
216 
toUpper(String & rStr) const217 			void				toUpper( String& rStr ) const
218 									{ rStr = toUpper( rStr, 0, rStr.Len() ); }
toLower(String & rStr) const219 			void				toLower( String& rStr ) const
220 									{ rStr = toLower( rStr, 0, rStr.Len() ); }
upper(const String & rStr) const221 	inline	String				upper( const String& rStr ) const
222 									{ return toUpper( rStr, 0, rStr.Len() ); }
lower(const String & rStr) const223 	inline	String				lower( const String& rStr ) const
224 									{ return toLower( rStr, 0, rStr.Len() ); }
225 
toUpper_rtl(::rtl::OUString & _rStr) const226 		const ::rtl::OUString&	toUpper_rtl( ::rtl::OUString& _rStr ) const { return _rStr = toUpper_rtl( _rStr, 0, _rStr.getLength() ); }
toLower_rtl(::rtl::OUString & _rStr) const227 		const ::rtl::OUString&	toLower_rtl( ::rtl::OUString& _rStr ) const { return _rStr = toLower_rtl( _rStr, 0, _rStr.getLength() ); }
228 };
229 
230 
231 
232 #endif // _UNOTOOLS_CHARCLASS_HXX
233