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#pragma once
20
21#include <sal/config.h>
22
23#include <algorithm>
24#include <vector>
26#include <sal/types.h>
27#include <rtl/strbuf.hxx>
28#include <rtl/ustrbuf.hxx>
29#include <com/sun/star/uno/Sequence.h>
30#include <com/sun/star/uno/Reference.hxx>
31
32#include <com/sun/star/lang/Locale.hpp>
33
34namespace com::sun::star::i18n { class XBreakIterator; }
35namespace com::sun::star::i18n { class XCollator; }
36namespace com::sun::star::uno { class XComponentContext; }
37
38// OUString helper functions that are not widespread or mature enough to
39// go into the stable URE API:
40namespace comphelper::string {
41
49inline OUStringBuffer& remove(OUStringBuffer &rIn,
51{
52 sal_Int32 index = 0;
53 while (true)
54 {
55 if (index >= rIn.getLength())
56 break;
57 index = rIn.indexOf(c, index);
58 if (index == -1)
59 break;
60 rIn.remove(index, 1);
61 }
62 return rIn;
63}
64
72COMPHELPER_DLLPUBLIC OString stripStart(const OString& rIn,
73 char c);
74COMPHELPER_DLLPUBLIC std::string_view stripStart(std::string_view rIn,
75 char c);
76
84COMPHELPER_DLLPUBLIC OUString stripStart(const OUString& rIn,
85 sal_Unicode c);
86COMPHELPER_DLLPUBLIC std::u16string_view stripStart(std::u16string_view rIn,
87 sal_Unicode c);
88
96COMPHELPER_DLLPUBLIC OString stripEnd(const OString& rIn,
97 char c);
98COMPHELPER_DLLPUBLIC std::string_view stripEnd(std::string_view rIn,
99 char c);
100
108COMPHELPER_DLLPUBLIC OUString stripEnd(const OUString& rIn,
109 sal_Unicode c);
110COMPHELPER_DLLPUBLIC std::u16string_view stripEnd(std::u16string_view rIn,
111 sal_Unicode c);
112
120COMPHELPER_DLLPUBLIC OString strip(const OString& rIn,
121 char c);
122COMPHELPER_DLLPUBLIC std::string_view strip(std::string_view rIn,
123 char c);
124
132COMPHELPER_DLLPUBLIC OUString strip(const OUString& rIn,
133 sal_Unicode c);
134COMPHELPER_DLLPUBLIC std::u16string_view strip(std::u16string_view rIn,
135 sal_Unicode c);
136
143COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(std::string_view rIn, char cTok);
144
151COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(std::u16string_view rIn, sal_Unicode cTok);
152
158COMPHELPER_DLLPUBLIC OUString reverseString(std::u16string_view rStr);
159
165COMPHELPER_DLLPUBLIC OString reverseString(std::string_view rStr);
166
167
168namespace detail
169{
170 template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen)
171 {
172 if (nLen < rBuffer.getLength())
173 rBuffer.setLength(nLen);
174 return rBuffer;
175 }
176}
177
190inline OUStringBuffer& truncateToLength(
191 OUStringBuffer& rBuffer, sal_Int32 nLength)
192{
193 return detail::truncateToLength(rBuffer, nLength);
194}
195
196namespace detail
197{
198 template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen, U cFill)
199 {
200 const sal_Int32 nPadLen = nLen - rBuffer.getLength();
201 if (nPadLen > 0)
202 std::fill_n(rBuffer.appendUninitialized(nPadLen), nPadLen, cFill);
203 return rBuffer;
204 }
205}
206
220inline OStringBuffer& padToLength(
221 OStringBuffer& rBuffer, sal_Int32 nLength,
222 char cFill = '\0')
223{
224 return detail::padToLength(rBuffer, nLength, cFill);
225}
226
227inline OUStringBuffer& padToLength(
228 OUStringBuffer& rBuffer, sal_Int32 nLength,
229 sal_Unicode cFill = '\0')
230{
231 return detail::padToLength(rBuffer, nLength, cFill);
232}
233
239COMPHELPER_DLLPUBLIC void replaceAt(OUStringBuffer& rIn, sal_Int32 index, sal_Int32 count, std::u16string_view newStr );
240
249COMPHELPER_DLLPUBLIC OUString setToken(const OUString& rIn, sal_Int32 nToken, sal_Unicode cTok,
250 std::u16string_view rNewToken);
251
260COMPHELPER_DLLPUBLIC sal_Int32 indexOfAny(std::u16string_view rIn,
261 sal_Unicode const*const pChars, sal_Int32 const nPos);
262
269COMPHELPER_DLLPUBLIC OUString removeAny(std::u16string_view rIn,
270 sal_Unicode const*const pChars);
271
282 css::uno::Sequence< OUString > const & i_rSeq);
283
285COMPHELPER_DLLPUBLIC OString join(std::string_view rSeparator, const std::vector<OString>& rSequence);
286
306 OUString const & str );
307
308COMPHELPER_DLLPUBLIC std::vector<OUString>
309 split(std::u16string_view rString, const sal_Unicode cSeparator);
310
320COMPHELPER_DLLPUBLIC css::uno::Sequence< OUString >
321 convertCommaSeparated( std::u16string_view i_rString );
322
339COMPHELPER_DLLPUBLIC sal_Int32 compareNatural( const OUString &rLHS, const OUString &rRHS,
340 const css::uno::Reference< css::i18n::XCollator > &rCollator,
341 const css::uno::Reference< css::i18n::XBreakIterator > &rBI,
342 const css::lang::Locale &rLocale );
343
345{
346private:
347 css::lang::Locale const m_aLocale;
348 css::uno::Reference< css::i18n::XCollator > m_xCollator;
349 css::uno::Reference< css::i18n::XBreakIterator > m_xBI;
350public:
352 const css::uno::Reference< css::uno::XComponentContext > &rContext,
353 css::lang::Locale aLocale);
354 sal_Int32 compare(const OUString &rLHS, const OUString &rRHS) const
355 {
356 return compareNatural(rLHS, rRHS, m_xCollator, m_xBI, m_aLocale);
357 }
358 const css::lang::Locale& getLocale() const { return m_aLocale; }
359};
360
369COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::string_view rString);
370
379COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::u16string_view rString);
380
381} // namespace comphelper::string
382
383/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::lang::Locale const m_aLocale
Definition: string.hxx:347
sal_Int32 compare(const OUString &rLHS, const OUString &rRHS) const
Definition: string.hxx:354
css::uno::Reference< css::i18n::XBreakIterator > m_xBI
Definition: string.hxx:349
css::uno::Reference< css::i18n::XCollator > m_xCollator
Definition: string.hxx:348
const css::lang::Locale & getLocale() const
Definition: string.hxx:358
#define COMPHELPER_DLLPUBLIC
B & padToLength(B &rBuffer, sal_Int32 nLen, U cFill)
Definition: string.hxx:198
B & truncateToLength(B &rBuffer, sal_Int32 nLen)
Definition: string.hxx:170
bool isdigitAsciiString(std::string_view rString)
Determine if an OString contains solely ASCII numeric digits.
Definition: string.cxx:507
OUString setToken(const OUString &rIn, sal_Int32 nToken, sal_Unicode cTok, std::u16string_view rNewToken)
Replace a token in a string.
Definition: string.cxx:599
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:406
OUStringBuffer & remove(OUStringBuffer &rIn, sal_Unicode c)
Removes all occurrences of a character from within the source string.
Definition: string.hxx:49
static sal_uInt32 decimalStringToNumber(OUString const &str, sal_Int32 nStart, sal_Int32 nLength)
Definition: string.cxx:264
OString strip(const OString &rIn, char c)
Strips occurrences of a character from the start and end of the source string.
Definition: string.cxx:215
OString stripEnd(const OString &rIn, char c)
Strips occurrences of a character from the end of the source string.
Definition: string.cxx:144
void replaceAt(OUStringBuffer &rIn, sal_Int32 nIndex, sal_Int32 nCount, std::u16string_view newStr)
Similar to OUString::replaceAt, but for an OUStringBuffer.
Definition: string.cxx:634
OUString reverseString(std::u16string_view rStr)
Reverse an OUString.
Definition: string.cxx:536
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:563
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:546
OStringBuffer & padToLength(OStringBuffer &rBuffer, sal_Int32 nLength, char cFill='\0')
Pad a buffer to a given length using a given char.
Definition: string.hxx:220
OUStringBuffer & truncateToLength(OUStringBuffer &rBuffer, sal_Int32 nLength)
Truncate a buffer to a given length.
Definition: string.hxx:190
OUString convertCommaSeparated(uno::Sequence< OUString > const &i_rSeq)
Definition: string.cxx:371
OString stripStart(const OString &rIn, char c)
Strips occurrences of a character from the start of the source string.
Definition: string.cxx:86
std::vector< OUString > split(std::u16string_view rStr, sal_Unicode cSeparator)
Definition: string.cxx:381
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:418
sal_Int32 getTokenCount(std::string_view rIn, char cTok)
Returns number of tokens in an OUString.
Definition: string.cxx:254
index
std::locale m_aLocale
sal_uInt16 sal_Unicode
sal_Int32 nLength