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 : /** 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 rtl::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 136 : inline bool equals(const rtl::OUString& rIn, sal_Unicode c)
58 136 : { return rIn.getLength() == 1 && rIn[0] == c; }
59 :
60 : /** Removes all occurrences of a character from within the source string
61 :
62 : @deprecated Use rtl::OString::replaceAll(rtl::OString(c), rtl::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 823 : inline rtl::OString remove(const rtl::OString &rIn,
71 : sal_Char c)
72 823 : { return rIn.replaceAll(rtl::OString(c), rtl::OString()); }
73 :
74 : /** Removes all occurrences of a character from within the source string
75 :
76 : @deprecated Use
77 : rtl::OUString::replaceAll(rtl::OUString(c), rtl::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 1287 : inline rtl::OUString remove(const rtl::OUString &rIn,
85 : sal_Unicode c)
86 1287 : { return rIn.replaceAll(rtl::OUString(c), rtl::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 rtl::OString stripStart(const rtl::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 rtl::OUString stripStart(const rtl::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 rtl::OString stripEnd(const rtl::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 rtl::OUString stripEnd(const rtl::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 rtl::OString strip(const rtl::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 rtl::OUString strip(const rtl::OUString &rIn,
146 : sal_Unicode c);
147 :
148 : /** Returns a token in an OString
149 :
150 : @deprecated Use rtl::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 seperate the tokens.
155 : @return the token if token is negative or doesn't exist an empty token
156 : is returned
157 : */
158 93441 : inline rtl::OString getToken(const rtl::OString &rIn,
159 : sal_Int32 nToken, sal_Char cTok) SAL_THROW(())
160 : {
161 93441 : return rIn.getToken(nToken, cTok);
162 : }
163 :
164 : /** Returns a token in an OUString
165 :
166 : @deprecated Use rtl::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 seperate the tokens.
171 : @return the token if token is negative or doesn't exist an empty token
172 : is returned
173 : */
174 5 : inline rtl::OUString getToken(const rtl::OUString &rIn,
175 : sal_Int32 nToken, sal_Unicode cTok) SAL_THROW(())
176 : {
177 5 : 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 seperate the tokens.
184 : @return the number of tokens
185 : */
186 : COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const rtl::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 seperate the tokens.
192 : @return the number of tokens
193 : */
194 : COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const rtl::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 rtl::OUString reverseString(const rtl::OUString &rStr);
202 :
203 : /** Reverse an OString
204 :
205 : @param rIn the input OString
206 : @return the reversed input
207 : */
208 : COMPHELPER_DLLPUBLIC rtl::OString reverseString(const rtl::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 0 : COMPHELPER_DLLPUBLIC inline rtl::OStringBuffer& truncateToLength(
234 : rtl::OStringBuffer& rBuffer, sal_Int32 nLength) SAL_THROW(())
235 : {
236 0 : return detail::truncateToLength(rBuffer, nLength);
237 : }
238 :
239 0 : COMPHELPER_DLLPUBLIC inline rtl::OUStringBuffer& truncateToLength(
240 : rtl::OUStringBuffer& rBuffer, sal_Int32 nLength) SAL_THROW(())
241 : {
242 0 : return detail::truncateToLength(rBuffer, nLength);
243 : }
244 :
245 : namespace detail
246 : {
247 1971 : template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen,
248 : U cFill = '\0')
249 : {
250 1971 : sal_Int32 nOrigLen = rBuffer.getLength();
251 1971 : if (nLen > nOrigLen)
252 : {
253 1971 : rBuffer.setLength(nLen);
254 9630 : for (sal_Int32 i = nOrigLen; i < nLen; ++i)
255 7659 : rBuffer[i] = cFill;
256 : }
257 1971 : 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 0 : COMPHELPER_DLLPUBLIC inline rtl::OStringBuffer& padToLength(
275 : rtl::OStringBuffer& rBuffer, sal_Int32 nLength,
276 : sal_Char cFill = '\0') SAL_THROW(())
277 : {
278 0 : return detail::padToLength(rBuffer, nLength, cFill);
279 : }
280 :
281 1971 : COMPHELPER_DLLPUBLIC inline rtl::OUStringBuffer& padToLength(
282 : rtl::OUStringBuffer& rBuffer, sal_Int32 nLength,
283 : sal_Unicode cFill = '\0') SAL_THROW(())
284 : {
285 1971 : return detail::padToLength(rBuffer, nLength, cFill);
286 : }
287 :
288 : /** Convert a sequence of strings to a single comma separated string.
289 :
290 : Note that no escaping of commas or anything fancy is done.
291 :
292 : @param i_rSeq A list of strings to be concatenated.
293 :
294 : @return A single string containing the concatenation of the given
295 : list, interspersed with the string ", ".
296 : */
297 : COMPHELPER_DLLPUBLIC ::rtl::OUString convertCommaSeparated(
298 : ::com::sun::star::uno::Sequence< ::rtl::OUString > const & i_rSeq);
299 :
300 : /** Convert a decimal string to a number.
301 :
302 : The string must be base-10, no sign but can contain any
303 : codepoint listed in the "Number, Decimal Digit" Unicode
304 : category.
305 :
306 : No verification is made about the validity of the string,
307 : passing string not containing decimal digit code points
308 : gives unspecified results
309 :
310 : If your string is guaranteed to contain only ASCII digit
311 : use rtl::OUString::toInt32 instead.
312 :
313 : @param str The string to convert containing only decimal
314 : digit codepoints.
315 :
316 : @return The value of the string as an int32.
317 : */
318 : COMPHELPER_DLLPUBLIC sal_uInt32 decimalStringToNumber(
319 : ::rtl::OUString const & str );
320 :
321 : /** Convert a single comma separated string to a sequence of strings.
322 :
323 : Note that no escaping of commas or anything fancy is done.
324 :
325 : @param i_rString A string containing comma-separated words.
326 :
327 : @return A sequence of strings resulting from splitting the given
328 : string at ',' tokens and stripping whitespace.
329 : */
330 : COMPHELPER_DLLPUBLIC ::com::sun::star::uno::Sequence< ::rtl::OUString >
331 : convertCommaSeparated( ::rtl::OUString const & i_rString );
332 :
333 : /**
334 : Compares two strings using natural order.
335 :
336 : For non digit characters, the comparison use the same algorithm as
337 : rtl_str_compare. When a number is encountered during the comparison,
338 : natural order is used. Thus, Heading 10 will be considered as greater
339 : than Heading 2. Numerical comparison is done using decimal representation.
340 :
341 : Beware that "MyString 001" and "MyString 1" will be considered as equal
342 : since leading 0 are meaningless.
343 :
344 : @param str the object to be compared.
345 : @return 0 - if both strings are equal
346 : < 0 - if this string is less than the string argument
347 : > 0 - if this string is greater than the string argument
348 : */
349 : COMPHELPER_DLLPUBLIC sal_Int32 compareNatural( const ::rtl::OUString &rLHS, const ::rtl::OUString &rRHS,
350 : const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > &rCollator,
351 : const ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > &rBI,
352 : const ::com::sun::star::lang::Locale &rLocale );
353 :
354 0 : class COMPHELPER_DLLPUBLIC NaturalStringSorter
355 : {
356 : private:
357 : ::com::sun::star::lang::Locale m_aLocale;
358 : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XCollator > m_xCollator;
359 : ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XBreakIterator > m_xBI;
360 : public:
361 : NaturalStringSorter(
362 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > &rContext,
363 : const ::com::sun::star::lang::Locale &rLocale);
364 0 : sal_Int32 compare(const rtl::OUString &rLHS, const rtl::OUString &rRHS) const
365 : {
366 0 : return compareNatural(rLHS, rRHS, m_xCollator, m_xBI, m_aLocale);
367 : }
368 0 : const ::com::sun::star::lang::Locale& getLocale() const { return m_aLocale; }
369 : };
370 :
371 : /** Determine if an OString contains solely ASCII numeric digits
372 :
373 : @param rString An OString
374 :
375 : @return false if string contains any characters outside
376 : the ASCII '0'-'9' range
377 : true otherwise, including for empty string
378 : */
379 : COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const rtl::OString &rString);
380 :
381 : /** Determine if an OUString contains solely ASCII numeric digits
382 :
383 : @param rString An OUString
384 :
385 : @return false if string contains any characters outside
386 : the ASCII '0'-'9' range
387 : true otherwise, including for empty string
388 : */
389 : COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const rtl::OUString &rString);
390 :
391 3530 : COMPHELPER_DLLPUBLIC inline bool isdigitAscii(sal_Unicode c)
392 : {
393 3530 : return ((c >= '0') && (c <= '9'));
394 : }
395 :
396 0 : COMPHELPER_DLLPUBLIC inline bool isxdigitAscii(sal_Unicode c)
397 : {
398 0 : return isdigitAscii(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
399 : }
400 :
401 4765 : COMPHELPER_DLLPUBLIC inline bool islowerAscii(sal_Unicode c)
402 : {
403 4765 : return ((c >= 'a') && (c <= 'z'));
404 : }
405 :
406 774 : COMPHELPER_DLLPUBLIC inline bool isupperAscii(sal_Unicode c)
407 : {
408 774 : return ((c >= 'A') && (c <= 'Z'));
409 : }
410 :
411 4765 : COMPHELPER_DLLPUBLIC inline bool isalphaAscii(sal_Unicode c)
412 : {
413 4765 : return islowerAscii(c) || isupperAscii(c);
414 : }
415 :
416 0 : COMPHELPER_DLLPUBLIC inline bool isalnumAscii(sal_Unicode c)
417 : {
418 0 : return isalphaAscii(c) || isdigitAscii(c);
419 : }
420 :
421 : //============================================================
422 : //= a helper for static ascii pseudo-unicode strings
423 : //============================================================
424 : struct COMPHELPER_DLLPUBLIC ConstAsciiString
425 : {
426 : const sal_Char* ascii;
427 : sal_Int32 length;
428 :
429 6184 : operator rtl::OUString() const
430 : {
431 6184 : return rtl::OUString(ascii, length, RTL_TEXTENCODING_ASCII_US);
432 : }
433 : };
434 :
435 : } }
436 :
437 : #ifdef RTL_FAST_STRING
438 : // TODO The whole ConstAsciiString class should probably be dumped
439 : // and replaced with plain 'const char[]'.
440 : namespace rtl
441 : {
442 : template<>
443 : struct ToStringHelper< comphelper::string::ConstAsciiString >
444 : {
445 0 : static int length( const comphelper::string::ConstAsciiString& str ) { return str.length; }
446 0 : static sal_Unicode* addData( sal_Unicode* buffer, const comphelper::string::ConstAsciiString& str ) { return addDataLiteral( buffer, str.ascii, str.length ); }
447 : static const bool allowOStringConcat = false;
448 : static const bool allowOUStringConcat = true;
449 : };
450 : }
451 : #endif
452 :
453 : #endif
454 :
455 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|