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 :
20 : #ifndef INCLUDED_COMPHELPER_STRING_HXX
21 : #define INCLUDED_COMPHELPER_STRING_HXX
22 :
23 : #include "sal/config.h"
24 :
25 : #include <cstddef>
26 : #include "comphelper/comphelperdllapi.h"
27 : #include <sal/types.h>
28 : #include <rtl/strbuf.hxx>
29 : #include <rtl/ustrbuf.hxx>
30 : #include <com/sun/star/uno/Sequence.hxx>
31 :
32 : #include <com/sun/star/uno/XComponentContext.hpp>
33 : #include <com/sun/star/i18n/XCollator.hpp>
34 : #include <com/sun/star/i18n/XBreakIterator.hpp>
35 :
36 : // OUString helper functions that are not widespread or mature enough to
37 : // go into the stable URE API:
38 : namespace comphelper { namespace string {
39 :
40 : /** Compare an OString to a single char
41 :
42 : @param rIn The input OString
43 : @param c The character to compare againsg
44 :
45 : @return true if rIn has one char and its equal to c
46 : */
47 4 : inline bool equals(const OString& rIn, sal_Char c)
48 4 : { return rIn.getLength() == 1 && rIn[0] == c; }
49 :
50 : /** Compare an OUString to a single char
51 :
52 : @param rIn The input OUString
53 : @param c The character to compare againsg
54 :
55 : @return true if rIn has one char and its equal to c
56 : */
57 2206 : inline bool equals(const OUString& rIn, sal_Unicode c)
58 2206 : { return rIn.getLength() == 1 && rIn[0] == c; }
59 :
60 : /** Removes all occurrences of a character from within the source string
61 :
62 : @deprecated Use OString::replaceAll(OString(c), OString())
63 : instead.
64 :
65 : @param rIn The input OString
66 : @param c The character to be removed
67 :
68 : @return The resulting OString
69 : */
70 829 : inline OString remove(const OString &rIn,
71 : sal_Char c)
72 829 : { return rIn.replaceAll(OString(c), OString()); }
73 :
74 : /** Removes all occurrences of a character from within the source string
75 :
76 : @deprecated Use
77 : OUString::replaceAll(OUString(c), OUString()) instead.
78 :
79 : @param rIn The input OUString
80 : @param c The character to be removed
81 :
82 : @return The resulting OUString
83 : */
84 97370 : inline OUString remove(const OUString &rIn,
85 : sal_Unicode c)
86 97370 : { return rIn.replaceAll(OUString(c), OUString()); }
87 :
88 : /** Strips occurrences of a character from the start of the source string
89 :
90 : @param rIn The input OString
91 : @param c The character to be stripped from the start
92 :
93 : @return The resulting OString
94 : */
95 : COMPHELPER_DLLPUBLIC OString stripStart(const OString &rIn,
96 : sal_Char c);
97 :
98 : /** Strips occurrences of a character from the start of the source string
99 :
100 : @param rIn The input OUString
101 : @param c The character to be stripped from the start
102 :
103 : @return The resulting OUString
104 : */
105 : COMPHELPER_DLLPUBLIC OUString stripStart(const OUString &rIn,
106 : sal_Unicode c);
107 :
108 : /** Strips occurrences of a character from the end of the source string
109 :
110 : @param rIn The input OString
111 : @param c The character to be stripped from the end
112 :
113 : @return The resulting OString
114 : */
115 : COMPHELPER_DLLPUBLIC OString stripEnd(const OString &rIn,
116 : sal_Char c);
117 :
118 : /** Strips occurrences of a character from the end of the source string
119 :
120 : @param rIn The input OUString
121 : @param c The character to be stripped from the end
122 :
123 : @return The resulting OUString
124 : */
125 : COMPHELPER_DLLPUBLIC OUString stripEnd(const OUString &rIn,
126 : sal_Unicode c);
127 :
128 : /** Strips occurrences of a character from the start and end of the source string
129 :
130 : @param rIn The input OString
131 : @param c The character to be stripped from the start and end
132 :
133 : @return The resulting OString
134 : */
135 : COMPHELPER_DLLPUBLIC OString strip(const OString &rIn,
136 : sal_Char c);
137 :
138 : /** Strips occurrences of a character from the start and end of the source string
139 :
140 : @param rIn The input OUString
141 : @param c The character to be stripped from the start and end
142 :
143 : @return The resulting OUString
144 : */
145 : COMPHELPER_DLLPUBLIC OUString strip(const OUString &rIn,
146 : sal_Unicode c);
147 :
148 : /** Returns a token in an OString
149 :
150 : @deprecated Use OString::getToken(nToken, cTok) instead.
151 :
152 : @param rIn the input OString
153 : @param nToken the number of the token to return
154 : @param cTok the character which separate the tokens.
155 : @return the token if token is negative or doesn't exist an empty token
156 : is returned
157 : */
158 91989 : inline OString getToken(const OString &rIn,
159 : sal_Int32 nToken, sal_Char cTok) SAL_THROW(())
160 : {
161 91989 : return rIn.getToken(nToken, cTok);
162 : }
163 :
164 : /** Returns a token in an OUString
165 :
166 : @deprecated Use OUString::getToken(nToken, cTok) instead.
167 :
168 : @param rIn the input OUString
169 : @param nToken the number of the token to return
170 : @param cTok the character which separate the tokens.
171 : @return the token if token is negative or doesn't exist an empty token
172 : is returned
173 : */
174 53 : inline OUString getToken(const OUString &rIn,
175 : sal_Int32 nToken, sal_Unicode cTok) SAL_THROW(())
176 : {
177 53 : return rIn.getToken(nToken, cTok);
178 : }
179 :
180 : /** Returns number of tokens in an OUString
181 :
182 : @param rIn the input OString
183 : @param cTok the character which separate the tokens.
184 : @return the number of tokens
185 : */
186 : COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const OString &rIn, sal_Char cTok);
187 :
188 : /** Returns number of tokens in an OUString
189 :
190 : @param rIn the input OUString
191 : @param cTok the character which separate the tokens.
192 : @return the number of tokens
193 : */
194 : COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const OUString &rIn, sal_Unicode cTok);
195 :
196 : /** Reverse an OUString
197 :
198 : @param rIn the input OUString
199 : @return the reversed input
200 : */
201 : COMPHELPER_DLLPUBLIC OUString reverseString(const OUString &rStr);
202 :
203 : /** Reverse an OString
204 :
205 : @param rIn the input OString
206 : @return the reversed input
207 : */
208 : COMPHELPER_DLLPUBLIC OString reverseString(const OString &rStr);
209 :
210 :
211 : namespace detail
212 : {
213 0 : template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen)
214 : {
215 0 : if (nLen < rBuffer.getLength())
216 0 : rBuffer.remove(nLen, rBuffer.getLength()-nLen);
217 0 : return rBuffer;
218 : }
219 : }
220 :
221 : /** Truncate a buffer to a given length.
222 :
223 : If the StringBuffer has more characters than nLength it will be truncated
224 : on the right to nLength characters.
225 :
226 : Has no effect if the StringBuffer is <= nLength
227 :
228 : @param rBuf StringBuffer to operate on
229 : @param nLength Length to truncate the buffer to
230 :
231 : @return rBuf;
232 : */
233 : COMPHELPER_DLLPUBLIC inline OStringBuffer& truncateToLength(
234 : OStringBuffer& rBuffer, sal_Int32 nLength) SAL_THROW(())
235 : {
236 : return detail::truncateToLength(rBuffer, nLength);
237 : }
238 :
239 0 : COMPHELPER_DLLPUBLIC inline OUStringBuffer& truncateToLength(
240 : OUStringBuffer& rBuffer, sal_Int32 nLength) SAL_THROW(())
241 : {
242 0 : return detail::truncateToLength(rBuffer, nLength);
243 : }
244 :
245 : namespace detail
246 : {
247 9475 : template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen,
248 : U cFill = '\0')
249 : {
250 9475 : sal_Int32 nOrigLen = rBuffer.getLength();
251 9475 : if (nLen > nOrigLen)
252 : {
253 8570 : rBuffer.setLength(nLen);
254 34895 : for (sal_Int32 i = nOrigLen; i < nLen; ++i)
255 26325 : rBuffer[i] = cFill;
256 : }
257 9475 : return rBuffer;
258 : }
259 : }
260 :
261 : /** Pad a buffer to a given length using a given char.
262 :
263 : If the StringBuffer has less characters than nLength it will be expanded on
264 : the right to nLength characters, with the expansion filled using cFill.
265 :
266 : Has no effect if the StringBuffer is >= nLength
267 :
268 : @param rBuf StringBuffer to operate on
269 : @param nLength Length to pad the buffer to
270 : @param cFill character to fill expansion with
271 :
272 : @return rBuf;
273 : */
274 1 : COMPHELPER_DLLPUBLIC inline OStringBuffer& padToLength(
275 : OStringBuffer& rBuffer, sal_Int32 nLength,
276 : sal_Char cFill = '\0') SAL_THROW(())
277 : {
278 1 : return detail::padToLength(rBuffer, nLength, cFill);
279 : }
280 :
281 9474 : COMPHELPER_DLLPUBLIC inline OUStringBuffer& padToLength(
282 : OUStringBuffer& rBuffer, sal_Int32 nLength,
283 : sal_Unicode cFill = '\0') SAL_THROW(())
284 : {
285 9474 : return detail::padToLength(rBuffer, nLength, cFill);
286 : }
287 :
288 : /** Find any of a list of code units in the string.
289 : @param rIn OUString to search
290 : @param pChars 0-terminated array of sal_Unicode code units to search for
291 : @param nPos start position
292 :
293 : @return position of first occurrence of any of the elements of pChars
294 : or -1 if none of the code units occur in the string
295 : */
296 : COMPHELPER_DLLPUBLIC sal_Int32 indexOfAny(OUString const& rIn,
297 : sal_Unicode const*const pChars, sal_Int32 const nPos = 0);
298 :
299 : /** Convert a sequence of strings to a single comma separated string.
300 :
301 : Note that no escaping of commas or anything fancy is done.
302 :
303 : @param i_rSeq A list of strings to be concatenated.
304 :
305 : @return A single string containing the concatenation of the given
306 : list, interspersed with the string ", ".
307 : */
308 : COMPHELPER_DLLPUBLIC OUString convertCommaSeparated(
309 : ::com::sun::star::uno::Sequence< OUString > const & i_rSeq);
310 :
311 : /** Convert a decimal string to a number.
312 :
313 : The string must be base-10, no sign but can contain any
314 : codepoint listed in the "Number, Decimal Digit" Unicode
315 : category.
316 :
317 : No verification is made about the validity of the string,
318 : passing string not containing decimal digit code points
319 : gives unspecified results
320 :
321 : If your string is guaranteed to contain only ASCII digit
322 : use OUString::toInt32 instead.
323 :
324 : @param str The string to convert containing only decimal
325 : digit codepoints.
326 :
327 : @return The value of the string as an int32.
328 : */
329 : COMPHELPER_DLLPUBLIC sal_uInt32 decimalStringToNumber(
330 : OUString const & str );
331 :
332 : /** Convert a single comma separated string to a sequence of strings.
333 :
334 : Note that no escaping of commas or anything fancy is done.
335 :
336 : @param i_rString A string containing comma-separated words.
337 :
338 : @return A sequence of strings resulting from splitting the given
339 : string at ',' tokens and stripping whitespace.
340 : */
341 : COMPHELPER_DLLPUBLIC ::com::sun::star::uno::Sequence< OUString >
342 : convertCommaSeparated( OUString const & i_rString );
343 :
344 : /**
345 : Compares two strings using natural order.
346 :
347 : For non digit characters, the comparison use the same algorithm as
348 : rtl_str_compare. When a number is encountered during the comparison,
349 : natural order is used. Thus, Heading 10 will be considered as greater
350 : than Heading 2. Numerical comparison is done using decimal representation.
351 :
352 : Beware that "MyString 001" and "MyString 1" will be considered as equal
353 : since leading 0 are meaningless.
354 :
355 : @param str the object to be compared.
356 : @return 0 - if both strings are equal
357 : < 0 - if this string is less than the string argument
358 : > 0 - if this string is greater than the string argument
359 : */
360 : COMPHELPER_DLLPUBLIC sal_Int32 compareNatural( const OUString &rLHS, const OUString &rRHS,
361 : const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > &rCollator,
362 : const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > &rBI,
363 : const ::com::sun::star::lang::Locale &rLocale );
364 :
365 308 : class COMPHELPER_DLLPUBLIC NaturalStringSorter
366 : {
367 : private:
368 : ::com::sun::star::lang::Locale m_aLocale;
369 : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > m_xCollator;
370 : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > m_xBI;
371 : public:
372 : NaturalStringSorter(
373 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > &rContext,
374 : const ::com::sun::star::lang::Locale &rLocale);
375 591803 : sal_Int32 compare(const OUString &rLHS, const OUString &rRHS) const
376 : {
377 591803 : return compareNatural(rLHS, rRHS, m_xCollator, m_xBI, m_aLocale);
378 : }
379 42882 : const ::com::sun::star::lang::Locale& getLocale() const { return m_aLocale; }
380 : };
381 :
382 : /** Determine if an OString contains solely ASCII numeric digits
383 :
384 : @param rString An OString
385 :
386 : @return false if string contains any characters outside
387 : the ASCII '0'-'9' range
388 : true otherwise, including for empty string
389 : */
390 : COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const OString &rString);
391 :
392 : /** Determine if an OUString contains solely ASCII numeric digits
393 :
394 : @param rString An OUString
395 :
396 : @return false if string contains any characters outside
397 : the ASCII '0'-'9' range
398 : true otherwise, including for empty string
399 : */
400 : COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const OUString &rString);
401 :
402 10297 : COMPHELPER_DLLPUBLIC inline bool isdigitAscii(sal_Unicode c)
403 : {
404 10297 : return ((c >= '0') && (c <= '9'));
405 : }
406 :
407 0 : COMPHELPER_DLLPUBLIC inline bool isxdigitAscii(sal_Unicode c)
408 : {
409 0 : return isdigitAscii(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
410 : }
411 :
412 50854 : COMPHELPER_DLLPUBLIC inline bool islowerAscii(sal_Unicode c)
413 : {
414 50854 : return ((c >= 'a') && (c <= 'z'));
415 : }
416 :
417 8048 : COMPHELPER_DLLPUBLIC inline bool isupperAscii(sal_Unicode c)
418 : {
419 8048 : return ((c >= 'A') && (c <= 'Z'));
420 : }
421 :
422 50854 : COMPHELPER_DLLPUBLIC inline bool isalphaAscii(sal_Unicode c)
423 : {
424 50854 : return islowerAscii(c) || isupperAscii(c);
425 : }
426 :
427 2251 : COMPHELPER_DLLPUBLIC inline bool isalnumAscii(sal_Unicode c)
428 : {
429 2251 : return isalphaAscii(c) || isdigitAscii(c);
430 : }
431 :
432 : } }
433 :
434 : #endif
435 :
436 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|