LibreOffice Module comphelper (master)  1
string.hxx
Go to the documentation of this file.
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 <vector>
27 #include <sal/types.h>
28 #include <rtl/strbuf.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <com/sun/star/uno/Sequence.h>
31 #include <com/sun/star/uno/Reference.hxx>
32 
33 #include <com/sun/star/lang/Locale.hpp>
34 
35 namespace com::sun::star::i18n { class XBreakIterator; }
36 namespace com::sun::star::i18n { class XCollator; }
37 namespace com::sun::star::uno { class XComponentContext; }
38 
39 // OUString helper functions that are not widespread or mature enough to
40 // go into the stable URE API:
41 namespace comphelper::string {
42 
50 inline bool equals(const OUString& rIn, sal_Unicode c)
51 { return rIn.getLength() == 1 && rIn[0] == c; }
52 
60 inline OUStringBuffer& remove(OUStringBuffer &rIn,
61  sal_Unicode c)
62 {
63  sal_Int32 index = 0;
64  while (true)
65  {
66  if (index >= rIn.getLength())
67  break;
68  index = rIn.indexOf(c, index);
69  if (index == -1)
70  break;
71  rIn.remove(index, 1);
72  }
73  return rIn;
74 }
75 
83 COMPHELPER_DLLPUBLIC OString stripStart(const OString &rIn,
84  char c);
85 
93 COMPHELPER_DLLPUBLIC OUString stripStart(const OUString &rIn,
94  sal_Unicode c);
95 
103 COMPHELPER_DLLPUBLIC OString stripEnd(const OString &rIn,
104  char c);
105 
113 COMPHELPER_DLLPUBLIC OUString stripEnd(const OUString &rIn,
114  sal_Unicode c);
115 
123 COMPHELPER_DLLPUBLIC OString strip(const OString &rIn,
124  char c);
125 
133 COMPHELPER_DLLPUBLIC OUString strip(const OUString &rIn,
134  sal_Unicode c);
135 
142 COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const OString &rIn, char cTok);
143 
150 COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const OUString &rIn, sal_Unicode cTok);
151 
157 COMPHELPER_DLLPUBLIC OUString reverseString(const OUString &rStr);
158 
164 COMPHELPER_DLLPUBLIC OString reverseString(const OString &rStr);
165 
166 
167 namespace detail
168 {
169  template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen)
170  {
171  if (nLen < rBuffer.getLength())
172  rBuffer.remove(nLen, rBuffer.getLength()-nLen);
173  return rBuffer;
174  }
175 }
176 
189 inline OUStringBuffer& truncateToLength(
190  OUStringBuffer& rBuffer, sal_Int32 nLength)
191 {
192  return detail::truncateToLength(rBuffer, nLength);
193 }
194 
195 namespace detail
196 {
197  template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen,
198  U cFill = '\0')
199  {
200  sal_Int32 nOrigLen = rBuffer.getLength();
201  if (nLen > nOrigLen)
202  {
203  rBuffer.setLength(nLen);
204  for (sal_Int32 i = nOrigLen; i < nLen; ++i)
205  rBuffer[i] = cFill;
206  }
207  return rBuffer;
208  }
209 }
210 
224 inline OStringBuffer& padToLength(
225  OStringBuffer& rBuffer, sal_Int32 nLength,
226  char cFill = '\0')
227 {
228  return detail::padToLength(rBuffer, nLength, cFill);
229 }
230 
231 inline OUStringBuffer& padToLength(
232  OUStringBuffer& rBuffer, sal_Int32 nLength,
233  sal_Unicode cFill = '\0')
234 {
235  return detail::padToLength(rBuffer, nLength, cFill);
236 }
237 
246 COMPHELPER_DLLPUBLIC OUString setToken(const OUString& rIn, sal_Int32 nToken, sal_Unicode cTok,
247  const OUString& rNewToken);
248 
257 COMPHELPER_DLLPUBLIC sal_Int32 indexOfAny(OUString const& rIn,
258  sal_Unicode const*const pChars, sal_Int32 const nPos);
259 
266 COMPHELPER_DLLPUBLIC OUString removeAny(OUString const& rIn,
267  sal_Unicode const*const pChars);
268 
279  css::uno::Sequence< OUString > const & i_rSeq);
280 
282 COMPHELPER_DLLPUBLIC OString join(const OString& rSeparator, const std::vector<OString>& rSequence);
283 
303  OUString const & str );
304 
305 COMPHELPER_DLLPUBLIC std::vector<OUString>
306  split(const OUString& rString, const sal_Unicode cSeparator);
307 
317 COMPHELPER_DLLPUBLIC css::uno::Sequence< OUString >
318  convertCommaSeparated( OUString const & i_rString );
319 
336 COMPHELPER_DLLPUBLIC sal_Int32 compareNatural( const OUString &rLHS, const OUString &rRHS,
337  const css::uno::Reference< css::i18n::XCollator > &rCollator,
338  const css::uno::Reference< css::i18n::XBreakIterator > &rBI,
339  const css::lang::Locale &rLocale );
340 
342 {
343 private:
344  css::lang::Locale const m_aLocale;
345  css::uno::Reference< css::i18n::XCollator > m_xCollator;
346  css::uno::Reference< css::i18n::XBreakIterator > m_xBI;
347 public:
349  const css::uno::Reference< css::uno::XComponentContext > &rContext,
350  const css::lang::Locale &rLocale);
351  sal_Int32 compare(const OUString &rLHS, const OUString &rRHS) const
352  {
353  return compareNatural(rLHS, rRHS, m_xCollator, m_xBI, m_aLocale);
354  }
355  const css::lang::Locale& getLocale() const { return m_aLocale; }
356 };
357 
366 COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const OString &rString);
367 
376 COMPHELPER_DLLPUBLIC bool isdigitAsciiString(const OUString &rString);
377 
378 }
379 
380 #endif
381 
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OString stripEnd(const OString &rIn, char c)
Strips occurrences of a character from the end of the source string.
Definition: string.cxx:97
css::lang::Locale const m_aLocale
Definition: string.hxx:344
OString join(const OString &rSeparator, const std::vector< OString > &rSequence)
Return a string which is the concatenation of the strings in the sequence.
Definition: string.cxx:289
sal_Int32 getTokenCount(const OString &rIn, char cTok)
Returns number of tokens in an OUString.
Definition: string.cxx:136
std::vector< OUString > split(const OUString &rStr, sal_Unicode cSeparator)
Definition: string.cxx:263
sal_uInt16 sal_Unicode
bool isdigitAsciiString(const OString &rString)
Determine if an OString contains solely ASCII numeric digits.
Definition: string.cxx:373
int i
OUStringBuffer & truncateToLength(OUStringBuffer &rBuffer, sal_Int32 nLength)
Truncate a buffer to a given length.
Definition: string.hxx:189
const css::lang::Locale & getLocale() const
Definition: string.hxx:355
css::uno::Reference< css::i18n::XBreakIterator > m_xBI
Definition: string.hxx:346
bool equals(const OUString &rIn, sal_Unicode c)
Compare an OUString to a single char.
Definition: string.hxx:50
sal_Int32 compareNatural(const OUString &rLHS, const OUString &rRHS, const uno::Reference< i18n::XCollator > &rCollator, const uno::Reference< i18n::XBreakIterator > &rBI, const lang::Locale &rLocale)
Definition: string.cxx:301
sal_Int32 indexOfAny(OUString const &rIn, sal_Unicode const *const pChars, sal_Int32 const nPos)
Find any of a list of code units in the string.
Definition: string.cxx:412
tuple index
OUString removeAny(OUString const &rIn, sal_Unicode const *const pChars)
Remove any of a list of code units in the string.
Definition: string.cxx:429
static sal_uInt32 decimalStringToNumber(OUString const &str, sal_Int32 nStart, sal_Int32 nLength)
Definition: string.cxx:146
OString strip(const OString &rIn, char c)
Strips occurrences of a character from the start and end of the source string.
Definition: string.cxx:107
B & padToLength(B &rBuffer, sal_Int32 nLen, U cFill= '\0')
Definition: string.hxx:197
OString stripStart(const OString &rIn, char c)
Strips occurrences of a character from the start of the source string.
Definition: string.cxx:66
B & truncateToLength(B &rBuffer, sal_Int32 nLen)
Definition: string.hxx:169
#define COMPHELPER_DLLPUBLIC
sal_Int32 compare(const OUString &rLHS, const OUString &rRHS) const
Definition: string.hxx:351
OUString setToken(const OUString &rIn, sal_Int32 nToken, sal_Unicode cTok, const OUString &rNewToken)
Replace a token in a string.
Definition: string.cxx:465
OUString reverseString(const OUString &rStr)
Reverse an OUString.
Definition: string.cxx:402
OUString convertCommaSeparated(uno::Sequence< OUString > const &i_rSeq)
Definition: string.cxx:253
css::uno::Reference< css::i18n::XCollator > m_xCollator
Definition: string.hxx:345
OStringBuffer & padToLength(OStringBuffer &rBuffer, sal_Int32 nLength, char cFill= '\0')
Pad a buffer to a given length using a given char.
Definition: string.hxx:224