xref: /aoo41x/main/comphelper/source/misc/string.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 #include "precompiled_comphelper.hxx"
29 #include "sal/config.h"
30 
31 #include <cstddef>
32 #include <string.h>
33 #include <vector>
34 #include <algorithm>
35 
36 #include <rtl/ustring.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <sal/types.h>
39 
40 #include <comphelper/string.hxx>
41 #include <comphelper/stlunosequence.hxx>
42 #include <comphelper/stl_types.hxx>
43 
44 
45 namespace comphelper { namespace string {
46 
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)
50 {
51     sal_Int32 n = source.indexOfAsciiL(from, fromLength, beginAt);
52     if (replacedAt != NULL) {
53         *replacedAt = n;
54     }
55     return n == -1 ? source : source.replaceAt(n, fromLength, to);
56 }
57 
58 ::rtl::OUString searchAndReplaceAllAsciiWithAscii(
59     const ::rtl::OUString& _source, const sal_Char* _from, const sal_Char* _to,
60     const sal_Int32 _beginAt )
61 {
62     sal_Int32 fromLength = strlen( _from );
63     sal_Int32 n = _source.indexOfAsciiL( _from, fromLength, _beginAt );
64     if ( n == -1 )
65         return _source;
66 
67     ::rtl::OUString dest( _source );
68     ::rtl::OUString to( ::rtl::OUString::createFromAscii( _to ) );
69     do
70     {
71         dest = dest.replaceAt( n, fromLength, to );
72         n = dest.indexOfAsciiL( _from, fromLength, n + to.getLength() );
73     }
74     while ( n != -1 );
75 
76     return dest;
77 }
78 
79 ::rtl::OUString& searchAndReplaceAsciiI(
80     ::rtl::OUString & _source, sal_Char const * _asciiPattern, ::rtl::OUString const & _replace,
81     sal_Int32 _beginAt, sal_Int32 * _replacedAt )
82 {
83     sal_Int32 fromLength = strlen( _asciiPattern );
84     sal_Int32 n = _source.indexOfAsciiL( _asciiPattern, fromLength, _beginAt );
85     if ( _replacedAt != NULL )
86         *_replacedAt = n;
87 
88     if ( n != -1 )
89         _source = _source.replaceAt( n, fromLength, _replace );
90 
91     return _source;
92 }
93 
94 // convert between sequence of string and comma separated string
95 
96 ::rtl::OUString convertCommaSeparated(
97     ::com::sun::star::uno::Sequence< ::rtl::OUString > const& i_rSeq)
98 {
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();
105 }
106 
107 ::com::sun::star::uno::Sequence< ::rtl::OUString >
108     convertCommaSeparated( ::rtl::OUString const& i_rString )
109 {
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);
118       }
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;
123 }
124 
125 } }
126