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