LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / include / comphelper / string.hxx
blobd4c9ccd1b947ab9ffe8356f48c51ddb67745954a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <vector>
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.h>
31 #include <com/sun/star/uno/Reference.hxx>
33 #include <com/sun/star/lang/Locale.hpp>
35 namespace com::sun::star::i18n { class XBreakIterator; }
36 namespace com::sun::star::i18n { class XCollator; }
37 namespace com::sun::star::uno { class XComponentContext; }
39 // OUString helper functions that are not widespread or mature enough to
40 // go into the stable URE API:
41 namespace comphelper::string {
43 /** Removes all occurrences of a character from within the source string
45 @param rIn The input OUStringBuffer
46 @param c The character to be removed
48 @return The resulting OUStringBuffer
50 inline OUStringBuffer& remove(OUStringBuffer &rIn,
51 sal_Unicode c)
53 sal_Int32 index = 0;
54 while (true)
56 if (index >= rIn.getLength())
57 break;
58 index = rIn.indexOf(c, index);
59 if (index == -1)
60 break;
61 rIn.remove(index, 1);
63 return rIn;
66 /** Strips occurrences of a character from the start of the source string
68 @param rIn The input OString
69 @param c The character to be stripped from the start
71 @return The resulting OString
73 COMPHELPER_DLLPUBLIC OString stripStart(std::string_view rIn,
74 char c);
76 /** Strips occurrences of a character from the start of the source string
78 @param rIn The input OUString
79 @param c The character to be stripped from the start
81 @return The resulting OUString
83 COMPHELPER_DLLPUBLIC OUString stripStart(std::u16string_view rIn,
84 sal_Unicode c);
86 /** Strips occurrences of a character from the end of the source string
88 @param rIn The input OString
89 @param c The character to be stripped from the end
91 @return The resulting OString
93 COMPHELPER_DLLPUBLIC OString stripEnd(std::string_view rIn,
94 char c);
96 /** Strips occurrences of a character from the end of the source string
98 @param rIn The input OUString
99 @param c The character to be stripped from the end
101 @return The resulting OUString
103 COMPHELPER_DLLPUBLIC OUString stripEnd(std::u16string_view rIn,
104 sal_Unicode c);
106 /** Strips occurrences of a character from the start and end of the source string
108 @param rIn The input OString
109 @param c The character to be stripped from the start and end
111 @return The resulting OString
113 COMPHELPER_DLLPUBLIC OString strip(std::string_view rIn,
114 char c);
116 /** Strips occurrences of a character from the start and end of the source string
118 @param rIn The input OUString
119 @param c The character to be stripped from the start and end
121 @return The resulting OUString
123 COMPHELPER_DLLPUBLIC OUString strip(std::u16string_view rIn,
124 sal_Unicode c);
126 /** Returns number of tokens in an OUString
128 @param rIn the input OString
129 @param cTok the character which separate the tokens.
130 @return the number of tokens
132 COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(std::string_view rIn, char cTok);
134 /** Returns number of tokens in an OUString
136 @param rIn the input OUString
137 @param cTok the character which separate the tokens.
138 @return the number of tokens
140 COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(std::u16string_view rIn, sal_Unicode cTok);
142 /** Reverse an OUString
144 @param rIn the input OUString
145 @return the reversed input
147 COMPHELPER_DLLPUBLIC OUString reverseString(std::u16string_view rStr);
149 /** Reverse an OString
151 @param rIn the input OString
152 @return the reversed input
154 COMPHELPER_DLLPUBLIC OString reverseString(std::string_view rStr);
157 namespace detail
159 template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen)
161 if (nLen < rBuffer.getLength())
162 rBuffer.remove(nLen, rBuffer.getLength()-nLen);
163 return rBuffer;
167 /** Truncate a buffer to a given length.
169 If the StringBuffer has more characters than nLength it will be truncated
170 on the right to nLength characters.
172 Has no effect if the StringBuffer is <= nLength
174 @param rBuf StringBuffer to operate on
175 @param nLength Length to truncate the buffer to
177 @return rBuf;
179 inline OUStringBuffer& truncateToLength(
180 OUStringBuffer& rBuffer, sal_Int32 nLength)
182 return detail::truncateToLength(rBuffer, nLength);
185 namespace detail
187 template<typename B, typename U> B& padToLength(B& rBuffer, sal_Int32 nLen,
188 U cFill = '\0')
190 sal_Int32 nOrigLen = rBuffer.getLength();
191 if (nLen > nOrigLen)
193 rBuffer.setLength(nLen);
194 for (sal_Int32 i = nOrigLen; i < nLen; ++i)
195 rBuffer[i] = cFill;
197 return rBuffer;
201 /** Pad a buffer to a given length using a given char.
203 If the StringBuffer has less characters than nLength it will be expanded on
204 the right to nLength characters, with the expansion filled using cFill.
206 Has no effect if the StringBuffer is >= nLength
208 @param rBuf StringBuffer to operate on
209 @param nLength Length to pad the buffer to
210 @param cFill character to fill expansion with
212 @return rBuf;
214 inline OStringBuffer& padToLength(
215 OStringBuffer& rBuffer, sal_Int32 nLength,
216 char cFill = '\0')
218 return detail::padToLength(rBuffer, nLength, cFill);
221 inline OUStringBuffer& padToLength(
222 OUStringBuffer& rBuffer, sal_Int32 nLength,
223 sal_Unicode cFill = '\0')
225 return detail::padToLength(rBuffer, nLength, cFill);
228 /** Replace a token in a string
229 @param rIn OUString in which the token is to be replaced
230 @param nToken which nToken to replace
231 @param cTok token delimiter
232 @param rNewToken replacement token
234 @return original string with token nToken replaced by rNewToken
236 COMPHELPER_DLLPUBLIC OUString setToken(const OUString& rIn, sal_Int32 nToken, sal_Unicode cTok,
237 std::u16string_view rNewToken);
239 /** Find any of a list of code units in the string.
240 @param rIn OUString to search
241 @param pChars 0-terminated array of sal_Unicode code units to search for
242 @param nPos start position
244 @return position of first occurrence of any of the elements of pChars
245 or -1 if none of the code units occur in the string
247 COMPHELPER_DLLPUBLIC sal_Int32 indexOfAny(std::u16string_view rIn,
248 sal_Unicode const*const pChars, sal_Int32 const nPos);
250 /** Remove any of a list of code units in the string.
251 @param rIn OUString to search
252 @param pChars 0-terminated array of sal_Unicode code units to search for
254 @return OUString that has all of the pChars code units removed
256 COMPHELPER_DLLPUBLIC OUString removeAny(std::u16string_view rIn,
257 sal_Unicode const*const pChars);
259 /** Convert a sequence of strings to a single comma separated string.
261 Note that no escaping of commas or anything fancy is done.
263 @param i_rSeq A list of strings to be concatenated.
265 @return A single string containing the concatenation of the given
266 list, interspersed with the string ", ".
268 COMPHELPER_DLLPUBLIC OUString convertCommaSeparated(
269 css::uno::Sequence< OUString > const & i_rSeq);
271 /// Return a string which is the concatenation of the strings in the sequence.
272 COMPHELPER_DLLPUBLIC OString join(std::string_view rSeparator, const std::vector<OString>& rSequence);
274 /** Convert a decimal string to a number.
276 The string must be base-10, no sign but can contain any
277 codepoint listed in the "Number, Decimal Digit" Unicode
278 category.
280 No verification is made about the validity of the string,
281 passing string not containing decimal digit code points
282 gives unspecified results
284 If your string is guaranteed to contain only ASCII digit
285 use OUString::toInt32 instead.
287 @param str The string to convert containing only decimal
288 digit codepoints.
290 @return The value of the string as an int32.
292 COMPHELPER_DLLPUBLIC sal_uInt32 decimalStringToNumber(
293 OUString const & str );
295 COMPHELPER_DLLPUBLIC std::vector<OUString>
296 split(const OUString& rString, const sal_Unicode cSeparator);
298 /** Convert a single comma separated string to a sequence of strings.
300 Note that no escaping of commas or anything fancy is done.
302 @param i_rString A string containing comma-separated words.
304 @return A sequence of strings resulting from splitting the given
305 string at ',' tokens and stripping whitespace.
307 COMPHELPER_DLLPUBLIC css::uno::Sequence< OUString >
308 convertCommaSeparated( OUString const & i_rString );
311 Compares two strings using natural order.
313 For non digit characters, the comparison use the same algorithm as
314 rtl_str_compare. When a number is encountered during the comparison,
315 natural order is used. Thus, Heading 10 will be considered as greater
316 than Heading 2. Numerical comparison is done using decimal representation.
318 Beware that "MyString 001" and "MyString 1" will be considered as equal
319 since leading 0 are meaningless.
321 @param str the object to be compared.
322 @return 0 - if both strings are equal
323 < 0 - if this string is less than the string argument
324 > 0 - if this string is greater than the string argument
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 );
331 class COMPHELPER_DLLPUBLIC NaturalStringSorter
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:
338 NaturalStringSorter(
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
343 return compareNatural(rLHS, rRHS, m_xCollator, m_xBI, m_aLocale);
345 const css::lang::Locale& getLocale() const { return m_aLocale; }
348 /** Determine if an OString contains solely ASCII numeric digits
350 @param rString An OString
352 @return false if string contains any characters outside
353 the ASCII '0'-'9' range
354 true otherwise, including for empty string
356 COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::string_view rString);
358 /** Determine if an OUString contains solely ASCII numeric digits
360 @param rString An OUString
362 @return false if string contains any characters outside
363 the ASCII '0'-'9' range
364 true otherwise, including for empty string
366 COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::u16string_view rString);
370 #endif
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */