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