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
162COMPHELPER_DLLPUBLIC OUString reverseCodePoints(OUString const & str);
163
164
165namespace detail
166{
167 template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen)
168 {
169 if (nLen < rBuffer.getLength())
170 rBuffer.setLength(nLen);
171 return rBuffer;
172 }
173}
174
187inline OUStringBuffer& truncateToLength(
188 OUStringBuffer& rBuffer, sal_Int32 nLength)
189{
190 return detail::truncateToLength(rBuffer, nLength);
191}
192
193namespace detail
194{
195 template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen, U cFill)
196 {
197 const sal_Int32 nPadLen = nLen - rBuffer.getLength();
198 if (nPadLen > 0)
199 std::fill_n(rBuffer.appendUninitialized(nPadLen), nPadLen, cFill);
200 return rBuffer;
201 }
202}
203
217inline OStringBuffer& padToLength(
218 OStringBuffer& rBuffer, sal_Int32 nLength,
219 char cFill = '\0')
220{
221 return detail::padToLength(rBuffer, nLength, cFill);
222}
223
224inline OUStringBuffer& padToLength(
225 OUStringBuffer& rBuffer, sal_Int32 nLength,
226 sal_Unicode cFill = '\0')
227{
228 return detail::padToLength(rBuffer, nLength, cFill);
229}
230
236COMPHELPER_DLLPUBLIC void replaceAt(OUStringBuffer& rIn, sal_Int32 index, sal_Int32 count, std::u16string_view newStr );
237
246COMPHELPER_DLLPUBLIC OUString setToken(const OUString& rIn, sal_Int32 nToken, sal_Unicode cTok,
247 std::u16string_view rNewToken);
248
257COMPHELPER_DLLPUBLIC sal_Int32 indexOfAny(std::u16string_view rIn,
258 sal_Unicode const*const pChars, sal_Int32 const nPos);
259
266COMPHELPER_DLLPUBLIC OUString removeAny(std::u16string_view rIn,
267 sal_Unicode const*const pChars);
268
279 css::uno::Sequence< OUString > const & i_rSeq);
280
282COMPHELPER_DLLPUBLIC OString join(std::string_view rSeparator, const std::vector<OString>& rSequence);
283
303 std::u16string_view str );
304
305COMPHELPER_DLLPUBLIC std::vector<OUString>
306 split(std::u16string_view rString, const sal_Unicode cSeparator);
307
317COMPHELPER_DLLPUBLIC css::uno::Sequence< OUString >
318 convertCommaSeparated( std::u16string_view i_rString );
319
336COMPHELPER_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{
343private:
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;
347public:
349 const css::uno::Reference< css::uno::XComponentContext > &rContext,
350 css::lang::Locale aLocale);
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
366COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::string_view rString);
367
376COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::u16string_view rString);
377
385COMPHELPER_DLLPUBLIC OUString sanitizeStringSurrogates(const OUString& rString);
386
387} // namespace comphelper::string
388
389/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::lang::Locale const m_aLocale
Definition: string.hxx:344
sal_Int32 compare(const OUString &rLHS, const OUString &rRHS) const
Definition: string.hxx:351
css::uno::Reference< css::i18n::XBreakIterator > m_xBI
Definition: string.hxx:346
css::uno::Reference< css::i18n::XCollator > m_xCollator
Definition: string.hxx:345
const css::lang::Locale & getLocale() const
Definition: string.hxx:355
#define COMPHELPER_DLLPUBLIC
B & padToLength(B &rBuffer, sal_Int32 nLen, U cFill)
Definition: string.hxx:195
B & truncateToLength(B &rBuffer, sal_Int32 nLen)
Definition: string.hxx:167
bool isdigitAsciiString(std::string_view rString)
Determine if an OString contains solely ASCII numeric digits.
Definition: string.cxx:502
OUString setToken(const OUString &rIn, sal_Int32 nToken, sal_Unicode cTok, std::u16string_view rNewToken)
Replace a token in a string.
Definition: string.cxx:590
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:401
OUStringBuffer & remove(OUStringBuffer &rIn, sal_Unicode c)
Removes all occurrences of a character from within the source string.
Definition: string.hxx:49
OUString sanitizeStringSurrogates(const OUString &rString)
Sanitize an OUString to not have invalid surrogates.
Definition: string.cxx:650
OString strip(const OString &rIn, char c)
Strips occurrences of a character from the start and end of the source string.
Definition: string.cxx:217
OString stripEnd(const OString &rIn, char c)
Strips occurrences of a character from the end of the source string.
Definition: string.cxx:146
sal_uInt32 decimalStringToNumber(std::u16string_view str)
Convert a decimal string to a number.
Definition: string.cxx:266
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:625
OUString reverseString(std::u16string_view rStr)
Reverse an OUString's UTF-16 code units.
Definition: string.cxx:516
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:554
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:537
OStringBuffer & padToLength(OStringBuffer &rBuffer, sal_Int32 nLength, char cFill='\0')
Pad a buffer to a given length using a given char.
Definition: string.hxx:217
OUString reverseCodePoints(OUString const &str)
Reverse an OUString's Unicode code points.
Definition: string.cxx:528
OUStringBuffer & truncateToLength(OUStringBuffer &rBuffer, sal_Int32 nLength)
Truncate a buffer to a given length.
Definition: string.hxx:187
OUString convertCommaSeparated(uno::Sequence< OUString > const &i_rSeq)
Definition: string.cxx:366
OString stripStart(const OString &rIn, char c)
Strips occurrences of a character from the start of the source string.
Definition: string.cxx:88
std::vector< OUString > split(std::u16string_view rStr, sal_Unicode cSeparator)
Definition: string.cxx:376
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:413
sal_Int32 getTokenCount(std::string_view rIn, char cTok)
Returns number of tokens in an OUString.
Definition: string.cxx:256
index
std::locale m_aLocale
sal_uInt16 sal_Unicode
sal_Int32 nLength