Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / file / FStringFunctions.cxx
blobf19dbc207db5a24ef3d449b8b4f118630e212ea4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "file/FStringFunctions.hxx"
22 #include <rtl/ustrbuf.hxx>
24 using namespace connectivity;
25 using namespace connectivity::file;
27 ORowSetValue OOp_Upper::operate(const ORowSetValue& lhs) const
29 if ( lhs.isNull() )
30 return lhs;
32 return lhs.getString().toAsciiUpperCase();
35 ORowSetValue OOp_Lower::operate(const ORowSetValue& lhs) const
37 if ( lhs.isNull() )
38 return lhs;
40 return lhs.getString().toAsciiLowerCase();
43 ORowSetValue OOp_Ascii::operate(const ORowSetValue& lhs) const
45 if ( lhs.isNull() )
46 return lhs;
47 OString sStr(OUStringToOString(lhs,RTL_TEXTENCODING_ASCII_US));
48 sal_Int32 nAscii = sStr.toChar();
49 return nAscii;
52 ORowSetValue OOp_CharLength::operate(const ORowSetValue& lhs) const
54 if ( lhs.isNull() )
55 return lhs;
57 return lhs.getString().getLength();
60 ORowSetValue OOp_Char::operate(const ::std::vector<ORowSetValue>& lhs) const
62 if ( lhs.empty() )
63 return ORowSetValue();
65 OUString sRet;
66 ::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
67 ::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
68 for (; aIter != aEnd; ++aIter)
70 if ( !aIter->isNull() )
72 sal_Char c = static_cast<sal_Char>(static_cast<sal_Int32>(*aIter));
74 sRet += OUString(&c,1,RTL_TEXTENCODING_ASCII_US);
78 return sRet;
81 ORowSetValue OOp_Concat::operate(const ::std::vector<ORowSetValue>& lhs) const
83 if ( lhs.empty() )
84 return ORowSetValue();
86 OUStringBuffer sRet;
87 ::std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
88 ::std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
89 for (; aIter != aEnd; ++aIter)
91 if ( aIter->isNull() )
92 return ORowSetValue();
94 sRet.append(aIter->operator OUString());
97 return sRet.makeStringAndClear();
100 ORowSetValue OOp_Locate::operate(const ::std::vector<ORowSetValue>& lhs) const
102 ::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
103 ::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
104 for (; aIter != aEnd; ++aIter)
106 if ( aIter->isNull() )
107 return ORowSetValue();
109 if ( lhs.size() == 2 )
110 return OUString::number(lhs[0].getString().indexOf(lhs[1].getString())+1);
112 else if ( lhs.size() != 3 )
113 return ORowSetValue();
115 return lhs[1].getString().indexOf(lhs[2].getString(),lhs[0]) + 1;
118 ORowSetValue OOp_SubString::operate(const ::std::vector<ORowSetValue>& lhs) const
120 ::std::vector<ORowSetValue>::const_iterator aIter = lhs.begin();
121 ::std::vector<ORowSetValue>::const_iterator aEnd = lhs.end();
122 for (; aIter != aEnd; ++aIter)
124 if ( aIter->isNull() )
125 return ORowSetValue();
127 if ( lhs.size() == 2 && static_cast<sal_Int32>(lhs[0]) >= sal_Int32(0) )
128 return lhs[1].getString().copy(static_cast<sal_Int32>(lhs[0])-1);
130 else if ( lhs.size() != 3 || static_cast<sal_Int32>(lhs[1]) < sal_Int32(0))
131 return ORowSetValue();
133 return lhs[2].getString().copy(static_cast<sal_Int32>(lhs[1])-1,lhs[0]);
136 ORowSetValue OOp_LTrim::operate(const ORowSetValue& lhs) const
138 if ( lhs.isNull() )
139 return lhs;
141 OUString sRet = lhs;
142 OUString sNew = sRet.trim();
143 return sRet.copy(sRet.indexOf(sNew));
146 ORowSetValue OOp_RTrim::operate(const ORowSetValue& lhs) const
148 if ( lhs.isNull() )
149 return lhs;
151 OUString sRet = lhs;
152 OUString sNew = sRet.trim();
153 return sRet.copy(0,sRet.lastIndexOf(sNew[sNew.getLength()-1])+1);
156 ORowSetValue OOp_Space::operate(const ORowSetValue& lhs) const
158 if ( lhs.isNull() )
159 return lhs;
161 const sal_Char c = ' ';
162 OUStringBuffer sRet;
163 sal_Int32 nCount = lhs;
164 for (sal_Int32 i=0; i < nCount; ++i)
166 sRet.appendAscii(&c,1);
168 return sRet.makeStringAndClear();
171 ORowSetValue OOp_Replace::operate(const ::std::vector<ORowSetValue>& lhs) const
173 if ( lhs.size() != 3 )
174 return ORowSetValue();
176 OUString sStr = lhs[2];
177 OUString sFrom = lhs[1];
178 OUString sTo = lhs[0];
179 sal_Int32 nIndexOf = sStr.indexOf(sFrom);
180 while( nIndexOf != -1 )
182 sStr = sStr.replaceAt(nIndexOf,sFrom.getLength(),sTo);
183 nIndexOf = sStr.indexOf(sFrom,nIndexOf + sTo.getLength());
186 return sStr;
189 ORowSetValue OOp_Repeat::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
191 if ( lhs.isNull() || rhs.isNull() )
192 return lhs;
194 OUString sRet;
195 sal_Int32 nCount = rhs;
196 for (sal_Int32 i=0; i < nCount; ++i)
198 sRet += lhs;
200 return sRet;
203 ORowSetValue OOp_Insert::operate(const ::std::vector<ORowSetValue>& lhs) const
205 if ( lhs.size() != 4 )
206 return ORowSetValue();
208 OUString sStr = lhs[3];
210 sal_Int32 nStart = static_cast<sal_Int32>(lhs[2]);
211 if ( nStart < 1 )
212 nStart = 1;
213 return sStr.replaceAt(nStart-1,static_cast<sal_Int32>(lhs[1]),lhs[0]);
216 ORowSetValue OOp_Left::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
218 if ( lhs.isNull() || rhs.isNull() )
219 return lhs;
221 OUString sRet = lhs;
222 sal_Int32 nCount = rhs;
223 if ( nCount < 0 )
224 return ORowSetValue();
225 return sRet.copy(0,nCount);
228 ORowSetValue OOp_Right::operate(const ORowSetValue& lhs,const ORowSetValue& rhs) const
230 if ( lhs.isNull() || rhs.isNull() )
231 return lhs;
233 sal_Int32 nCount = rhs;
234 OUString sRet = lhs;
235 if ( nCount < 0 || nCount >= sRet.getLength() )
236 return ORowSetValue();
238 return sRet.copy(sRet.getLength()-nCount,nCount);
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */