1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_COMPHELPER_STRING_HXX
21 #define INCLUDED_COMPHELPER_STRING_HXX
23 #include "sal/config.h"
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>
32 #include <com/sun/star/uno/XComponentContext.hpp>
33 #include <com/sun/star/i18n/XCollator.hpp>
34 #include <com/sun/star/i18n/XBreakIterator.hpp>
36 // OUString helper functions that are not widespread or mature enough to
37 // go into the stable URE API:
38 namespace comphelper
{ namespace string
{
40 /** Compare an OString to a single char
42 @param rIn The input OString
43 @param c The character to compare againsg
45 @return true if rIn has one char and its equal to c
47 inline bool equals(const OString
& rIn
, sal_Char c
)
48 { return rIn
.getLength() == 1 && rIn
[0] == c
; }
50 /** Compare an OUString to a single char
52 @param rIn The input OUString
53 @param c The character to compare againsg
55 @return true if rIn has one char and its equal to c
57 inline bool equals(const OUString
& rIn
, sal_Unicode c
)
58 { return rIn
.getLength() == 1 && rIn
[0] == c
; }
60 /** Removes all occurrences of a character from within the source string
62 @deprecated Use OString::replaceAll(OString(c), OString())
65 @param rIn The input OString
66 @param c The character to be removed
68 @return The resulting OString
70 inline OString
remove(const OString
&rIn
,
72 { return rIn
.replaceAll(OString(c
), OString()); }
74 /** Removes all occurrences of a character from within the source string
77 OUString::replaceAll(OUString(c), OUString()) instead.
79 @param rIn The input OUString
80 @param c The character to be removed
82 @return The resulting OUString
84 inline OUString
remove(const OUString
&rIn
,
86 { return rIn
.replaceAll(OUString(c
), OUString()); }
88 /** Strips occurrences of a character from the start of the source string
90 @param rIn The input OString
91 @param c The character to be stripped from the start
93 @return The resulting OString
95 COMPHELPER_DLLPUBLIC OString
stripStart(const OString
&rIn
,
98 /** Strips occurrences of a character from the start of the source string
100 @param rIn The input OUString
101 @param c The character to be stripped from the start
103 @return The resulting OUString
105 COMPHELPER_DLLPUBLIC OUString
stripStart(const OUString
&rIn
,
108 /** Strips occurrences of a character from the end of the source string
110 @param rIn The input OString
111 @param c The character to be stripped from the end
113 @return The resulting OString
115 COMPHELPER_DLLPUBLIC OString
stripEnd(const OString
&rIn
,
118 /** Strips occurrences of a character from the end of the source string
120 @param rIn The input OUString
121 @param c The character to be stripped from the end
123 @return The resulting OUString
125 COMPHELPER_DLLPUBLIC OUString
stripEnd(const OUString
&rIn
,
128 /** Strips occurrences of a character from the start and end of the source string
130 @param rIn The input OString
131 @param c The character to be stripped from the start and end
133 @return The resulting OString
135 COMPHELPER_DLLPUBLIC OString
strip(const OString
&rIn
,
138 /** Strips occurrences of a character from the start and end of the source string
140 @param rIn The input OUString
141 @param c The character to be stripped from the start and end
143 @return The resulting OUString
145 COMPHELPER_DLLPUBLIC OUString
strip(const OUString
&rIn
,
148 /** Returns a token in an OString
150 @deprecated Use OString::getToken(nToken, cTok) instead.
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
158 inline OString
getToken(const OString
&rIn
,
159 sal_Int32 nToken
, sal_Char cTok
) SAL_THROW(())
161 return rIn
.getToken(nToken
, cTok
);
164 /** Returns a token in an OUString
166 @deprecated Use OUString::getToken(nToken, cTok) instead.
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
174 inline OUString
getToken(const OUString
&rIn
,
175 sal_Int32 nToken
, sal_Unicode cTok
) SAL_THROW(())
177 return rIn
.getToken(nToken
, cTok
);
180 /** Returns number of tokens in an OUString
182 @param rIn the input OString
183 @param cTok the character which separate the tokens.
184 @return the number of tokens
186 COMPHELPER_DLLPUBLIC sal_Int32
getTokenCount(const OString
&rIn
, sal_Char cTok
);
188 /** Returns number of tokens in an OUString
190 @param rIn the input OUString
191 @param cTok the character which separate the tokens.
192 @return the number of tokens
194 COMPHELPER_DLLPUBLIC sal_Int32
getTokenCount(const OUString
&rIn
, sal_Unicode cTok
);
196 /** Reverse an OUString
198 @param rIn the input OUString
199 @return the reversed input
201 COMPHELPER_DLLPUBLIC OUString
reverseString(const OUString
&rStr
);
203 /** Reverse an OString
205 @param rIn the input OString
206 @return the reversed input
208 COMPHELPER_DLLPUBLIC OString
reverseString(const OString
&rStr
);
213 template<typename B
> B
& truncateToLength(B
& rBuffer
, sal_Int32 nLen
)
215 if (nLen
< rBuffer
.getLength())
216 rBuffer
.remove(nLen
, rBuffer
.getLength()-nLen
);
221 /** Truncate a buffer to a given length.
223 If the StringBuffer has more characters than nLength it will be truncated
224 on the right to nLength characters.
226 Has no effect if the StringBuffer is <= nLength
228 @param rBuf StringBuffer to operate on
229 @param nLength Length to truncate the buffer to
233 COMPHELPER_DLLPUBLIC
inline OStringBuffer
& truncateToLength(
234 OStringBuffer
& rBuffer
, sal_Int32 nLength
) SAL_THROW(())
236 return detail::truncateToLength(rBuffer
, nLength
);
239 COMPHELPER_DLLPUBLIC
inline OUStringBuffer
& truncateToLength(
240 OUStringBuffer
& rBuffer
, sal_Int32 nLength
) SAL_THROW(())
242 return detail::truncateToLength(rBuffer
, nLength
);
247 template<typename B
, typename U
> B
& padToLength(B
& rBuffer
, sal_Int32 nLen
,
250 sal_Int32 nOrigLen
= rBuffer
.getLength();
253 rBuffer
.setLength(nLen
);
254 for (sal_Int32 i
= nOrigLen
; i
< nLen
; ++i
)
261 /** Pad a buffer to a given length using a given char.
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.
266 Has no effect if the StringBuffer is >= nLength
268 @param rBuf StringBuffer to operate on
269 @param nLength Length to pad the buffer to
270 @param cFill character to fill expansion with
274 COMPHELPER_DLLPUBLIC
inline OStringBuffer
& padToLength(
275 OStringBuffer
& rBuffer
, sal_Int32 nLength
,
276 sal_Char cFill
= '\0') SAL_THROW(())
278 return detail::padToLength(rBuffer
, nLength
, cFill
);
281 COMPHELPER_DLLPUBLIC
inline OUStringBuffer
& padToLength(
282 OUStringBuffer
& rBuffer
, sal_Int32 nLength
,
283 sal_Unicode cFill
= '\0') SAL_THROW(())
285 return detail::padToLength(rBuffer
, nLength
, cFill
);
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
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
296 COMPHELPER_DLLPUBLIC sal_Int32
indexOfAny(OUString
const& rIn
,
297 sal_Unicode
const*const pChars
, sal_Int32
const nPos
= 0);
299 /** Convert a sequence of strings to a single comma separated string.
301 Note that no escaping of commas or anything fancy is done.
303 @param i_rSeq A list of strings to be concatenated.
305 @return A single string containing the concatenation of the given
306 list, interspersed with the string ", ".
308 COMPHELPER_DLLPUBLIC OUString
convertCommaSeparated(
309 ::com::sun::star::uno::Sequence
< OUString
> const & i_rSeq
);
311 /** Convert a decimal string to a number.
313 The string must be base-10, no sign but can contain any
314 codepoint listed in the "Number, Decimal Digit" Unicode
317 No verification is made about the validity of the string,
318 passing string not containing decimal digit code points
319 gives unspecified results
321 If your string is guaranteed to contain only ASCII digit
322 use OUString::toInt32 instead.
324 @param str The string to convert containing only decimal
327 @return The value of the string as an int32.
329 COMPHELPER_DLLPUBLIC sal_uInt32
decimalStringToNumber(
330 OUString
const & str
);
332 /** Convert a single comma separated string to a sequence of strings.
334 Note that no escaping of commas or anything fancy is done.
336 @param i_rString A string containing comma-separated words.
338 @return A sequence of strings resulting from splitting the given
339 string at ',' tokens and stripping whitespace.
341 COMPHELPER_DLLPUBLIC ::com::sun::star::uno::Sequence
< OUString
>
342 convertCommaSeparated( OUString
const & i_rString
);
345 Compares two strings using natural order.
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.
352 Beware that "MyString 001" and "MyString 1" will be considered as equal
353 since leading 0 are meaningless.
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
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
);
365 class COMPHELPER_DLLPUBLIC NaturalStringSorter
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
;
373 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> &rContext
,
374 const ::com::sun::star::lang::Locale
&rLocale
);
375 sal_Int32
compare(const OUString
&rLHS
, const OUString
&rRHS
) const
377 return compareNatural(rLHS
, rRHS
, m_xCollator
, m_xBI
, m_aLocale
);
379 const ::com::sun::star::lang::Locale
& getLocale() const { return m_aLocale
; }
382 /** Determine if an OString contains solely ASCII numeric digits
384 @param rString An OString
386 @return false if string contains any characters outside
387 the ASCII '0'-'9' range
388 true otherwise, including for empty string
390 COMPHELPER_DLLPUBLIC
bool isdigitAsciiString(const OString
&rString
);
392 /** Determine if an OUString contains solely ASCII numeric digits
394 @param rString An OUString
396 @return false if string contains any characters outside
397 the ASCII '0'-'9' range
398 true otherwise, including for empty string
400 COMPHELPER_DLLPUBLIC
bool isdigitAsciiString(const OUString
&rString
);
402 COMPHELPER_DLLPUBLIC
inline bool isdigitAscii(sal_Unicode c
)
404 return ((c
>= '0') && (c
<= '9'));
407 COMPHELPER_DLLPUBLIC
inline bool isxdigitAscii(sal_Unicode c
)
409 return isdigitAscii(c
) || (c
>= 'A' && c
<= 'F') || (c
>= 'a' && c
<= 'f');
412 COMPHELPER_DLLPUBLIC
inline bool islowerAscii(sal_Unicode c
)
414 return ((c
>= 'a') && (c
<= 'z'));
417 COMPHELPER_DLLPUBLIC
inline bool isupperAscii(sal_Unicode c
)
419 return ((c
>= 'A') && (c
<= 'Z'));
422 COMPHELPER_DLLPUBLIC
inline bool isalphaAscii(sal_Unicode c
)
424 return islowerAscii(c
) || isupperAscii(c
);
427 COMPHELPER_DLLPUBLIC
inline bool isalnumAscii(sal_Unicode c
)
429 return isalphaAscii(c
) || isdigitAscii(c
);
436 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */