| 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 <sal/config.h> |
| 21 |
|
| 22 |
#include <cstdlib> |
| 23 |
#include <optional> |
| 24 |
#include <string_view> |
| 25 |
|
| 26 |
#include <config_features.h> |
| 27 |
|
| 28 |
#include <comphelper/errcode.hxx> |
| 29 |
#include <unotools/resmgr.hxx> |
| 30 |
#include "sbxconv.hxx" |
| 31 |
|
| 32 |
#include <unotools/syslocale.hxx> |
| 33 |
#include <unotools/charclass.hxx> |
| 34 |
|
| 35 |
#include <vcl/svapp.hxx> |
| 36 |
#include <vcl/settings.hxx> |
| 37 |
|
| 38 |
#include <math.h> |
| 39 |
|
| 40 |
#include <sbxbase.hxx> |
| 41 |
#include <sbintern.hxx> |
| 42 |
#include <sbxform.hxx> |
| 43 |
|
| 44 |
#include <date.hxx> |
| 45 |
#include <runtime.hxx> |
| 46 |
#include <strings.hrc> |
| 47 |
|
| 48 |
#include <rtl/character.hxx> |
| 49 |
#include <rtl/math.hxx> |
| 50 |
#include <svl/numformat.hxx> |
| 51 |
#include <svl/zforlist.hxx> |
| 52 |
#include <o3tl/temporary.hxx> |
| 53 |
#include <o3tl/string_view.hxx> |
| 54 |
#include <officecfg/Office/Scripting.hxx> |
| 55 |
|
| 56 |
|
| 57 |
void ImpGetIntntlSep( sal_Unicode& rcDecimalSep, sal_Unicode& rcThousandSep, sal_Unicode& rcDecimalSepAlt ) |
| 58 |
{ |
| 59 |
SvtSysLocale aSysLocale; |
| 60 |
const LocaleDataWrapper& rData = aSysLocale.GetLocaleData(); |
| 61 |
rcDecimalSep = rData.getNumDecimalSep()[0]; |
| 62 |
rcThousandSep = rData.getNumThousandSep()[0]; |
| 63 |
rcDecimalSepAlt = rData.getNumDecimalSepAlt().toChar(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
static bool ImpStrChr( std::u16string_view str, sal_Unicode c ) { return str.find(c) != std::u16string_view::npos; } |
| 68 |
|
| 69 |
|
| 70 |
// scanning a string according to BASIC-conventions |
| 71 |
// but exponent may also be a D, so data type is SbxDOUBLE |
| 72 |
// conversion error if data type is fixed and it doesn't fit |
| 73 |
|
| 74 |
ErrCode ImpScan( std::u16string_view rWSrc, double& nVal, SbxDataType& rType, |
| 75 |
sal_Int32* pLen, bool* pHasNumber, bool bOnlyIntntl ) |
| 76 |
{ |
| 77 |
sal_Unicode cDecSep, cGrpSep, cDecSepAlt; |
| 78 |
if( bOnlyIntntl ) |
| 79 |
{ |
| 80 |
ImpGetIntntlSep(cDecSep, cGrpSep, cDecSepAlt); |
| 81 |
// Ensure that the decimal separator alternative is really one. |
| 82 |
if (cDecSepAlt == cDecSep) |
| 83 |
cDecSepAlt = 0; |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
cDecSep = '.'; |
| 88 |
cGrpSep = 0; // no group separator accepted in non-i18n |
| 89 |
cDecSepAlt = 0; |
| 90 |
} |
| 91 |
|
| 92 |
auto const pStart = rWSrc.begin(); |
| 93 |
auto p = pStart; |
| 94 |
bool bMinus = false; |
| 95 |
nVal = 0; |
| 96 |
SbxDataType eScanType = SbxSINGLE; |
| 97 |
while (p != rWSrc.end() && (*p == ' ' || *p == '\t')) |
| 98 |
p++; |
| 99 |
if (p != rWSrc.end() && *p == '+') |
| 100 |
p++; |
| 101 |
else if (p != rWSrc.end() && *p == '-') |
| 102 |
{ |
| 103 |
p++; |
| 104 |
bMinus = true; |
| 105 |
} |
| 106 |
#if HAVE_FEATURE_SCRIPTING |
| 107 |
if (SbiRuntime::isVBAEnabled()) |
| 108 |
{ |
| 109 |
while (p != rWSrc.end() && (*p == ' ' || *p == '\t')) |
| 110 |
p++; |
| 111 |
} |
| 112 |
#endif |
| 113 |
const auto pNumberStart = p; |
| 114 |
if (p != rWSrc.end() |
| 115 |
&& (rtl::isAsciiDigit(*p) |
| 116 |
|| ((*p == cDecSep || (cGrpSep && *p == cGrpSep) || (cDecSepAlt && *p == cDecSepAlt)) |
| 117 |
&& p + 1 != rWSrc.end() && rtl::isAsciiDigit(*(p + 1))))) |
| 118 |
{ |
| 119 |
bool exp = false; |
| 120 |
bool decsep = false; |
| 121 |
short ndig = 0; |
| 122 |
short ncdig = 0; // number of digits after decimal point |
| 123 |
OUStringBuffer aSearchStr("0123456789DEde" + OUStringChar(cDecSep)); |
| 124 |
if (cDecSepAlt) |
| 125 |
aSearchStr.append(cDecSepAlt); |
| 126 |
if (cGrpSep) |
| 127 |
aSearchStr.append(cGrpSep); |
| 128 |
OUStringBuffer aBuf(rWSrc.end() - p); |
| 129 |
for (; p != rWSrc.end() && ImpStrChr(aSearchStr, *p); ++p) |
| 130 |
{ |
| 131 |
if (rtl::isAsciiDigit(*p)) |
| 132 |
{ |
| 133 |
aBuf.append(*p); |
| 134 |
if (!exp) |
| 135 |
{ |
| 136 |
ndig++; |
| 137 |
if (decsep) |
| 138 |
ncdig++; |
| 139 |
} |
| 140 |
} |
| 141 |
else if (cGrpSep && *p == cGrpSep) |
| 142 |
{ |
| 143 |
aBuf.append(*p); |
| 144 |
} |
| 145 |
else if (*p == cDecSep || (cDecSepAlt && *p == cDecSepAlt)) |
| 146 |
{ |
| 147 |
if (decsep) |
| 148 |
return ERRCODE_BASIC_CONVERSION; |
| 149 |
decsep = true; |
| 150 |
|
| 151 |
// Use the separator that is passed to stringToDouble() |
| 152 |
aBuf.append(cDecSep); |
| 153 |
} |
| 154 |
else // DdEe |
| 155 |
{ |
| 156 |
if (exp) |
| 157 |
return ERRCODE_BASIC_CONVERSION; |
| 158 |
exp = true; |
| 159 |
|
| 160 |
if( *p == 'D' || *p == 'd' ) |
| 161 |
eScanType = SbxDOUBLE; |
| 162 |
aBuf.append('E'); |
| 163 |
if (auto pNext = p + 1; pNext != rWSrc.end()) |
| 164 |
{ |
| 165 |
if (*pNext == '+') |
| 166 |
++p; |
| 167 |
else if (*pNext == '-') |
| 168 |
{ |
| 169 |
aBuf.append('-'); |
| 170 |
++p; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok; |
| 177 |
sal_Int32 nParseEnd = 0; |
| 178 |
nVal = rtl::math::stringToDouble(aBuf, cDecSep, cGrpSep, &eStatus, &nParseEnd); |
| 179 |
if( eStatus != rtl_math_ConversionStatus_Ok || nParseEnd != aBuf.getLength() ) |
| 180 |
return ERRCODE_BASIC_CONVERSION; |
| 181 |
|
| 182 |
if( !decsep && !exp ) |
| 183 |
{ |
| 184 |
if( nVal >= SbxMININT && nVal <= SbxMAXINT ) |
| 185 |
eScanType = SbxINTEGER; |
| 186 |
else if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG ) |
| 187 |
eScanType = SbxLONG; |
| 188 |
} |
| 189 |
|
| 190 |
// too many numbers for SINGLE? |
| 191 |
if( ndig > 15 || ncdig > 6 ) |
| 192 |
eScanType = SbxDOUBLE; |
| 193 |
|
| 194 |
// type detection? |
| 195 |
static constexpr std::u16string_view pTypes = u"%!&#"; |
| 196 |
if (p != rWSrc.end() && ImpStrChr(pTypes, *p)) |
| 197 |
p++; |
| 198 |
} |
| 199 |
// hex/octal number? read in and convert: |
| 200 |
else if (p != rWSrc.end() && *p == '&') |
| 201 |
{ |
| 202 |
if (++p == rWSrc.end()) |
| 203 |
return ERRCODE_BASIC_CONVERSION; |
| 204 |
eScanType = SbxLONG; |
| 205 |
auto isValidDigit = rtl::isAsciiHexDigit<sal_Unicode>; |
| 206 |
char base = 16; |
| 207 |
char ndig = 8; |
| 208 |
switch( *p++ ) |
| 209 |
{ |
| 210 |
case 'O': |
| 211 |
case 'o': |
| 212 |
isValidDigit = rtl::isAsciiOctalDigit<sal_Unicode>; |
| 213 |
base = 8; |
| 214 |
ndig = 11; |
| 215 |
break; |
| 216 |
case 'H': |
| 217 |
case 'h': |
| 218 |
break; |
| 219 |
default : |
| 220 |
return ERRCODE_BASIC_CONVERSION; |
| 221 |
} |
| 222 |
const auto pDigitsStart = p; |
| 223 |
for (; p != rWSrc.end() && rtl::isAsciiAlphanumeric(*p); ++p) |
| 224 |
{ |
| 225 |
if (!isValidDigit(*p)) |
| 226 |
return ERRCODE_BASIC_CONVERSION; |
| 227 |
} |
| 228 |
if (p - pDigitsStart > ndig) |
| 229 |
return ERRCODE_BASIC_CONVERSION; |
| 230 |
sal_Int32 l = o3tl::toInt32(rWSrc.substr(pDigitsStart - pStart, p - pDigitsStart), base); |
| 231 |
if (p != rWSrc.end() && *p == '&') |
| 232 |
p++; |
| 233 |
nVal = static_cast<double>(l); |
| 234 |
if( l >= SbxMININT && l <= SbxMAXINT ) |
| 235 |
eScanType = SbxINTEGER; |
| 236 |
} |
| 237 |
#if HAVE_FEATURE_SCRIPTING |
| 238 |
else if ( SbiRuntime::isVBAEnabled() ) |
| 239 |
{ |
| 240 |
return ERRCODE_BASIC_CONVERSION; |
| 241 |
} |
| 242 |
#endif |
| 243 |
const auto pNumberEnd = p; |
| 244 |
// tdf#146672 - skip whitespaces and tabs at the end of the scanned string |
| 245 |
while (p != rWSrc.end() && (*p == ' ' || *p == '\t')) |
| 246 |
p++; |
| 247 |
if( pLen ) |
| 248 |
*pLen = p - pStart; |
| 249 |
if (pHasNumber) |
| 250 |
*pHasNumber = pNumberEnd > pNumberStart; |
| 251 |
if( bMinus ) |
| 252 |
nVal = -nVal; |
| 253 |
rType = eScanType; |
| 254 |
return ERRCODE_NONE; |
| 255 |
} |
| 256 |
|
| 257 |
ErrCode ImpScan(std::u16string_view rSrc, double& nVal, SbxDataType& rType, sal_Int32* pLen) |
| 258 |
{ |
| 259 |
using namespace officecfg::Office::Scripting; |
| 260 |
static const bool bEnv = std::getenv("LIBREOFFICE6FLOATINGPOINTMODE") != nullptr; |
| 261 |
bool bMode = bEnv || Basic::Compatibility::UseLibreOffice6FloatingPointConversion::get(); |
| 262 |
|
| 263 |
return ImpScan(rSrc, nVal, rType, pLen, nullptr, !bMode); |
| 264 |
} |
| 265 |
|
| 266 |
// port for CDbl in the Basic |
| 267 |
ErrCode SbxValue::ScanNumIntnl( const OUString& rSrc, double& nVal, bool bSingle ) |
| 268 |
{ |
| 269 |
sal_Int32 nLen = 0; |
| 270 |
ErrCode nRetError = ImpScan( rSrc, nVal, o3tl::temporary(SbxDataType()), &nLen, nullptr, |
| 271 |
/*bOnlyIntntl*/true ); |
| 272 |
// read completely? |
| 273 |
if( nRetError == ERRCODE_NONE && nLen != rSrc.getLength() ) |
| 274 |
{ |
| 275 |
nRetError = ERRCODE_BASIC_CONVERSION; |
| 276 |
} |
| 277 |
if( bSingle ) |
| 278 |
{ |
| 279 |
SbxValues aValues( nVal ); |
| 280 |
nVal = static_cast<double>(ImpGetSingle( &aValues )); // here error at overflow |
| 281 |
} |
| 282 |
return nRetError; |
| 283 |
} |
| 284 |
|
| 285 |
// The number is prepared unformattedly with the given number of |
| 286 |
// NK-positions. A leading minus is added if applicable. |
| 287 |
// This routine is public because it's also used by the Put-functions |
| 288 |
// in the class SbxImpSTRING. |
| 289 |
|
| 290 |
void ImpCvtNum( double nNum, short nPrec, OUString& rRes, bool bCoreString ) |
| 291 |
{ |
| 292 |
sal_Unicode cDecimalSep; |
| 293 |
if( bCoreString ) |
| 294 |
cDecimalSep = '.'; |
| 295 |
else |
| 296 |
ImpGetIntntlSep(cDecimalSep, o3tl::temporary(sal_Unicode()), o3tl::temporary(sal_Unicode())); |
| 297 |
|
| 298 |
// tdf#143575 - use rtl::math::doubleToUString to convert numbers to strings in basic |
| 299 |
rRes = rtl::math::doubleToUString(nNum, rtl_math_StringFormat_Automatic, nPrec, cDecimalSep, true); |
| 300 |
} |
| 301 |
|
| 302 |
bool SbxValue::Scan(std::u16string_view rSrc, sal_Int32* pLen) |
| 303 |
{ |
| 304 |
ErrCode eRes = ERRCODE_NONE; |
| 305 |
if( !CanWrite() ) |
| 306 |
{ |
| 307 |
eRes = ERRCODE_BASIC_PROP_READONLY; |
| 308 |
} |
| 309 |
else |
| 310 |
{ |
| 311 |
double n; |
| 312 |
SbxDataType t; |
| 313 |
eRes = ImpScan( rSrc, n, t, pLen ); |
| 314 |
if( eRes == ERRCODE_NONE ) |
| 315 |
{ |
| 316 |
if( !IsFixed() ) |
| 317 |
{ |
| 318 |
SetType( t ); |
| 319 |
} |
| 320 |
PutDouble( n ); |
| 321 |
} |
| 322 |
} |
| 323 |
if( eRes ) |
| 324 |
{ |
| 325 |
SetError( eRes ); |
| 326 |
return false; |
| 327 |
} |
| 328 |
else |
| 329 |
{ |
| 330 |
return true; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
std::locale BasResLocale() |
| 335 |
{ |
| 336 |
return Translate::Create("sb"); |
| 337 |
} |
| 338 |
|
| 339 |
OUString BasResId(TranslateId aId) |
| 340 |
{ |
| 341 |
return Translate::get(aId, BasResLocale()); |
| 342 |
} |
| 343 |
|
| 344 |
namespace |
| 345 |
{ |
| 346 |
|
| 347 |
enum class VbaFormatType |
| 348 |
{ |
| 349 |
Offset, // standard number format |
| 350 |
UserDefined, // user defined number format |
| 351 |
}; |
| 352 |
|
| 353 |
#if HAVE_FEATURE_SCRIPTING |
| 354 |
|
| 355 |
struct VbaFormatInfo |
| 356 |
{ |
| 357 |
VbaFormatType meType; |
| 358 |
std::u16string_view mpVbaFormat; // Format string in vba |
| 359 |
NfIndexTableOffset meOffset; // SvNumberFormatter format index, if meType = VbaFormatType::Offset |
| 360 |
OUString mpOOoFormat; // if meType = VbaFormatType::UserDefined |
| 361 |
}; |
| 362 |
|
| 363 |
const VbaFormatInfo* getFormatInfo( std::u16string_view rFmt ) |
| 364 |
{ |
| 365 |
static constexpr const VbaFormatInfo formatInfoTable[] = |
| 366 |
{ |
| 367 |
{ VbaFormatType::Offset, u"Long Date", NF_DATE_SYSTEM_LONG, u""_ustr }, |
| 368 |
{ VbaFormatType::UserDefined, u"Medium Date", NF_NUMBER_STANDARD, u"DD-MMM-YY"_ustr }, |
| 369 |
{ VbaFormatType::Offset, u"Short Date", NF_DATE_SYSTEM_SHORT, u""_ustr }, |
| 370 |
{ VbaFormatType::UserDefined, u"Long Time", NF_NUMBER_STANDARD, u"H:MM:SS AM/PM"_ustr }, |
| 371 |
{ VbaFormatType::Offset, u"Medium Time", NF_TIME_HHMMAMPM, u""_ustr }, |
| 372 |
{ VbaFormatType::Offset, u"Short Time", NF_TIME_HHMM, u""_ustr }, |
| 373 |
{ VbaFormatType::Offset, u"ddddd", NF_DATE_SYSTEM_SHORT, u""_ustr }, |
| 374 |
{ VbaFormatType::Offset, u"dddddd", NF_DATE_SYSTEM_LONG, u""_ustr }, |
| 375 |
{ VbaFormatType::UserDefined, u"ttttt", NF_NUMBER_STANDARD, u"H:MM:SS AM/PM"_ustr }, |
| 376 |
{ VbaFormatType::Offset, u"ww", NF_DATE_WW, u""_ustr }, |
| 377 |
}; |
| 378 |
|
| 379 |
for (auto& info : formatInfoTable) |
| 380 |
if (o3tl::equalsIgnoreAsciiCase(rFmt, info.mpVbaFormat)) |
| 381 |
return &info; |
| 382 |
return nullptr; |
| 383 |
} |
| 384 |
#endif |
| 385 |
|
| 386 |
void BasicFormatNum(double d, const OUString& rFmt, OUString& rRes) |
| 387 |
{ |
| 388 |
SbxAppData& rAppData = GetSbxData_Impl(); |
| 389 |
|
| 390 |
LanguageType eLangType = Application::GetSettings().GetLanguageTag().getLanguageType(); |
| 391 |
if (rAppData.pBasicFormater) |
| 392 |
if (rAppData.eBasicFormaterLangType != eLangType) |
| 393 |
rAppData.pBasicFormater.reset(); |
| 394 |
rAppData.eBasicFormaterLangType = eLangType; |
| 395 |
|
| 396 |
if (!rAppData.pBasicFormater) |
| 397 |
{ |
| 398 |
SvtSysLocale aSysLocale; |
| 399 |
const LocaleDataWrapper& rData = aSysLocale.GetLocaleData(); |
| 400 |
sal_Unicode cComma = rData.getNumDecimalSep()[0]; |
| 401 |
sal_Unicode c1000 = rData.getNumThousandSep()[0]; |
| 402 |
const OUString& aCurrencyStrg = rData.getCurrSymbol(); |
| 403 |
|
| 404 |
// initialize the Basic-formater help object: |
| 405 |
// get resources for predefined output |
| 406 |
// of the Format()-command, e. g. for "On/Off" |
| 407 |
OUString aOnStrg = BasResId(STR_BASICKEY_FORMAT_ON); |
| 408 |
OUString aOffStrg = BasResId(STR_BASICKEY_FORMAT_OFF); |
| 409 |
OUString aYesStrg = BasResId(STR_BASICKEY_FORMAT_YES); |
| 410 |
OUString aNoStrg = BasResId(STR_BASICKEY_FORMAT_NO); |
| 411 |
OUString aTrueStrg = BasResId(STR_BASICKEY_FORMAT_TRUE); |
| 412 |
OUString aFalseStrg = BasResId(STR_BASICKEY_FORMAT_FALSE); |
| 413 |
OUString aCurrencyFormatStrg = BasResId(STR_BASICKEY_FORMAT_CURRENCY); |
| 414 |
|
| 415 |
rAppData.pBasicFormater = std::make_unique<SbxBasicFormater>( |
| 416 |
cComma, c1000, aOnStrg, aOffStrg, aYesStrg, aNoStrg, aTrueStrg, aFalseStrg, |
| 417 |
aCurrencyStrg, aCurrencyFormatStrg); |
| 418 |
} |
| 419 |
// Remark: For performance reasons there's only ONE BasicFormater- |
| 420 |
// object created and 'stored', so that the expensive resource- |
| 421 |
// loading is saved (for country-specific predefined outputs, |
| 422 |
// e. g. "On/Off") and the continuous string-creation |
| 423 |
// operations, too. |
| 424 |
// BUT: therefore this code is NOT multithreading capable! |
| 425 |
rRes = rAppData.pBasicFormater->BasicFormat(d, rFmt); |
| 426 |
} |
| 427 |
|
| 428 |
std::shared_ptr<SvNumberFormatter> GetFormatter() |
| 429 |
{ |
| 430 |
if (auto pInst = GetSbData()->pInst) |
| 431 |
{ |
| 432 |
return pInst->GetNumberFormatter(); |
| 433 |
} |
| 434 |
else |
| 435 |
{ |
| 436 |
return SbiInstance::PrepareNumberFormatter(o3tl::temporary(sal_uInt32()), |
| 437 |
o3tl::temporary(sal_uInt32()), |
| 438 |
o3tl::temporary(sal_uInt32())); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
std::optional<double> StrToNumberIntl(const OUString& s, |
| 443 |
std::shared_ptr<SvNumberFormatter>& rpFormatter) |
| 444 |
{ |
| 445 |
double ret; |
| 446 |
sal_Int32 nLen = 0; |
| 447 |
bool bHasNumber = false; |
| 448 |
if (ImpScan(s, ret, o3tl::temporary(SbxDataType()), &nLen, &bHasNumber, true) == ERRCODE_NONE |
| 449 |
&& bHasNumber && nLen == s.getLength()) |
| 450 |
return ret; |
| 451 |
|
| 452 |
// We couldn't detect a Basic-formatted number (including type characters & specific exponents). |
| 453 |
// Try generic number detection (works also for dates/times). |
| 454 |
|
| 455 |
rpFormatter = GetFormatter(); |
| 456 |
assert(rpFormatter); |
| 457 |
LanguageType eLangType = Application::GetSettings().GetLanguageTag().getLanguageType(); |
| 458 |
|
| 459 |
// Passing an index of a locale switches IsNumberFormat() to use that |
| 460 |
// locale in case the formatter wasn't default created with it. |
| 461 |
sal_uInt32 nIndex = rpFormatter->GetStandardIndex(eLangType); |
| 462 |
|
| 463 |
if (rpFormatter->IsNumberFormat(s, nIndex, ret)) |
| 464 |
return ret; |
| 465 |
|
| 466 |
return {}; |
| 467 |
} |
| 468 |
|
| 469 |
// For numeric types, takes the number directly; otherwise, tries to take string and convert it |
| 470 |
std::optional<double> GetNumberIntl(const SbxValue& val, OUString& rStrVal, |
| 471 |
std::shared_ptr<SvNumberFormatter>& rpFormatter, |
| 472 |
bool extendedNumberDetection) |
| 473 |
{ |
| 474 |
switch (SbxDataType type = val.GetType()) |
| 475 |
{ |
| 476 |
case SbxCHAR: |
| 477 |
case SbxBYTE: |
| 478 |
case SbxINTEGER: |
| 479 |
case SbxUSHORT: |
| 480 |
case SbxLONG: |
| 481 |
case SbxULONG: |
| 482 |
case SbxINT: |
| 483 |
case SbxUINT: |
| 484 |
case SbxSINGLE: |
| 485 |
case SbxDOUBLE: |
| 486 |
return val.GetDouble(); |
| 487 |
case SbxDATE: |
| 488 |
case SbxBOOL: |
| 489 |
if (extendedNumberDetection) |
| 490 |
return val.GetDouble(); |
| 491 |
[[fallthrough]]; |
| 492 |
case SbxSTRING: |
| 493 |
default: |
| 494 |
rStrVal = val.GetOUString(); |
| 495 |
return extendedNumberDetection || type == SbxSTRING |
| 496 |
? StrToNumberIntl(rStrVal, rpFormatter) |
| 497 |
: std::nullopt; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
void printfmtstr(const OUString& rStr, OUString& rRes, const OUString& rFmt) |
| 502 |
{ |
| 503 |
rRes = rStr; |
| 504 |
|
| 505 |
switch (rFmt.isEmpty() ? '&' : rFmt[0]) |
| 506 |
{ |
| 507 |
case '!': |
| 508 |
if (!rStr.isEmpty()) |
| 509 |
rRes = rStr.copy(0, 1); |
| 510 |
break; |
| 511 |
case '\\': |
| 512 |
if (auto i = rFmt.indexOf('\\', 1) + 1, l = i ? i : rFmt.getLength(); |
| 513 |
l < rStr.getLength()) |
| 514 |
rRes = rStr.copy(0, l); |
| 515 |
else if (l > rStr.getLength()) |
| 516 |
rRes += OUString::Concat(RepeatedUChar(' ', l - rStr.getLength())); |
| 517 |
break; |
| 518 |
case '&': |
| 519 |
break; // Already assigned the correct value |
| 520 |
default: |
| 521 |
if (auto formatter = GetFormatter()) |
| 522 |
{ |
| 523 |
sal_uInt32 nIndex; |
| 524 |
formatter->PutandConvertEntry( |
| 525 |
// [-loplugin:redundantfcast] a temporary is required here |
| 526 |
o3tl::temporary(OUString(rFmt)), o3tl::temporary(sal_Int32()), |
| 527 |
o3tl::temporary(SvNumFormatType()), nIndex, LANGUAGE_ENGLISH_US, |
| 528 |
Application::GetSettings().GetLanguageTag().getLanguageType(), true); |
| 529 |
formatter->GetOutputString(rStr, nIndex, rRes, &o3tl::temporary<const Color*>({})); |
| 530 |
} |
| 531 |
break; |
| 532 |
} |
| 533 |
} |
| 534 |
} // namespace |
| 535 |
|
| 536 |
void SbxValue::Format( OUString& rRes, const OUString* pFmt ) const |
| 537 |
{ |
| 538 |
if (pFmt) |
| 539 |
{ |
| 540 |
if (*pFmt == "<") // VBA lowercase |
| 541 |
{ |
| 542 |
rRes = SvtSysLocale().GetCharClass().lowercase(GetOUString()); |
| 543 |
return; |
| 544 |
} |
| 545 |
if (*pFmt == ">") // VBA uppercase |
| 546 |
{ |
| 547 |
rRes = SvtSysLocale().GetCharClass().uppercase(GetOUString()); |
| 548 |
return; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
const SbxDataType eType = GetType(); |
| 553 |
if (eType == SbxNULL) |
| 554 |
{ |
| 555 |
rRes = SbxBasicFormater::BasicFormatNull(pFmt ? *pFmt : std::u16string_view{}); |
| 556 |
return; |
| 557 |
} |
| 558 |
|
| 559 |
std::shared_ptr<SvNumberFormatter> pFormatter; |
| 560 |
std::optional<double> number = GetNumberIntl(*this, rRes, pFormatter, pFmt != nullptr); |
| 561 |
|
| 562 |
if (!number) |
| 563 |
{ |
| 564 |
if (eType == SbxSTRING && pFmt) |
| 565 |
printfmtstr(rRes, rRes, *pFmt); |
| 566 |
return; |
| 567 |
} |
| 568 |
|
| 569 |
if (!pFmt) |
| 570 |
{ |
| 571 |
ImpCvtNum(*number, eType == SbxSINGLE ? 6 : eType == SbxDOUBLE ? 14 : 0, rRes); |
| 572 |
return; |
| 573 |
} |
| 574 |
|
| 575 |
if (SbxBasicFormater::isBasicFormat(*pFmt)) |
| 576 |
{ |
| 577 |
BasicFormatNum(*number, *pFmt, rRes); |
| 578 |
return; |
| 579 |
} |
| 580 |
|
| 581 |
// pflin, It is better to use SvNumberFormatter to handle the date/time/number format. |
| 582 |
// the SvNumberFormatter output is mostly compatible with |
| 583 |
// VBA output besides the OOo-basic output |
| 584 |
#if HAVE_FEATURE_SCRIPTING |
| 585 |
// number format, use SvNumberFormatter to handle it. |
| 586 |
if (!pFormatter) |
| 587 |
pFormatter = GetFormatter(); |
| 588 |
|
| 589 |
LanguageType eLangType = Application::GetSettings().GetLanguageTag().getLanguageType(); |
| 590 |
|
| 591 |
sal_uInt32 nIndex; |
| 592 |
const Color* pCol; |
| 593 |
sal_Int32 nCheckPos = 0; |
| 594 |
SvNumFormatType nType; |
| 595 |
OUString aFmtStr = *pFmt; |
| 596 |
if (const VbaFormatInfo* pInfo = getFormatInfo(aFmtStr)) |
| 597 |
{ |
| 598 |
if( pInfo->meType == VbaFormatType::Offset ) |
| 599 |
{ |
| 600 |
nIndex = pFormatter->GetFormatIndex( pInfo->meOffset, eLangType ); |
| 601 |
} |
| 602 |
else |
| 603 |
{ |
| 604 |
aFmtStr = pInfo->mpOOoFormat; |
| 605 |
pFormatter->PutandConvertEntry( aFmtStr, nCheckPos, nType, nIndex, LANGUAGE_ENGLISH_US, eLangType, true); |
| 606 |
} |
| 607 |
pFormatter->GetOutputString(*number, nIndex, rRes, &pCol); |
| 608 |
} |
| 609 |
else if (aFmtStr.equalsIgnoreAsciiCase("General Date") // VBA general date variants |
| 610 |
|| aFmtStr.equalsIgnoreAsciiCase("c")) |
| 611 |
{ |
| 612 |
OUString dateStr; |
| 613 |
if (*number <= -1.0 || *number >= 1.0) |
| 614 |
{ |
| 615 |
// short date |
| 616 |
nIndex = pFormatter->GetFormatIndex( NF_DATE_SYSTEM_SHORT, eLangType ); |
| 617 |
pFormatter->GetOutputString(*number, nIndex, dateStr, &pCol); |
| 618 |
|
| 619 |
if (floor(*number) == *number) |
| 620 |
{ |
| 621 |
rRes = dateStr; |
| 622 |
return; |
| 623 |
} |
| 624 |
} |
| 625 |
// long time |
| 626 |
aFmtStr = u"H:MM:SS AM/PM"_ustr; |
| 627 |
pFormatter->PutandConvertEntry(aFmtStr, nCheckPos, nType, nIndex, |
| 628 |
LANGUAGE_ENGLISH_US, eLangType, true); |
| 629 |
pFormatter->GetOutputString(*number, nIndex, rRes, &pCol); |
| 630 |
if (!dateStr.isEmpty()) |
| 631 |
rRes = dateStr + " " + rRes; |
| 632 |
} |
| 633 |
else if (aFmtStr.equalsIgnoreAsciiCase("n") // VBA minute variants |
| 634 |
|| aFmtStr.equalsIgnoreAsciiCase("nn")) |
| 635 |
{ |
| 636 |
sal_Int32 nMin = implGetMinute(*number); |
| 637 |
if (nMin < 10 && aFmtStr.equalsIgnoreAsciiCase("nn")) |
| 638 |
{ |
| 639 |
// Minute in two digits |
| 640 |
sal_Unicode aBuf[2]; |
| 641 |
aBuf[0] = '0'; |
| 642 |
aBuf[1] = '0' + nMin; |
| 643 |
rRes = OUString(aBuf, std::size(aBuf)); |
| 644 |
} |
| 645 |
else |
| 646 |
{ |
| 647 |
rRes = OUString::number(nMin); |
| 648 |
} |
| 649 |
} |
| 650 |
else if (aFmtStr.equalsIgnoreAsciiCase("w")) // VBA weekday number |
| 651 |
{ |
| 652 |
rRes = OUString::number(implGetWeekDay(*number)); |
| 653 |
} |
| 654 |
else if (aFmtStr.equalsIgnoreAsciiCase("y")) // VBA year day number |
| 655 |
{ |
| 656 |
sal_Int16 nYear = implGetDateYear(*number); |
| 657 |
double dBaseDate; |
| 658 |
implDateSerial( nYear, 1, 1, true, SbDateCorrection::None, dBaseDate ); |
| 659 |
sal_Int32 nYear32 = 1 + sal_Int32(*number - dBaseDate); |
| 660 |
rRes = OUString::number(nYear32); |
| 661 |
} |
| 662 |
else |
| 663 |
{ |
| 664 |
pFormatter->PutandConvertEntry( aFmtStr, nCheckPos, nType, nIndex, LANGUAGE_ENGLISH_US, eLangType, true); |
| 665 |
if (nType == SvNumFormatType::TEXT && IsString()) |
| 666 |
pFormatter->GetOutputString(GetOUString(), nIndex, rRes, &pCol); |
| 667 |
else |
| 668 |
pFormatter->GetOutputString(*number, nIndex, rRes, &pCol); |
| 669 |
} |
| 670 |
#endif |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |