Debian Codegraph @

basic/source/sbx/sbxconv.hxx

raw
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 #pragma once
21
22 #include "sbxdec.hxx"
23 #include <basic/sberrors.hxx>
24 #include <basic/sbx.hxx>
25 #include <basic/sbxcore.hxx>
26 #include <basic/sbxdef.hxx>
27 #include <runtime.hxx>
28
29 #include <o3tl/float_int_conversion.hxx>
30 #include <o3tl/safeint.hxx>
31 #include <rtl/math.hxx>
32 #include <sal/types.h>
33
34 class SbxArray;
35
36 template <std::integral I> I ConvertWithOverflowTo(double f)
37 {
38 // tdf#162711 - VBASupport mode requires banker’s rounding for integer conversions
39 f = rtl_math_round(f, 0,
40 SbiRuntime::isVBAEnabled() ? rtl_math_RoundingMode_HalfEven
41 : rtl_math_RoundingMode_Corrected);
42 if (!o3tl::convertsToAtMost(f, std::numeric_limits<I>::max()))
43 {
44 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
45 return std::numeric_limits<I>::max();
46 }
47 if (!o3tl::convertsToAtLeast(f, std::numeric_limits<I>::min()))
48 {
49 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
50 return std::numeric_limits<I>::min();
51 }
52 return f;
53 }
54
55 template <std::integral To, std::integral From> To ConvertWithOverflowTo(From n)
56 {
57 using ValidRange = o3tl::ValidRange<To>;
58 if (ValidRange::isAbove(n))
59 {
60 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
61 return std::numeric_limits<To>::max();
62 }
63 if (ValidRange::isBelow(n))
64 {
65 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
66 return std::numeric_limits<To>::min();
67 }
68 return n;
69 }
70
71 template <std::floating_point To, std::floating_point From> inline To ConvertWithOverflowTo(From n)
72 {
73 if constexpr (std::numeric_limits<From>::max() > std::numeric_limits<To>::max())
74 {
75 if (n > std::numeric_limits<To>::max())
76 {
77 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
78 return std::numeric_limits<To>::max();
79 }
80 }
81 if constexpr (std::numeric_limits<From>::lowest() < std::numeric_limits<To>::lowest())
82 {
83 if (n < std::numeric_limits<To>::lowest())
84 {
85 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
86 return std::numeric_limits<To>::lowest();
87 }
88 }
89 // tests for underflow - storing value too small for precision of single
90 if constexpr (std::numeric_limits<From>::min() < std::numeric_limits<To>::min())
91 {
92 if (n > 0 && n < std::numeric_limits<To>::min())
93 {
94 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
95 return std::numeric_limits<To>::min();
96 }
97 if (n < 0 && n > -std::numeric_limits<To>::min())
98 {
99 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
100 return -std::numeric_limits<To>::min();
101 }
102 }
103 return n;
104 }
105
106 template <std::floating_point To, std::integral From> inline To ConvertWithOverflowTo(From n)
107 {
108 // No integral types overflow floats
109 return static_cast<To>(n);
110 }
111
112 template <typename T1, typename T2> void assignWithOverflowTo(T1& dest, T2 src)
113 {
114 dest = ConvertWithOverflowTo<T1>(src);
115 }
116
117 template <typename T> void assignWithOverflowTo(T& dest, SbxDecimal& dec)
118 {
119 dest = dec.getWithOverflow<T>();
120 }
121
122 // SBXSCAN.CXX
123 extern void ImpCvtNum( double nNum, short nPrec, OUString& rRes, bool bCoreString=false );
124 extern ErrCode ImpScan
125 ( std::u16string_view rSrc, double& nVal, SbxDataType& rType, sal_Int32* pLen,
126 bool* pHasNumber, bool bOnlyIntntl );
127 // A version that uses defaults / compatibility settings for bOnlyIntntl
128 extern ErrCode ImpScan
129 ( std::u16string_view rSrc, double& nVal, SbxDataType& rType, sal_Int32* pLen );
130
131 void ImpGetIntntlSep( sal_Unicode& rcDecimalSep, sal_Unicode& rcThousandSep, sal_Unicode& rcDecimalSepAlt );
132
133 // SBXINT.CXX
134
135 sal_Unicode ImpGetChar( const SbxValues* );
136 void ImpPutChar( SbxValues*, sal_Unicode );
137 sal_uInt8 ImpGetByte( const SbxValues* );
138 void ImpPutByte( SbxValues*, sal_uInt8 );
139 sal_Int16 ImpGetInteger( const SbxValues* );
140 void ImpPutInteger( SbxValues*, sal_Int16 );
141 sal_uInt16 ImpGetUShort( const SbxValues* );
142 void ImpPutUShort( SbxValues*, sal_uInt16 );
143 sal_Int32 ImpGetLong( const SbxValues* );
144 void ImpPutLong( SbxValues*, sal_Int32 );
145 sal_uInt32 ImpGetULong( const SbxValues* );
146 void ImpPutULong( SbxValues*, sal_uInt32 );
147 sal_Int64 ImpGetInt64( const SbxValues* );
148 void ImpPutInt64( SbxValues*, sal_Int64 );
149 sal_uInt64 ImpGetUInt64( const SbxValues* );
150 void ImpPutUInt64( SbxValues*, sal_uInt64 );
151 double ImpGetDouble( const SbxValues* );
152 void ImpPutDouble( SbxValues*, double, bool bCoreString=false );
153 float ImpGetSingle( const SbxValues* );
154 void ImpPutSingle( SbxValues*, float );
155
156 // SBXCURR.CXX
157
158 sal_Int64 ImpGetCurrency( const SbxValues* );
159 void ImpPutCurrency( SbxValues*, const sal_Int64 );
160
161 inline sal_Int64 CurFrom(double d)
162 {
163 double result = d > 0 ? (d * CURRENCY_FACTOR + 0.5) : (d * CURRENCY_FACTOR - 0.5);
164 if (result >= double(SAL_MAX_INT64)) // double(SAL_MAX_INT64) is greater than SAL_MAX_INT64
165 {
166 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
167 return SAL_MAX_INT64;
168 }
169 if (result < double(SAL_MIN_INT64))
170 {
171 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
172 return SAL_MIN_INT64;
173 }
174 return result;
175 }
176
177 template <std::integral I> sal_Int64 CurFrom(I n)
178 {
179 using ValidRange = o3tl::ValidRange<sal_Int64, SAL_MIN_INT64 / CURRENCY_FACTOR, SAL_MAX_INT64 / CURRENCY_FACTOR>;
180 if (ValidRange::isAbove(n))
181 {
182 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
183 return SAL_MAX_INT64;
184 }
185 if (ValidRange::isBelow(n))
186 {
187 SbxBase::SetError(ERRCODE_BASIC_MATH_OVERFLOW);
188 return SAL_MIN_INT64;
189 }
190 return n * CURRENCY_FACTOR;
191 }
192
193 template <std::integral I> I CurTo(sal_Int64 cur_val)
194 {
195 return ConvertWithOverflowTo<I>(CurTo<sal_Int64>(cur_val));
196 }
197
198 template <> inline sal_Int64 CurTo<sal_Int64>(sal_Int64 cur_val)
199 {
200 sal_Int64 i = cur_val / CURRENCY_FACTOR;
201 // Rounding (half-to-even)
202 int f = cur_val % CURRENCY_FACTOR;
203 if (i % 2 == 1)
204 {
205 if (f < 0)
206 --f;
207 else
208 ++f;
209 }
210 if (f > CURRENCY_FACTOR / 2)
211 ++i;
212 else if (f < -CURRENCY_FACTOR / 2)
213 --i;
214 return i;
215 }
216
217 template <std::floating_point F> F CurTo(sal_Int64 r)
218 {
219 return ConvertWithOverflowTo<F>(static_cast<double>(r) / CURRENCY_FACTOR);
220 }
221
222 // SBXDEC.CXX
223
224 SbxDecimal* ImpCreateDecimal( SbxValues* p );
225 SbxDecimal* ImpGetDecimal( const SbxValues* p );
226 void ImpPutDecimal( SbxValues* p, SbxDecimal* pDec );
227
228 // SBXDATE.CXX
229
230 double ImpGetDate( const SbxValues* );
231 void ImpPutDate( SbxValues*, double );
232
233 // SBXSTR.CXX
234
235 OUString ImpGetString( const SbxValues* );
236 OUString ImpGetCoreString( const SbxValues* );
237 void ImpPutString( SbxValues*, const OUString* );
238
239 // SBXBOOL.CXX
240
241 enum SbxBOOL ImpGetBool( const SbxValues* );
242 void ImpPutBool( SbxValues*, sal_Int16 );
243
244 // ByteArray <--> String
245 SbxArray* StringToByteArray(const OUString& rStr);
246 OUString ByteArrayToString(SbxArray* pArr);
247
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */