/src/libreoffice/connectivity/source/drivers/file/FStringFunctions.cxx
Line | Count | Source |
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 | | |
22 | | #include <comphelper/string.hxx> |
23 | | #include <rtl/ustrbuf.hxx> |
24 | | |
25 | | using namespace connectivity; |
26 | | using namespace connectivity::file; |
27 | | |
28 | | ORowSetValue OOp_Upper::operate(const ORowSetValue& lhs) const |
29 | 0 | { |
30 | 0 | if (lhs.isNull()) |
31 | 0 | return lhs; |
32 | | |
33 | 0 | return lhs.getString().toAsciiUpperCase(); |
34 | 0 | } |
35 | | |
36 | | ORowSetValue OOp_Lower::operate(const ORowSetValue& lhs) const |
37 | 0 | { |
38 | 0 | if (lhs.isNull()) |
39 | 0 | return lhs; |
40 | | |
41 | 0 | return lhs.getString().toAsciiLowerCase(); |
42 | 0 | } |
43 | | |
44 | | ORowSetValue OOp_Ascii::operate(const ORowSetValue& lhs) const |
45 | 0 | { |
46 | 0 | if (lhs.isNull()) |
47 | 0 | return lhs; |
48 | 0 | OString sStr(OUStringToOString(lhs.getString(), RTL_TEXTENCODING_ASCII_US)); |
49 | 0 | sal_Int32 nAscii = sStr.toChar(); |
50 | 0 | return nAscii; |
51 | 0 | } |
52 | | |
53 | | ORowSetValue OOp_CharLength::operate(const ORowSetValue& lhs) const |
54 | 0 | { |
55 | 0 | if (lhs.isNull()) |
56 | 0 | return lhs; |
57 | | |
58 | 0 | return lhs.getString().getLength(); |
59 | 0 | } |
60 | | |
61 | | ORowSetValue OOp_Char::operate(const std::vector<ORowSetValue>& lhs) const |
62 | 0 | { |
63 | 0 | if (lhs.empty()) |
64 | 0 | return ORowSetValue(); |
65 | | |
66 | 0 | OUStringBuffer sRet(static_cast<sal_Int32>(lhs.size())); |
67 | 0 | std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin(); |
68 | 0 | std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend(); |
69 | 0 | for (; aIter != aEnd; ++aIter) |
70 | 0 | { |
71 | 0 | if (!aIter->isNull()) |
72 | 0 | { |
73 | 0 | char c = static_cast<char>(aIter->getInt32()); |
74 | |
|
75 | 0 | sRet.appendAscii(&c, 1); |
76 | 0 | } |
77 | 0 | } |
78 | |
|
79 | 0 | return sRet.makeStringAndClear(); |
80 | 0 | } |
81 | | |
82 | | ORowSetValue OOp_Concat::operate(const std::vector<ORowSetValue>& lhs) const |
83 | 0 | { |
84 | 0 | if (lhs.empty()) |
85 | 0 | return ORowSetValue(); |
86 | | |
87 | 0 | OUStringBuffer sRet; |
88 | 0 | std::vector<ORowSetValue>::const_reverse_iterator aIter = lhs.rbegin(); |
89 | 0 | std::vector<ORowSetValue>::const_reverse_iterator aEnd = lhs.rend(); |
90 | 0 | for (; aIter != aEnd; ++aIter) |
91 | 0 | { |
92 | 0 | if (aIter->isNull()) |
93 | 0 | return ORowSetValue(); |
94 | | |
95 | 0 | sRet.append(aIter->getString()); |
96 | 0 | } |
97 | | |
98 | 0 | return sRet.makeStringAndClear(); |
99 | 0 | } |
100 | | |
101 | | ORowSetValue OOp_Locate::operate(const std::vector<ORowSetValue>& lhs) const |
102 | 0 | { |
103 | 0 | if (std::any_of(lhs.begin(), lhs.end(), |
104 | 0 | [](const ORowSetValue& rValue) { return rValue.isNull(); })) |
105 | 0 | return ORowSetValue(); |
106 | | |
107 | 0 | if (lhs.size() == 2) |
108 | 0 | return OUString(OUString::number(lhs[0].getString().indexOf(lhs[1].getString()) + 1)); |
109 | | |
110 | 0 | else if (lhs.size() != 3) |
111 | 0 | return ORowSetValue(); |
112 | | |
113 | 0 | return lhs[1].getString().indexOf(lhs[2].getString(), lhs[0].getInt32()) + 1; |
114 | 0 | } |
115 | | |
116 | | ORowSetValue OOp_SubString::operate(const std::vector<ORowSetValue>& lhs) const |
117 | 0 | { |
118 | 0 | if (std::any_of(lhs.begin(), lhs.end(), |
119 | 0 | [](const ORowSetValue& rValue) { return rValue.isNull(); })) |
120 | 0 | return ORowSetValue(); |
121 | | |
122 | 0 | if (lhs.size() == 2 && lhs[0].getInt32() >= sal_Int32(0)) |
123 | 0 | return lhs[1].getString().copy(lhs[0].getInt32() - 1); |
124 | | |
125 | 0 | else if (lhs.size() != 3 || lhs[1].getInt32() < sal_Int32(0)) |
126 | 0 | return ORowSetValue(); |
127 | | |
128 | 0 | return lhs[2].getString().copy(lhs[1].getInt32() - 1, lhs[0].getInt32()); |
129 | 0 | } |
130 | | |
131 | | ORowSetValue OOp_LTrim::operate(const ORowSetValue& lhs) const |
132 | 0 | { |
133 | 0 | if (lhs.isNull()) |
134 | 0 | return lhs; |
135 | | |
136 | 0 | OUString sRet = lhs.getString(); |
137 | 0 | OUString sNew = sRet.trim(); |
138 | 0 | return sRet.copy(sRet.indexOf(sNew)); |
139 | 0 | } |
140 | | |
141 | | ORowSetValue OOp_RTrim::operate(const ORowSetValue& lhs) const |
142 | 0 | { |
143 | 0 | if (lhs.isNull()) |
144 | 0 | return lhs; |
145 | | |
146 | 0 | OUString sRet = lhs.getString(); |
147 | 0 | OUString sNew = sRet.trim(); |
148 | 0 | return sRet.copy(0, sRet.lastIndexOf(sNew[sNew.getLength() - 1]) + 1); |
149 | 0 | } |
150 | | |
151 | | ORowSetValue OOp_Space::operate(const ORowSetValue& lhs) const |
152 | 0 | { |
153 | 0 | if (lhs.isNull()) |
154 | 0 | return lhs; |
155 | | |
156 | 0 | sal_Int32 nCount = std::max(lhs.getInt32(), sal_Int32(0)); |
157 | 0 | return OUString(OUString::Concat(RepeatedUChar(' ', nCount))); |
158 | 0 | } |
159 | | |
160 | | ORowSetValue OOp_Replace::operate(const std::vector<ORowSetValue>& lhs) const |
161 | 0 | { |
162 | 0 | if (lhs.size() != 3) |
163 | 0 | return ORowSetValue(); |
164 | | |
165 | 0 | OUString sStr = lhs[2].getString(); |
166 | 0 | OUString sFrom = lhs[1].getString(); |
167 | 0 | OUString sTo = lhs[0].getString(); |
168 | 0 | return sStr.replaceAll(sFrom, sTo); |
169 | 0 | } |
170 | | |
171 | | ORowSetValue OOp_Repeat::operate(const ORowSetValue& lhs, const ORowSetValue& rhs) const |
172 | 0 | { |
173 | 0 | if (lhs.isNull() || rhs.isNull()) |
174 | 0 | return lhs; |
175 | | |
176 | 0 | const OUString s = lhs.getString(); |
177 | 0 | const sal_Int32 nCount = std::max(rhs.getInt32(), sal_Int32(0)); |
178 | 0 | OUStringBuffer sRet(s.getLength() * nCount); |
179 | 0 | for (sal_Int32 i = 0; i < nCount; ++i) |
180 | 0 | { |
181 | 0 | sRet.append(s); |
182 | 0 | } |
183 | 0 | return sRet.makeStringAndClear(); |
184 | 0 | } |
185 | | |
186 | | ORowSetValue OOp_Insert::operate(const std::vector<ORowSetValue>& lhs) const |
187 | 0 | { |
188 | 0 | if (lhs.size() != 4) |
189 | 0 | return ORowSetValue(); |
190 | | |
191 | 0 | OUString sStr = lhs[3].getString(); |
192 | |
|
193 | 0 | sal_Int32 nStart = lhs[2].getInt32(); |
194 | 0 | if (nStart < 1) |
195 | 0 | nStart = 1; |
196 | 0 | return sStr.replaceAt(nStart - 1, lhs[1].getInt32(), lhs[0].getString()); |
197 | 0 | } |
198 | | |
199 | | ORowSetValue OOp_Left::operate(const ORowSetValue& lhs, const ORowSetValue& rhs) const |
200 | 0 | { |
201 | 0 | if (lhs.isNull() || rhs.isNull()) |
202 | 0 | return lhs; |
203 | | |
204 | 0 | OUString sRet = lhs.getString(); |
205 | 0 | sal_Int32 nCount = rhs.getInt32(); |
206 | 0 | if (nCount < 0) |
207 | 0 | return ORowSetValue(); |
208 | 0 | return sRet.copy(0, nCount); |
209 | 0 | } |
210 | | |
211 | | ORowSetValue OOp_Right::operate(const ORowSetValue& lhs, const ORowSetValue& rhs) const |
212 | 0 | { |
213 | 0 | if (lhs.isNull() || rhs.isNull()) |
214 | 0 | return lhs; |
215 | | |
216 | 0 | sal_Int32 nCount = rhs.getInt32(); |
217 | 0 | OUString sRet = lhs.getString(); |
218 | 0 | if (nCount < 0 || nCount >= sRet.getLength()) |
219 | 0 | return ORowSetValue(); |
220 | | |
221 | 0 | return sRet.copy(sRet.getLength() - nCount, nCount); |
222 | 0 | } |
223 | | |
224 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |