nss: upgrade to release 3.73
[LibreOffice.git] / connectivity / source / drivers / file / FStringFunctions.cxx
blob1df5e723c4ee238ddda5d81281b220e34dcd45f9
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 .
20 #include <file/FStringFunctions.hxx>
21 #include <rtl/ustrbuf.hxx>
23 using namespace connectivity;
24 using namespace connectivity::file;
26 ORowSetValue OOp_Upper::operate(const ORowSetValue& lhs) const
28 if (lhs.isNull())
29 return lhs;
31 return lhs.getString().toAsciiUpperCase();
34 ORowSetValue OOp_Lower::operate(const ORowSetValue& lhs) const
36 if (lhs.isNull())
37 return lhs;
39 return lhs.getString().toAsciiLowerCase();
42 ORowSetValue OOp_Ascii::operate(const ORowSetValue& lhs) const
44 if (lhs.isNull())
45 return lhs;
46 OString sStr(OUStringToOString(lhs, RTL_TEXTENCODING_ASCII_US));
47 sal_Int32 nAscii = sStr.toChar();
48 return nAscii;
51 ORowSetValue OOp_CharLength::operate(const ORowSetValue& lhs) const
53 if (lhs.isNull())
54 return lhs;
56 return lhs.getString().getLength();
59 ORowSetValue OOp_Char::operate(const std::vector<ORowSetValue>& lhs) const
61 if (lhs.empty())
62 return ORowSetValue();
64 OUStringBuffer sRet;
65 std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
66 std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
67 for (; aIter != aEnd; ++aIter)
69 if (!aIter->isNull())
71 char c = static_cast<char>(static_cast<sal_Int32>(*aIter));
73 sRet.appendAscii(&c, 1);
77 return sRet.makeStringAndClear();
80 ORowSetValue OOp_Concat::operate(const std::vector<ORowSetValue>& lhs) const
82 if (lhs.empty())
83 return ORowSetValue();
85 OUStringBuffer sRet;
86 std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin();
87 std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend();
88 for (; aIter != aEnd; ++aIter)
90 if (aIter->isNull())
91 return ORowSetValue();
93 sRet.append(aIter->operator OUString());
96 return sRet.makeStringAndClear();
99 ORowSetValue OOp_Locate::operate(const std::vector<ORowSetValue>& lhs) const
101 if (std::any_of(lhs.begin(), lhs.end(),
102 [](const ORowSetValue& rValue) { return rValue.isNull(); }))
103 return ORowSetValue();
105 if (lhs.size() == 2)
106 return OUString(OUString::number(lhs[0].getString().indexOf(lhs[1].getString()) + 1));
108 else if (lhs.size() != 3)
109 return ORowSetValue();
111 return lhs[1].getString().indexOf(lhs[2].getString(), lhs[0]) + 1;
114 ORowSetValue OOp_SubString::operate(const std::vector<ORowSetValue>& lhs) const
116 if (std::any_of(lhs.begin(), lhs.end(),
117 [](const ORowSetValue& rValue) { return rValue.isNull(); }))
118 return ORowSetValue();
120 if (lhs.size() == 2 && static_cast<sal_Int32>(lhs[0]) >= sal_Int32(0))
121 return lhs[1].getString().copy(static_cast<sal_Int32>(lhs[0]) - 1);
123 else if (lhs.size() != 3 || static_cast<sal_Int32>(lhs[1]) < sal_Int32(0))
124 return ORowSetValue();
126 return lhs[2].getString().copy(static_cast<sal_Int32>(lhs[1]) - 1, lhs[0]);
129 ORowSetValue OOp_LTrim::operate(const ORowSetValue& lhs) const
131 if (lhs.isNull())
132 return lhs;
134 OUString sRet = lhs;
135 OUString sNew = sRet.trim();
136 return sRet.copy(sRet.indexOf(sNew));
139 ORowSetValue OOp_RTrim::operate(const ORowSetValue& lhs) const
141 if (lhs.isNull())
142 return lhs;
144 OUString sRet = lhs;
145 OUString sNew = sRet.trim();
146 return sRet.copy(0, sRet.lastIndexOf(sNew[sNew.getLength() - 1]) + 1);
149 ORowSetValue OOp_Space::operate(const ORowSetValue& lhs) const
151 if (lhs.isNull())
152 return lhs;
154 const char c = ' ';
155 OUStringBuffer sRet;
156 sal_Int32 nCount = lhs;
157 for (sal_Int32 i = 0; i < nCount; ++i)
159 sRet.appendAscii(&c, 1);
161 return sRet.makeStringAndClear();
164 ORowSetValue OOp_Replace::operate(const std::vector<ORowSetValue>& lhs) const
166 if (lhs.size() != 3)
167 return ORowSetValue();
169 OUString sStr = lhs[2];
170 OUString sFrom = lhs[1];
171 OUString sTo = lhs[0];
172 sal_Int32 nIndexOf = sStr.indexOf(sFrom);
173 while (nIndexOf != -1)
175 sStr = sStr.replaceAt(nIndexOf, sFrom.getLength(), sTo);
176 nIndexOf = sStr.indexOf(sFrom, nIndexOf + sTo.getLength());
179 return sStr;
182 ORowSetValue OOp_Repeat::operate(const ORowSetValue& lhs, const ORowSetValue& rhs) const
184 if (lhs.isNull() || rhs.isNull())
185 return lhs;
187 OUStringBuffer sRet;
188 sal_Int32 nCount = rhs;
189 for (sal_Int32 i = 0; i < nCount; ++i)
191 sRet.append(lhs.operator OUString());
193 return sRet.makeStringAndClear();
196 ORowSetValue OOp_Insert::operate(const std::vector<ORowSetValue>& lhs) const
198 if (lhs.size() != 4)
199 return ORowSetValue();
201 OUString sStr = lhs[3];
203 sal_Int32 nStart = static_cast<sal_Int32>(lhs[2]);
204 if (nStart < 1)
205 nStart = 1;
206 return sStr.replaceAt(nStart - 1, static_cast<sal_Int32>(lhs[1]), lhs[0]);
209 ORowSetValue OOp_Left::operate(const ORowSetValue& lhs, const ORowSetValue& rhs) const
211 if (lhs.isNull() || rhs.isNull())
212 return lhs;
214 OUString sRet = lhs;
215 sal_Int32 nCount = rhs;
216 if (nCount < 0)
217 return ORowSetValue();
218 return sRet.copy(0, nCount);
221 ORowSetValue OOp_Right::operate(const ORowSetValue& lhs, const ORowSetValue& rhs) const
223 if (lhs.isNull() || rhs.isNull())
224 return lhs;
226 sal_Int32 nCount = rhs;
227 OUString sRet = lhs;
228 if (nCount < 0 || nCount >= sRet.getLength())
229 return ORowSetValue();
231 return sRet.copy(sRet.getLength() - nCount, nCount);
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */