1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <file/FStringFunctions.hxx>
21 #include <rtl/ustrbuf.hxx>
22
23 using namespace connectivity;
24 using namespace connectivity::file;
25
operate(const ORowSetValue & lhs) const26 ORowSetValue OOp_Upper::operate(const ORowSetValue& lhs) const
27 {
28 if (lhs.isNull())
29 return lhs;
30
31 return lhs.getString().toAsciiUpperCase();
32 }
33
operate(const ORowSetValue & lhs) const34 ORowSetValue OOp_Lower::operate(const ORowSetValue& lhs) const
35 {
36 if (lhs.isNull())
37 return lhs;
38
39 return lhs.getString().toAsciiLowerCase();
40 }
41
operate(const ORowSetValue & lhs) const42 ORowSetValue OOp_Ascii::operate(const ORowSetValue& lhs) const
43 {
44 if (lhs.isNull())
45 return lhs;
46 OString sStr(OUStringToOString(lhs.getString(), RTL_TEXTENCODING_ASCII_US));
47 sal_Int32 nAscii = sStr.toChar();
48 return nAscii;
49 }
50
operate(const ORowSetValue & lhs) const51 ORowSetValue OOp_CharLength::operate(const ORowSetValue& lhs) const
52 {
53 if (lhs.isNull())
54 return lhs;
55
56 return lhs.getString().getLength();
57 }
58
operate(const std::vector<ORowSetValue> & lhs) const59 ORowSetValue OOp_Char::operate(const std::vector<ORowSetValue>& lhs) const
60 {
61 if (lhs.empty())
62 return ORowSetValue();
63
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)
68 {
69 if (!aIter->isNull())
70 {
71 char c = static_cast<char>(static_cast<sal_Int32>(*aIter));
72
73 sRet.appendAscii(&c, 1);
74 }
75 }
76
77 return sRet.makeStringAndClear();
78 }
79
operate(const std::vector<ORowSetValue> & lhs) const80 ORowSetValue OOp_Concat::operate(const std::vector<ORowSetValue>& lhs) const
81 {
82 if (lhs.empty())
83 return ORowSetValue();
84
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)
89 {
90 if (aIter->isNull())
91 return ORowSetValue();
92
93 sRet.append(aIter->operator OUString());
94 }
95
96 return sRet.makeStringAndClear();
97 }
98
operate(const std::vector<ORowSetValue> & lhs) const99 ORowSetValue OOp_Locate::operate(const std::vector<ORowSetValue>& lhs) const
100 {
101 if (std::any_of(lhs.begin(), lhs.end(),
102 [](const ORowSetValue& rValue) { return rValue.isNull(); }))
103 return ORowSetValue();
104
105 if (lhs.size() == 2)
106 return OUString(OUString::number(lhs[0].getString().indexOf(lhs[1].getString()) + 1));
107
108 else if (lhs.size() != 3)
109 return ORowSetValue();
110
111 return lhs[1].getString().indexOf(lhs[2].getString(), lhs[0]) + 1;
112 }
113
operate(const std::vector<ORowSetValue> & lhs) const114 ORowSetValue OOp_SubString::operate(const std::vector<ORowSetValue>& lhs) const
115 {
116 if (std::any_of(lhs.begin(), lhs.end(),
117 [](const ORowSetValue& rValue) { return rValue.isNull(); }))
118 return ORowSetValue();
119
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);
122
123 else if (lhs.size() != 3 || static_cast<sal_Int32>(lhs[1]) < sal_Int32(0))
124 return ORowSetValue();
125
126 return lhs[2].getString().copy(static_cast<sal_Int32>(lhs[1]) - 1, lhs[0]);
127 }
128
operate(const ORowSetValue & lhs) const129 ORowSetValue OOp_LTrim::operate(const ORowSetValue& lhs) const
130 {
131 if (lhs.isNull())
132 return lhs;
133
134 OUString sRet = lhs;
135 OUString sNew = sRet.trim();
136 return sRet.copy(sRet.indexOf(sNew));
137 }
138
operate(const ORowSetValue & lhs) const139 ORowSetValue OOp_RTrim::operate(const ORowSetValue& lhs) const
140 {
141 if (lhs.isNull())
142 return lhs;
143
144 OUString sRet = lhs;
145 OUString sNew = sRet.trim();
146 return sRet.copy(0, sRet.lastIndexOf(sNew[sNew.getLength() - 1]) + 1);
147 }
148
operate(const ORowSetValue & lhs) const149 ORowSetValue OOp_Space::operate(const ORowSetValue& lhs) const
150 {
151 if (lhs.isNull())
152 return lhs;
153
154 const char c = ' ';
155 OUStringBuffer sRet;
156 sal_Int32 nCount = lhs;
157 for (sal_Int32 i = 0; i < nCount; ++i)
158 {
159 sRet.appendAscii(&c, 1);
160 }
161 return sRet.makeStringAndClear();
162 }
163
operate(const std::vector<ORowSetValue> & lhs) const164 ORowSetValue OOp_Replace::operate(const std::vector<ORowSetValue>& lhs) const
165 {
166 if (lhs.size() != 3)
167 return ORowSetValue();
168
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)
174 {
175 sStr = sStr.replaceAt(nIndexOf, sFrom.getLength(), sTo);
176 nIndexOf = sStr.indexOf(sFrom, nIndexOf + sTo.getLength());
177 }
178
179 return sStr;
180 }
181
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const182 ORowSetValue OOp_Repeat::operate(const ORowSetValue& lhs, const ORowSetValue& rhs) const
183 {
184 if (lhs.isNull() || rhs.isNull())
185 return lhs;
186
187 OUStringBuffer sRet;
188 sal_Int32 nCount = rhs;
189 for (sal_Int32 i = 0; i < nCount; ++i)
190 {
191 sRet.append(lhs.operator OUString());
192 }
193 return sRet.makeStringAndClear();
194 }
195
operate(const std::vector<ORowSetValue> & lhs) const196 ORowSetValue OOp_Insert::operate(const std::vector<ORowSetValue>& lhs) const
197 {
198 if (lhs.size() != 4)
199 return ORowSetValue();
200
201 OUString sStr = lhs[3];
202
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]);
207 }
208
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const209 ORowSetValue OOp_Left::operate(const ORowSetValue& lhs, const ORowSetValue& rhs) const
210 {
211 if (lhs.isNull() || rhs.isNull())
212 return lhs;
213
214 OUString sRet = lhs;
215 sal_Int32 nCount = rhs;
216 if (nCount < 0)
217 return ORowSetValue();
218 return sRet.copy(0, nCount);
219 }
220
operate(const ORowSetValue & lhs,const ORowSetValue & rhs) const221 ORowSetValue OOp_Right::operate(const ORowSetValue& lhs, const ORowSetValue& rhs) const
222 {
223 if (lhs.isNull() || rhs.isNull())
224 return lhs;
225
226 sal_Int32 nCount = rhs;
227 OUString sRet = lhs;
228 if (nCount < 0 || nCount >= sRet.getLength())
229 return ORowSetValue();
230
231 return sRet.copy(sRet.getLength() - nCount, nCount);
232 }
233
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
235