| 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 |
|
| 21 |
#include <stdlib.h> |
| 22 |
|
| 23 |
#include <sbxform.hxx> |
| 24 |
#include <rtl/ustrbuf.hxx> |
| 25 |
|
| 26 |
#include <rtl/character.hxx> |
| 27 |
#include <o3tl/sprintf.hxx> |
| 28 |
#include <o3tl/string_view.hxx> |
| 29 |
#include <string_view> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
/* |
| 33 |
TODO: are there any Star-Basic characteristics unconsidered? |
| 34 |
|
| 35 |
what means: * as placeholder |
| 36 |
|
| 37 |
COMMENT: Visual-Basic treats the following (invalid) format-strings |
| 38 |
as shown: |
| 39 |
|
| 40 |
##0##.##0## --> ##000.000## |
| 41 |
|
| 42 |
(this class behaves the same way) |
| 43 |
*/ |
| 44 |
|
| 45 |
#include <float.h> |
| 46 |
#include <math.h> |
| 47 |
|
| 48 |
#define NO_DIGIT_ -1 |
| 49 |
|
| 50 |
#define MAX_NO_OF_DIGITS DBL_DIG |
| 51 |
#define MAX_DOUBLE_BUFFER_LENGTH MAX_NO_OF_DIGITS + 9 |
| 52 |
// +1 for leading sign |
| 53 |
// +1 for digit before the decimal point |
| 54 |
// +1 for decimal point |
| 55 |
// +2 for exponent E and exp. leading sign |
| 56 |
// +3 for the exponent's value |
| 57 |
// +1 for closing 0 |
| 58 |
|
| 59 |
#define CREATE_1000SEP_CHAR '@' |
| 60 |
|
| 61 |
#define FORMAT_SEPARATOR ';' |
| 62 |
|
| 63 |
// predefined formats for the Format$()-command: |
| 64 |
constexpr std::u16string_view BASICFORMAT_GENERALNUMBER = u"General Number"; |
| 65 |
constexpr std::u16string_view BASICFORMAT_CURRENCY = u"Currency"; |
| 66 |
constexpr std::u16string_view BASICFORMAT_FIXED = u"Fixed"; |
| 67 |
constexpr std::u16string_view BASICFORMAT_STANDARD = u"Standard"; |
| 68 |
constexpr std::u16string_view BASICFORMAT_PERCENT = u"Percent"; |
| 69 |
constexpr std::u16string_view BASICFORMAT_SCIENTIFIC = u"Scientific"; |
| 70 |
constexpr std::u16string_view BASICFORMAT_YESNO = u"Yes/No"; |
| 71 |
constexpr std::u16string_view BASICFORMAT_TRUEFALSE = u"True/False"; |
| 72 |
constexpr std::u16string_view BASICFORMAT_ONOFF = u"On/Off"; |
| 73 |
|
| 74 |
// Comment: Visual-Basic has a maximum of 12 positions after the |
| 75 |
// decimal point for floating-point-numbers. |
| 76 |
// all format-strings are compatible to Visual-Basic: |
| 77 |
constexpr OUString GENERALNUMBER_FORMAT = u"0.############"_ustr; |
| 78 |
constexpr OUString FIXED_FORMAT = u"0.00"_ustr; |
| 79 |
constexpr OUString STANDARD_FORMAT = u"@0.00"_ustr; |
| 80 |
constexpr OUString PERCENT_FORMAT = u"0.00%"_ustr; |
| 81 |
constexpr OUString SCIENTIFIC_FORMAT = u"#.00E+00"_ustr; |
| 82 |
// Comment: the character @ means that thousand-separators shall |
| 83 |
// be generated. That's a StarBasic 'extension'. |
| 84 |
|
| 85 |
|
| 86 |
static double get_number_of_digits( double dNumber ) |
| 87 |
//double floor_log10_fabs( double dNumber ) |
| 88 |
{ |
| 89 |
if( dNumber==0.0 ) |
| 90 |
return 0.0; // used to be 1.0, now 0.0 because of #40025; |
| 91 |
else |
| 92 |
return floor( log10( fabs( dNumber ) ) ); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
SbxBasicFormater::SbxBasicFormater( sal_Unicode _cDecPoint, sal_Unicode _cThousandSep, |
| 97 |
OUString _sOnStrg, |
| 98 |
OUString _sOffStrg, |
| 99 |
OUString _sYesStrg, |
| 100 |
OUString _sNoStrg, |
| 101 |
OUString _sTrueStrg, |
| 102 |
OUString _sFalseStrg, |
| 103 |
OUString _sCurrencyStrg, |
| 104 |
OUString _sCurrencyFormatStrg ) |
| 105 |
: cDecPoint(_cDecPoint) |
| 106 |
, cThousandSep(_cThousandSep) |
| 107 |
, sOnStrg(std::move(_sOnStrg)) |
| 108 |
, sOffStrg(std::move(_sOffStrg)) |
| 109 |
, sYesStrg(std::move(_sYesStrg)) |
| 110 |
, sNoStrg(std::move(_sNoStrg)) |
| 111 |
, sTrueStrg(std::move(_sTrueStrg)) |
| 112 |
, sFalseStrg(std::move(_sFalseStrg)) |
| 113 |
, sCurrencyStrg(std::move(_sCurrencyStrg)) |
| 114 |
, sCurrencyFormatStrg(std::move(_sCurrencyFormatStrg)) |
| 115 |
, dNum(0.0) |
| 116 |
, nNumExp(0) |
| 117 |
, nExpExp(0) |
| 118 |
{ |
| 119 |
} |
| 120 |
|
| 121 |
// function to output an error-text (for debugging) |
| 122 |
// displaces all characters of the string, starting from nStartPos |
| 123 |
// for one position to larger indexes, i. e. place for a new |
| 124 |
// character (which is to be inserted) is created. |
| 125 |
// ATTENTION: the string MUST be long enough! |
| 126 |
inline void SbxBasicFormater::ShiftString( OUStringBuffer& sStrg, sal_uInt16 nStartPos ) |
| 127 |
{ |
| 128 |
sStrg.remove(nStartPos,1); |
| 129 |
} |
| 130 |
|
| 131 |
void SbxBasicFormater::AppendDigit( OUStringBuffer& sStrg, short nDigit ) |
| 132 |
{ |
| 133 |
if( nDigit>=0 && nDigit<=9 ) |
| 134 |
{ |
| 135 |
sStrg.append(static_cast<sal_Unicode>(nDigit+'0')); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
void SbxBasicFormater::LeftShiftDecimalPoint( OUStringBuffer& sStrg ) |
| 140 |
{ |
| 141 |
sal_Int32 nPos = -1; |
| 142 |
|
| 143 |
for(sal_Int32 i = 0; i < sStrg.getLength(); i++) |
| 144 |
{ |
| 145 |
if(sStrg[i] == cDecPoint) |
| 146 |
{ |
| 147 |
nPos = i; |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
if( nPos >= 0 ) |
| 152 |
{ |
| 153 |
sStrg[nPos] = sStrg[nPos - 1]; |
| 154 |
sStrg[nPos - 1] = cDecPoint; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// returns a flag if rounding a 9 |
| 159 |
void SbxBasicFormater::StrRoundDigit( OUStringBuffer& sStrg, short nPos, bool& bOverflow ) |
| 160 |
{ |
| 161 |
if( nPos<0 ) |
| 162 |
{ |
| 163 |
return; |
| 164 |
} |
| 165 |
bOverflow = false; |
| 166 |
sal_Unicode c = sStrg[nPos]; |
| 167 |
if( nPos > 0 && (c == cDecPoint || c == cThousandSep) ) |
| 168 |
{ |
| 169 |
StrRoundDigit( sStrg, nPos - 1, bOverflow ); |
| 170 |
// CHANGE from 9.3.1997: end the method immediately after recursive call! |
| 171 |
return; |
| 172 |
} |
| 173 |
// skip non-digits: |
| 174 |
// COMMENT: |
| 175 |
// in a valid format-string the number's output should be done |
| 176 |
// in one piece, i. e. special characters should ONLY be in |
| 177 |
// front OR behind the number and not right in the middle of |
| 178 |
// the format information for the number |
| 179 |
while( nPos >= 0 && ! rtl::isAsciiDigit(sStrg[nPos])) |
| 180 |
{ |
| 181 |
nPos--; |
| 182 |
} |
| 183 |
if( nPos==-1 ) |
| 184 |
{ |
| 185 |
ShiftString( sStrg, 0 ); |
| 186 |
sStrg[0] = '1'; |
| 187 |
bOverflow = true; |
| 188 |
} |
| 189 |
else |
| 190 |
{ |
| 191 |
sal_Unicode c2 = sStrg[nPos]; |
| 192 |
if( rtl::isAsciiDigit(c2) ) |
| 193 |
{ |
| 194 |
if( c2 == '9' ) |
| 195 |
{ |
| 196 |
sStrg[nPos] = '0'; |
| 197 |
StrRoundDigit( sStrg, nPos - 1, bOverflow ); |
| 198 |
} |
| 199 |
else |
| 200 |
{ |
| 201 |
sStrg[nPos] = c2 + 1; |
| 202 |
} |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
ShiftString( sStrg,nPos+1 ); |
| 207 |
sStrg[nPos + 1] = '1'; |
| 208 |
bOverflow = true; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
void SbxBasicFormater::StrRoundDigit( OUStringBuffer& sStrg, short nPos ) |
| 214 |
{ |
| 215 |
bool bOverflow; |
| 216 |
|
| 217 |
StrRoundDigit( sStrg, nPos, bOverflow ); |
| 218 |
} |
| 219 |
|
| 220 |
void SbxBasicFormater::ParseBack( OUStringBuffer& sStrg, std::u16string_view sFormatStrg, |
| 221 |
short nFormatPos ) |
| 222 |
{ |
| 223 |
for( sal_Int32 i = nFormatPos; |
| 224 |
i>0 && sFormatStrg[ i ] == '#' && sStrg[sStrg.getLength() - 1] == '0'; |
| 225 |
i-- ) |
| 226 |
{ |
| 227 |
sStrg.setLength(sStrg.getLength() - 1 ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
void SbxBasicFormater::InitScan( double _dNum ) |
| 232 |
{ |
| 233 |
char sBuffer[ MAX_DOUBLE_BUFFER_LENGTH ]; |
| 234 |
|
| 235 |
dNum = _dNum; |
| 236 |
InitExp( get_number_of_digits( dNum ) ); |
| 237 |
// maximum of 15 positions behind the decimal point, example: -1.234000000000000E-001 |
| 238 |
/*int nCount =*/ o3tl::sprintf( sBuffer,"%+22.15lE",dNum ); |
| 239 |
sSciNumStrg = OUString::createFromAscii( sBuffer ); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
void SbxBasicFormater::InitExp( double _dNewExp ) |
| 244 |
{ |
| 245 |
nNumExp = static_cast<short>(_dNewExp); |
| 246 |
sNumExpStrg = (nNumExp >= 0 ? std::u16string_view(u"+") : std::u16string_view(u"")) |
| 247 |
+ OUString::number(nNumExp); |
| 248 |
nExpExp = static_cast<short>(get_number_of_digits( static_cast<double>(nNumExp) )); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
short SbxBasicFormater::GetDigitAtPosScan( short nPos, bool& bFoundFirstDigit ) |
| 253 |
{ |
| 254 |
// trying to read a higher digit, |
| 255 |
// e. g. position 4 in 1.234, |
| 256 |
// or to read a digit outside of the |
| 257 |
// number's dissolution (double) |
| 258 |
if( nPos>nNumExp || abs(nNumExp-nPos)>MAX_NO_OF_DIGITS ) |
| 259 |
{ |
| 260 |
return NO_DIGIT_; |
| 261 |
} |
| 262 |
// determine the index of the position in the number-string: |
| 263 |
// skip the leading sign |
| 264 |
sal_uInt16 no = 1; |
| 265 |
// skip the decimal point if necessary |
| 266 |
if( nPos<nNumExp ) |
| 267 |
no++; |
| 268 |
no += nNumExp-nPos; |
| 269 |
// query of the number's first valid digit --> set flag |
| 270 |
if( nPos==nNumExp ) |
| 271 |
bFoundFirstDigit = true; |
| 272 |
return static_cast<short>(sSciNumStrg[ no ] - '0'); |
| 273 |
} |
| 274 |
|
| 275 |
short SbxBasicFormater::GetDigitAtPosExpScan( short nPos, bool& bFoundFirstDigit ) |
| 276 |
{ |
| 277 |
if( nPos>nExpExp ) |
| 278 |
return -1; |
| 279 |
|
| 280 |
sal_uInt16 no = 1; |
| 281 |
no += nExpExp-nPos; |
| 282 |
|
| 283 |
if( nPos==nExpExp ) |
| 284 |
bFoundFirstDigit = true; |
| 285 |
return static_cast<short>(sNumExpStrg[ no ] - '0'); |
| 286 |
} |
| 287 |
|
| 288 |
// a value for the exponent can be given because the number maybe shall |
| 289 |
// not be displayed in a normed way (e. g. 1.2345e-03) but maybe 123.345e-3 ! |
| 290 |
short SbxBasicFormater::GetDigitAtPosExpScan( double dNewExponent, short nPos, |
| 291 |
bool& bFoundFirstDigit ) |
| 292 |
{ |
| 293 |
InitExp( dNewExponent ); |
| 294 |
|
| 295 |
return GetDigitAtPosExpScan( nPos,bFoundFirstDigit ); |
| 296 |
} |
| 297 |
|
| 298 |
// Copies the respective part of the format-string, if existing, and returns it. |
| 299 |
// So a new string is created, which has to be freed by the caller later. |
| 300 |
OUString SbxBasicFormater::GetPosFormatString( std::u16string_view sFormatStrg, bool & bFound ) |
| 301 |
{ |
| 302 |
bFound = false; // default... |
| 303 |
size_t nPos = sFormatStrg.find( FORMAT_SEPARATOR ); |
| 304 |
|
| 305 |
if( nPos != std::u16string_view::npos ) |
| 306 |
{ |
| 307 |
bFound = true; |
| 308 |
// the format-string for positive numbers is |
| 309 |
// everything before the first ';' |
| 310 |
return OUString(sFormatStrg.substr( 0,nPos )); |
| 311 |
} |
| 312 |
|
| 313 |
return OUString(); |
| 314 |
} |
| 315 |
|
| 316 |
// see also GetPosFormatString() |
| 317 |
OUString SbxBasicFormater::GetNegFormatString( std::u16string_view sFormatStrg, bool & bFound ) |
| 318 |
{ |
| 319 |
bFound = false; // default... |
| 320 |
size_t nPos = sFormatStrg.find( FORMAT_SEPARATOR ); |
| 321 |
|
| 322 |
if( nPos != std::u16string_view::npos) |
| 323 |
{ |
| 324 |
// the format-string for negative numbers is |
| 325 |
// everything between the first and the second ';' |
| 326 |
std::u16string_view sTempStrg = sFormatStrg.substr( nPos+1 ); |
| 327 |
nPos = sTempStrg.find( FORMAT_SEPARATOR ); |
| 328 |
bFound = true; |
| 329 |
if( nPos == std::u16string_view::npos ) |
| 330 |
{ |
| 331 |
return OUString(sTempStrg); |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
return OUString(sTempStrg.substr( 0,nPos )); |
| 336 |
} |
| 337 |
} |
| 338 |
return OUString(); |
| 339 |
} |
| 340 |
|
| 341 |
// see also GetPosFormatString() |
| 342 |
OUString SbxBasicFormater::Get0FormatString( std::u16string_view sFormatStrg, bool & bFound ) |
| 343 |
{ |
| 344 |
bFound = false; // default... |
| 345 |
size_t nPos = sFormatStrg.find( FORMAT_SEPARATOR ); |
| 346 |
|
| 347 |
if( nPos != std::u16string_view::npos ) |
| 348 |
{ |
| 349 |
// the format string for the zero is |
| 350 |
// everything after the second ';' |
| 351 |
std::u16string_view sTempStrg = sFormatStrg.substr( nPos+1 ); |
| 352 |
nPos = sTempStrg.find( FORMAT_SEPARATOR ); |
| 353 |
if( nPos != std::u16string_view::npos ) |
| 354 |
{ |
| 355 |
bFound = true; |
| 356 |
sTempStrg = sTempStrg.substr( nPos+1 ); |
| 357 |
nPos = sTempStrg.find( FORMAT_SEPARATOR ); |
| 358 |
if( nPos == std::u16string_view::npos ) |
| 359 |
{ |
| 360 |
return OUString(sTempStrg); |
| 361 |
} |
| 362 |
else |
| 363 |
{ |
| 364 |
return OUString(sTempStrg.substr( 0,nPos )); |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return OUString(); |
| 370 |
} |
| 371 |
|
| 372 |
// see also GetPosFormatString() |
| 373 |
OUString SbxBasicFormater::GetNullFormatString( std::u16string_view sFormatStrg, bool & bFound ) |
| 374 |
{ |
| 375 |
bFound = false; // default... |
| 376 |
size_t nPos = sFormatStrg.find( FORMAT_SEPARATOR ); |
| 377 |
|
| 378 |
if( nPos != std::u16string_view::npos ) |
| 379 |
{ |
| 380 |
// the format-string for the Null is |
| 381 |
// everything after the third ';' |
| 382 |
std::u16string_view sTempStrg = sFormatStrg.substr( nPos+1 ); |
| 383 |
nPos = sTempStrg.find( FORMAT_SEPARATOR ); |
| 384 |
if( nPos != std::u16string_view::npos ) |
| 385 |
{ |
| 386 |
sTempStrg = sTempStrg.substr( nPos+1 ); |
| 387 |
nPos = sTempStrg.find( FORMAT_SEPARATOR ); |
| 388 |
if( nPos != std::u16string_view::npos ) |
| 389 |
{ |
| 390 |
bFound = true; |
| 391 |
return OUString(sTempStrg.substr( nPos+1 )); |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
return OUString(); |
| 397 |
} |
| 398 |
|
| 399 |
// returns value <> 0 in case of an error |
| 400 |
void SbxBasicFormater::AnalyseFormatString( std::u16string_view sFormatStrg, |
| 401 |
short& nNoOfDigitsLeft, short& nNoOfDigitsRight, |
| 402 |
short& nNoOfOptionalDigitsLeft, |
| 403 |
short& nNoOfExponentDigits, short& nNoOfOptionalExponentDigits, |
| 404 |
bool& bPercent, bool& bCurrency, bool& bScientific, |
| 405 |
bool& bGenerateThousandSeparator, |
| 406 |
short& nMultipleThousandSeparators ) |
| 407 |
{ |
| 408 |
sal_Int32 nLen; |
| 409 |
short nState = 0; |
| 410 |
|
| 411 |
nLen = sFormatStrg.size(); |
| 412 |
nNoOfDigitsLeft = 0; |
| 413 |
nNoOfDigitsRight = 0; |
| 414 |
nNoOfOptionalDigitsLeft = 0; |
| 415 |
nNoOfExponentDigits = 0; |
| 416 |
nNoOfOptionalExponentDigits = 0; |
| 417 |
bPercent = false; |
| 418 |
bCurrency = false; |
| 419 |
bScientific = false; |
| 420 |
// from 11.7.97: as soon as a comma (point?) is found in the format string, |
| 421 |
// all three decimal powers are marked (i. e. thousand, million, ...) |
| 422 |
bGenerateThousandSeparator = sFormatStrg.find( ',' ) != std::u16string_view::npos; |
| 423 |
nMultipleThousandSeparators = 0; |
| 424 |
|
| 425 |
for( sal_Int32 i = 0; i < nLen; i++ ) |
| 426 |
{ |
| 427 |
sal_Unicode c = sFormatStrg[ i ]; |
| 428 |
switch( c ) |
| 429 |
{ |
| 430 |
case '#': |
| 431 |
case '0': |
| 432 |
if( nState==0 ) |
| 433 |
{ |
| 434 |
nNoOfDigitsLeft++; |
| 435 |
// TODO here maybe better error inspection of the mantissa for valid syntax (see grammar)h |
| 436 |
// ATTENTION: 'undefined' behaviour if # and 0 are combined! |
| 437 |
// REMARK: #-placeholders are actually useless for |
| 438 |
// scientific display before the decimal point! |
| 439 |
if( c=='#' ) |
| 440 |
{ |
| 441 |
nNoOfOptionalDigitsLeft++; |
| 442 |
} |
| 443 |
} |
| 444 |
else if( nState==1 ) |
| 445 |
{ |
| 446 |
nNoOfDigitsRight++; |
| 447 |
} |
| 448 |
else if( nState==-1 ) // search 0 in the exponent |
| 449 |
{ |
| 450 |
if( c=='#' ) // # switches on the condition |
| 451 |
{ |
| 452 |
nNoOfOptionalExponentDigits++; |
| 453 |
nState = -2; |
| 454 |
} |
| 455 |
nNoOfExponentDigits++; |
| 456 |
} |
| 457 |
else if( nState==-2 ) // search # in the exponent |
| 458 |
{ |
| 459 |
if( c=='0' ) |
| 460 |
{ |
| 461 |
// ERROR: 0 after # in the exponent is NOT allowed!! |
| 462 |
return; |
| 463 |
} |
| 464 |
nNoOfOptionalExponentDigits++; |
| 465 |
nNoOfExponentDigits++; |
| 466 |
} |
| 467 |
break; |
| 468 |
case '.': |
| 469 |
nState++; |
| 470 |
if( nState>1 ) |
| 471 |
{ |
| 472 |
return; // ERROR: too many decimal points |
| 473 |
} |
| 474 |
break; |
| 475 |
case '%': |
| 476 |
bPercent = true; |
| 477 |
break; |
| 478 |
case '(': |
| 479 |
bCurrency = true; |
| 480 |
break; |
| 481 |
case ',': |
| 482 |
{ |
| 483 |
sal_Unicode ch = sFormatStrg[ i+1 ]; |
| 484 |
|
| 485 |
if( ch!=0 && (ch==',' || ch=='.') ) |
| 486 |
{ |
| 487 |
nMultipleThousandSeparators++; |
| 488 |
} |
| 489 |
} |
| 490 |
break; |
| 491 |
case 'e': |
| 492 |
case 'E': |
| 493 |
// #i13821 not when no digits before |
| 494 |
if( nNoOfDigitsLeft > 0 || nNoOfDigitsRight > 0 ) |
| 495 |
{ |
| 496 |
nState = -1; // abort counting digits |
| 497 |
bScientific = true; |
| 498 |
} |
| 499 |
break; |
| 500 |
// OWN command-character which turns on |
| 501 |
// the creation of thousand-separators |
| 502 |
case '\\': |
| 503 |
// Ignore next char |
| 504 |
i++; |
| 505 |
break; |
| 506 |
case CREATE_1000SEP_CHAR: |
| 507 |
bGenerateThousandSeparator = true; |
| 508 |
break; |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
// the flag bCreateSign says that at the mantissa a leading sign |
| 514 |
// shall be created |
| 515 |
void SbxBasicFormater::ScanFormatString( double dNumber, |
| 516 |
std::u16string_view sFormatStrg, OUString& sReturnStrgFinal, |
| 517 |
bool bCreateSign ) |
| 518 |
{ |
| 519 |
short /*nErr,*/nNoOfDigitsLeft,nNoOfDigitsRight,nNoOfOptionalDigitsLeft, |
| 520 |
nNoOfExponentDigits,nNoOfOptionalExponentDigits, |
| 521 |
nMultipleThousandSeparators; |
| 522 |
bool bPercent,bCurrency,bScientific,bGenerateThousandSeparator; |
| 523 |
|
| 524 |
OUStringBuffer sReturnStrg(32); |
| 525 |
|
| 526 |
// analyse the format-string, i. e. determine the following values: |
| 527 |
/* |
| 528 |
- number of digits before decimal point |
| 529 |
- number of digits after decimal point |
| 530 |
- optional digits before decimal point |
| 531 |
- number of digits in the exponent |
| 532 |
- optional digits in the exponent |
| 533 |
- percent-character found? |
| 534 |
- () for negative leading sign? |
| 535 |
- exponential-notation? |
| 536 |
- shall thousand-separators be generated? |
| 537 |
- is a percent-character being found? --> dNumber *= 100.0; |
| 538 |
- are there thousand-separators in a row? |
| 539 |
,, or ,. --> dNumber /= 1000.0; |
| 540 |
- other errors? multiple decimal points, E's, etc. |
| 541 |
--> errors are simply ignored at the moment |
| 542 |
*/ |
| 543 |
AnalyseFormatString( sFormatStrg, nNoOfDigitsLeft, nNoOfDigitsRight, |
| 544 |
nNoOfOptionalDigitsLeft, nNoOfExponentDigits, |
| 545 |
nNoOfOptionalExponentDigits, |
| 546 |
bPercent, bCurrency, bScientific, |
| 547 |
bGenerateThousandSeparator, nMultipleThousandSeparators ); |
| 548 |
// special handling for special characters |
| 549 |
if( bPercent ) |
| 550 |
{ |
| 551 |
dNumber *= 100.0; |
| 552 |
} |
| 553 |
// TODO: this condition (,, or ,.) is NOT Visual-Basic compatible! |
| 554 |
// Question: shall this stay here (requirements)? |
| 555 |
if( nMultipleThousandSeparators ) |
| 556 |
{ |
| 557 |
dNumber /= 1000.0; |
| 558 |
} |
| 559 |
double dExponent; |
| 560 |
short i,nLen; |
| 561 |
short nState,nDigitPos,nExponentPos,nMaxDigit,nMaxExponentDigit; |
| 562 |
bool bFirstDigit,bFirstExponentDigit,bFoundFirstDigit, |
| 563 |
bIsNegative,bZeroSpaceOn, bSignHappend,bDigitPosNegative; |
| 564 |
|
| 565 |
bSignHappend = false; |
| 566 |
bFoundFirstDigit = false; |
| 567 |
bIsNegative = dNumber < 0.0; |
| 568 |
nLen = sFormatStrg.size(); |
| 569 |
dExponent = get_number_of_digits( dNumber ); |
| 570 |
nExponentPos = 0; |
| 571 |
nMaxExponentDigit = 0; |
| 572 |
nMaxDigit = static_cast<short>(dExponent); |
| 573 |
bDigitPosNegative = false; |
| 574 |
if( bScientific ) |
| 575 |
{ |
| 576 |
dExponent = dExponent - static_cast<double>(nNoOfDigitsLeft-1); |
| 577 |
nDigitPos = nMaxDigit; |
| 578 |
nMaxExponentDigit = static_cast<short>(get_number_of_digits( dExponent )); |
| 579 |
nExponentPos = nNoOfExponentDigits - 1 - nNoOfOptionalExponentDigits; |
| 580 |
} |
| 581 |
else |
| 582 |
{ |
| 583 |
nDigitPos = nNoOfDigitsLeft - 1; // counting starts at 0, 10^0 |
| 584 |
// no exponent-data is needed here! |
| 585 |
bDigitPosNegative = (nDigitPos < 0); |
| 586 |
} |
| 587 |
bFirstDigit = true; |
| 588 |
bFirstExponentDigit = true; |
| 589 |
nState = 0; // 0 --> mantissa; 1 --> exponent |
| 590 |
bZeroSpaceOn = false; |
| 591 |
|
| 592 |
|
| 593 |
InitScan( dNumber ); |
| 594 |
// scanning the format-string: |
| 595 |
sal_Unicode cForce = 0; |
| 596 |
for( i = 0; i < nLen; i++ ) |
| 597 |
{ |
| 598 |
sal_Unicode c; |
| 599 |
if( cForce ) |
| 600 |
{ |
| 601 |
c = cForce; |
| 602 |
cForce = 0; |
| 603 |
} |
| 604 |
else |
| 605 |
{ |
| 606 |
c = sFormatStrg[ i ]; |
| 607 |
} |
| 608 |
switch( c ) |
| 609 |
{ |
| 610 |
case '0': |
| 611 |
case '#': |
| 612 |
if( nState==0 ) |
| 613 |
{ |
| 614 |
// handling of the mantissa |
| 615 |
if( bFirstDigit ) |
| 616 |
{ |
| 617 |
// remark: at bCurrency the negative |
| 618 |
// leading sign shall be shown with () |
| 619 |
if( bIsNegative && !bCreateSign && !bSignHappend ) |
| 620 |
{ |
| 621 |
bSignHappend = true; |
| 622 |
sReturnStrg.append('-'); |
| 623 |
} |
| 624 |
// output redundant positions, i. e. those which |
| 625 |
// are undocumented by the format-string |
| 626 |
if( nMaxDigit > nDigitPos ) |
| 627 |
{ |
| 628 |
for( short j = nMaxDigit; j > nDigitPos; j-- ) |
| 629 |
{ |
| 630 |
short nTempDigit = GetDigitAtPosScan( j, bFoundFirstDigit ); |
| 631 |
AppendDigit( sReturnStrg, nTempDigit ); |
| 632 |
if( nTempDigit != NO_DIGIT_ ) |
| 633 |
{ |
| 634 |
bFirstDigit = false; |
| 635 |
} |
| 636 |
if( bGenerateThousandSeparator && c=='0' && j > 0 && (j % 3 == 0) ) |
| 637 |
{ |
| 638 |
sReturnStrg.append(cThousandSep ); |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
if( nMaxDigit<nDigitPos && ( c=='0' || bZeroSpaceOn ) ) |
| 645 |
{ |
| 646 |
AppendDigit( sReturnStrg, 0 ); |
| 647 |
bFirstDigit = false; |
| 648 |
bZeroSpaceOn = true; |
| 649 |
// Remark: in Visual-Basic the first 0 turns on the 0 for |
| 650 |
// all the following # (up to the decimal point), |
| 651 |
// this behaviour is simulated here with the flag. |
| 652 |
if (bGenerateThousandSeparator && c == '0' && nDigitPos > 0 && (nDigitPos % 3 == 0)) |
| 653 |
{ |
| 654 |
sReturnStrg.append(cThousandSep); |
| 655 |
} |
| 656 |
} |
| 657 |
else |
| 658 |
{ |
| 659 |
short nTempDigit = GetDigitAtPosScan( nDigitPos, bFoundFirstDigit ) ; |
| 660 |
AppendDigit( sReturnStrg, nTempDigit ); |
| 661 |
|
| 662 |
if( nTempDigit != NO_DIGIT_ ) |
| 663 |
{ |
| 664 |
bFirstDigit = false; |
| 665 |
} |
| 666 |
if( bGenerateThousandSeparator && ( c=='0' || nMaxDigit>=nDigitPos ) && nDigitPos>0 && (nDigitPos % 3 == 0) ) |
| 667 |
{ |
| 668 |
sReturnStrg.append(cThousandSep); |
| 669 |
} |
| 670 |
} |
| 671 |
nDigitPos--; |
| 672 |
} |
| 673 |
else |
| 674 |
{ |
| 675 |
// handling the exponent |
| 676 |
if( bFirstExponentDigit ) |
| 677 |
{ |
| 678 |
// leading sign has been given out at e/E already |
| 679 |
bFirstExponentDigit = false; |
| 680 |
if( nMaxExponentDigit > nExponentPos ) |
| 681 |
// output redundant positions, i. e. those which |
| 682 |
// are undocumented by the format-string |
| 683 |
{ |
| 684 |
for( short j = nMaxExponentDigit; j > nExponentPos; j-- ) |
| 685 |
{ |
| 686 |
AppendDigit( sReturnStrg, GetDigitAtPosExpScan( dExponent, j, bFoundFirstDigit ) ); |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
if( nMaxExponentDigit < nExponentPos && c=='0' ) |
| 692 |
{ |
| 693 |
AppendDigit( sReturnStrg, 0 ); |
| 694 |
} |
| 695 |
else |
| 696 |
{ |
| 697 |
AppendDigit( sReturnStrg, GetDigitAtPosExpScan( dExponent, nExponentPos, bFoundFirstDigit ) ); |
| 698 |
} |
| 699 |
nExponentPos--; |
| 700 |
} |
| 701 |
break; |
| 702 |
case '.': |
| 703 |
if( bDigitPosNegative ) // #i13821: If no digits before . |
| 704 |
{ |
| 705 |
bDigitPosNegative = false; |
| 706 |
nDigitPos = 0; |
| 707 |
cForce = '#'; |
| 708 |
i-=2; |
| 709 |
break; |
| 710 |
} |
| 711 |
sReturnStrg.append(cDecPoint); |
| 712 |
break; |
| 713 |
case '%': |
| 714 |
// maybe remove redundant 0s, e. g. 4.500e4 in 0.0##e-00 |
| 715 |
ParseBack( sReturnStrg, sFormatStrg, i-1 ); |
| 716 |
sReturnStrg.append('%'); |
| 717 |
break; |
| 718 |
case 'e': |
| 719 |
case 'E': |
| 720 |
// does mantissa have to be rounded, before the exponent is displayed? |
| 721 |
{ |
| 722 |
// is there a mantissa at all? |
| 723 |
if( bFirstDigit ) |
| 724 |
{ |
| 725 |
// apparently not, i. e. invalid format string, e. g. E000.00 |
| 726 |
// so ignore these e and E characters |
| 727 |
// maybe output an error (like in Visual Basic)? |
| 728 |
|
| 729 |
// #i13821: VB 6 behaviour |
| 730 |
sReturnStrg.append(c); |
| 731 |
break; |
| 732 |
} |
| 733 |
|
| 734 |
bool bOverflow = false; |
| 735 |
short nNextDigit = GetDigitAtPosScan( nDigitPos, bFoundFirstDigit ); |
| 736 |
if( nNextDigit>=5 ) |
| 737 |
{ |
| 738 |
StrRoundDigit( sReturnStrg, sReturnStrg.getLength() - 1, bOverflow ); |
| 739 |
} |
| 740 |
if( bOverflow ) |
| 741 |
{ |
| 742 |
// a leading 9 has been rounded |
| 743 |
LeftShiftDecimalPoint( sReturnStrg ); |
| 744 |
sReturnStrg[sReturnStrg.getLength() - 1] = 0; |
| 745 |
dExponent += 1.0; |
| 746 |
} |
| 747 |
// maybe remove redundant 0s, e. g. 4.500e4 in 0.0##e-00 |
| 748 |
ParseBack( sReturnStrg, sFormatStrg, i-1 ); |
| 749 |
} |
| 750 |
// change the scanner's condition |
| 751 |
nState++; |
| 752 |
// output exponent character |
| 753 |
sReturnStrg.append(c); |
| 754 |
// i++; // MANIPULATION of the loop-variable! |
| 755 |
c = sFormatStrg[ ++i ]; |
| 756 |
// output leading sign / exponent |
| 757 |
if( c != 0 ) |
| 758 |
{ |
| 759 |
if( c == '-' ) |
| 760 |
{ |
| 761 |
if( dExponent < 0.0 ) |
| 762 |
{ |
| 763 |
sReturnStrg.append('-'); |
| 764 |
} |
| 765 |
} |
| 766 |
else if( c == '+' ) |
| 767 |
{ |
| 768 |
if( dExponent < 0.0 ) |
| 769 |
{ |
| 770 |
sReturnStrg.append('-'); |
| 771 |
} |
| 772 |
else |
| 773 |
{ |
| 774 |
sReturnStrg.append('+'); |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
break; |
| 779 |
case ',': |
| 780 |
break; |
| 781 |
case ';': |
| 782 |
break; |
| 783 |
case '(': |
| 784 |
case ')': |
| 785 |
// maybe remove redundant 0s, e. g. 4.500e4 in 0.0##e-00 |
| 786 |
ParseBack( sReturnStrg, sFormatStrg, i-1 ); |
| 787 |
if( bIsNegative ) |
| 788 |
{ |
| 789 |
sReturnStrg.append(c); |
| 790 |
} |
| 791 |
break; |
| 792 |
case '$': |
| 793 |
// append the string for the currency: |
| 794 |
sReturnStrg.append(sCurrencyStrg); |
| 795 |
break; |
| 796 |
case ' ': |
| 797 |
case '-': |
| 798 |
case '+': |
| 799 |
ParseBack( sReturnStrg, sFormatStrg, i-1 ); |
| 800 |
sReturnStrg.append(c); |
| 801 |
break; |
| 802 |
case '\\': |
| 803 |
ParseBack( sReturnStrg, sFormatStrg, i-1 ); |
| 804 |
// special character found, output next |
| 805 |
// character directly (if existing) |
| 806 |
c = sFormatStrg[ ++i ]; |
| 807 |
if( c!=0 ) |
| 808 |
{ |
| 809 |
sReturnStrg.append(c); |
| 810 |
} |
| 811 |
break; |
| 812 |
case CREATE_1000SEP_CHAR: |
| 813 |
// ignore here, action has already been |
| 814 |
// executed in AnalyseFormatString |
| 815 |
break; |
| 816 |
default: |
| 817 |
// output characters and digits, too (like in Visual-Basic) |
| 818 |
if( ( c>='a' && c<='z' ) || |
| 819 |
( c>='A' && c<='Z' ) || |
| 820 |
( c>='1' && c<='9' ) ) |
| 821 |
{ |
| 822 |
sReturnStrg.append(c); |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
// scan completed - rounding necessary? |
| 828 |
if( !bScientific ) |
| 829 |
{ |
| 830 |
short nNextDigit = GetDigitAtPosScan( nDigitPos, bFoundFirstDigit ); |
| 831 |
if( nNextDigit>=5 ) |
| 832 |
{ |
| 833 |
StrRoundDigit( sReturnStrg, sReturnStrg.getLength() - 1 ); |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
if( nNoOfDigitsRight>0 ) |
| 838 |
{ |
| 839 |
ParseBack( sReturnStrg, sFormatStrg, sFormatStrg.size()-1 ); |
| 840 |
} |
| 841 |
sReturnStrgFinal = sReturnStrg.makeStringAndClear(); |
| 842 |
} |
| 843 |
|
| 844 |
OUString SbxBasicFormater::BasicFormatNull( std::u16string_view sFormatStrg ) |
| 845 |
{ |
| 846 |
bool bNullFormatFound; |
| 847 |
OUString sNullFormatStrg = GetNullFormatString( sFormatStrg, bNullFormatFound ); |
| 848 |
|
| 849 |
if( bNullFormatFound ) |
| 850 |
{ |
| 851 |
return sNullFormatStrg; |
| 852 |
} |
| 853 |
return u"null"_ustr; |
| 854 |
} |
| 855 |
|
| 856 |
OUString SbxBasicFormater::BasicFormat( double dNumber, const OUString& _sFormatStrg ) |
| 857 |
{ |
| 858 |
bool bPosFormatFound,bNegFormatFound,b0FormatFound; |
| 859 |
OUString sFormatStrg = _sFormatStrg; |
| 860 |
|
| 861 |
// analyse format-string concerning predefined formats: |
| 862 |
if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_GENERALNUMBER ) ) |
| 863 |
{ |
| 864 |
sFormatStrg = GENERALNUMBER_FORMAT; |
| 865 |
} |
| 866 |
if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_CURRENCY ) ) |
| 867 |
{ |
| 868 |
sFormatStrg = sCurrencyFormatStrg; |
| 869 |
} |
| 870 |
if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_FIXED ) ) |
| 871 |
{ |
| 872 |
sFormatStrg = FIXED_FORMAT; |
| 873 |
} |
| 874 |
if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_STANDARD ) ) |
| 875 |
{ |
| 876 |
sFormatStrg = STANDARD_FORMAT; |
| 877 |
} |
| 878 |
if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_PERCENT ) ) |
| 879 |
{ |
| 880 |
sFormatStrg = PERCENT_FORMAT; |
| 881 |
} |
| 882 |
if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_SCIENTIFIC ) ) |
| 883 |
{ |
| 884 |
sFormatStrg = SCIENTIFIC_FORMAT; |
| 885 |
} |
| 886 |
if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_YESNO ) ) |
| 887 |
{ |
| 888 |
return ( dNumber==0.0 ) ? sNoStrg : sYesStrg ; |
| 889 |
} |
| 890 |
if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_TRUEFALSE ) ) |
| 891 |
{ |
| 892 |
return ( dNumber==0.0 ) ? sFalseStrg : sTrueStrg ; |
| 893 |
} |
| 894 |
if( sFormatStrg.equalsIgnoreAsciiCase( BASICFORMAT_ONOFF ) ) |
| 895 |
{ |
| 896 |
return ( dNumber==0.0 ) ? sOffStrg : sOnStrg ; |
| 897 |
} |
| 898 |
|
| 899 |
// analyse format-string concerning ';', i. e. format-strings for |
| 900 |
// positive-, negative- and 0-values |
| 901 |
OUString sPosFormatStrg = GetPosFormatString( sFormatStrg, bPosFormatFound ); |
| 902 |
OUString sNegFormatStrg = GetNegFormatString( sFormatStrg, bNegFormatFound ); |
| 903 |
OUString s0FormatStrg = Get0FormatString( sFormatStrg, b0FormatFound ); |
| 904 |
|
| 905 |
OUString sReturnStrg; |
| 906 |
OUString sTempStrg; |
| 907 |
|
| 908 |
if( dNumber==0.0 ) |
| 909 |
{ |
| 910 |
sTempStrg = sFormatStrg; |
| 911 |
if( b0FormatFound ) |
| 912 |
{ |
| 913 |
if( s0FormatStrg.isEmpty() && bPosFormatFound ) |
| 914 |
{ |
| 915 |
sTempStrg = sPosFormatStrg; |
| 916 |
} |
| 917 |
else |
| 918 |
{ |
| 919 |
sTempStrg = s0FormatStrg; |
| 920 |
} |
| 921 |
} |
| 922 |
else if( bPosFormatFound ) |
| 923 |
{ |
| 924 |
sTempStrg = sPosFormatStrg; |
| 925 |
} |
| 926 |
ScanFormatString( dNumber, sTempStrg, sReturnStrg,/*bCreateSign=*/false ); |
| 927 |
} |
| 928 |
else |
| 929 |
{ |
| 930 |
if( dNumber<0.0 ) |
| 931 |
{ |
| 932 |
if( bNegFormatFound ) |
| 933 |
{ |
| 934 |
if( sNegFormatStrg.isEmpty() && bPosFormatFound ) |
| 935 |
{ |
| 936 |
sTempStrg = "-" + sPosFormatStrg; |
| 937 |
} |
| 938 |
else |
| 939 |
{ |
| 940 |
sTempStrg = sNegFormatStrg; |
| 941 |
} |
| 942 |
} |
| 943 |
else |
| 944 |
{ |
| 945 |
sTempStrg = sFormatStrg; |
| 946 |
} |
| 947 |
// if NO format-string especially for negative |
| 948 |
// values is given, output the leading sign |
| 949 |
ScanFormatString( dNumber, sTempStrg, sReturnStrg,/*bCreateSign=*/bNegFormatFound/*sNegFormatStrg!=EMPTYFORMATSTRING*/ ); |
| 950 |
} |
| 951 |
else // if( dNumber>0.0 ) |
| 952 |
{ |
| 953 |
ScanFormatString( dNumber, |
| 954 |
(/*sPosFormatStrg!=EMPTYFORMATSTRING*/bPosFormatFound ? sPosFormatStrg : sFormatStrg), |
| 955 |
sReturnStrg,/*bCreateSign=*/false ); |
| 956 |
} |
| 957 |
} |
| 958 |
return sReturnStrg; |
| 959 |
} |
| 960 |
|
| 961 |
bool SbxBasicFormater::isBasicFormat( std::u16string_view sFormatStrg ) |
| 962 |
{ |
| 963 |
if( o3tl::equalsIgnoreAsciiCase( sFormatStrg, BASICFORMAT_GENERALNUMBER ) ) |
| 964 |
{ |
| 965 |
return true; |
| 966 |
} |
| 967 |
if( o3tl::equalsIgnoreAsciiCase( sFormatStrg, BASICFORMAT_CURRENCY ) ) |
| 968 |
{ |
| 969 |
return true; |
| 970 |
} |
| 971 |
if( o3tl::equalsIgnoreAsciiCase( sFormatStrg, BASICFORMAT_FIXED ) ) |
| 972 |
{ |
| 973 |
return true; |
| 974 |
} |
| 975 |
if( o3tl::equalsIgnoreAsciiCase( sFormatStrg, BASICFORMAT_STANDARD ) ) |
| 976 |
{ |
| 977 |
return true; |
| 978 |
} |
| 979 |
if( o3tl::equalsIgnoreAsciiCase( sFormatStrg, BASICFORMAT_PERCENT ) ) |
| 980 |
{ |
| 981 |
return true; |
| 982 |
} |
| 983 |
if( o3tl::equalsIgnoreAsciiCase( sFormatStrg, BASICFORMAT_SCIENTIFIC ) ) |
| 984 |
{ |
| 985 |
return true; |
| 986 |
} |
| 987 |
if( o3tl::equalsIgnoreAsciiCase( sFormatStrg, BASICFORMAT_YESNO ) ) |
| 988 |
{ |
| 989 |
return true; |
| 990 |
} |
| 991 |
if( o3tl::equalsIgnoreAsciiCase( sFormatStrg, BASICFORMAT_TRUEFALSE ) ) |
| 992 |
{ |
| 993 |
return true; |
| 994 |
} |
| 995 |
if( o3tl::equalsIgnoreAsciiCase( sFormatStrg, BASICFORMAT_ONOFF ) ) |
| 996 |
{ |
| 997 |
return true; |
| 998 |
} |
| 999 |
return false; |
| 1000 |
} |
| 1001 |
|
| 1002 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |