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 : :
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 : : // rtl::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 : : /** Allocate a new string containing space for a given number of characters.
41 : :
42 : : The reference count of the new string will be 1. The length of the string
43 : : will be nLen. This function throws std::bad_alloc on out-of-memory
44 : : conditions.
45 : :
46 : : The characters of the capacity are not cleared, and the length is set to
47 : : nLen, unlike the similar method of rtl_uString_new_WithLength which
48 : : zeros out the buffer, and sets the length to 0. So should be somewhat
49 : : more efficient for allocating a new string.
50 : :
51 : : call rtl_uString_release to release the string
52 : : alternatively pass ownership to an OUString with
53 : : rtl::OUString(newStr, SAL_NO_ACQUIRE);
54 : :
55 : : @param newStr
56 : : pointer to the new string.
57 : :
58 : : @param len
59 : : the number of characters.
60 : : */
61 : : COMPHELPER_DLLPUBLIC rtl_uString * SAL_CALL rtl_uString_alloc(sal_Int32 nLen);
62 : :
63 : : /** Allocate a new string containing space for a given number of characters.
64 : :
65 : : The reference count of the new string will be 1. The length of the string
66 : : will be nLen. This function does not handle out-of-memory conditions.
67 : :
68 : : The characters of the capacity are not cleared, and the length is set to
69 : : nLen, unlike the similar method of rtl_String_new_WithLength which
70 : : zeros out the buffer, and sets the length to 0. So should be somewhat
71 : : more efficient for allocating a new string.
72 : :
73 : : call rtl_String_release to release the string
74 : : alternatively pass ownership to an OUString with
75 : : rtl::OUString(newStr, SAL_NO_ACQUIRE);
76 : :
77 : : @param newStr
78 : : pointer to the new string.
79 : :
80 : : @param len
81 : : the number of characters.
82 : : */
83 : : COMPHELPER_DLLPUBLIC rtl_String * SAL_CALL rtl_string_alloc(sal_Int32 nLen);
84 : :
85 : : /** Compare an OString to a single char
86 : :
87 : : @param rIn The input OString
88 : : @param c The character to compare againsg
89 : :
90 : : @return true if rIn has one char and its equal to c
91 : : */
92 : 12 : inline bool equals(const rtl::OString& rIn, sal_Char c)
93 [ + + ][ + + ]: 12 : { return rIn.getLength() == 1 && rIn[0] == c; }
94 : :
95 : : /** Compare an OUString to a single char
96 : :
97 : : @param rIn The input OUString
98 : : @param c The character to compare againsg
99 : :
100 : : @return true if rIn has one char and its equal to c
101 : : */
102 : : inline bool equals(const rtl::OUString& rIn, sal_Unicode c)
103 : : { return rIn.getLength() == 1 && rIn[0] == c; }
104 : :
105 : : /** Removes all occurrences of a character from within the source string
106 : :
107 : : @deprecated Use rtl::OString::replaceAll(rtl::OString(c), rtl::OString())
108 : : instead.
109 : :
110 : : @param rIn The input OString
111 : : @param c The character to be removed
112 : :
113 : : @return The resulting OString
114 : : */
115 : 6 : inline rtl::OString remove(const rtl::OString &rIn,
116 : : sal_Char c)
117 : 6 : { return rIn.replaceAll(rtl::OString(c), rtl::OString()); }
118 : :
119 : : /** Removes all occurrences of a character from within the source string
120 : :
121 : : @deprecated Use
122 : : rtl::OUString::replaceAll(rtl::OUString(c), rtl::OUString()) instead.
123 : :
124 : : @param rIn The input OUString
125 : : @param c The character to be removed
126 : :
127 : : @return The resulting OUString
128 : : */
129 : : inline rtl::OUString remove(const rtl::OUString &rIn,
130 : : sal_Unicode c)
131 : : { return rIn.replaceAll(rtl::OUString(c), rtl::OUString()); }
132 : :
133 : : /** Strips occurrences of a character from the start of the source string
134 : :
135 : : @param rIn The input OString
136 : : @param c The character to be stripped from the start
137 : :
138 : : @return The resulting OString
139 : : */
140 : : COMPHELPER_DLLPUBLIC rtl::OString stripStart(const rtl::OString &rIn,
141 : : sal_Char c);
142 : :
143 : : /** Strips occurrences of a character from the start of the source string
144 : :
145 : : @param rIn The input OUString
146 : : @param c The character to be stripped from the start
147 : :
148 : : @return The resulting OUString
149 : : */
150 : : COMPHELPER_DLLPUBLIC rtl::OUString stripStart(const rtl::OUString &rIn,
151 : : sal_Unicode c);
152 : :
153 : : /** Strips occurrences of a character from the end of the source string
154 : :
155 : : @param rIn The input OString
156 : : @param c The character to be stripped from the end
157 : :
158 : : @return The resulting OString
159 : : */
160 : : COMPHELPER_DLLPUBLIC rtl::OString stripEnd(const rtl::OString &rIn,
161 : : sal_Char c);
162 : :
163 : : /** Strips occurrences of a character from the end of the source string
164 : :
165 : : @param rIn The input OUString
166 : : @param c The character to be stripped from the end
167 : :
168 : : @return The resulting OUString
169 : : */
170 : : COMPHELPER_DLLPUBLIC rtl::OUString stripEnd(const rtl::OUString &rIn,
171 : : sal_Unicode c);
172 : :
173 : : /** Strips occurrences of a character from the start and end of the source string
174 : :
175 : : @param rIn The input OString
176 : : @param c The character to be stripped from the start and end
177 : :
178 : : @return The resulting OString
179 : : */
180 : : COMPHELPER_DLLPUBLIC rtl::OString strip(const rtl::OString &rIn,
181 : : sal_Char c);
182 : :
183 : : /** Strips occurrences of a character from the start and end of the source string
184 : :
185 : : @param rIn The input OUString
186 : : @param c The character to be stripped from the start and end
187 : :
188 : : @return The resulting OUString
189 : : */
190 : : COMPHELPER_DLLPUBLIC rtl::OUString strip(const rtl::OUString &rIn,
191 : : sal_Unicode c);
192 : :
193 : : /** Returns a token in an OString
194 : :
195 : : @deprecated Use rtl::OString::getToken(nToken, cTok) instead.
196 : :
197 : : @param rIn the input OString
198 : : @param nToken the number of the token to return
199 : : @param cTok the character which seperate the tokens.
200 : : @return the token if token is negative or doesn't exist an empty token
201 : : is returned
202 : : */
203 : 15 : inline rtl::OString getToken(const rtl::OString &rIn,
204 : : sal_Int32 nToken, sal_Char cTok) SAL_THROW(())
205 : : {
206 : 15 : return rIn.getToken(nToken, cTok);
207 : : }
208 : :
209 : : /** Returns a token in an OUString
210 : :
211 : : @deprecated Use rtl::OUString::getToken(nToken, cTok) instead.
212 : :
213 : : @param rIn the input OUString
214 : : @param nToken the number of the token to return
215 : : @param cTok the character which seperate the tokens.
216 : : @return the token if token is negative or doesn't exist an empty token
217 : : is returned
218 : : */
219 : : inline rtl::OUString getToken(const rtl::OUString &rIn,
220 : : sal_Int32 nToken, sal_Unicode cTok) SAL_THROW(())
221 : : {
222 : : return rIn.getToken(nToken, cTok);
223 : : }
224 : :
225 : : /** Returns number of tokens in an OUString
226 : :
227 : : @param rIn the input OString
228 : : @param cTok the character which seperate the tokens.
229 : : @return the number of tokens
230 : : */
231 : : COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const rtl::OString &rIn, sal_Char cTok);
232 : :
233 : : /** Returns number of tokens in an OUString
234 : :
235 : : @param rIn the input OUString
236 : : @param cTok the character which seperate the tokens.
237 : : @return the number of tokens
238 : : */
239 : : COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const rtl::OUString &rIn, sal_Unicode cTok);
240 : :
241 : : /** Reverse an OUString
242 : :
243 : : @param rIn the input OUString
244 : : @return the reversed input
245 : : */
246 : : COMPHELPER_DLLPUBLIC rtl::OUString reverseString(const rtl::OUString &rStr);
247 : :
248 : : /** Reverse an OString
249 : :
250 : : @param rIn the input OString
251 : : @return the reversed input
252 : : */
253 : : COMPHELPER_DLLPUBLIC rtl::OString reverseString(const rtl::OString &rStr);
254 : :
255 : :
256 : : namespace detail
257 : : {
258 : : template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen)
259 : : {
260 : : if (nLen < rBuffer.getLength())
261 : : rBuffer.remove(nLen, rBuffer.getLength()-nLen);
262 : : return rBuffer;
263 : : }
264 : : }
265 : :
266 : : /** Truncate a buffer to a given length.
267 : :
268 : : If the StringBuffer has more characters than nLength it will be truncated
269 : : on the right to nLength characters.
270 : :
271 : : Has no effect if the StringBuffer is <= nLength
272 : :
273 : : @param rBuf StringBuffer to operate on
274 : : @param nLength Length to truncate the buffer to
275 : :
276 : : @return rBuf;
277 : : */
278 : : COMPHELPER_DLLPUBLIC inline rtl::OStringBuffer& truncateToLength(
279 : : rtl::OStringBuffer& rBuffer, sal_Int32 nLength) SAL_THROW(())
280 : : {
281 : : return detail::truncateToLength(rBuffer, nLength);
282 : : }
283 : :
284 : : COMPHELPER_DLLPUBLIC inline rtl::OUStringBuffer& truncateToLength(
285 : : rtl::OUStringBuffer& rBuffer, sal_Int32 nLength) SAL_THROW(())
286 : : {
287 : : return detail::truncateToLength(rBuffer, nLength);
288 : : }
289 : :
290 : : namespace detail
291 : : {
292 : : template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen,
293 : : U cFill = '\0')
294 : : {
295 : : sal_Int32 nOrigLen = rBuffer.getLength();
296 : : if (nLen > nOrigLen)
297 : : {
298 : : rBuffer.setLength(nLen);
299 : : for (sal_Int32 i = nOrigLen; i < nLen; ++i)
300 : : rBuffer[i] = cFill;
301 : : }
302 : : return rBuffer;
303 : : }
304 : : }
305 : :
306 : : /** Pad a buffer to a given length using a given char.
307 : :
308 : : If the StringBuffer has less characters than nLength it will be expanded on
309 : : the right to nLength characters, with the expansion filled using cFill.
310 : :
311 : : Has no effect if the StringBuffer is >= nLength
312 : :
313 : : @param rBuf StringBuffer to operate on
314 : : @param nLength Length to pad the buffer to
315 : : @param cFill character to fill expansion with
316 : :
317 : : @return rBuf;
318 : : */
319 : : COMPHELPER_DLLPUBLIC inline rtl::OStringBuffer& padToLength(
320 : : rtl::OStringBuffer& rBuffer, sal_Int32 nLength,
321 : : sal_Char cFill = '\0') SAL_THROW(())
322 : : {
323 : : return detail::padToLength(rBuffer, nLength, cFill);
324 : : }
325 : :
326 : : COMPHELPER_DLLPUBLIC inline rtl::OUStringBuffer& padToLength(
327 : : rtl::OUStringBuffer& rBuffer, sal_Int32 nLength,
328 : : sal_Unicode cFill = '\0') SAL_THROW(())
329 : : {
330 : : return detail::padToLength(rBuffer, nLength, cFill);
331 : : }
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 ::rtl::OUString convertCommaSeparated(
343 : : ::com::sun::star::uno::Sequence< ::rtl::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 rtl::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 : : ::rtl::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< ::rtl::OUString >
376 : : convertCommaSeparated( ::rtl::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 ::rtl::OUString &rLHS, const ::rtl::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 : : 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 : : sal_Int32 compare(const rtl::OUString &rLHS, const rtl::OUString &rRHS) const
410 : : {
411 : : return compareNatural(rLHS, rRHS, m_xCollator, m_xBI, m_aLocale);
412 : : }
413 : : 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 rtl::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 rtl::OUString &rString);
435 : :
436 : : COMPHELPER_DLLPUBLIC inline bool isdigitAscii(sal_Unicode c)
437 : : {
438 : : return ((c >= '0') && (c <= '9'));
439 : : }
440 : :
441 : : COMPHELPER_DLLPUBLIC inline bool isxdigitAscii(sal_Unicode c)
442 : : {
443 : : return isdigitAscii(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
444 : : }
445 : :
446 : : COMPHELPER_DLLPUBLIC inline bool islowerAscii(sal_Unicode c)
447 : : {
448 : : return ((c >= 'a') && (c <= 'z'));
449 : : }
450 : :
451 : : COMPHELPER_DLLPUBLIC inline bool isupperAscii(sal_Unicode c)
452 : : {
453 : : return ((c >= 'A') && (c <= 'Z'));
454 : : }
455 : :
456 : : COMPHELPER_DLLPUBLIC inline bool isalphaAscii(sal_Unicode c)
457 : : {
458 : : return islowerAscii(c) || isupperAscii(c);
459 : : }
460 : :
461 : : COMPHELPER_DLLPUBLIC inline bool isalnumAscii(sal_Unicode c)
462 : : {
463 : : return isalphaAscii(c) || isdigitAscii(c);
464 : : }
465 : :
466 : : //============================================================
467 : : //= a helper for static ascii pseudo-unicode strings
468 : : //============================================================
469 : : struct COMPHELPER_DLLPUBLIC ConstAsciiString
470 : : {
471 : : const sal_Char* ascii;
472 : : sal_Int32 length;
473 : :
474 : : operator rtl::OUString() const
475 : : {
476 : : return rtl::OUString(ascii, length, RTL_TEXTENCODING_ASCII_US);
477 : : }
478 : : };
479 : :
480 : : } }
481 : :
482 : : #endif
483 : :
484 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|