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