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