Branch data 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 <ctype.h> // isdigit(), isalpha()
25 : : #include <boost/noncopyable.hpp>
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 : :
35 : : class String;
36 : : namespace com { namespace sun { namespace star {
37 : : namespace lang {
38 : : class XMultiServiceFactory;
39 : : }
40 : : }}}
41 : :
42 : : const sal_Int32 nCharClassAlphaType =
43 : : ::com::sun::star::i18n::KCharacterType::UPPER |
44 : : ::com::sun::star::i18n::KCharacterType::LOWER |
45 : : ::com::sun::star::i18n::KCharacterType::TITLE_CASE;
46 : :
47 : : const sal_Int32 nCharClassAlphaTypeMask =
48 : : nCharClassAlphaType |
49 : : ::com::sun::star::i18n::KCharacterType::PRINTABLE |
50 : : ::com::sun::star::i18n::KCharacterType::BASE_FORM;
51 : :
52 : : const sal_Int32 nCharClassLetterType =
53 : : nCharClassAlphaType |
54 : : ::com::sun::star::i18n::KCharacterType::LETTER;
55 : :
56 : : const sal_Int32 nCharClassLetterTypeMask =
57 : : nCharClassAlphaTypeMask |
58 : : ::com::sun::star::i18n::KCharacterType::LETTER;
59 : :
60 : : const sal_Int32 nCharClassNumericType =
61 : : ::com::sun::star::i18n::KCharacterType::DIGIT;
62 : :
63 : : const sal_Int32 nCharClassNumericTypeMask =
64 : : nCharClassNumericType |
65 : : ::com::sun::star::i18n::KCharacterType::PRINTABLE |
66 : : ::com::sun::star::i18n::KCharacterType::BASE_FORM;
67 : :
68 : :
69 : : class UNOTOOLS_DLLPUBLIC CharClass : private boost::noncopyable
70 : : {
71 : : ::com::sun::star::lang::Locale aLocale;
72 : : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCharacterClassification > xCC;
73 : : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xSMgr;
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::lang::XMultiServiceFactory > & xSF,
80 : : const ::com::sun::star::lang::Locale& rLocale);
81 : :
82 : : /// Depricated ctor, tries to get a process service manager or to load the
83 : : /// library directly.
84 : : CharClass(const ::com::sun::star::lang::Locale& rLocale);
85 : :
86 : : ~CharClass();
87 : :
88 : : /// set a new Locale
89 : : void setLocale( const ::com::sun::star::lang::Locale& rLocale );
90 : :
91 : : /// get current Locale
92 : : const ::com::sun::star::lang::Locale& getLocale() const;
93 : :
94 : :
95 : : /// isdigit() on ascii values
96 : 63622 : static inline sal_Bool isAsciiDigit( sal_Unicode c )
97 : : {
98 [ + - ][ + + ]: 63622 : return c < 128 ? sal_Bool(isdigit( (unsigned char) c ) != 0) : sal_False;
99 : : }
100 : :
101 : : /// isalpha() on ascii values
102 : 583220 : static inline sal_Bool isAsciiAlpha( sal_Unicode c )
103 : : {
104 [ + - ][ + + ]: 583220 : return c < 128 ? sal_Bool(isalpha( (unsigned char) c ) != 0) : sal_False;
105 : : }
106 : :
107 : : /// isalnum() on ascii values
108 : 53899 : static inline sal_Bool isAsciiAlphaNumeric( sal_Unicode c )
109 : : {
110 [ + - ][ + + ]: 53899 : return c < 128 ? sal_Bool(isalnum( (unsigned char) c ) != 0) : sal_False;
111 : : }
112 : :
113 : : /// isdigit() on ascii values of entire string
114 : : static sal_Bool isAsciiNumeric( const String& rStr );
115 : :
116 : : /// isalpha() on ascii values of entire string
117 : : static sal_Bool isAsciiAlpha( const String& rStr );
118 : :
119 : : /// isalnum() on ascii values of entire string
120 : : static sal_Bool isAsciiAlphaNumeric( const String& rStr );
121 : :
122 : : /// whether type is pure alpha or not, e.g. return of getStringType
123 : : static inline sal_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 : 80 : static inline sal_Bool isNumericType( sal_Int32 nType )
131 : : {
132 : : return ((nType & nCharClassNumericType) != 0) &&
133 [ - + ][ # # ]: 80 : ((nType & ~(nCharClassNumericTypeMask)) == 0);
134 : : }
135 : :
136 : : /// whether type is pure alphanumeric or not, e.g. return of getStringType
137 : 0 : static inline sal_Bool isAlphaNumericType( sal_Int32 nType )
138 : : {
139 : : return ((nType & (nCharClassAlphaType |
140 : : nCharClassNumericType)) != 0) &&
141 : : ((nType & ~(nCharClassAlphaTypeMask |
142 [ # # ][ # # ]: 0 : nCharClassNumericTypeMask)) == 0);
143 : : }
144 : :
145 : : /// whether type is pure letter or not, e.g. return of getStringType
146 : 0 : static inline sal_Bool isLetterType( sal_Int32 nType )
147 : : {
148 : : return ((nType & nCharClassLetterType) != 0) &&
149 [ # # ][ # # ]: 0 : ((nType & ~(nCharClassLetterTypeMask)) == 0);
150 : : }
151 : :
152 : : /// whether type is pure letternumeric or not, e.g. return of getStringType
153 : 34009 : static inline sal_Bool isLetterNumericType( sal_Int32 nType )
154 : : {
155 : : return ((nType & (nCharClassLetterType |
156 : : nCharClassNumericType)) != 0) &&
157 : : ((nType & ~(nCharClassLetterTypeMask |
158 [ + + ][ + - ]: 34009 : nCharClassNumericTypeMask)) == 0);
159 : : }
160 : :
161 : :
162 : : // Wrapper implementations of class CharacterClassification
163 : :
164 : : ::rtl::OUString uppercase( const ::rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
165 : : ::rtl::OUString lowercase( const ::rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
166 : : ::rtl::OUString titlecase( const ::rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const;
167 : :
168 : 299414 : ::rtl::OUString uppercase( const ::rtl::OUString& _rStr ) const
169 : : {
170 : 299414 : return uppercase(_rStr, 0, _rStr.getLength());
171 : : }
172 : 34313 : ::rtl::OUString lowercase( const ::rtl::OUString& _rStr ) const
173 : : {
174 : 34313 : return lowercase(_rStr, 0, _rStr.getLength());
175 : : }
176 : : ::rtl::OUString titlecase( const ::rtl::OUString& _rStr ) const
177 : : {
178 : : return titlecase(_rStr, 0, _rStr.getLength());
179 : : }
180 : :
181 : : sal_Int16 getType( const String& rStr, xub_StrLen nPos ) const;
182 : : sal_Int16 getCharacterDirection( const String& rStr, xub_StrLen nPos ) const;
183 : : sal_Int16 getScript( const String& rStr, xub_StrLen nPos ) const;
184 : : sal_Int32 getCharacterType( const String& rStr, xub_StrLen nPos ) const;
185 : : sal_Int32 getStringType( const String& rStr, xub_StrLen nPos, xub_StrLen nCount ) const;
186 : :
187 : : ::com::sun::star::i18n::ParseResult parseAnyToken(
188 : : const String& rStr,
189 : : sal_Int32 nPos,
190 : : sal_Int32 nStartCharFlags,
191 : : const String& userDefinedCharactersStart,
192 : : sal_Int32 nContCharFlags,
193 : : const String& userDefinedCharactersCont ) const;
194 : :
195 : : ::com::sun::star::i18n::ParseResult parsePredefinedToken(
196 : : sal_Int32 nTokenType,
197 : : const String& rStr,
198 : : sal_Int32 nPos,
199 : : sal_Int32 nStartCharFlags,
200 : : const String& userDefinedCharactersStart,
201 : : sal_Int32 nContCharFlags,
202 : : const String& userDefinedCharactersCont ) const;
203 : :
204 : :
205 : : // Functionality of class International methods
206 : :
207 : : sal_Bool isAlpha( const String& rStr, xub_StrLen nPos ) const;
208 : : sal_Bool isLetter( const String& rStr, xub_StrLen nPos ) const;
209 : : sal_Bool isDigit( const String& rStr, xub_StrLen nPos ) const;
210 : : sal_Bool isAlphaNumeric( const String& rStr, xub_StrLen nPos ) const;
211 : : sal_Bool isLetterNumeric( const String& rStr, xub_StrLen nPos ) const;
212 : : sal_Bool isAlpha( const String& rStr ) const;
213 : : sal_Bool isLetter( const String& rStr ) const;
214 : : sal_Bool isNumeric( const String& rStr ) const;
215 : : sal_Bool isAlphaNumeric( const String& rStr ) const;
216 : : sal_Bool isLetterNumeric( const String& rStr ) const;
217 : : };
218 : :
219 : : #endif // _UNOTOOLS_CHARCLASS_HXX
220 : :
221 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|