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 OUStringBuffer& remove(OUStringBuffer &rIn,
51  sal_Unicode c)
52 {
53  sal_Int32 index = 0;
54  while (true)
55  {
56  if (index >= rIn.getLength())
57  break;
58  index = rIn.indexOf(c, index);
59  if (index == -1)
60  break;
61  rIn.remove(index, 1);
62  }
63  return rIn;
64 }
65 
73 COMPHELPER_DLLPUBLIC OString stripStart(std::string_view rIn,
74  char c);
75 
83 COMPHELPER_DLLPUBLIC OUString stripStart(std::u16string_view rIn,
84  sal_Unicode c);
85 
93 COMPHELPER_DLLPUBLIC OString stripEnd(std::string_view rIn,
94  char c);
95 
103 COMPHELPER_DLLPUBLIC OUString stripEnd(std::u16string_view rIn,
104  sal_Unicode c);
105 
113 COMPHELPER_DLLPUBLIC OString strip(std::string_view rIn,
114  char c);
115 
123 COMPHELPER_DLLPUBLIC OUString strip(std::u16string_view rIn,
124  sal_Unicode c);
125 
132 COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(std::string_view rIn, char cTok);
133 
140 COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(std::u16string_view rIn, sal_Unicode cTok);
141 
147 COMPHELPER_DLLPUBLIC OUString reverseString(std::u16string_view rStr);
148 
154 COMPHELPER_DLLPUBLIC OString reverseString(std::string_view rStr);
155 
156 
157 namespace detail
158 {
159  template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen)
160  {
161  if (nLen < rBuffer.getLength())
162  rBuffer.remove(nLen, rBuffer.getLength()-nLen);
163  return rBuffer;
164  }
165 }
166 
179 inline OUStringBuffer& truncateToLength(
180  OUStringBuffer& rBuffer, sal_Int32 nLength)
181 {
182  return detail::truncateToLength(rBuffer, nLength);
183 }
184 
185 namespace detail
186 {
187  template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen,
188  U cFill = '\0')
189  {
190  sal_Int32 nOrigLen = rBuffer.getLength();
191  if (nLen > nOrigLen)
192  {
193  rBuffer.setLength(nLen);
194  for (sal_Int32 i = nOrigLen; i < nLen; ++i)
195  rBuffer[i] = cFill;
196  }
197  return rBuffer;
198  }
199 }
200 
214 inline OStringBuffer& padToLength(
215  OStringBuffer& rBuffer, sal_Int32 nLength,
216  char cFill = '\0')
217 {
218  return detail::padToLength(rBuffer, nLength, cFill);
219 }
220 
221 inline OUStringBuffer& padToLength(
222  OUStringBuffer& rBuffer, sal_Int32 nLength,
223  sal_Unicode cFill = '\0')
224 {
225  return detail::padToLength(rBuffer, nLength, cFill);
226 }
227 
236 COMPHELPER_DLLPUBLIC OUString setToken(const OUString& rIn, sal_Int32 nToken, sal_Unicode cTok,
237  const OUString& rNewToken);
238 
247 COMPHELPER_DLLPUBLIC sal_Int32 indexOfAny(std::u16string_view rIn,
248  sal_Unicode const*const pChars, sal_Int32 const nPos);
249 
256 COMPHELPER_DLLPUBLIC OUString removeAny(std::u16string_view rIn,
257  sal_Unicode const*const pChars);
258 
269  css::uno::Sequence< OUString > const & i_rSeq);
270 
272 COMPHELPER_DLLPUBLIC OString join(std::string_view rSeparator, const std::vector<OString>& rSequence);
273 
293  OUString const & str );
294 
295 COMPHELPER_DLLPUBLIC std::vector<OUString>
296  split(const OUString& rString, const sal_Unicode cSeparator);
297 
307 COMPHELPER_DLLPUBLIC css::uno::Sequence< OUString >
308  convertCommaSeparated( OUString const & i_rString );
309 
326 COMPHELPER_DLLPUBLIC sal_Int32 compareNatural( const OUString &rLHS, const OUString &rRHS,
327  const css::uno::Reference< css::i18n::XCollator > &rCollator,
328  const css::uno::Reference< css::i18n::XBreakIterator > &rBI,
329  const css::lang::Locale &rLocale );
330 
332 {
333 private:
334  css::lang::Locale const m_aLocale;
335  css::uno::Reference< css::i18n::XCollator > m_xCollator;
336  css::uno::Reference< css::i18n::XBreakIterator > m_xBI;
337 public:
339  const css::uno::Reference< css::uno::XComponentContext > &rContext,
340  const css::lang::Locale &rLocale);
341  sal_Int32 compare(const OUString &rLHS, const OUString &rRHS) const
342  {
343  return compareNatural(rLHS, rRHS, m_xCollator, m_xBI, m_aLocale);
344  }
345  const css::lang::Locale& getLocale() const { return m_aLocale; }
346 };
347 
356 COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::string_view rString);
357 
366 COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::u16string_view rString);
367 
368 }
369 
370 #endif
371 
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OString stripEnd(std::string_view 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:334
OString strip(std::string_view rIn, char c)
Strips occurrences of a character from the start and end of the source string.
Definition: string.cxx:107
bool isdigitAsciiString(std::string_view rString)
Determine if an OString contains solely ASCII numeric digits.
Definition: string.cxx:375
std::vector< OUString > split(const OUString &rStr, sal_Unicode cSeparator)
Definition: string.cxx:265
sal_uInt16 sal_Unicode
OString join(std::string_view rSeparator, const std::vector< OString > &rSequence)
Return a string which is the concatenation of the strings in the sequence.
Definition: string.cxx:291
OUString removeAny(std::u16string_view rIn, sal_Unicode const *const pChars)
Remove any of a list of code units in the string.
Definition: string.cxx:431
sal_Int32 getTokenCount(std::string_view rIn, char cTok)
Returns number of tokens in an OUString.
Definition: string.cxx:138
int i
OUStringBuffer & truncateToLength(OUStringBuffer &rBuffer, sal_Int32 nLength)
Truncate a buffer to a given length.
Definition: string.hxx:179
const css::lang::Locale & getLocale() const
Definition: string.hxx:345
css::uno::Reference< css::i18n::XBreakIterator > m_xBI
Definition: string.hxx:336
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:303
sal_Int32 indexOfAny(std::u16string_view rIn, sal_Unicode const *const pChars, sal_Int32 const nPos)
Find any of a list of code units in the string.
Definition: string.cxx:414
tuple index
static sal_uInt32 decimalStringToNumber(OUString const &str, sal_Int32 nStart, sal_Int32 nLength)
Definition: string.cxx:148
OString stripStart(std::string_view rIn, char c)
Strips occurrences of a character from the start of the source string.
Definition: string.cxx:66
B & padToLength(B &rBuffer, sal_Int32 nLen, U cFill= '\0')
Definition: string.hxx:187
B & truncateToLength(B &rBuffer, sal_Int32 nLen)
Definition: string.hxx:159
#define COMPHELPER_DLLPUBLIC
sal_Int32 compare(const OUString &rLHS, const OUString &rRHS) const
Definition: string.hxx:341
OUString reverseString(std::u16string_view rStr)
Reverse an OUString.
Definition: string.cxx:404
OUString setToken(const OUString &rIn, sal_Int32 nToken, sal_Unicode cTok, const OUString &rNewToken)
Replace a token in a string.
Definition: string.cxx:467
OUString convertCommaSeparated(uno::Sequence< OUString > const &i_rSeq)
Definition: string.cxx:255
css::uno::Reference< css::i18n::XCollator > m_xCollator
Definition: string.hxx:335
OStringBuffer & padToLength(OStringBuffer &rBuffer, sal_Int32 nLength, char cFill= '\0')
Pad a buffer to a given length using a given char.
Definition: string.hxx:214