1dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3dde7d3faSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4dde7d3faSAndrew Rist * or more contributor license agreements. See the NOTICE file
5dde7d3faSAndrew Rist * distributed with this work for additional information
6dde7d3faSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7dde7d3faSAndrew Rist * to you under the Apache License, Version 2.0 (the
8dde7d3faSAndrew Rist * "License"); you may not use this file except in compliance
9dde7d3faSAndrew Rist * with the License. You may obtain a copy of the License at
10dde7d3faSAndrew Rist *
11dde7d3faSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12dde7d3faSAndrew Rist *
13dde7d3faSAndrew Rist * Unless required by applicable law or agreed to in writing,
14dde7d3faSAndrew Rist * software distributed under the License is distributed on an
15dde7d3faSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16dde7d3faSAndrew Rist * KIND, either express or implied. See the License for the
17dde7d3faSAndrew Rist * specific language governing permissions and limitations
18dde7d3faSAndrew Rist * under the License.
19dde7d3faSAndrew Rist *
20dde7d3faSAndrew Rist *************************************************************/
21dde7d3faSAndrew Rist
22dde7d3faSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
25cdf0e10cSrcweir #include "sal/config.h"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <cstddef>
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir #include <vector>
30cdf0e10cSrcweir #include <algorithm>
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include <rtl/ustring.hxx>
33cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
34cdf0e10cSrcweir #include <sal/types.h>
35cdf0e10cSrcweir
36cdf0e10cSrcweir #include <comphelper/string.hxx>
37cdf0e10cSrcweir #include <comphelper/stlunosequence.hxx>
38cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
39cdf0e10cSrcweir
40cdf0e10cSrcweir
41cdf0e10cSrcweir namespace comphelper { namespace string {
42cdf0e10cSrcweir
searchAndReplaceAsciiL(rtl::OUString const & source,char const * from,sal_Int32 fromLength,rtl::OUString const & to,sal_Int32 beginAt,sal_Int32 * replacedAt)43cdf0e10cSrcweir rtl::OUString searchAndReplaceAsciiL(
44cdf0e10cSrcweir rtl::OUString const & source, char const * from, sal_Int32 fromLength,
45cdf0e10cSrcweir rtl::OUString const & to, sal_Int32 beginAt, sal_Int32 * replacedAt)
46cdf0e10cSrcweir {
47cdf0e10cSrcweir sal_Int32 n = source.indexOfAsciiL(from, fromLength, beginAt);
48cdf0e10cSrcweir if (replacedAt != NULL) {
49cdf0e10cSrcweir *replacedAt = n;
50cdf0e10cSrcweir }
51cdf0e10cSrcweir return n == -1 ? source : source.replaceAt(n, fromLength, to);
52cdf0e10cSrcweir }
53cdf0e10cSrcweir
searchAndReplaceAllAsciiWithAscii(const::rtl::OUString & _source,const sal_Char * _from,const sal_Char * _to,const sal_Int32 _beginAt)54cdf0e10cSrcweir ::rtl::OUString searchAndReplaceAllAsciiWithAscii(
55cdf0e10cSrcweir const ::rtl::OUString& _source, const sal_Char* _from, const sal_Char* _to,
56cdf0e10cSrcweir const sal_Int32 _beginAt )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir sal_Int32 fromLength = strlen( _from );
59cdf0e10cSrcweir sal_Int32 n = _source.indexOfAsciiL( _from, fromLength, _beginAt );
60cdf0e10cSrcweir if ( n == -1 )
61cdf0e10cSrcweir return _source;
62cdf0e10cSrcweir
63cdf0e10cSrcweir ::rtl::OUString dest( _source );
64cdf0e10cSrcweir ::rtl::OUString to( ::rtl::OUString::createFromAscii( _to ) );
65cdf0e10cSrcweir do
66cdf0e10cSrcweir {
67cdf0e10cSrcweir dest = dest.replaceAt( n, fromLength, to );
68cdf0e10cSrcweir n = dest.indexOfAsciiL( _from, fromLength, n + to.getLength() );
69cdf0e10cSrcweir }
70cdf0e10cSrcweir while ( n != -1 );
71cdf0e10cSrcweir
72cdf0e10cSrcweir return dest;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir
searchAndReplaceAsciiI(::rtl::OUString & _source,sal_Char const * _asciiPattern,::rtl::OUString const & _replace,sal_Int32 _beginAt,sal_Int32 * _replacedAt)75cdf0e10cSrcweir ::rtl::OUString& searchAndReplaceAsciiI(
76cdf0e10cSrcweir ::rtl::OUString & _source, sal_Char const * _asciiPattern, ::rtl::OUString const & _replace,
77cdf0e10cSrcweir sal_Int32 _beginAt, sal_Int32 * _replacedAt )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir sal_Int32 fromLength = strlen( _asciiPattern );
80cdf0e10cSrcweir sal_Int32 n = _source.indexOfAsciiL( _asciiPattern, fromLength, _beginAt );
81cdf0e10cSrcweir if ( _replacedAt != NULL )
82cdf0e10cSrcweir *_replacedAt = n;
83cdf0e10cSrcweir
84cdf0e10cSrcweir if ( n != -1 )
85cdf0e10cSrcweir _source = _source.replaceAt( n, fromLength, _replace );
86cdf0e10cSrcweir
87cdf0e10cSrcweir return _source;
88cdf0e10cSrcweir }
89cdf0e10cSrcweir
90cdf0e10cSrcweir // convert between sequence of string and comma separated string
91cdf0e10cSrcweir
convertCommaSeparated(::com::sun::star::uno::Sequence<::rtl::OUString> const & i_rSeq)92cdf0e10cSrcweir ::rtl::OUString convertCommaSeparated(
93cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString > const& i_rSeq)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir ::rtl::OUStringBuffer buf;
96cdf0e10cSrcweir ::comphelper::intersperse(
97cdf0e10cSrcweir ::comphelper::stl_begin(i_rSeq), ::comphelper::stl_end(i_rSeq),
98cdf0e10cSrcweir ::comphelper::OUStringBufferAppender(buf),
99cdf0e10cSrcweir ::rtl::OUString::createFromAscii(", "));
100cdf0e10cSrcweir return buf.makeStringAndClear();
101cdf0e10cSrcweir }
102cdf0e10cSrcweir
103cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString >
convertCommaSeparated(::rtl::OUString const & i_rString)104cdf0e10cSrcweir convertCommaSeparated( ::rtl::OUString const& i_rString )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir std::vector< ::rtl::OUString > vec;
107cdf0e10cSrcweir sal_Int32 idx = 0;
108cdf0e10cSrcweir do {
109cdf0e10cSrcweir ::rtl::OUString kw =
110cdf0e10cSrcweir i_rString.getToken(0, static_cast<sal_Unicode> (','), idx);
111cdf0e10cSrcweir kw = kw.trim();
112*49b34792SHerbert Dürr if ( !kw.isEmpty() ) {
113cdf0e10cSrcweir vec.push_back(kw);
114cdf0e10cSrcweir }
115cdf0e10cSrcweir } while (idx >= 0);
116cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString > kws(vec.size());
117cdf0e10cSrcweir std::copy(vec.begin(), vec.end(), stl_begin(kws));
118cdf0e10cSrcweir return kws;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir
121cdf0e10cSrcweir } }
122