xref: /aoo41x/main/comphelper/source/misc/string.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
29*cdf0e10cSrcweir #include "sal/config.h"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <cstddef>
32*cdf0e10cSrcweir #include <string.h>
33*cdf0e10cSrcweir #include <vector>
34*cdf0e10cSrcweir #include <algorithm>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <rtl/ustring.hxx>
37*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
38*cdf0e10cSrcweir #include <sal/types.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <comphelper/string.hxx>
41*cdf0e10cSrcweir #include <comphelper/stlunosequence.hxx>
42*cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace comphelper { namespace string {
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir rtl::OUString searchAndReplaceAsciiL(
48*cdf0e10cSrcweir     rtl::OUString const & source, char const * from, sal_Int32 fromLength,
49*cdf0e10cSrcweir     rtl::OUString const & to, sal_Int32 beginAt, sal_Int32 * replacedAt)
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir     sal_Int32 n = source.indexOfAsciiL(from, fromLength, beginAt);
52*cdf0e10cSrcweir     if (replacedAt != NULL) {
53*cdf0e10cSrcweir         *replacedAt = n;
54*cdf0e10cSrcweir     }
55*cdf0e10cSrcweir     return n == -1 ? source : source.replaceAt(n, fromLength, to);
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir ::rtl::OUString searchAndReplaceAllAsciiWithAscii(
59*cdf0e10cSrcweir     const ::rtl::OUString& _source, const sal_Char* _from, const sal_Char* _to,
60*cdf0e10cSrcweir     const sal_Int32 _beginAt )
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir     sal_Int32 fromLength = strlen( _from );
63*cdf0e10cSrcweir     sal_Int32 n = _source.indexOfAsciiL( _from, fromLength, _beginAt );
64*cdf0e10cSrcweir     if ( n == -1 )
65*cdf0e10cSrcweir         return _source;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     ::rtl::OUString dest( _source );
68*cdf0e10cSrcweir     ::rtl::OUString to( ::rtl::OUString::createFromAscii( _to ) );
69*cdf0e10cSrcweir     do
70*cdf0e10cSrcweir     {
71*cdf0e10cSrcweir         dest = dest.replaceAt( n, fromLength, to );
72*cdf0e10cSrcweir         n = dest.indexOfAsciiL( _from, fromLength, n + to.getLength() );
73*cdf0e10cSrcweir     }
74*cdf0e10cSrcweir     while ( n != -1 );
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     return dest;
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir ::rtl::OUString& searchAndReplaceAsciiI(
80*cdf0e10cSrcweir     ::rtl::OUString & _source, sal_Char const * _asciiPattern, ::rtl::OUString const & _replace,
81*cdf0e10cSrcweir     sal_Int32 _beginAt, sal_Int32 * _replacedAt )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     sal_Int32 fromLength = strlen( _asciiPattern );
84*cdf0e10cSrcweir     sal_Int32 n = _source.indexOfAsciiL( _asciiPattern, fromLength, _beginAt );
85*cdf0e10cSrcweir     if ( _replacedAt != NULL )
86*cdf0e10cSrcweir         *_replacedAt = n;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     if ( n != -1 )
89*cdf0e10cSrcweir         _source = _source.replaceAt( n, fromLength, _replace );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     return _source;
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir // convert between sequence of string and comma separated string
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir ::rtl::OUString convertCommaSeparated(
97*cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::rtl::OUString > const& i_rSeq)
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
100*cdf0e10cSrcweir     ::comphelper::intersperse(
101*cdf0e10cSrcweir         ::comphelper::stl_begin(i_rSeq), ::comphelper::stl_end(i_rSeq),
102*cdf0e10cSrcweir         ::comphelper::OUStringBufferAppender(buf),
103*cdf0e10cSrcweir         ::rtl::OUString::createFromAscii(", "));
104*cdf0e10cSrcweir     return buf.makeStringAndClear();
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::rtl::OUString >
108*cdf0e10cSrcweir     convertCommaSeparated( ::rtl::OUString const& i_rString )
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir     std::vector< ::rtl::OUString > vec;
111*cdf0e10cSrcweir     sal_Int32 idx = 0;
112*cdf0e10cSrcweir     do {
113*cdf0e10cSrcweir       ::rtl::OUString kw =
114*cdf0e10cSrcweir         i_rString.getToken(0, static_cast<sal_Unicode> (','), idx);
115*cdf0e10cSrcweir       kw = kw.trim();
116*cdf0e10cSrcweir       if (kw.getLength() > 0) {
117*cdf0e10cSrcweir           vec.push_back(kw);
118*cdf0e10cSrcweir       }
119*cdf0e10cSrcweir     } while (idx >= 0);
120*cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::rtl::OUString > kws(vec.size());
121*cdf0e10cSrcweir     std::copy(vec.begin(), vec.end(), stl_begin(kws));
122*cdf0e10cSrcweir     return kws;
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir } }
126