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