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